RandomChurn.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include <assert.h>
00025
00026 #include <TransportAddress.h>
00027 #include <GlobalStatistics.h>
00028 #include <GlobalStatisticsAccess.h>
00029 #include <UnderlayConfigurator.h>
00030
00031 #include "RandomChurn.h"
00032
00033 Define_Module(RandomChurn);
00034
00035 void RandomChurn::initializeChurn()
00036 {
00037 Enter_Method_Silent();
00038
00039 creationProbability = par("creationProbability");
00040 migrationProbability = par("migrationProbability");
00041 removalProbability = par("removalProbability");
00042 initialMean = par("initPhaseCreationInterval");
00043 initialDeviation = initialMean / 3;
00044 targetMean = par("targetMobilityDelay");
00045 targetOverlayTerminalNum = par("targetOverlayTerminalNum");
00046 WATCH(targetMean);
00047
00048 churnTimer = NULL;
00049 churnIntervalChanged = false;
00050 churnChangeInterval = par("churnChangeInterval");
00051 initAddMoreTerminals = true;
00052
00053 globalStatistics = GlobalStatisticsAccess().get();
00054
00055
00056 mobilityTimer = NULL;
00057 mobilityTimer = new cMessage("mobilityTimer");
00058 scheduleAt(simTime(), mobilityTimer);
00059
00060 if (churnChangeInterval > 0) {
00061 churnTimer = new cMessage("churnTimer");
00062 scheduleAt(simTime() + churnChangeInterval, churnTimer);
00063 }
00064 }
00065
00066 void RandomChurn::handleMessage(cMessage* msg)
00067 {
00068 if (!msg->isSelfMessage()) {
00069 delete msg;
00070 return;
00071 }
00072
00073 if (msg == churnTimer) {
00074 cancelEvent(churnTimer);
00075 scheduleAt(simTime() + churnChangeInterval, churnTimer);
00076 if (churnIntervalChanged) {
00077 targetMean = par("targetMobilityDelay");
00078 churnIntervalChanged = false;
00079 }
00080 else {
00081 targetMean = par("targetMobilityDelay2");
00082 churnIntervalChanged = true;
00083 }
00084 std::stringstream temp;
00085 temp << "Churn-rate changed to " << targetMean;
00086 bubble(temp.str().c_str());
00087 } else if (msg == mobilityTimer) {
00088 if (initAddMoreTerminals) {
00089
00090 if (terminalCount < targetOverlayTerminalNum) {
00091 TransportAddress* ta = underlayConfigurator->createNode(type);
00092 delete ta;
00093 }
00094
00095 if (terminalCount >= targetOverlayTerminalNum) {
00096 initAddMoreTerminals = false;
00097 underlayConfigurator->initFinished();
00098 }
00099 scheduleAt(simTime()
00100 + truncnormal(initialMean, initialDeviation), msg);
00101 }
00102 else {
00103 double random = uniform(0, 1);
00104
00105
00106 if (random < creationProbability) {
00107 TransportAddress* ta = underlayConfigurator->createNode(type);
00108 delete ta;
00109 } else if (creationProbability <= random &&
00110 random < creationProbability + removalProbability &&
00111 terminalCount > 1) {
00112 int oldTerminalCount = terminalCount;
00113 underlayConfigurator->preKillNode(type);
00114 assert ((oldTerminalCount - 1) == terminalCount);
00115 } else if (creationProbability + removalProbability <= random &&
00116 random < creationProbability + removalProbability
00117 + migrationProbability && terminalCount > 1) {
00118 underlayConfigurator->migrateNode(type);
00119 }
00120 scheduleAt(simTime()
00121 + truncnormal(targetMean, targetMean / 3), msg);
00122 }
00123 }
00124 }
00125
00126 void RandomChurn::updateDisplayString()
00127 {
00128 char buf[80];
00129 sprintf(buf, "random churn");
00130 getDisplayString().setTagArg("t", 0, buf);
00131 }
00132
00133 RandomChurn::~RandomChurn() {
00134
00135 cancelAndDelete(churnTimer);
00136 cancelAndDelete(mobilityTimer);
00137 }
00138