ProbabilisticBroadcast.h

00001 /*
00002  * ProbabilisticBroadcast.h
00003  *
00004  *  Created on: Nov 4, 2008
00005  *      Author: Damien Piguet
00006  */
00007 
00008 #ifndef PROBABILISTICBROADCAST_H_
00009 #define PROBABILISTICBROADCAST_H_
00010 
00011 #include <fstream>
00012 #include <set>
00013 #include <map>
00014 
00015 #include "ProbabilisticBroadcastPkt_m.h"
00016 #include "MacPkt_m.h"
00017 #include "BaseMacLayer.h"
00018 #include "SimTracer.h"
00019 #include "ProbBcastNetwControlInfo.h"
00020 #include "MacToNetwControlInfo.h"
00021 #include "NetwToMacControlInfo.h"
00022 #include "BaseNetwLayer.h"
00023 
00024 using namespace std;
00025 
00026 
00036 class ProbabilisticBroadcast : public BaseNetwLayer
00037 {
00038 public:
00039 
00040 
00042     virtual void initialize(int);
00043 
00044     virtual void finish();
00045 
00046 protected:
00047   enum messagesTypes {
00048     UNKNOWN=0,
00049     BROADCAST_TIMER,
00050     NEIGHBOR_TIMER,
00051     BETA_TIMER,
00052     DATA
00053   };
00054 
00058   typedef struct tMsgDesc {
00059     ProbabilisticBroadcastPkt* pkt;
00060     int                        nbBcast;     // number of times the present node has passed the
00061                                             // message through a broadcast attempt.
00062     bool                       initialSend; // true if message to be sent for first
00063                                             // time by its creator.
00064   } tMsgDesc;
00065 
00066   typedef set<unsigned int> MsgIdSet;
00067   typedef multimap<simtime_t, tMsgDesc*> TimeMsgMap;
00068 
00070     virtual void handleUpperMsg(cMessage* msg);
00071 
00073     virtual void handleLowerMsg(cMessage* msg);
00074 
00076     virtual void handleSelfMsg(cMessage* msg);
00077 
00079     virtual void handleLowerControl(cMessage* msg);
00080 
00082     virtual bool messageKnown(unsigned int msgId);
00083 
00085     virtual bool debugMessageKnown(unsigned int msgId);
00086 
00093   virtual void insertMessage(simtime_t bcastDelay, tMsgDesc* msgDesc);
00094 
00100   virtual tMsgDesc* popFirstMessageUpdateQueue(void);
00101 
00105   virtual ProbabilisticBroadcastPkt* encapsMsg(cMessage* msg);
00106 
00110   virtual cMessage* decapsMsg(ProbabilisticBroadcastPkt *msg);
00111 
00122   virtual void insertNewMessage(ProbabilisticBroadcastPkt* pkt, bool iAmInitialSender = false);
00123 
00128   simtime_t broadcastPeriod;
00129 
00134   double beta;
00135 
00136 
00137   /*
00138    * @brief Default time to live for packets to send.
00139    */
00140   simtime_t timeToLive;
00141 
00142   static long id_counter;
00143 
00144   static long getNextID() {
00145     long nextID = id_counter;
00146     id_counter++;
00147     return nextID;
00148   }
00153   int maxNbBcast;
00154 
00159   int maxFirstBcastBackoff;
00160 
00168   simtime_t timeInQueueAfterDeath;
00169 
00174     int headerLength;
00175 
00176     bool trace, stats, debug;
00177 
00178     // perform broadcast attempt for the first message in the list each time it expires
00179     cMessage* broadcastTimer;
00180 
00181     // we use two containers: a set which stores the ID's of the messages which are kept
00182     // in memory and a multimap which stores a pair <Key, Value> where Key is the next
00183     // broadcasting attempt time of the message and Value is a pointer to the message
00184     // (see typedef's above).
00185     MsgIdSet knownMsgIds;
00186     TimeMsgMap msgQueue;
00187     MsgIdSet debugMsgIdSet;
00188 
00189     // keep MAC broadcast address at class level:
00190     int convertedMacBroadcastAddr;
00191 
00192     // variables for statistics
00193     long nbDataPacketsReceived; // total number of received packets from lower layer
00194     long nbDataPacketsSent;
00195     long nbHops;
00196     int debugNbMessageKnown;
00197     long nbDataPacketsForwarded;
00198     // records the time packets take between submission to MAC layer and reception at
00199     // networking layer (over one hop).
00200     cOutVector oneHopLatencies;
00201 };
00202 
00203 #endif /* PROBABILISTICBROADCAST_H_ */