INET Framework for OMNeT++/OMNEST
inet::Flood Class Reference

A simple flooding protocol. More...

#include <Flood.h>

Inheritance diagram for inet::Flood:
inet::NetworkProtocolBase inet::INetworkProtocol inet::LayeredProtocolBase inet::OperationalBase inet::ILifecycle

Classes

class  Bcast
 

Public Member Functions

 Flood ()
 
virtual int numInitStages () const override
 Initialization of omnetpp.ini parameters. More...
 
virtual void initialize (int) override
 Reads all parameters from the ini file. More...
 
virtual void finish () override
 
- Public Member Functions inherited from inet::OperationalBase
 OperationalBase ()
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 
- Public Member Functions inherited from inet::INetworkProtocol
virtual ~INetworkProtocol ()
 

Protected Types

typedef std::list< BcastcBroadcastList
 

Protected Member Functions

virtual void handleUpperPacket (cPacket *) override
 Handle messages from upper layer. More...
 
virtual void handleLowerPacket (cPacket *) override
 Handle messages from lower layer. More...
 
bool notBroadcasted (FloodDatagram *)
 Checks whether a message was already broadcasted. More...
 
cMessage * decapsMsg (FloodDatagram *)
 Decapsulates the packet from the received Network packet. More...
 
FloodDatagramencapsMsg (cPacket *)
 Encapsulates the received ApplPkt into a NetwPkt and set all needed header fields. More...
 
virtual cObject * setDownControlInfo (cMessage *const pMsg, const MACAddress &pDestAddr)
 Attaches a "control info" (NetwToMac) structure (object) to the message pMsg. More...
 
- Protected Member Functions inherited from inet::NetworkProtocolBase
 NetworkProtocolBase ()
 
virtual void handleUpperCommand (cMessage *message) override
 
virtual void sendUp (cMessage *message, int transportProtocol)
 
virtual void sendDown (cMessage *message, int interfaceId=-1)
 
virtual bool isUpperMessage (cMessage *message) override
 
virtual bool isLowerMessage (cMessage *message) override
 
virtual bool isInitializeStage (int stage) override
 
virtual bool isNodeStartStage (int stage) override
 
virtual bool isNodeShutdownStage (int stage) override
 
- Protected Member Functions inherited from inet::LayeredProtocolBase
virtual void handleMessageWhenUp (cMessage *message) override
 
virtual void handleSelfMessage (cMessage *message)
 
virtual void handleLowerCommand (cMessage *message)
 
- Protected Member Functions inherited from inet::OperationalBase
virtual void handleMessage (cMessage *msg) override
 
virtual void handleMessageWhenDown (cMessage *msg)
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
virtual bool handleNodeStart (IDoneCallback *doneCallback)
 
virtual bool handleNodeShutdown (IDoneCallback *doneCallback)
 
virtual void handleNodeCrash ()
 
virtual void setOperational (bool isOperational)
 

Protected Attributes

unsigned long seqNum = 0
 Network layer sequence number. More...
 
L3Address myNetwAddr
 cached variable of my networ address More...
 
int headerLength = 0
 Length of the header. More...
 
int defaultTtl = 0
 Default time-to-live (ttl) used for this module. More...
 
bool plainFlooding = false
 Defines whether to use plain flooding or not. More...
 
cBroadcastList bcMsgs
 List of already broadcasted messages. More...
 
unsigned int bcMaxEntries = 0
 Max number of entries in the list of already broadcasted messages. More...
 
simtime_t bcDelTime
 Time after which an entry for an already broadcasted msg can be deleted. More...
 
long nbDataPacketsReceived = 0
 
long nbDataPacketsSent = 0
 
long nbDataPacketsForwarded = 0
 
long nbHops = 0
 
- Protected Attributes inherited from inet::NetworkProtocolBase
ProtocolMapping protocolMapping
 
IInterfaceTableinterfaceTable
 
