OMNeT++ Simulation Library
5.6.1
|
#include <cmessage.h>
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities in a simulation. To represent network packets, use the cPacket subclass.
Messages may be scheduled (to arrive back at the same module at a later time), cancelled, sent out on a gate, or sent directly to another module; all via methods of cSimpleModule.
cMessage can be assigned a name (a property inherited from cNamedObject); other attributes include message kind, priority, and time stamp. Messages may be cloned with the dup() function. The control info field facilitates modelling communication between protocol layers. The context pointer field makes it easier to work with several timers (self-messages) at a time. A message also stores information about its last sending, including sending time, arrival time, arrival module and gate.
Useful methods are isSelfMessage(), which tells apart self-messages from messages received from other modules, and isScheduled(), which returns whether a self-message is currently scheduled.
Further fields can be added to cMessage via message declaration files (.msg) which are translated into C++ classes. An example message declaration:
Public Member Functions | |
virtual bool | isPacket () const |
Constructors, destructor, assignment | |
cMessage (const cMessage &msg) | |
cMessage (const char *name=nullptr, short kind=0) | |
virtual | ~cMessage () |
cMessage & | operator= (const cMessage &msg) |
Redefined cObject member functions. | |
virtual cMessage * | dup () const override |
virtual std::string | str () const override |
virtual void | forEachChild (cVisitor *v) override |
virtual void | parsimPack (cCommBuffer *buffer) const override |
virtual void | parsimUnpack (cCommBuffer *buffer) override |
Message attributes. | |
void | setKind (short k) |
void | setTimestamp () |
void | setTimestamp (simtime_t t) |
void | setContextPointer (void *p) |
virtual void | setControlInfo (cObject *p) |
cObject * | removeControlInfo () |
short | getKind () const |
simtime_t_cref | getTimestamp () const |
void * | getContextPointer () const |
cObject * | getControlInfo () const |
Dynamically attaching objects. | |
virtual cArray & | getParList () |
virtual cMsgPar & | addPar (const char *name) |
virtual cMsgPar & | addPar (cMsgPar *par) |
virtual cMsgPar & | par (int index) |
virtual cMsgPar & | par (const char *name) |
virtual int | findPar (const char *name) const |
virtual bool | hasPar (const char *name) const |
virtual cObject * | addObject (cObject *par) |
virtual cObject * | getObject (const char *name) |
virtual bool | hasObject (const char *name) |
virtual cObject * | removeObject (const char *name) |
virtual cObject * | removeObject (cObject *par) |
Sending/arrival information. | |
bool | isSelfMessage () const |
cModule * | getSenderModule () const |
cGate * | getSenderGate () const |
cModule * | getArrivalModule () const |
cGate * | getArrivalGate () const |
int | getSenderModuleId () const |
int | getSenderGateId () const |
int | getArrivalModuleId () const |
int | getArrivalGateId () const |
simtime_t_cref | getCreationTime () const |
simtime_t_cref | getSendingTime () const |
simtime_t_cref | getArrivalTime () const |
bool | arrivedOn (int gateId) const |
bool | arrivedOn (const char *gateName) const |
bool | arrivedOn (const char *gateName, int gateIndex) const |
long | getId () const |
long | getTreeId () const |
Miscellaneous. | |
virtual const char * | getDisplayString () const |
void | setArrival (int moduleId, int gateId) |
void | setArrival (int moduleId, int gateId, simtime_t_cref t) |
Public Member Functions inherited from cEvent | |
cEvent (const cEvent &event) | |
cEvent (const char *name) | |
virtual | ~cEvent () |
cEvent & | operator= (const cEvent &event) |
void | setSchedulingPriority (short p) |
short | getSchedulingPriority () const |
bool | isScheduled () const |
simtime_t_cref | getArrivalTime () const |
bool | shouldPrecede (const cEvent *event) const |
Public Member Functions inherited from cOwnedObject | |
cOwnedObject () | |
cOwnedObject (const char *name, bool namepooling=true) | |
cOwnedObject (const cOwnedObject &obj) | |
virtual | ~cOwnedObject () |
cOwnedObject & | operator= (const cOwnedObject &o) |
virtual cObject * | getOwner () const override |
virtual bool | isOwnedObject () const override |
virtual bool | isSoftOwner () const |
Public Member Functions inherited from cNamedObject | |
cNamedObject () | |
cNamedObject (const char *name, bool namepooling=true) | |
cNamedObject (const cNamedObject &obj) | |
virtual | ~cNamedObject () |
cNamedObject & | operator= (const cNamedObject &o) |
virtual void | setName (const char *s) |
virtual const char * | getName () const override |
virtual void | setNamePooling (bool b) |
virtual bool | getNamePooling () |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other) | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
virtual const char * | getFullName () const |
virtual std::string | getFullPath () const |
const cObject * | getThisPtr () const |
virtual _OPPDEPRECATED std::string | info () const |
virtual _OPPDEPRECATED std::string | detailedInfo () const |
cObject * | findObject (const char *name, bool deep=true) |
void | copyNotSupported () const |
Static Public Member Functions | |
Statistics. | |
static long | getTotalMessageCount () |
static long | getLiveMessageCount () |
static void | resetMessageCounters () |
Static Public Member Functions inherited from cOwnedObject | |
static long | getTotalObjectCount () |
static long | getLiveObjectCount () |
static void | resetObjectCounters () |
static cDefaultList * | getDefaultOwner () |
Additional Inherited Members | |
Protected Member Functions inherited from cObject | |
virtual void | take (cOwnedObject *obj) |
virtual void | drop (cOwnedObject *obj) |
void | dropAndDelete (cOwnedObject *obj) |
|
explicit |
Constructor.
|
virtual |
Destructor.
Assignment operator. The data members NOT copied are: object name (see cNamedObject's operator=() for more details) and message ID. All other members, including creation time and message tree ID, are copied.
|
inlinevirtual |
|
inlineoverridevirtual |
Creates and returns an exact copy of this object, except for the message ID (the clone is assigned a new ID). Note that the message creation time is also copied, so clones of the same message object have the same creation time. See cObject for more details.
Implements cEvent.
Reimplemented in cPacket.
|
overridevirtual |
|
overridevirtual |
|
overridevirtual |
|
overridevirtual |
|
inline |
Sets the message kind. Nonnegative values can be freely used by the user; negative values are reserved by OMNeT++ for internal purposes.
|
inline |
Sets the message's time stamp to the current simulation time.
References cSimulation::getSimTime(), and omnetpp::getSimulation().
|
inline |
Directly sets the message's time stamp.
|
inline |
Sets the context pointer. This pointer may store an arbitrary value. It is useful when managing several timers (self-messages): when scheduling the message one can set the context pointer to the data structure the timer corresponds to (e.g. the buffer whose timeout the message represents), so that when the self-message arrives it is easier to identify where it belongs.
|
virtual |
Attaches a "control info" structure (object) to the message. This is most useful when passing packets between protocol layers of a protocol stack: e.g. when sending down an IP datagram to Ethernet, the attached "control info" can contain the destination MAC address.
The "control info" object will be deleted when the message is deleted. Only one "control info" structure can be attached (the second setControlInfo() call throws an error).
When the message is duplicated or copied, copies will have their control info set to nullptr because the cObject interface does not define dup/copy operations. The assignment operator does not change control info.
cObject* removeControlInfo | ( | ) |
Removes the "control info" structure (object) from the message and returns its pointer. Returns nullptr if there was no control info in the message.
|
inline |
Returns the message kind.
|
inline |
Returns the message's time stamp.
|
inline |
Returns the context pointer.
|
inline |
Returns pointer to the attached "control info".
|
inlinevirtual |
Returns reference to the 'object list' of the message: a cArray which is used to store parameter (cMsgPar) objects and other objects attached to the message.
One can use either getParList() combined with cArray methods, or several convenience methods (addPar(), addObject(), par(), etc.) to add, retrieve or remove cMsgPars and other objects.
NOTE: using the object list has alternatives which may better suit your needs. For more information, see class description for discussion about message subclassing vs dynamically attached objects.
|
inlinevirtual |
Add a new, empty parameter (cMsgPar object) with the given name to the message's object list.
NOTE: This is a convenience function: one may use getParList() and cArray::add() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
Add a parameter object to the message's object list.
NOTE: This is a convenience function: one may use getParList() and cArray::add() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
virtual |
Returns the nth object in the message's object list, converting it to a cMsgPar. If the object does not exist or it cannot be cast to cMsgPar (using dynamic_cast<>), the method throws a cRuntimeError.
NOTE: This is a convenience function: one may use getParList() and cArray::get() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
virtual |
Returns the object with the given name in the message's object list, converting it to a cMsgPar. If the object does not exist or it cannot be cast to cMsgPar (using dynamic_cast<>), the method throws a cRuntimeError.
NOTE: This is a convenience function: one may use getParList() and cArray::get() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
virtual |
Returns the index of the parameter with the given name in the message's object list, or -1 if it could not be found.
NOTE: This is a convenience function: one may use getParList() and cArray::find() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
inlinevirtual |
Check if a parameter with the given name exists in the message's object list.
NOTE: This is a convenience function: one may use getParList() and cArray::exist() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
Add an object to the message's object list.
NOTE: This is a convenience function: one may use getParList() and cArray::add() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
inlinevirtual |
Returns the object with the given name in the message's object list. If the object is not found, it returns nullptr.
NOTE: This is a convenience function: one may use getParList() and cArray::get() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
inlinevirtual |
Check if an object with the given name exists in the message's object list.
NOTE: This is a convenience function: one may use getParList() and cArray::exist() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
References cArray::find().
|
inlinevirtual |
Remove the object with the given name from the message's object list, and return its pointer. If the object does not exist, nullptr is returned.
NOTE: This is a convenience function: one may use getParList() and cArray::remove() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
Remove the object with the given name from the message's object list, and return its pointer. If the object does not exist, nullptr is returned.
NOTE: This is a convenience function: one may use getParList() and cArray::remove() instead. See also class description for discussion about message subclassing vs dynamically attached objects.
|
inline |
Return true if message was posted by scheduleAt().
|
inline |
Returns a pointer to the sender module. It returns nullptr if the message has not been sent/scheduled yet, or if the sender module got deleted in the meantime.
References cSimulation::getModule(), and omnetpp::getSimulation().
cGate* getSenderGate | ( | ) | const |
Returns pointers to the gate from which the message was sent and on which gate it arrived. nullptr is returned for new (unsent) messages and messages sent via scheduleAt().
|
inline |
Returns a pointer to the arrival module. It returns nullptr if the message has not been sent/scheduled yet, or if the module was deleted in the meantime.
References cSimulation::getModule(), and omnetpp::getSimulation().
cGate* getArrivalGate | ( | ) | const |
Returns pointers to the gate from which the message was sent and on which gate it arrived. nullptr is returned for new (unsent) messages and messages sent via scheduleAt().
|
inline |
Returns the module ID of the sender module, or -1 if the message has not been sent/scheduled yet.
|
inline |
Returns the gate ID of the gate in the sender module on which the message was sent, or -1 if the message has not been sent/scheduled yet. Note: this is not the same as the gate's index (cGate::getIndex()).
|
inline |
Returns the module ID of the receiver module, or -1 if the message has not been sent/scheduled yet.
|
inline |
Returns the gate ID of the gate in the receiver module on which the message was received, or -1 if the message has not been sent/scheduled yet. Note: this is not the same as the gate's index (cGate::getIndex()).
|
inline |
Returns time when the message was created; for cloned messages, it returns the creation time of the original message, not the time of the dup() call.
|
inline |
Returns time when the message was sent/scheduled or 0 if the message has not been sent yet.
|
inline |
Returns time when the message arrived (or will arrive if it is currently scheduled or is underway), or 0 if the message has not been sent/scheduled yet.
When the message has nonzero length and it travelled though a channel with nonzero data rate, arrival time may represent either the start or the end of the reception, as returned by the isReceptionStart() method. By default it is the end of the reception; it can be changed by calling setDeliverOnReceptionStart(true) on the gate at receiving end of the channel that has the nonzero data rate.
|
inline |
Return true if the message arrived through the given gate.
bool arrivedOn | ( | const char * | gateName | ) | const |
Return true if the message arrived on the gate given with its name. If it is a vector gate, the method returns true if the message arrived on any gate in the vector.
bool arrivedOn | ( | const char * | gateName, |
int | gateIndex | ||
) | const |
Return true if the message arrived through the given gate in the named gate vector.
|
inline |
Returns a unique message identifier assigned upon message creation.
|
inline |
Returns an identifier which is shared among a message object and all messages created by copying it (i.e. by dup() or the copy constructor).
|
virtual |
Override to define a display string for the message. Display string affects message appearance in Tkenv. This default implementation returns "".
Reimplemented in cPacket.
|
inline |
For use by custom scheduler classes (see cScheduler): set the arrival module and gate for messages inserted into the FES directly by the scheduler. If you pass gateId=-1, the message will arrive as a self-message.
|
inline |
Like setArrival(int moduleId, int gateId), but also sets the arrival time for the message.
|
inlinestatic |
Returns the total number of messages created since the last reset (reset is usually called by user interfaces at the beginning of each simulation run). The counter is incremented by cMessage constructor. Counter is signed
to make it easier to detect if it overflows during very long simulation runs. May be useful for profiling or debugging memory leaks.
|
inlinestatic |
Returns the number of message objects that currently exist in the program. The counter is incremented by cMessage constructor and decremented by the destructor. May be useful for profiling or debugging memory leaks caused by forgetting to delete messages.
|
inlinestatic |
Reset counters used by getTotalMessageCount() and getLiveMessageCount().