OverSim
LifetimeChurn.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #include <algorithm>
25 
26 #include "GlobalStatisticsAccess.h"
27 #include "UnderlayConfigurator.h"
28 #include "Churn_m.h"
29 
30 #include "LifetimeChurn.h"
31 
33 
35 {
36  Enter_Method_Silent();
37 
38  initialMean = par("initPhaseCreationInterval");
40  lifetimeMean = par("lifetimeMean");
41  lifetimeDistName = par("lifetimeDistName").stdstringValue();
42  lifetimeDistPar1 = par("lifetimeDistPar1");
43 
44  WATCH(lifetimeMean);
45 
47 
48  lastCreate = lastDelete = simTime();
49 
50  simtime_t initFinishedTime = initialMean * targetOverlayTerminalNum;
51 
52  // create the remaining nodes in bootstrap phase
53  int targetOverlayTerminalNum = par("targetOverlayTerminalNum");
54  contextVector.assign(2*targetOverlayTerminalNum, (cObject*)NULL);
55 
56  for (int i = 0; i < targetOverlayTerminalNum; i++) {
57 
59  initFinishedTime + distributionFunction()
60  - truncnormal(initialMean * i,
61  initialDeviation), i);
62 
63  // create same number of currently dead nodes
64  scheduleCreateNodeAt(initFinishedTime + distributionFunction(),
65  distributionFunction(), targetOverlayTerminalNum + i);
66  }
67 
68  initFinishedTimer = new cMessage("initFinishedTimer");
69 
70  scheduleAt(initFinishedTime, initFinishedTimer);
71 }
72 
73 void LifetimeChurn::handleMessage(cMessage* msg)
74 {
75  if (!msg->isSelfMessage()) {
76  delete msg;
77  return;
78  }
79 
80  // init phase finished
81  if (msg == initFinishedTimer) {
83  cancelEvent(initFinishedTimer);
84  delete initFinishedTimer;
85  initFinishedTimer = NULL;
86  return;
87  }
88 
89  ChurnMessage* churnMsg = check_and_cast<ChurnMessage*> (msg);
90 
91  if (churnMsg->getCreateNode() == true) {
92  createNode(churnMsg->getLifetime(), false, churnMsg->getContextPos());
93  } else {
94  deleteNode(churnMsg->getAddr(), churnMsg->getContextPos());
95  }
96 
97  delete msg;
98 }
99 
100 void LifetimeChurn::createNode(simtime_t lifetime, bool initialize,
101  int contextPos)
102 {
103 
104  ChurnMessage* churnMsg = new ChurnMessage("DeleteNode");
105  NodeType createType = type;
106  createType.context = &(contextVector[contextPos]);
107  TransportAddress* ta = underlayConfigurator->createNode(createType, initialize);
108  churnMsg->setAddr(*ta);
109  churnMsg->setContextPos(contextPos);
110  delete ta;
111  churnMsg->setCreateNode(false);
112  scheduleAt(std::max(simTime(), simTime() + lifetime
114 
116  "LifetimeChurn: Session Time", SIMTIME_DBL(lifetime)));
118  "LifetimeChurn: Time between creates",
119  SIMTIME_DBL(simTime() - lastCreate)));
120 
121  lastCreate = simTime();
122 }
123 
124 void LifetimeChurn::deleteNode(TransportAddress& addr, int contextPos)
125 {
127 
129  distributionFunction(), contextPos);
130 
132  "LifetimeChurn: Time between deletes",
133  SIMTIME_DBL(simTime() - lastDelete)));
134 
135  lastDelete = simTime();
136 }
137 
138 void LifetimeChurn::scheduleCreateNodeAt(simtime_t creationTime,
139  simtime_t lifetime, int contextPos)
140 {
141  ChurnMessage* churnMsg = new ChurnMessage("CreateNode");
142  churnMsg->setCreateNode(true);
143  churnMsg->setLifetime(SIMTIME_DBL(lifetime));
144  churnMsg->setContextPos(contextPos);
145  scheduleAt(creationTime, churnMsg);
146 }
147 
149 {
150  double par;
151 
152  if (lifetimeDistName == "weibull") {
153  par = lifetimeMean / tgamma(1 + (1 / lifetimeDistPar1));
154  return weibull(par, lifetimeDistPar1);
155  } else if (lifetimeDistName == "pareto_shifted") {
157  return pareto_shifted(lifetimeDistPar1, par, 0);
158  } else if (lifetimeDistName == "truncnormal") {
159  par = lifetimeMean;
160  return truncnormal(par, par/3.0);
161  } else {
162  opp_error("LifetimeChurn::distribution function: Invalid value "
163  "for parameter lifetimeDistName!");
164  }
165 
166  return 0;
167 }
168 
170 {
171  char buf[80];
172  sprintf(buf, "lifetime churn");
173  getDisplayString().setTagArg("t", 0, buf);
174 }
175 
177 {
178  cancelAndDelete(initFinishedTimer);
179  for (std::vector<cObject*>::iterator it = contextVector.begin();
180  it != contextVector.end(); it++) {
181  if (*it) {
182  delete *it;
183  }
184  }
185 
186 }