- Protected Attributes inherited from inet::OperationalBase
bool isOperational
 
simtime_t lastChange
 

Additional Inherited Members

- Static Public Attributes inherited from inet::LayeredProtocolBase
static simsignal_t packetSentToUpperSignal = registerSignal("packetSentToUpper")
 
static simsignal_t packetReceivedFromUpperSignal = registerSignal("packetReceivedFromUpper")
 
static simsignal_t packetFromUpperDroppedSignal = registerSignal("packetFromUpperDropped")
 
static simsignal_t packetSentToLowerSignal = registerSignal("packetSentToLower")
 
static simsignal_t packetReceivedFromLowerSignal = registerSignal("packetReceivedFromLower")
 
static simsignal_t packetFromLowerDroppedSignal = registerSignal("packetFromLowerDropped")
 

Detailed Description

A simple flooding protocol.

This implementation uses plain flooding, i.e. it "remembers" (stores) already broadcasted messages in a list and does not rebroadcast them again, if it gets another copy of that message.

The maximum number of entires for that list can be defined in the .ini file (bcMaxEntries) as well as the time after which an entry is deleted (bcDelTime).

If you prefere a memory-less version you can comment out the

#define PLAINFLOODING 
Author
Daniel Willkomm

ported to Mixim 2.0 by Theodoros Kapourniotis

Member Typedef Documentation

typedef std::list<Bcast> inet::Flood::cBroadcastList
protected

Constructor & Destructor Documentation

inet::Flood::Flood ( )
inline
108 {}

Member Function Documentation

cMessage * inet::Flood::decapsMsg ( FloodDatagram floodDatagram)
protected

Decapsulates the packet from the received Network packet.

Referenced by handleLowerPacket().

250 {
251  SimpleNetworkProtocolControlInfo *controlInfo = new SimpleNetworkProtocolControlInfo();
252  controlInfo->setSourceAddress(floodDatagram->getSourceAddress());
253  controlInfo->setProtocol(floodDatagram->getTransportProtocol());
254  cPacket *transportPacket = floodDatagram->decapsulate();
255  transportPacket->setControlInfo(controlInfo);
256  delete floodDatagram;
257  return transportPacket;
258 }
FloodDatagram * inet::Flood::encapsMsg ( cPacket *  appPkt)
protected

Encapsulates the received ApplPkt into a NetwPkt and set all needed header fields.

Referenced by handleUpperPacket().

