RandomChurn.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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     // initialize simulation
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             // increase the number of nodes steadily during setup
00090             if (terminalCount < targetOverlayTerminalNum) {
00091                 TransportAddress* ta = underlayConfigurator->createNode(type);
00092                 delete ta; // Address not needed in this churn model
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             // modify the number of nodes according to user parameters
00106             if (random < creationProbability) {
00107                 TransportAddress* ta = underlayConfigurator->createNode(type);
00108                 delete ta; // Address not needed in this churn model
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     // destroy self timer messages
00135     cancelAndDelete(churnTimer);
00136     cancelAndDelete(mobilityTimer);
00137 }
00138 
Generated on Wed May 26 16:21:15 2010 for OverSim by  doxygen 1.6.3