UnderlayConfigurator.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 <omnetpp.h>
00025 #include <GlobalNodeListAccess.h>
00026 #include <ChurnGeneratorAccess.h>
00027 #include <GlobalStatisticsAccess.h>
00028 
00029 #include "UnderlayConfigurator.h"
00030 
00031 const uint32_t UnderlayConfigurator::NUM_COLORS=8;
00032 const char* UnderlayConfigurator::colorNames[] = {
00033      "red", "green", "yellow", "brown", "grey", "violet", "pink", "orange"};
00034 
00035 
00036 UnderlayConfigurator::UnderlayConfigurator()
00037 {
00038     endSimulationTimer = NULL;
00039     endSimulationNotificationTimer = NULL;
00040     endTransitionTimer = NULL;
00041     initFinishedTime.tv_sec = 0;
00042     initFinishedTime.tv_usec = 0;
00043 }
00044 
00045 UnderlayConfigurator::~UnderlayConfigurator()
00046 {
00047     cancelAndDelete(endSimulationNotificationTimer);
00048     cancelAndDelete(endSimulationTimer);
00049     cancelAndDelete(endTransitionTimer);
00050 }
00051 
00052 int UnderlayConfigurator::numInitStages() const
00053 {
00054     return MAX_STAGE_UNDERLAY + 1;
00055 }
00056 
00057 void UnderlayConfigurator::initialize(int stage)
00058 {
00059     if (stage == MIN_STAGE_UNDERLAY) {
00060         gracefulLeaveDelay = par("gracefulLeaveDelay");
00061         gracefulLeaveProbability = par("gracefulLeaveProbability");
00062         channelTypes = cStringTokenizer(par("channelTypes"), " ").asVector();
00063 
00064         transitionTime = par("transitionTime");
00065         measurementTime = par("measurementTime");
00066 
00067         globalNodeList = GlobalNodeListAccess().get();
00068         globalStatistics =GlobalStatisticsAccess().get();
00069 
00070         endSimulationNotificationTimer =
00071             new cMessage("endSimulationNotificationTimer");
00072         endSimulationTimer = new cMessage("endSimulationTimer");
00073         endTransitionTimer = new cMessage("endTransitionTimer");
00074 
00075         gettimeofday(&initStartTime, NULL);
00076         init = true;
00077         simulationEndingSoon = false;
00078         initCounter = 0;
00079 
00080         firstNodeId = -1;
00081         WATCH(firstNodeId);
00082         WATCH(overlayTerminalCount);
00083     }
00084 
00085     if (stage >= MIN_STAGE_UNDERLAY && stage <= MAX_STAGE_UNDERLAY) {
00086         initializeUnderlay(stage);
00087     }
00088 
00089     if (stage == MAX_STAGE_UNDERLAY) {
00090         // Create churn generators
00091         NodeType t;
00092         std::vector<std::string> churnGeneratorTypes =
00093             cStringTokenizer(par("churnGeneratorTypes"), " ").asVector();
00094         std::vector<std::string> terminalTypes =
00095             cStringTokenizer(par("terminalTypes"), " ").asVector();
00096 
00097         if (terminalTypes.size() != 1
00098                 && churnGeneratorTypes.size() != terminalTypes.size())
00099         {
00100             opp_error("UnderlayConfigurator.initialize(): "
00101                       "terminalTypes size does not match churnGenerator size");
00102         }
00103 
00104         for (std::vector<std::string>::iterator it =
00105                 churnGeneratorTypes.begin(); it != churnGeneratorTypes.end(); ++it) {
00106 
00107             cModuleType* genType = cModuleType::get(it->c_str());
00108 
00109             if (genType == NULL) {
00110                 throw cRuntimeError((std::string("UnderlayConfigurator::"
00111                    "initialize(): invalid churn generator: ") + *it).c_str());
00112             }
00113 
00114             ChurnGenerator* gen = check_and_cast<ChurnGenerator*>
00115                                 (genType->create("churnGenerator",
00116                                                  getParentModule(),
00117                                                  t.typeID + 1, t.typeID));
00118 
00119             // check threshold for noChurnThreshold hack
00120             gen->finalizeParameters();
00121 
00122             if ((*it == "oversim.common.LifetimeChurn" ||
00123                  *it == "oversim.common.ParetoChurn") &&
00124                 ((double)gen->par("noChurnThreshold") > 0) &&
00125                 ((double)gen->par("lifetimeMean") >=
00126                  (double)gen->par("noChurnThreshold"))) {
00127                 gen->callFinish();
00128                 gen->deleteModule();
00129                 cModuleType* genType =
00130                     cModuleType::get("oversim.common.NoChurn");
00131                 gen = check_and_cast<ChurnGenerator*>
00132                         (genType->create("churnGenerator", getParentModule(),
00133                                          t.typeID + 1, t.typeID));
00134                 gen->finalizeParameters();
00135                 EV << "[UnderlayConfigurator::initialize()]\n"
00136                    << "    churnGenerator[" << t.typeID
00137                    << "]: \"oversim.common.NoChurn\" is used instead of \""
00138                    << *it << "\"!\n    (lifetimeMean exceeds noChurnThreshold)"
00139                    << endl;
00140             }
00141 
00142             // Add it to the list of generators and initialize it
00143             churnGenerator.push_back(gen);
00144             t.typeID++;
00145             t.terminalType = (terminalTypes.size() == 1) ?
00146                 terminalTypes[0] :
00147                 terminalTypes[it - churnGeneratorTypes.begin()];
00148 
00149             gen->setNodeType(t);
00150             gen->buildInside();
00151         }
00152     }
00153 }
00154 
00155 void UnderlayConfigurator::initFinished()
00156 {
00157     Enter_Method_Silent();
00158 
00159     if (++initCounter == churnGenerator.size()) {
00160         init = false;
00161         gettimeofday(&initFinishedTime, NULL);
00162 
00163         scheduleAt(simTime() + transitionTime,
00164                 endTransitionTimer);
00165 
00166         if (measurementTime >= 0) {
00167             scheduleAt(simTime() + transitionTime + measurementTime,
00168                        endSimulationTimer);
00169 
00170             if ((transitionTime + measurementTime) < gracefulLeaveDelay) {
00171                 throw cRuntimeError("UnderlayConfigurator::initFinished():"
00172                                         " gracefulLeaveDelay must be bigger "
00173                                         "than transitionTime + measurementTime!");
00174             }
00175 
00176             scheduleAt(simTime() + transitionTime + measurementTime
00177                     - gracefulLeaveDelay,
00178                     endSimulationNotificationTimer);
00179         }
00180         consoleOut("INIT phase finished");
00181     }
00182 }
00183 
00184 void UnderlayConfigurator::handleMessage(cMessage* msg)
00185 {
00186     if (msg == endSimulationNotificationTimer) {
00187         simulationEndingSoon = true;
00188         //      globalNodeList->sendNotificationToAllPeers(NF_OVERLAY_NODE_LEAVE);
00189     } else if (msg == endSimulationTimer) {
00190         endSimulation();
00191     } else if (msg == endTransitionTimer) {
00192         consoleOut("transition time finished");
00193         globalStatistics->startMeasuring();
00194     } else {
00195         handleTimerEvent(msg);
00196     }
00197 }
00198 
00199 void UnderlayConfigurator::handleTimerEvent(cMessage* msg)
00200 {
00201     delete msg;
00202 }
00203 
00204 void UnderlayConfigurator::finish()
00205 {
00206     finishUnderlay();
00207 }
00208 
00209 void UnderlayConfigurator::finishUnderlay()
00210 {
00211     //...
00212 }
00213 
00214 void UnderlayConfigurator::consoleOut(const std::string& text)
00215 {
00216     if (!ev.isGUI()) {
00217         struct timeval now, diff;
00218         gettimeofday(&now, NULL);
00219         diff = timeval_substract(now, initStartTime);
00220 
00221         std::stringstream ss;
00222         std::string line1(71, '*');
00223         std::string line2(71, '*');
00224 
00225         ss << "   " << text << "   ";
00226         line1.replace(35 - ss.str().size() / 2,
00227                       ss.str().size(),
00228                       ss.str());
00229         ss.str("");
00230 
00231         ss << "   (sim time: " << simTime()
00232            << ", real time: " << diff.tv_sec
00233            << "." << diff.tv_usec << ")   ";
00234         line2.replace(35 - ss.str().size() / 2,
00235                       ss.str().size(),
00236                       ss.str());
00237 
00238         std::cout << "\n" << line1 << "\n"
00239                   << line2 << "\n" << std::endl;
00240     } else {
00241         EV << "[UnderlayConfigurator::consoleOut()] " << text;
00242     }
00243 }
00244 
00245 ChurnGenerator* UnderlayConfigurator::getChurnGenerator(int typeID)
00246 {
00247     Enter_Method_Silent();
00248 
00249     return churnGenerator[typeID];
00250 }
00251 

Generated on Wed Mar 25 16:32:50 2009 for OverSim by  doxygen 1.5.5