265 {
266  L3Address netwAddr;
267 
268  EV << "in encaps...\n";
269 
270  INetworkProtocolControlInfo *cInfo = check_and_cast_nullable<INetworkProtocolControlInfo *>(appPkt->removeControlInfo());
271  FloodDatagram *pkt = new FloodDatagram(appPkt->getName(), appPkt->getKind());
272  pkt->setBitLength(headerLength);
273 
274  if (cInfo == nullptr) {
275  EV << "warning: Application layer did not specifiy a destination L3 address\n"
276  << "\tusing broadcast address instead\n";
277  netwAddr = netwAddr.getAddressType()->getBroadcastAddress();
278  }
279  else {
280  pkt->setTransportProtocol(cInfo->getTransportProtocol());
281  netwAddr = cInfo->getDestinationAddress();
282  EV << "CInfo removed, netw addr=" << netwAddr << endl;
283  delete cInfo;
284  }
285 
286  pkt->setSrcAddr(myNetwAddr);
287  pkt->setDestAddr(netwAddr);
288  EV << " netw " << myNetwAddr << " sending packet" << endl;
289 
290  EV << "sendDown: nHop=L3BROADCAST -> message has to be broadcasted"
291  << " -> set destMac=L2BROADCAST" << endl;
292 
294 
295  //encapsulate the application packet
296  pkt->encapsulate(appPkt);
297  EV << " pkt encapsulated\n";
298  return pkt;
299 }
L3Address myNetwAddr
cached variable of my networ address
Definition: Flood.h:60
virtual cObject * setDownControlInfo(cMessage *const pMsg, const MACAddress &pDestAddr)
Attaches a "control info" (NetwToMac) structure (object) to the message pMsg.
Definition: Flood.cc:304
int headerLength
Length of the header.
Definition: Flood.h:63
static const MACAddress BROADCAST_ADDRESS
The broadcast MAC address, ff:ff:ff:ff:ff:ff.
Definition: MACAddress.h:60
void inet::Flood::finish ( )
overridevirtual
72 {
73  if (plainFlooding) {
74  bcMsgs.clear();
75  }
76  recordScalar("nbDataPacketsReceived", nbDataPacketsReceived);
77  recordScalar("nbDataPacketsSent", nbDataPacketsSent);
78  recordScalar("nbDataPacketsForwarded", nbDataPacketsForwarded);
79  if (nbDataPacketsReceived > 0) {
80  recordScalar("meanNbHops", (double)nbHops / (double)nbDataPacketsReceived);
81  }
82  else {
83  recordScalar("meanNbHops", 0);
84  }
85 }
long nbDataPacketsReceived
Definition: Flood.h:102
long nbHops
Definition: Flood.h:105
cBroadcastList bcMsgs
List of already broadcasted messages.
Definition: Flood.h:88
bool plainFlooding
Defines whether to use plain flooding or not.
Definition: Flood.h:69
long nbDataPacketsForwarded
Definition: Flood.h:104
long nbDataPacketsSent
Definition: Flood.h:103
void inet::Flood::handleLowerPacket ( cPacket *  m)
overrideprotectedvirtual

Handle messages from lower layer.

Messages from the mac layer will be forwarded to the application only if the are broadcast or destined for this node.

If the arrived message is a broadcast message it is also reflooded only if the tll field is bigger than one. Before the message is handed back to the mac layer the ttl field is reduced by one to account for this hop.

In the case of plain flooding the message will only be processed if there is no corresponding entry in the bcMsgs list (notBroadcasted). Otherwise the message will be deleted.

Implements inet::LayeredProtocolBase.

