file name: ctrling.hpp, ctrling.cpp
classification: simulation
contents: class CControllingObject
derived from: class ACProcess
use: a basic interface between the simulation modules and the
evaluation; it provides a function for reporting events and executes the
base loop that services the socket to the evaluation
global items used:
INT32 SecondsToTicks
(double period): converts period (in seconds) into pSOS ticks.
member variables:
CBitMask
classMask_: the bitmask for turning on/off event classes.
default: all bits turned off
CBitMask
eventMask_: the bitmask for turning on/off events.
default: all bits turned off
INT32 eventQueueID_:
ID of the queue used to store the events.
default value: parameter qid of the ctor
static
EBool initialized_: is True if the event table is initialized. The
first object that is created should initialize the table in its ctor.
default: False
EBool resync_:
is set to True, if the communication is out of sync, False if everything
is okay.
default: False
CStreamInterface
*socket_: the socket that is the IFC to the evaluation.
static
EEventClass table_[EventCount]: a table for mapping events to their
event classes.
double
waitPeriod_: the period of the base loop in seconds.
default and reset: DefaultIfcPeriod
member functions:
CControllingObject
(char qname[4], ACSocketAddress* addr, SINT32 nodeID = DefaultProcessID):
Creates a queue with the name qname and stores its ID. Also creates
a stream socket with the address addr. Sets all members to their
default values.
virtual
~CControllingObject (): Calls EmptyQueue() to empty the event
queue, then deletes it. Also deletes the socket.
virtual
void Body (): This function is executed in pure simulation and should
be used as main function in hardware-based simulation (ie., call this function
in the task body). It periodically reads the messages from the socket (the
period is obtained by using the pSOS+ system call tm_wkafter())
and calls Set/GetParameter() or ExecuteCommand() accordingly.
Furthermore, it checks the event queue and send all events in the queue
to EvalSys. If a socket message cannot be interpreted or is truncated,
resync is set to True and the resynchronization byte is requested
from the peer. As long as resync is True, all data from the socket
is discarded until the resynchronization byte is recognized. If it was
received, resync is set to False. Both socket and queue are emptied
in every period. At the end of the period, DoPeriodicActivities()
is called.
virtual
void DoPeriodicActivities(): Empty.
The function is called once at the end of every period. Derived classes
should overload it if they have own periodic work to do.
void EmptyQueue
(): Deletes all events that are stored in the queue.
EBool EventReportAllowed
(EEvent event): Returns True if the event should be reported, else
False.
virtual
EBool ExecuteCommand (const SCommand& command): All supported commands
return True.
static
EBool InitEventTable (): Initializes the event table. Returns False
if there are events which do not have a classification. In that case, add
the classification to the code of InitEventTable().
void GetBit
(const CBitMask& bitmask, SEventReport *report) const: Store the
value of a bit queried in report in the appropriate member of
report.
SEvent
*GetEvent (): Returns the next event from the queue, or NULL if the
queue is empty.
virtual
EBool GetParameter (SParameter& param) const: Returns True if the
parameter could be read.
ACSocketAddress
*GetSocketAddress (): Returns the address of the socket.
virtual
void NoteEvent (SEvent *event): The function first timestamps the
event with GetGlobalTime(), then checks if the event may be reported
(i.e., both its type and its class are enabled). If yes, then the event
type is checked, and if the event is of type Snapshot
the event timestamp is set to GetSnapshotTime(). Then, the event
is put into the event queue until it can be sent to the evaluation. If
the event may not be reported, then it is deleted. If event is
NULL, an assert fails.
void ProcessData
(EMessageType msgtype): Process socket messages of type
MsgSetParameter, MsgGetParameter
and MsgCommand. The function first reads the
message body, then calls the appropriate function and calls ReplyToMessage()
with its result (either the return value or the structure of the parameter
being read).
void
ReplyToMessage (EBool value): Sends a MsgReply of type ReplyBoolean
over the socket, which contains the result of a message.
void
ReplyToMessage (double value): Sends a MsgReply of type ReplyDouble
over the socket. The message contains value.
void
ReplyToMessage (SBase *data): Sends a MsgReply of type ReplyStructure
over the socket, which contains the data.
virtual
int ReportError (EReportType type, char *file, int line, char *expression):
Just calls the global function, does not add any own data. It needs to
be there because it is virtual and derived classes may overload it.
virtual
void Reset(): Empties the event queue, resets the event and event
class masks and sets waitPeriod to its reset value.
void Resynchronize():
Asks the evaluation to resynchronize (that is to send the resynchronization
byte) and sets resync to True.
void
SendByteOrder(): Sends the byte-order message over the socket.
void
SendEnumSizes(): Sends the enum-size message over the socket.
void
SendModule (EModule module): Sends the module type over the socket.
If module is not one of Controller or Supervisor,
an assert fails.
virtual
EBool SetParameter (const SParameter& param): Returns True if the
parameter could be set.
void TransmitEvent
(SEvent *event): Sends the event to the evaluation. If event
is NULL, an assert fails.
void UpdateMask
(CBitMask& bitmask, const SEventReport *report): Updates the bitmask
according to the data in report.