Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

Flood Class Reference
[netwLayer - network layer modules]

A simple flooding protocol. More...

#include <Flood.h>

Inherits BaseNetwLayer.

Collaboration diagram for Flood:
Collaboration graph
[legend]

List of all members.

Classes

class  Bcast

Public Member Functions

virtual void initialize (int)
 Initialization of omnetpp.ini parameters.
virtual void finish ()
 Called when the simulation has finished.

Protected Types

typedef std::list< BcastcBroadcastList

Protected Member Functions

virtual void handleUpperMsg (cMessage *)
 Handle messages from upper layer.
virtual void handleLowerMsg (cMessage *)
 Handle messages from lower layer.
virtual void handleSelfMsg (cMessage *msg)
 we have no self messages
bool notBroadcasted (NetwPkt *)
 Checks whether a message was already broadcasted.
NetwPkt * encapsMsg (cPacket *)
 Encapsulate higher layer packet into an NetwPkt.

Protected Attributes

unsigned long seqNum
 Network layer sequence number.
int defaultTtl
 Default time-to-live (ttl) used for this module.
bool plainFlooding
 Defines whether to use plain flooding or not.
cBroadcastList bcMsgs
 List of already broadcasted messages.
unsigned int bcMaxEntries
 Max number of entries in the list of already broadcasted messages.
simtime_t bcDelTime
 Time after which an entry for an already broadcasted msg can be deleted.
long nbDataPacketsReceived
long nbDataPacketsSent
long nbDataPacketsForwarded
long nbHops

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

Definition at line 50 of file Flood.h.


Member Function Documentation

NetwPkt * Flood::encapsMsg ( cPacket *  appPkt  )  [protected, virtual]

Encapsulate higher layer packet into an NetwPkt.

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

Reimplemented from BaseNetwLayer.

Definition at line 240 of file Flood.cc.

References NetwControlInfo::getNetwAddr(), BaseNetwLayer::headerLength, and BaseNetwLayer::myNetwAddr.

Referenced by handleUpperMsg().

                                         {
  int macAddr;
  int netwAddr;

  EV<<"in encaps...\n";

  NetwPkt *pkt = new NetwPkt(appPkt->getName(), appPkt->getKind());
  pkt->setBitLength(headerLength);

  NetwControlInfo* cInfo = dynamic_cast<NetwControlInfo*>(appPkt->removeControlInfo());

    if(cInfo == 0){
  EV << "warning: Application layer did not specifiy a destination L3 address\n"
     << "\tusing broadcast address instead\n";
  netwAddr = L3BROADCAST;
    } else {
  EV <<"CInfo removed, netw addr="<< cInfo->getNetwAddr()<<endl;
        netwAddr = cInfo->getNetwAddr();
  delete cInfo;
    }

    pkt->setSrcAddr(myNetwAddr);
    pkt->setDestAddr(netwAddr);
    EV << " netw "<< myNetwAddr << " sending packet" <<endl;

        EV << "sendDown: nHop=L3BROADCAST -> message has to be broadcasted"
           << " -> set destMac=L2BROADCAST\n";
        macAddr = L2BROADCAST;


    pkt->setControlInfo(new NetwToMacControlInfo(macAddr));

    //encapsulate the application packet
    pkt->encapsulate(appPkt);
    EV <<" pkt encapsulated\n";
    return pkt;
}

void Flood::handleLowerMsg ( cMessage *  m  )  [protected, virtual]

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.

Reimplemented from BaseNetwLayer.

Definition at line 140 of file Flood.cc.

References BaseNetwLayer::decapsMsg(), defaultTtl, BaseNetwLayer::myNetwAddr, notBroadcasted(), BaseLayer::sendDown(), and BaseLayer::sendUp().

                                      {
  NetwPkt *msg = static_cast<NetwPkt *> (m);

  //msg not broadcastes yet
  if (notBroadcasted(msg)) {
    //msg is for me
    if (msg->getDestAddr() == myNetwAddr) {
      EV<<" data msg for me! send to Upper"<<endl;
      nbHops = nbHops + (defaultTtl + 1 - msg->getTtl());
      sendUp( decapsMsg(msg) );
      nbDataPacketsReceived++;
    }
    //broadcast message
    else if( msg->getDestAddr() == L3BROADCAST ) {
      //check ttl and rebroadcast
      if( msg->getTtl() > 1 ) {
        NetwPkt *dMsg;
        EV <<" data msg BROADCAST! ttl = "<<msg->getTtl()
        <<" > 1 -> rebroadcast msg & send to upper\n";
        msg->setTtl( msg->getTtl()-1 );
        dMsg = static_cast<NetwPkt*>(msg->dup());
        dMsg->setControlInfo(new NetwToMacControlInfo(L2BROADCAST));
        sendDown(dMsg);
        nbDataPacketsForwarded++;
      }
      else
      EV <<" max hops reached (ttl = "<<msg->getTtl()<<") -> only send to upper\n";

      // message has to be forwarded to upper layer
      nbHops = nbHops + (defaultTtl + 1 - msg->getTtl());
      sendUp( decapsMsg(msg) );
      nbDataPacketsReceived++;
    }
    //not for me -> rebroadcast
    else {
      //check ttl and rebroadcast
      if( msg->getTtl() > 1 ) {
        EV <<" data msg not for me! ttl = "<<msg->getTtl()
        <<" > 1 -> forward\n";
        msg->setTtl( msg->getTtl()-1 );
        // needs to set the next hop address again to broadcast
        msg->removeControlInfo();
        msg->setControlInfo(new NetwToMacControlInfo(L2BROADCAST));
        //            EV << static_cast<NetwToMacControlInfo*>(msg->getControlInfo())->getNextHopMac() << "\n";
        sendDown( msg );
        nbDataPacketsForwarded++;
      }
      else {
        //max hops reached -> delete
        EV <<" max hops reached (ttl = "<<msg->getTtl()<<") -> delete msg\n";
        delete msg;
      }
    }
  }
  else {
    EV <<" data msg already BROADCASTed! delete msg\n";
    delete msg;
  }
}