145 {
146  FloodDatagram *msg = check_and_cast<FloodDatagram *>(m);
147  int protocol = msg->getTransportProtocol();
148 
149  //msg not broadcasted yet
150  if (notBroadcasted(msg)) {
151  //msg is for me
152  if (interfaceTable->isLocalAddress(msg->getDestinationAddress())) {
153  EV << " data msg for me! send to Upper" << endl;
154  nbHops = nbHops + (defaultTtl + 1 - msg->getTtl());
155  sendUp(decapsMsg(msg), protocol);
157  }
158  //broadcast message
159  else if (msg->getDestinationAddress().isBroadcast()) {
160  //check ttl and rebroadcast
161  if (msg->getTtl() > 1) {
162  FloodDatagram *dMsg;
163  EV << " data msg BROADCAST! ttl = " << msg->getTtl()
164  << " > 1 -> rebroadcast msg & send to upper\n";
165  msg->setTtl(msg->getTtl() - 1);
166  dMsg = msg->dup();
168  sendDown(dMsg);
170  }
171  else
172  EV << " max hops reached (ttl = " << msg->getTtl() << ") -> only send to upper\n";
173 
174  // message has to be forwarded to upper layer
175  nbHops = nbHops + (defaultTtl + 1 - msg->getTtl());
176  sendUp(decapsMsg(msg), protocol);
178  }
179  //not for me -> rebroadcast
180  else {
181  //check ttl and rebroadcast
182  if (msg->getTtl() > 1) {
183  EV << " data msg not for me! ttl = " << msg->getTtl()
184  << " > 1 -> forward\n";
185  msg->setTtl(msg->getTtl() - 1);
186  // needs to set the next hop address again to broadcast
187  cObject *const pCtrlInfo = msg->removeControlInfo();
188  if (pCtrlInfo != nullptr)
189  delete pCtrlInfo;
191  sendDown(msg);
193  }
194  else {
195  //max hops reached -> delete
196  EV << " max hops reached (ttl = " << msg->getTtl() << ") -> delete msg\n";
197  delete msg;
198  }
199  }
200  }
201  else {
202  EV << " data msg already BROADCASTed! delete msg\n";
203  delete msg;
204  }
205 }
long nbDataPacketsReceived
Definition: Flood.h:102
uint8_t protocol
Definition: TCP_NSC.cc:88
virtual bool isLocalAddress(const L3Address &address) const =0
Checks if the address is a local one, i.e.
bool notBroadcasted(FloodDatagram *)
Checks whether a message was already broadcasted.
Definition: Flood.cc:216
long nbHops
Definition: Flood.h:105
virtual void sendUp(cMessage *message, int transportProtocol)
Definition: NetworkProtocolBase.cc:46
long nbDataPacketsForwarded
Definition: Flood.h:104
cMessage * decapsMsg(FloodDatagram *)
Decapsulates the packet from the received Network packet.
Definition: Flood.cc:249
IInterfaceTable * interfaceTable
Definition: NetworkProtocolBase.h:32
virtual cObject * setDownControlInfo(cMessage *const pMsg, const MACAddress &pDestAddr)
Attaches a "control info" (NetwToMac) structure (object) to the message pMsg.
Definition: Flood.cc:304
int defaultTtl
Default time-to-live (ttl) used for this module.
Definition: Flood.h:66
virtual void sendDown(cMessage *message, int interfaceId=-1)
Definition: NetworkProtocolBase.cc:53
value< double, units::m > m
Definition: Units.h:1047
static const MACAddress BROADCAST_ADDRESS
The broadcast MAC address, ff:ff:ff:ff:ff:ff.
Definition: MACAddress.h:60
void inet::Flood::handleUpperPacket ( cPacket *  m)
overrideprotectedvirtual

Handle messages from upper layer.

All messages have to get a sequence number and the ttl filed has to be specified.

Afterwards the messages can be handed to the mac layer. The mac address is set to -1 (broadcast address) because the message is flooded (i.e. has to be send to all neighbors)

In the case of plain flooding the message sequence number and source address has also be stored in the bcMsgs list, so that this message will not be rebroadcasted, if a copy will be flooded back from the neigbouring nodes.

If the maximum number of entries is reached the first (oldest) entry is deleted.

Implements inet::LayeredProtocolBase.

102 {
103  FloodDatagram *msg = encapsMsg(m);
104 
105  msg->setSeqNum(seqNum);
106  seqNum++;
107  msg->setTtl(defaultTtl);
108 
109  if (plainFlooding) {
110  if (bcMsgs.size() >= bcMaxEntries) {
111  //serach the broadcast list of outdated entries and delete them
112  for (auto it = bcMsgs.begin(); it != bcMsgs.end(); ) {
113  if (it->delTime < simTime())
114  it = bcMsgs.erase(it);
115  else
116  ++it;
117  }
118  //delete oldest entry if max size is reached
119  if (bcMsgs.size() >= bcMaxEntries) {
120  EV << "bcMsgs is full, delete oldest entry" << endl;
121  bcMsgs.pop_front();
122  }
123  }
124  bcMsgs.push_back(Bcast(msg->getSeqNum(), msg->getSourceAddress(), simTime() + bcDelTime));
125  }
126  //there is no routing so all messages are broadcast for the mac layer
127  sendDown(msg);
129 }
FloodDatagram * encapsMsg(cPacket *)
Encapsulates the received ApplPkt into a NetwPkt and set all needed header fields.
Definition: Flood.cc:264
cBroadcastList bcMsgs
List of already broadcasted messages.
Definition: Flood.h:88
bool plainFlooding
Defines whether to use plain flooding or not.
Definition: Flood.h:69
unsigned int bcMaxEntries
Max number of entries in the list of already broadcasted messages.
Definition: Flood.h:94
long nbDataPacketsSent
Definition: Flood.h:103
simtime_t bcDelTime
Time after which an entry for an already broadcasted msg can be deleted.
Definition: Flood.h:100
unsigned long seqNum
Network layer sequence number.
Definition: Flood.h:57
int defaultTtl
Default time-to-live (ttl) used for this module.
Definition: Flood.h:66
virtual void sendDown(cMessage *message, int interfaceId=-1)
Definition: NetworkProtocolBase.cc:53
value< double, units::m > m
Definition: Units.h:1047
void inet::Flood::initialize ( int  stage)
overridevirtual

