AdaptiveProbabilisticBroadcast.cc

00001 /*
00002  * ProbabilisticBroadcast.cc
00003  *
00004  *  Created on: Nov 26, 2008
00005  *      Author: Damien Piguet, Dimitris Kotsakos, Jérôme Rousselot
00006  */
00007 
00008 #include "AdaptiveProbabilisticBroadcast.h"
00009 
00010 Define_Module(AdaptiveProbabilisticBroadcast);
00011 
00012 void AdaptiveProbabilisticBroadcast::initialize(int stage)
00013 {
00014   ProbabilisticBroadcast::initialize(stage);
00015 
00016   if(stage == 1){
00017 
00018       beta = 1.0;
00019 
00020         bvec.setName("Beta Vector");
00021 
00022       timeInNeighboursTable = par("timeInNeighboursTable");
00023   }
00024 }
00025 
00026 void AdaptiveProbabilisticBroadcast::handleLowerMsg(cMessage* msg)
00027 {
00028   ProbabilisticBroadcastPkt* m = check_and_cast<ProbabilisticBroadcastPkt*>(msg);
00029   // Update neighbors table before calling the method of the super class
00030   // because it may delete the message.
00031   updateNeighMap(m);
00032   ProbabilisticBroadcast::handleLowerMsg(msg);
00033 }
00034 
00035 
00036 void AdaptiveProbabilisticBroadcast::updateNeighMap(ProbabilisticBroadcastPkt* m)
00037 {
00038   //find the network address of the node who sent the msg
00039   int nodeAddress = m->getSrcAddr();
00040   //EV << "updateNeighMap(): neighAddress: " << nodeAddress << endl;
00041 
00042     //search for it in the "already-neighbors" map
00043   NeighborMap::iterator it = neighMap.find(nodeAddress);
00044 
00045   //if the node is a "new" neighbor
00046   if (it == neighMap.end()) {
00047     EV << "updateNeighMap(): The message came from a new neighbor! " << endl;
00048 
00049         // insert key value pair <node address, event> in neighborhood map.
00050     cMessage *removeEvent = new cMessage("removeEvent", NEIGHBOR_TIMER);
00051 
00052         // schedule the event to remove the entry after initT seconds
00053     scheduleAt(simTime()+timeInNeighboursTable, removeEvent);
00054 
00055     pair<int, cMessage*> pairToInsert = make_pair(nodeAddress, removeEvent);
00056     pair<NeighborMap::iterator, bool> ret = neighMap.insert(pairToInsert);
00057 
00058         // set the context pointer to point to the integer that resembles to the address of
00059         // the node to be removed when the corresponding event occurs
00060     (ret.first)->second->setContextPointer((void*)(&(ret.first)->first));
00061 
00062 
00063   }
00064     //if the node is NOT a "new" neighbor update its timer
00065   else {
00066     EV << "updateNeighMap(): The message came from an already known neighbor! " << endl;
00067         //cancel the event that was scheduled to remove the entry for this neighbor
00068         cancelEvent(it->second);
00069         // Define a new event in order to remove the entry after initT seconds
00070         // Set the context pointer to point to the integer that resembles to the address of
00071         // the node to be removed when the corresponding event occurs
00072     it->second->setContextPointer((void*)(&it->first));
00073 
00074     scheduleAt(simTime()+timeInNeighboursTable, it->second);
00075   }
00076   updateBeta();
00077 }
00078 
00079 void AdaptiveProbabilisticBroadcast::handleSelfMsg(cMessage* msg)
00080 {
00081     if (msg->getKind() == NEIGHBOR_TIMER) {
00082     int node = *(int*) msg->getContextPointer();
00083         EV << "handleSelfMsg(): Remove node "<< node <<" from NeighMap!" << endl;
00084     map<int,cMessage*>::iterator it = neighMap.find(node);
00085     cancelAndDelete(neighMap.find(it->first)->second);
00086     neighMap.erase(it);
00087     updateBeta();
00088   }
00089   else {
00090     ProbabilisticBroadcast::handleSelfMsg(msg);
00091   }
00092 }
00093 
00094 void AdaptiveProbabilisticBroadcast::updateBeta()
00095 {
00096   int k = neighMap.size();
00097 
00098   // those values are derived from the simulations
00099   // with the non-adaptive protocol.
00100   if (k < 4)
00101     beta = 1.0;
00102   else if (k < 6)
00103     beta = 0.9;
00104   else if (k < 8)
00105     beta = 0.8;
00106   else if (k < 10)
00107     beta = 0.7;
00108   else if (k < 12)
00109     beta = 0.6;
00110   else if (k < 14)
00111     beta = 0.5;
00112   else if (k < 16)
00113     beta = 0.4;
00114   else if (k < 18)
00115     beta = 0.3;
00116   else if (k < 20)
00117     beta = 0.2;
00118   else
00119     beta = 0.1;
00120   if(trace) {
00121       bvec.record(beta);
00122   }
00123 }
00124