void Flood::handleUpperMsg ( cMessage *  m  )  [protected, virtual]

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.

Reimplemented from BaseNetwLayer.

Definition at line 92 of file Flood.cc.

References bcDelTime, bcMaxEntries, bcMsgs, defaultTtl, encapsMsg(), plainFlooding, BaseLayer::sendDown(), and seqNum.

                                      {

  assert(dynamic_cast<cPacket*> (m));
  NetwPkt *msg = encapsMsg(static_cast<cPacket*> (m));

  msg->setSeqNum(seqNum);
  seqNum++;
  msg->setTtl(defaultTtl);

  if (plainFlooding) {
    if (bcMsgs.size() >= bcMaxEntries) {
      cBroadcastList::iterator it;

      //serach the broadcast list of outdated entries and delete them
      for (it = bcMsgs.begin(); it != bcMsgs.end(); ++it) {
        if (it->delTime < simTime()) {
          bcMsgs.erase(it);
          it--;
          break;
        }
      }
      //delete oldest entry if max size is reached
      if (bcMsgs.size() >= bcMaxEntries) {
        EV<<"bcMsgs is full, delete oldest entry"<<endl;
        bcMsgs.pop_front();
      }
    }
    bcMsgs.push_back(Bcast(msg->getSeqNum(), msg->getSrcAddr(), simTime() +bcDelTime));
    }
        //there is no routing so all messages are broadacst for the mac layer

        sendDown(msg);
        nbDataPacketsSent++;
      }

void Flood::initialize ( int  stage  )  [virtual]

Initialization of omnetpp.ini parameters.

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 BaseNetwLayer.

Definition at line 36 of file Flood.cc.

References bcDelTime, bcMaxEntries, defaultTtl, plainFlooding, and seqNum.

                                {
  BaseNetwLayer::initialize(stage);

  if (stage == 0) {
    //initialize seqence number to 0
    seqNum = 0;
    nbDataPacketsReceived = 0;
    nbDataPacketsSent = 0;
    nbDataPacketsForwarded = 0;
    nbHops = 0;
    hasPar("defaultTtl") ? defaultTtl = par("defaultTtl") : defaultTtl = 5;
    hasPar("plainFlooding") ? plainFlooding = par("plainFlooding")
        : plainFlooding = true;

    EV<< "defaultTtl = " << defaultTtl
    << " plainFlooding = " << plainFlooding << endl;

    if(plainFlooding) {
      //these parameters are only needed for plain flooding
      hasPar("bcMaxEntries") ? bcMaxEntries = par("bcMaxEntries") : bcMaxEntries = 30;

      hasPar("bcDelTime") ? bcDelTime = par("bcDelTime") : bcDelTime = 3.0;
      EV <<"bcMaxEntries = "<<bcMaxEntries
      <<" bcDelTime = "<<bcDelTime<<endl;
    }
  }
}

bool Flood::notBroadcasted ( NetwPkt *  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.

Definition at line 209 of file Flood.cc.

References bcDelTime, bcMaxEntries, bcMsgs, and plainFlooding.

Referenced by handleLowerMsg().

                                       {
  if (!plainFlooding)
    return true;

  cBroadcastList::iterator it;

  //serach the broadcast list of outdated entries and delete them
  for (it = bcMsgs.begin(); it != bcMsgs.end(); it++) {
    if (it->delTime < simTime()) {
      bcMsgs.erase(it);
      it--;
    }
    //message was already broadcasted
    if ((it->srcAddr == msg->getSrcAddr()) && (it->seqNum
        == msg->getSeqNum())) {
      // update entry
      it->delTime = simTime() + bcDelTime;
      return false;
    }
  }

  //delete oldest entry if max size is reached
  if (bcMsgs.size() >= bcMaxEntries) {
    EV<<"bcMsgs is full, delete oldest entry\n";
    bcMsgs.pop_front();
  }

  bcMsgs.push_back(Bcast(msg->getSeqNum(), msg->getSrcAddr(), simTime() +bcDelTime));
    return true;
}


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