Reads all parameters from the ini file.

If a parameter is not specified in the ini file a default value will be set.

Reimplemented from inet::NetworkProtocolBase.

40 {
42  if (stage == INITSTAGE_LOCAL) {
43  //initialize seqence number to 0
44  seqNum = 0;
48  nbHops = 0;
49  headerLength = par("headerLength");
50  hasPar("defaultTtl") ? defaultTtl = par("defaultTtl") : defaultTtl = 5;
51  hasPar("plainFlooding") ? plainFlooding = par("plainFlooding")
52  : plainFlooding = true;
53 
54  EV << "defaultTtl = " << defaultTtl
55  << " plainFlooding = " << plainFlooding << endl;
56 
57  if (plainFlooding) {
58  //these parameters are only needed for plain flooding
59  hasPar("bcMaxEntries") ? bcMaxEntries = par("bcMaxEntries") : bcMaxEntries = 30;
60 
61  hasPar("bcDelTime") ? bcDelTime = par("bcDelTime").doubleValue() : bcDelTime = 3.0;
62  EV << "bcMaxEntries = " << bcMaxEntries
63  << " bcDelTime = " << bcDelTime << endl;
64  }
65  }
66  else if (stage == INITSTAGE_NETWORK_LAYER_3) {
68  }
69 }
virtual void initialize(int stage) override
Definition: NetworkProtocolBase.cc:28
long nbDataPacketsReceived
Definition: Flood.h:102
L3Address myNetwAddr
cached variable of my networ address
Definition: Flood.h:60
Initialization of network-layer protocols, stage 3.
Definition: InitStages.h:84
long nbHops
Definition: Flood.h:105
bool plainFlooding
Defines whether to use plain flooding or not.
Definition: Flood.h:69
long nbDataPacketsForwarded
Definition: Flood.h:104
Local initializations.
Definition: InitStages.h:35
IInterfaceTable * interfaceTable
Definition: NetworkProtocolBase.h:32
unsigned int bcMaxEntries
Max number of entries in the list of already broadcasted messages.
Definition: Flood.h:94
long nbDataPacketsSent
Definition: Flood.h:103
int headerLength
Length of the header.
Definition: Flood.h:63
simtime_t bcDelTime
Time after which an entry for an already broadcasted msg can be deleted.
Definition: Flood.h:100
unsigned long seqNum
Network layer sequence number.
Definition: Flood.h:57
int defaultTtl
Default time-to-live (ttl) used for this module.
Definition: Flood.h:66
const L3Address getNetworkAddress() const
Definition: InterfaceEntry.cc:239
virtual InterfaceEntry * getInterface(int pos) const =0
Returns the InterfaceEntry specified by an index 0..numInterfaces-1.
bool inet::Flood::notBroadcasted ( FloodDatagram msg)
protected

Checks whether a message was already broadcasted.

The bcMsgs list is searched for the arrived message.

If the message is in the list, it was already broadcasted and the function returns false.

Concurrently all outdated (older than bcDelTime) are deleted. If the list is full and a new message has to be entered, the oldest entry is deleted.

Referenced by handleLowerPacket().

