OMNeT++ Simulation Library  6.0.3
cmessage.h
1 //==========================================================================
2 // CMESSAGE.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CMESSAGE_H
17 #define __OMNETPP_CMESSAGE_H
18 
19 #include <vector>
20 #include "cevent.h"
21 #include "carray.h"
22 #include "cmsgpar.h"
23 #include "csimulation.h"
24 
25 namespace omnetpp {
26 
27 class cMsgPar;
28 class cGate;
29 class cChannel;
30 class cModule;
31 class cSimpleModule;
32 class cSimulation;
33 
42 enum eMessageKind
43 {
44  MK_STARTER = -1,
45  MK_TIMEOUT = -2,
46  MK_PARSIM_BEGIN = -1000
47 };
48 
55 // Note: it cannot go to cparsimcomm.h, without causing unwanted dependency on sim/parsim
56 #define MAX_PARSIM_PARTITIONS 32768 // srcprocid in cMessage
57 
58 
95 class SIM_API cMessage : public cEvent
96 {
97  private:
98  enum {
99  FL_ISPRIVATECOPY = 4,
100  };
101  // note: fields are in an order that maximizes packing (minimizes sizeof(cMessage))
102  short messageKind; // message kind -- 0>= user-defined meaning, <0 reserved
103  short srcProcId = -1; // reserved for use by parallel execution: id of source partition
104  cArray *parList = nullptr; // ptr to list of parameters
105  cObject *controlInfo = nullptr; // ptr to "control info"
106  void *contextPointer = nullptr; // a stored pointer -- user-defined meaning, used with self-messages
107 
108  int senderModuleId = -1; // sender module ID -- set internally
109  int senderGateId = -1; // source gate ID -- set internally
110  int targetModuleId = -1; // destination module ID -- set internally
111  int targetGateId = -1; // destination gate ID -- set internally
112  simtime_t creationTime; // creation time -- set be constructor
113  simtime_t sendTime = 0; // time of sending -- set internally
114  simtime_t timestamp = 0; // time stamp -- user-defined meaning
115 
116  msgid_t messageId; // a unique message identifier assigned upon message creation
117  msgid_t messageTreeId; // a message identifier that is inherited by dup, if non dupped it is msgid
118  static msgid_t nextMessageId; // the next unique message identifier to be assigned upon message creation
119 
120  // global variables for statistics
121  static uint64_t totalMsgCount;
122  static uint64_t liveMsgCount;
123 
124  private:
125  // internal: create parlist
126  void _createparlist();
127 
128  void copy(const cMessage& msg);
129 
130  // internal: used by LogBuffer for creating an *exact* copy of a message
131  void setId(msgid_t id) {messageId = id;}
132 
133  public:
134  // internal: create an exact clone (including msgid) that doesn't show up in the statistics
135  cMessage *privateDup() const;
136 
137  // internal: called by the simulation kernel as part of the send(),
138  // scheduleAt() calls to set the values returned by the
139  // getSenderModuleId(), getSenderGate(), getSendingTime() methods.
140  void setSentFrom(cModule *module, int gateId, simtime_t_cref t);
141 
142  // internal: used by the parallel simulation kernel.
143  void setSrcProcId(int procId) {srcProcId = (short)procId;}
144 
145  // internal: used by the parallel simulation kernel.
146  virtual int getSrcProcId() const override {return srcProcId;}
147 
148  // internal: returns the parameter list object, or nullptr if it hasn't been used yet
149  cArray *getParListPtr() {return parList;}
150 
151  protected: // hide cEvent methods from the cMessage API
152 
153  // overridden from cEvent: return true
154  virtual bool isMessage() const override {return true;}
155 
156  // overridden from cEvent: return true of the target module is still alive and well
157  virtual bool isStale() override;
158 
159  // overridden from cEvent: return the arrival module
160  virtual cObject *getTargetObject() const override;
161 
162  // overridden from cEvent
163  virtual void execute() override;
164 
165  public:
168 
172  cMessage(const cMessage& msg);
173 
177  explicit cMessage(const char *name=nullptr, short kind=0);
178 
182  virtual ~cMessage();
183 
190  cMessage& operator=(const cMessage& msg);
192 
197  virtual bool isPacket() const override {return false;}
198 
201 
208  virtual cMessage *dup() const override {return new cMessage(*this);}
209 
214  virtual std::string str() const override;
215 
220  virtual void forEachChild(cVisitor *v) override;
221 
227  virtual void parsimPack(cCommBuffer *buffer) const override;
228 
234  virtual void parsimUnpack(cCommBuffer *buffer) override;
236 
244  void setKind(short k) {messageKind=k;}
245 
249  void setTimestamp() {timestamp=getSimulation()->getSimTime();}
250 
254  void setTimestamp(simtime_t t) {timestamp=t;}
255 
264  void setContextPointer(void *p) {contextPointer=p;}
265 
281  virtual void setControlInfo(cObject *p);
282 
288  cObject *removeControlInfo();
289 
293  short getKind() const {return messageKind;}
294 
298  simtime_t_cref getTimestamp() const {return timestamp;}
299 
303  void *getContextPointer() const {return contextPointer;}
304 
308  cObject *getControlInfo() const {return controlInfo;}
310 
313 
327  virtual cArray& getParList() {if (!parList) _createparlist(); return *parList;}
328 
339  virtual cMsgPar& addPar(const char *name) {cMsgPar *p=new cMsgPar(name);getParList().add(p);return *p;}
340 
350  virtual cMsgPar& addPar(cMsgPar *par) {getParList().add(par); return *par;}
351 
363  virtual cMsgPar& par(int index);
364 
377  virtual cMsgPar& par(const char *name);
378 
389  virtual int findPar(const char *name) const;
390 
401  virtual bool hasPar(const char *name) const {return findPar(name)>=0;}
402 
412  virtual cObject *addObject(cObject *par) {getParList().add(par); return par;}
413 
424  virtual cObject *getObject(const char *name) {return getParList().get(name);}
425 
435  virtual bool hasObject(const char *name) {return !parList ? false : parList->find(name)>=0;}
436 
447  virtual cObject *removeObject(const char *name) {return getParList().remove(name);}
448 
459  virtual cObject *removeObject(cObject *par) {return getParList().remove(par);}
461 
464 
468  bool isSelfMessage() const {return targetGateId==-1;}
469 
475  cModule *getSenderModule() const {return getSimulation()->getModule(senderModuleId);}
476 
482  cGate *getSenderGate() const;
483 
489  cModule *getArrivalModule() const {return getSimulation()->getModule(targetModuleId);}
490 
496  cGate *getArrivalGate() const;
497 
504  int getSenderModuleId() const {return senderModuleId;}
505 
513  int getSenderGateId() const {return senderGateId;}
514 
521  int getArrivalModuleId() const {return targetModuleId;}
522 
530  int getArrivalGateId() const {return targetGateId;}
531 
537  simtime_t_cref getCreationTime() const {return creationTime;}
538 
543  simtime_t_cref getSendingTime() const {return sendTime;}
544 
559  // note: overridden to provide more specific documentation
560  simtime_t_cref getArrivalTime() const {return arrivalTime;}
561 
565  bool arrivedOn(int gateId) const {return gateId==targetGateId;}
566 
572  bool arrivedOn(const char *gateName) const;
573 
578  bool arrivedOn(const char *gateName, int gateIndex) const;
579 
583  msgid_t getId() const {return messageId;}
584 
589  msgid_t getTreeId() const {return messageTreeId;}
591 
599  virtual const char *getDisplayString() const;
600 
607  void setArrival(int moduleId, int gateId) {targetModuleId = moduleId; targetGateId = gateId;}
608 
613  void setArrival(int moduleId, int gateId, simtime_t_cref t) {targetModuleId = moduleId; targetGateId = gateId; setArrivalTime(t);}
615 
624  static uint64_t getTotalMessageCount() {return totalMsgCount;}
625 
633  static uint64_t getLiveMessageCount() {return liveMsgCount;}
634 
638  static void resetMessageCounters() {totalMsgCount=liveMsgCount=0;}
640 };
641 
642 } // namespace omnetpp
643 
644 #endif
645 
646 
omnetpp::cMessage::hasPar
virtual bool hasPar(const char *name) const
Definition: cmessage.h:401
omnetpp::cMessage::addPar
virtual cMsgPar & addPar(cMsgPar *par)
Definition: cmessage.h:350
omnetpp::simtime_t_cref
const typedef simtime_t & simtime_t_cref
Constant reference to a simtime_t.
Definition: simtime_t.h:48
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cSimulation::getSimTime
simtime_t_cref getSimTime() const
Definition: csimulation.h:400
omnetpp::msgid_t
int64_t msgid_t
Message ID. See cMessage::getId().
Definition: simkerneldefs.h:85
omnetpp::cMessage::arrivedOn
bool arrivedOn(int gateId) const
Definition: cmessage.h:565
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cMessage::getSenderGateId
int getSenderGateId() const
Definition: cmessage.h:513
omnetpp::cMessage::hasObject
virtual bool hasObject(const char *name)
Definition: cmessage.h:435
omnetpp::cMessage::getSendingTime
simtime_t_cref getSendingTime() const
Definition: cmessage.h:543
omnetpp::cMessage::getTotalMessageCount
static uint64_t getTotalMessageCount()
Definition: cmessage.h:624
omnetpp::cMessage::getObject
virtual cObject * getObject(const char *name)
Definition: cmessage.h:424
omnetpp::cMessage::isMessage
virtual bool isMessage() const override
Definition: cmessage.h:154
omnetpp::cEvent
Represents an event in the discrete event simulator.
Definition: cevent.h:46
omnetpp::cMessage::removeObject
virtual cObject * removeObject(cObject *par)
Definition: cmessage.h:459
omnetpp::cMessage::getContextPointer
void * getContextPointer() const
Definition: cmessage.h:303
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cMessage::setArrival
void setArrival(int moduleId, int gateId)
Definition: cmessage.h:607
omnetpp::cMessage::setArrival
void setArrival(int moduleId, int gateId, simtime_t_cref t)
Definition: cmessage.h:613
omnetpp::cMessage::getTreeId
msgid_t getTreeId() const
Definition: cmessage.h:589
omnetpp::cMessage::getSenderModuleId
int getSenderModuleId() const
Definition: cmessage.h:504
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::cMessage::setContextPointer
void setContextPointer(void *p)
Definition: cmessage.h:264
omnetpp::cMessage::getCreationTime
simtime_t_cref getCreationTime() const
Definition: cmessage.h:537
omnetpp::cMessage::removeObject
virtual cObject * removeObject(const char *name)
Definition: cmessage.h:447
omnetpp::cMessage::getLiveMessageCount
static uint64_t getLiveMessageCount()
Definition: cmessage.h:633
omnetpp::cSimulation::getModule
cModule * getModule(int id) const
Definition: csimulation.h:235
omnetpp::cMessage::getControlInfo
cObject * getControlInfo() const
Definition: cmessage.h:308
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:603
omnetpp::cGate
Represents a module gate.
Definition: cgate.h:62
omnetpp::cMessage::resetMessageCounters
static void resetMessageCounters()
Definition: cmessage.h:638
omnetpp::cMessage::isSelfMessage
bool isSelfMessage() const
Definition: cmessage.h:468
omnetpp::cMessage::isPacket
virtual bool isPacket() const override
Definition: cmessage.h:197
omnetpp::cMessage::addPar
virtual cMsgPar & addPar(const char *name)
Definition: cmessage.h:339
omnetpp::cArray
Vector-like container for objects derived from cObject.
Definition: carray.h:38
omnetpp::cMessage::getId
msgid_t getId() const
Definition: cmessage.h:583
omnetpp::cMessage
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
omnetpp::cMessage::addObject
virtual cObject * addObject(cObject *par)
Definition: cmessage.h:412
omnetpp::cMessage::dup
virtual cMessage * dup() const override
Definition: cmessage.h:208
omnetpp::cArray::find
virtual int find(cObject *obj) const
omnetpp::cMessage::getArrivalGateId
int getArrivalGateId() const
Definition: cmessage.h:530
omnetpp::cMessage::getArrivalModule
cModule * getArrivalModule() const
Definition: cmessage.h:489
omnetpp::cMessage::setTimestamp
void setTimestamp()
Definition: cmessage.h:249
omnetpp::cMessage::getArrivalModuleId
int getArrivalModuleId() const
Definition: cmessage.h:521
omnetpp::cMessage::getParList
virtual cArray & getParList()
Definition: cmessage.h:327
omnetpp::cMessage::setTimestamp
void setTimestamp(simtime_t t)
Definition: cmessage.h:254
omnetpp::cMessage::getKind
short getKind() const
Definition: cmessage.h:293
omnetpp::cMessage::getSenderModule
cModule * getSenderModule() const
Definition: cmessage.h:475
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cMessage::getTimestamp
simtime_t_cref getTimestamp() const
Definition: cmessage.h:298
omnetpp::cMsgPar
Allows a value (string, bool, double, etc) to be attached to a cMessage object.
Definition: cmsgpar.h:52
omnetpp::cMessage::getArrivalTime
simtime_t_cref getArrivalTime() const
Definition: cmessage.h:560
omnetpp::cMessage::setKind
void setKind(short k)
Definition: cmessage.h:244