00001
00002
00003
00004
00005
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
00030
00031 updateNeighMap(m);
00032 ProbabilisticBroadcast::handleLowerMsg(msg);
00033 }
00034
00035
00036 void AdaptiveProbabilisticBroadcast::updateNeighMap(ProbabilisticBroadcastPkt* m)
00037 {
00038
00039 int nodeAddress = m->getSrcAddr();
00040
00041
00042
00043 NeighborMap::iterator it = neighMap.find(nodeAddress);
00044
00045
00046 if (it == neighMap.end()) {
00047 EV << "updateNeighMap(): The message came from a new neighbor! " << endl;
00048
00049
00050 cMessage *removeEvent = new cMessage("removeEvent", NEIGHBOR_TIMER);
00051
00052
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
00059
00060 (ret.first)->second->setContextPointer((void*)(&(ret.first)->first));
00061
00062
00063 }
00064
00065 else {
00066 EV << "updateNeighMap(): The message came from an already known neighbor! " << endl;
00067
00068 cancelEvent(it->second);
00069
00070
00071
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
00099
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