217 {
218  if (!plainFlooding)
219  return true;
220 
221  //serach the broadcast list of outdated entries and delete them
222  for (auto it = bcMsgs.begin(); it != bcMsgs.end(); ) {
223  if (it->delTime < simTime()) {
224  it = bcMsgs.erase(it);
225  }
226  //message was already broadcasted
227  else if ((it->srcAddr == msg->getSourceAddress()) && (it->seqNum == msg->getSeqNum())) {
228  // update entry
229  it->delTime = simTime() + bcDelTime;
230  return false;
231  }
232  else
233  ++it;
234  }
235 
236  //delete oldest entry if max size is reached
237  if (bcMsgs.size() >= bcMaxEntries) {
238  EV << "bcMsgs is full, delete oldest entry\n";
239  bcMsgs.pop_front();
240  }
241 
242  bcMsgs.push_back(Bcast(msg->getSeqNum(), msg->getSourceAddress(), simTime() + bcDelTime));
243  return true;
244 }
cBroadcastList bcMsgs
List of already broadcasted messages.
Definition: Flood.h:88
bool plainFlooding
Defines whether to use plain flooding or not.
Definition: Flood.h:69
unsigned int bcMaxEntries
Max number of entries in the list of already broadcasted messages.
Definition: Flood.h:94
simtime_t bcDelTime
Time after which an entry for an already broadcasted msg can be deleted.
Definition: Flood.h:100
virtual int inet::Flood::numInitStages ( ) const
inlineoverridevirtual

Initialization of omnetpp.ini parameters.

Reimplemented from inet::OperationalBase.

111 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
cObject * inet::Flood::setDownControlInfo ( cMessage *const  pMsg,
const MACAddress pDestAddr 
)
protectedvirtual

Attaches a "control info" (NetwToMac) structure (object) to the message pMsg.

Attaches a "control info" structure (object) to the down message pMsg.

This is most useful when passing packets between protocol layers of a protocol stack, the control info will 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 setL3ToL2ControlInfo() call throws an error).

Parameters
pMsgThe message where the "control info" shall be attached.
pDestAddrThe MAC address of the message receiver.

Referenced by encapsMsg(), and handleLowerPacket().

305 {
306  SimpleLinkLayerControlInfo *const cCtrlInfo = new SimpleLinkLayerControlInfo();
307  cCtrlInfo->setDest(pDestAddr);
308  pMsg->setControlInfo(cCtrlInfo);
309  return cCtrlInfo;
310 }

Member Data Documentation

simtime_t inet::Flood::bcDelTime
protected

Time after which an entry for an already broadcasted msg can be deleted.

Referenced by handleUpperPacket(), initialize(), and notBroadcasted().

unsigned int inet::Flood::bcMaxEntries = 0
protected

Max number of entries in the list of already broadcasted messages.

Referenced by handleUpperPacket(), initialize(), and notBroadcasted().

cBroadcastList inet::Flood::bcMsgs
protected

List of already broadcasted messages.

Referenced by finish(), handleUpperPacket(), and notBroadcasted().

int inet::Flood::defaultTtl = 0
protected

Default time-to-live (ttl) used for this module.

Referenced by handleLowerPacket(), handleUpperPacket(), and initialize().

int inet::Flood::headerLength = 0
protected

Length of the header.

Referenced by encapsMsg(), and initialize().

L3Address inet::Flood::myNetwAddr
protected

cached variable of my networ address

Referenced by encapsMsg(), and initialize().

long inet::Flood::nbDataPacketsForwarded = 0
protected
long inet::Flood::nbDataPacketsReceived = 0
protected
long inet::Flood::nbDataPacketsSent = 0
protected
long inet::Flood::nbHops = 0
protected
bool inet::Flood::plainFlooding = false
protected

Defines whether to use plain flooding or not.

Referenced by finish(), handleUpperPacket(), initialize(), and notBroadcasted().

unsigned long inet::Flood::seqNum = 0
protected

Network layer sequence number.

Referenced by handleUpperPacket(), and initialize().


The documentation for this class was generated from the following files: