cmessage.h

00001 //==========================================================================
00002 //   CMESSAGE.H  -  header for
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cMessage : message and event object
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2008 Andras Varga
00014   Copyright (C) 2006-2008 OpenSim Ltd.
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CMESSAGE_H
00021 #define __CMESSAGE_H
00022 
00023 #include "cownedobject.h"
00024 #include "carray.h"
00025 #include "cmsgpar.h"
00026 #include "csimulation.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00030 class cMsgPar;
00031 class cGate;
00032 class cChannel;
00033 class cModule;
00034 class cSimpleModule;
00035 class cSimulation;
00036 class cMessageHeap;
00037 class LogBuffer;
00038 
00039 
00048 enum eMessageKind
00049 {
00050   MK_STARTER = -1,  
00051   MK_TIMEOUT = -2,  
00052   MK_PACKET  = -3,  
00053   MK_INFO    = -4,  
00054 
00055   MK_PARSIM_BEGIN = -1000  
00056 };
00057 
00064 // Note: it cannot go to cparsimcomm.h, without causing unwanted dependency on sim/parsim
00065 #define MAX_PARSIM_PARTITIONS  32768 // srcprocid in cMessage
00066 
00067 
00068 #ifdef WITHOUT_CPACKET
00069 #undef cMessage
00070 #endif
00071 
00109 //XXX note: sizeof(cMessage) could be made a little smaller: the fields created, frommod, fromgate are rarely used by models (note: sendtime and tstamp *do* get used in practice)
00110 class SIM_API cMessage : public cOwnedObject
00111 {
00112     friend class cMessageHeap;
00113     friend class LogBuffer;  // for setMessageId()
00114 
00115   private:
00116     // note: fields are in an order that maximizes packing (minimizes sizeof(cMessage))
00117     short msgkind;             // message kind -- 0>= user-defined meaning, <0 reserved
00118     short prior;               // priority -- used for scheduling msgs with equal times
00119     short srcprocid;           // reserved for use by parallel execution: id of source partition
00120     cArray *parlistp;          // ptr to list of parameters
00121     cObject *ctrlp;            // ptr to "control info"
00122     void *contextptr;          // a stored pointer -- user-defined meaning, used with self-messages
00123 
00124     int frommod, fromgate;     // source module and gate IDs -- set internally
00125     int tomod, togate;         // dest. module and gate IDs -- set internally
00126     simtime_t created;         // creation time -- set be constructor
00127     simtime_t sent,delivd;     // time of sending & delivery -- set internally
00128     simtime_t tstamp;          // time stamp -- user-defined meaning
00129 
00130     int heapindex;             // used by cMessageHeap (-1 if not on heap; all other values, including negative ones, means "on the heap")
00131     unsigned long insertordr;  // used by cMessageHeap
00132 
00133     eventnumber_t prev_event_num; // event number of the last time envir was notified about this message (e.g. creating/cloning/sending/scheduling/deleting this message)
00134 
00135     long msgid;                // a unique message identifier assigned upon message creation
00136     long msgtreeid;            // a message identifier that is inherited by dup, if non dupped it is msgid
00137     static long next_id;       // the next unique message identifier to be assigned upon message creation
00138 
00139     // global variables for statistics
00140     static long total_msgs;
00141     static long live_msgs;
00142 
00143   private:
00144     // internal: create parlist
00145     void _createparlist();
00146 
00147     void copy(const cMessage& msg);
00148 
00149     // internal: used by LogBuffer for creating an *exact* copy of a message
00150     void setId(long id) {msgid = id;}
00151 
00152   public:
00153     // internal: returns the event number which scheduled this event, or the event in which
00154     // this message was last delivered to a module. Stored for recording into the event log file.
00155     eventnumber_t getPreviousEventNumber() const {return prev_event_num;}
00156 
00157     // internal: sets previousEventNumber.
00158     void setPreviousEventNumber(eventnumber_t num) {prev_event_num = num;}
00159 
00160     // internal: used by cMessageHeap.
00161     unsigned long getInsertOrder() const {return insertordr;}
00162 
00163     // internal: called by the simulation kernel as part of the send(),
00164     // scheduleAt() calls to set the values returned by the
00165     // getSenderModuleId(), getSenderGate(), getSendingTime() methods.
00166     void setSentFrom(cModule *module, int gateId, simtime_t_cref t);
00167 
00168     // internal: called by the simulation kernel as part of processing
00169     // the send(), scheduleAt() calls to set the values returned
00170     // by the getArrivalModuleId(), getArrivalGate() methods.
00171     void setArrival(cModule *module, int gateId);
00172 
00173     // internal: called by the simulation kernel as part of processing
00174     // the send(), scheduleAt() calls to set the values returned
00175     // by the getArrivalModuleId(), getArrivalGate(), getArrivalTime() methods.
00176     void setArrival(cModule *module, int gate, simtime_t_cref t);
00177 
00178     // internal: called by the simulation kernel to set the value returned
00179     // by the getArrivalTime() method
00180     void setArrivalTime(simtime_t t);
00181 
00182     // internal: used by the parallel simulation kernel.
00183     void setSrcProcId(int procId) {srcprocid = (short)procId;}
00184 
00185     // internal: used by the parallel simulation kernel.
00186     int getSrcProcId() const {return srcprocid;}
00187 
00188   public:
00191 
00195     cMessage(const cMessage& msg);
00196 
00200     explicit cMessage(const char *name=NULL, short kind=0);
00201 
00205     virtual ~cMessage();
00206 
00213     cMessage& operator=(const cMessage& msg);
00215 
00220     virtual bool isPacket() const {return false;}
00221 
00224 
00231     virtual cMessage *dup() const  {return new cMessage(*this);}
00232 
00237     virtual std::string info() const;
00238 
00243     virtual std::string detailedInfo() const;
00244 
00249     virtual void forEachChild(cVisitor *v);
00250 
00256     virtual void parsimPack(cCommBuffer *buffer);
00257 
00263     virtual void parsimUnpack(cCommBuffer *buffer);
00265 
00268 
00274     void setKind(short k)  {msgkind=k;}
00275 
00281     void setSchedulingPriority(short p)  {prior=p;}
00282 
00286     void setTimestamp() {tstamp=simulation.getSimTime();}
00287 
00291     void setTimestamp(simtime_t t) {tstamp=t;}
00292 
00301     void setContextPointer(void *p) {contextptr=p;}
00302 
00318     void setControlInfo(cObject *p);
00319 
00325     cObject *removeControlInfo();
00326 
00330     short getKind() const  {return msgkind;}
00331 
00335     short getSchedulingPriority() const  {return prior;}
00336 
00340     simtime_t_cref getTimestamp() const {return tstamp;}
00341 
00345     void *getContextPointer() const {return contextptr;}
00346 
00350     cObject *getControlInfo() const {return ctrlp;}
00352 
00355 
00369     virtual cArray& getParList()  {if (!parlistp) _createparlist(); return *parlistp;}
00370 
00381     virtual cMsgPar& addPar(const char *s)  {cMsgPar *p=new cMsgPar(s);getParList().add(p);return *p;}
00382 
00392     virtual cMsgPar& addPar(cMsgPar *p)  {getParList().add(p); return *p;}
00393 
00397     _OPPDEPRECATED cMsgPar& addPar(cMsgPar& p)  {return addPar(&p);}
00398 
00410     virtual cMsgPar& par(int n);
00411 
00424     virtual cMsgPar& par(const char *s);
00425 
00436     virtual int findPar(const char *s) const;
00437 
00448     virtual bool hasPar(const char *s) const {return findPar(s)>=0;}
00449 
00459     virtual cObject *addObject(cObject *p)  {getParList().add(p); return p;}
00460 
00471     virtual cObject *getObject(const char *s)  {return getParList().get(s);}
00472 
00482     virtual bool hasObject(const char *s)  {return !parlistp ? false : parlistp->find(s)>=0;}
00483 
00494     virtual cObject *removeObject(const char *s)  {return getParList().remove(s);}
00495 
00506     virtual cObject *removeObject(cObject *p)  {return getParList().remove(p);}
00508 
00511 
00515     bool isSelfMessage() const {return togate==-1;}
00516 
00520     bool isScheduled() const {return heapindex!=-1;}
00521 
00527     cModule *getSenderModule() const {return simulation.getModule(frommod);}
00528 
00534     cGate *getSenderGate() const;
00535 
00541     cModule *getArrivalModule() const {return simulation.getModule(tomod);}
00542 
00548     cGate *getArrivalGate() const;
00549 
00556     int getSenderModuleId() const {return frommod;}
00557 
00565     int getSenderGateId() const   {return fromgate;}
00566 
00573     int getArrivalModuleId() const {return tomod;}
00574 
00582     int getArrivalGateId() const  {return togate;}
00583 
00589     simtime_t_cref getCreationTime() const {return created;}
00590 
00595     simtime_t_cref getSendingTime()  const {return sent;}
00596 
00611     simtime_t_cref getArrivalTime()  const {return delivd;}
00612 
00616     bool arrivedOn(int gateId) const {return gateId==togate;}
00617 
00623     bool arrivedOn(const char *gatename) const;
00624 
00629     bool arrivedOn(const char *gatename, int gateindex) const;
00630 
00634     long getId() const {return msgid;}
00635 
00640     long getTreeId() const {return msgtreeid;}
00642 
00645 
00651     virtual const char *getDisplayString() const;
00653 
00664     static long getTotalMessageCount() {return total_msgs;}
00665 
00673     static long getLiveMessageCount() {return live_msgs;}
00674 
00678     static void resetMessageCounters()  {total_msgs=live_msgs=0;}
00680 };
00681 
00682 
00710 class SIM_API cPacket : public cMessage
00711 {
00712   private:
00713     enum {
00714         FL_ISRECEPTIONSTART = 4,
00715         FL_BITERROR = 8,
00716     };
00717     int64 len;            // length of the packet in bits -- used for bit error and transmission delay modeling
00718     simtime_t duration;   // transmission duration on last channel with datarate
00719     cPacket *encapmsg;    // ptr to the encapsulated message
00720     unsigned short sharecount; // num of messages MINUS ONE that have this message encapsulated.
00721                                // 0: not shared (not encapsulated or encapsulated in one message);
00722                                // 1: shared once (shared among two messages);
00723                                // 2: shared twice (shared among three messages); etc.
00724                                // on reaching max sharecount a new packet gets created
00725 
00726   private:
00727     void copy(const cPacket& packet);
00728 
00729   public:
00730     // internal: sets the message duration; called by channel objects and sendDirect
00731     void setDuration(simtime_t d) {duration = d;}
00732 
00733     // internal: sets the isReceptionStart() flag
00734     void setReceptionStart(bool b) {setFlag(FL_ISRECEPTIONSTART, b);}
00735 
00736     // internal convenience method: returns the getId() of the innermost encapsulated message,
00737     // or itself if there is no encapsulated message
00738     long getEncapsulationId() const;
00739 
00740     // internal convenience method: returns getTreeId() of the innermost encapsulated message,
00741     // or itself if there is no encapsulated message
00742     long getEncapsulationTreeId() const;
00743 
00744     // internal: if encapmsg is shared (sharecount>0), creates a private copy for this packet,
00745     // and in any case it sets encapmsg's owner to be this object. This method
00746     // has to be called before any operation on encapmsg, to prevent trouble
00747     // that may arise from accessing shared message instances. E.g. without calling
00748     // _detachEncapMsg(), encapmsg's ownerp is unpredictable (may be any previous owner,
00749     // possibly not even existing any more) which makes even a call to its getFullPath()
00750     // method dangerous.
00751     void _detachEncapMsg();
00752 
00753     // internal: delete encapmsg, paying attention to its sharecount (assumes encapmsg!=NULL)
00754     void _deleteEncapMsg();
00755 
00756     // internal: only to be used by test cases
00757     int getShareCount() const {return sharecount;}
00758 
00759   public:
00765     cPacket(const cPacket& packet);
00766 
00771     explicit cPacket(const char *name=NULL, short kind=0, int64 bitLength=0);
00772 
00776     virtual ~cPacket();
00777 
00782     cPacket& operator=(const cPacket& packet);
00784 
00787 
00792     virtual cPacket *dup() const  {return new cPacket(*this);}
00793 
00798     virtual std::string info() const;
00799 
00804     virtual std::string detailedInfo() const;
00805 
00810     virtual void forEachChild(cVisitor *v);
00811 
00817     virtual void parsimPack(cCommBuffer *buffer);
00818 
00824     virtual void parsimUnpack(cCommBuffer *buffer);
00825 
00829     virtual bool isPacket() const {return true;}
00831 
00839     virtual void setBitLength(int64 l);
00840 
00846     void setByteLength(int64 l)  {setBitLength(l<<3);}
00847 
00858     virtual void addBitLength(int64 delta);
00859 
00866     void addByteLength(int64 delta)  {addBitLength(delta<<3);}
00867 
00871     virtual int64 getBitLength() const  {return len;}
00872 
00877     int64 getByteLength() const  {return (len+7)>>3;}
00878 
00882     virtual void setBitError(bool e) {setFlag(FL_BITERROR,e);}
00883 
00887     virtual bool hasBitError() const {return flags&FL_BITERROR;}
00889 
00892 
00906     virtual void encapsulate(cPacket *packet);
00907 
00915     virtual cPacket *decapsulate();
00916 
00924     virtual cPacket *getEncapsulatedPacket() const;
00925 
00930     _OPPDEPRECATED cPacket *getEncapsulatedMsg() const {return getEncapsulatedPacket();}
00931 
00938     virtual bool hasEncapsulatedPacket() const;
00940 
00949     simtime_t_cref getDuration() const {return duration;}
00950 
00959     bool isReceptionStart() const {return flags & FL_ISRECEPTIONSTART;}
00961 };
00962 
00963 #ifdef WITHOUT_CPACKET
00964 #define cMessage cPacket /* restore #define in simkerneldefs.h */
00965 #endif
00966 
00967 NAMESPACE_END
00968 
00969 #endif
00970 
00971 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3