OMNeT++ API 6.1
Discrete Event Simulation Library
cpacket.h
1 //==========================================================================
2 // CPACKET.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_CPACKET_H
17 #define __OMNETPP_CPACKET_H
18 
19 #include "cmessage.h"
20 
21 namespace omnetpp {
22 
23 
52 class SIM_API cPacket : public cMessage
53 {
54  private:
55  enum {
56  FL_BITERROR = 8, // has bit errors
57  FL_TXCHANNELSEEN = 16, // encountered a transmission channel during its last send
58  FL_ISUPDATE = 32, // if true, this packet is a transmission update
59  };
60 
61  int64_t bitLength; // length of the packet in bits -- used for bit error and transmission delay modeling
62  simtime_t duration; // transmission duration on last channel with datarate
63  cPacket *encapsulatedPacket = nullptr; // ptr to the encapsulated message
64  unsigned short shareCount = 0; // num of messages MINUS ONE that have this message encapsulated.
65  // 0: not shared (not encapsulated or encapsulated in one message);
66  // 1: shared once (shared among two messages);
67  // 2: shared twice (shared among three messages); etc.
68  // on reaching max sharecount a new packet gets created
69  txid_t transmissionId = -1; // for pairing transmission updates with the original transmission
70  simtime_t remainingDuration; // if transmission update: remaining duration (otherwise it must be equal to the duration)
71 
72  private:
73  void copy(const cPacket& packet);
74 
75  public:
76  // internal: setters used from within send()/sendDirect() and channel code
77  void setDuration(simtime_t d) {duration = d;}
78  void setRemainingDuration(simtime_t d) {remainingDuration = d;}
79  void setTransmissionId(txid_t id) {transmissionId = id;}
80  void setIsUpdate(bool b) {setFlag(FL_ISUPDATE, b);}
81  void setTxChannelEncountered(bool b) {setFlag(FL_TXCHANNELSEEN, b);}
82  void clearTxChannelEncountered() {flags &= ~FL_TXCHANNELSEEN;}
83  void setTxChannelEncountered() {flags |= FL_TXCHANNELSEEN;}
84 
85  // internal (for now)
86  bool getTxChannelEncountered() const {return flags&FL_TXCHANNELSEEN;}
87 
88  // internal convenience method: returns the getId() of the innermost encapsulated message,
89  // or itself if there is no encapsulated message
90  msgid_t getEncapsulationId() const;
91 
92  // internal convenience method: returns getTreeId() of the innermost encapsulated message,
93  // or itself if there is no encapsulated message
94  msgid_t getEncapsulationTreeId() const;
95 
96  cPacket *_getEncapMsg() { return encapsulatedPacket; }
97 
98  // internal: if encapmsg is shared (sharecount>0), creates a private copy for this packet,
99  // and in any case it sets encapmsg's owner to be this object. This method
100  // has to be called before any operation on encapmsg, to prevent trouble
101  // that may arise from accessing shared message instances. E.g. without calling
102  // _detachEncapMsg(), encapmsg's ownerp is unpredictable (may be any previous owner,
103  // possibly not even existing any more) which makes even a call to its getFullPath()
104  // method dangerous.
105  void _detachEncapMsg();
106 
107  // internal: delete encapmsg, paying attention to its sharecount (assumes encapmsg!=nullptr)
108  void _deleteEncapMsg();
109 
110  // internal: only to be used by test cases
111  int getShareCount() const {return shareCount;}
112 
113  public:
119  cPacket(const cPacket& packet);
120 
125  explicit cPacket(const char *name=nullptr, short kind=0, int64_t bitLength=0);
126 
130  virtual ~cPacket();
131 
135  cPacket& operator=(const cPacket& packet);
137 
140 
145  virtual cPacket *dup() const override {return new cPacket(*this);}
146 
151  virtual std::string str() const override;
152 
157  virtual void forEachChild(cVisitor *v) override;
158 
164  virtual void parsimPack(cCommBuffer *buffer) const override;
165 
171  virtual void parsimUnpack(cCommBuffer *buffer) override;
172 
176  virtual bool isPacket() const override {return true;}
177 
182  virtual const char *getDisplayString() const override;
184 
192  virtual void setBitLength(int64_t l);
193 
199  void setByteLength(int64_t l) {setBitLength(l<<3);}
200 
211  virtual void addBitLength(int64_t delta);
212 
219  void addByteLength(int64_t delta) {addBitLength(delta<<3);}
220 
224  virtual int64_t getBitLength() const {return bitLength;}
225 
230  int64_t getByteLength() const {return (getBitLength()+7)>>3;}
231 
235  virtual void setBitError(bool e) {setFlag(FL_BITERROR,e);}
236 
240  virtual bool hasBitError() const {return flags&FL_BITERROR;}
242 
245 
259  virtual void encapsulate(cPacket *packet);
260 
268  virtual cPacket *decapsulate();
269 
277  virtual cPacket *getEncapsulatedPacket() const;
278 
285  virtual bool hasEncapsulatedPacket() const;
287 
298  bool isUpdate() const {return flags&FL_ISUPDATE;}
299 
306  txid_t getTransmissionId() const {return transmissionId;}
307 
322  simtime_t_cref getDuration() const {return duration;}
323 
344  simtime_t_cref getRemainingDuration() const {return remainingDuration;}
345 
350  bool isReceptionStart() const {return duration == remainingDuration;}
351 
356  bool isReceptionEnd() const {return remainingDuration.isZero();}
358 };
359 
360 } // namespace omnetpp
361 
362 #endif
363 
364 
omnetpp::cPacket::getTransmissionId
txid_t getTransmissionId() const
Definition: cpacket.h:306
omnetpp::simtime_t_cref
const typedef simtime_t & simtime_t_cref
Constant reference to a simtime_t.
Definition: simtime_t.h:48
omnetpp::cPacket::getDuration
simtime_t_cref getDuration() const
Definition: cpacket.h:322
omnetpp::msgid_t
int64_t msgid_t
Message ID. See cMessage::getId().
Definition: simkerneldefs.h:85
omnetpp::cPacket::isPacket
virtual bool isPacket() const override
Definition: cpacket.h:176
omnetpp::cPacket::getRemainingDuration
simtime_t_cref getRemainingDuration() const
Definition: cpacket.h:344
omnetpp::cPacket::setBitError
virtual void setBitError(bool e)
Definition: cpacket.h:235
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cPacket::hasBitError
virtual bool hasBitError() const
Definition: cpacket.h:240
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::cPacket::setByteLength
void setByteLength(int64_t l)
Definition: cpacket.h:199
omnetpp::SimTime::isZero
bool isZero() const
Definition: simtime.h:338
omnetpp::cPacket::isReceptionStart
bool isReceptionStart() const
Definition: cpacket.h:350
omnetpp::cPacket::addByteLength
void addByteLength(int64_t delta)
Definition: cpacket.h:219
omnetpp::txid_t
int64_t txid_t
Transmission ID. See SendOptions::transmissionId(), cMessage::getTransmissionId().
Definition: simkerneldefs.h:92
omnetpp::cMessage
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
omnetpp::cPacket::isReceptionEnd
bool isReceptionEnd() const
Definition: cpacket.h:356
omnetpp::cPacket::getBitLength
virtual int64_t getBitLength() const
Definition: cpacket.h:224
omnetpp::cPacket
A subclass of cMessage to represent packets, frames, datagrams, application messages,...
Definition: cpacket.h:52
omnetpp::cPacket::getByteLength
int64_t getByteLength() const
Definition: cpacket.h:230
omnetpp::cPacket::dup
virtual cPacket * dup() const override
Definition: cpacket.h:145
omnetpp::cPacket::isUpdate
bool isUpdate() const
Definition: cpacket.h:298
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41