OverSim
ConnectivityProbe.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 "ConnectivityProbe.h"
25 
27 
29 {
30  this->moduleID = moduleID;
31  visited = false;
32 }
33 
35 {
36  return check_and_cast<Vast*>(simulation.getModule(moduleID));
37 }
38 
40 {
42  probeIntervall = par("connectivityProbeIntervall");
43  plotIntervall = par("visualizeNetworkIntervall");
44  probeTimer = new cMessage("probeTimer");
45  plotTimer = new cMessage("plotTimer");
46  plotConnections = par("plotConnections");
47  plotMissing= par("plotMissing");
48 
49  if(probeIntervall > 0.0) {
50  scheduleAt(simTime() + probeIntervall, probeTimer);
51 
52  cOV_NodeCount.setName("total node count");
53  cOV_MaximumComponent.setName("largest connected component");
54  cOV_MaxConnectivity.setName("connectivity in percent");
55  cOV_ZeroMissingNeighbors.setName("neighbor-error free nodes");
56  cOV_AverageMissingNeighbors.setName("average missing neighbors per node");
57  cOV_MaxMissingNeighbors.setName("largest missing neighbors count");
58  cOV_AverageDrift.setName("average drift");
59  }
60 
61  if(plotIntervall > 0.0) {
62  scheduleAt(simTime() + plotIntervall, plotTimer);
63  }
64 }
65 
67 {
68  // fill topology with all VAST modules
70 
71  if(Topology.size() == 0) {
72  return;
73  }
74 
75  // catch self timer messages
76  if(msg->isName("probeTimer")) {
77  //reset timer
78  cancelEvent(probeTimer);
79  scheduleAt(simTime() + probeIntervall, msg);
80 
81  unsigned int maxComponent = 0;
82  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
83  unsigned int count = getComponentSize(itTopology->second.getModule()->getHandle().getKey());
84  if(count > maxComponent) {
85  maxComponent = count;
86  }
88  if(count == Topology.size()) {
89  break;
90  }
91  }
92 
93  cOV_NodeCount.record((double)Topology.size());
94  cOV_MaximumComponent.record((double)maxComponent);
95  cOV_MaxConnectivity.record((double)maxComponent * 100.0 / (double)Topology.size());
96  RECORD_STATS (
97  globalStatistics->addStdDev("ConnectivityProbe: max connectivity", (double)maxComponent * 100.0 / (double)Topology.size());
98  );
99 
100  int mnMax = 0;
101  int mnZero = 0;
102  int driftCount = 0;
103  double mnAverage = 0.0;
104  double drift = 0.0;
105 
106  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
107  double AOIWidthSqr = itTopology->second.getModule()->getAOI();
108  AOIWidthSqr *= AOIWidthSqr;
109  Vector2D vastPosition = itTopology->second.getModule()->getPosition();
110  int missing = 0;
111  for(VTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
112  if(itI != itTopology && vastPosition.distanceSqr(itI->second.getModule()->getPosition()) <= AOIWidthSqr) {
113  SiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getHandle());
114  if(currentSite == itTopology->second.getModule()->Sites.end()) {
115  ++missing;
116  }
117  else {
118  drift += sqrt(currentSite->second->coord.distanceSqr(itI->second.getModule()->getPosition()));
119  ++driftCount;
120  }
121  }
122  }
123 
124  mnAverage += missing;
125  if(mnMax < missing) {
126  mnMax = missing;
127  }
128  if(missing == 0) {
129  ++mnZero;
130  }
131  }
132  mnAverage /= (double)Topology.size();
133  if(driftCount > 0) {
134  drift /= (double)driftCount;
135  }
136 
137  cOV_ZeroMissingNeighbors.record((double)mnZero);
138  RECORD_STATS (
139  globalStatistics->addStdDev("ConnectivityProbe: percentage zero missing neighbors", (double)mnZero * 100.0 / (double)Topology.size());
140  globalStatistics->addStdDev("ConnectivityProbe: average drift", drift);
141  );
142  cOV_AverageMissingNeighbors.record(mnAverage);
143  cOV_MaxMissingNeighbors.record((double)mnMax);
144  cOV_AverageDrift.record(drift);
145  }
146  else if(msg->isName("plotTimer")) {
147  //reset timer
148  cancelEvent(plotTimer);
149  scheduleAt(simTime() + plotIntervall, msg);
150 
151  bool missingFound = false;
152  if(plotMissing) {
153  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
154  double AOIWidthSqr = itTopology->second.getModule()->getAOI();
155  AOIWidthSqr *= AOIWidthSqr;
156  Vector2D vastPosition = itTopology->second.getModule()->getPosition();
157  for(VTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
158  if(itI != itTopology && vastPosition.distanceSqr(itI->second.getModule()->getPosition()) <= AOIWidthSqr) {
159  SiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getHandle());
160  if(currentSite == itTopology->second.getModule()->Sites.end()) {
161  missingFound=true;
162  }
163  }
164  }
165  }
166  }
167 
168  if(!plotMissing || missingFound) {
169 
170  int range = (int)Topology.begin()->second.getModule()->getAreaDimension();
171  std::stringstream oss;
172  std::string filename;
173  int simTimeInt, stellen = 1;
174  simTimeInt = (int)SIMTIME_DBL(simTime());
175  oss << "plot";
176  for(int i=0; i<6; i++) {
177  if(!(simTimeInt / stellen)) {
178  oss << "0";
179  }
180  stellen *= 10;
181  }
182  oss << simTimeInt;
183 
184  // open/ write plot file
185  filename = oss.str() + ".plot";
186  pltNetwork.open(filename.c_str(), std::ios::out);
187  pltNetwork << "set xrange [0:" << range << "]" << endl;
188  pltNetwork << "set yrange [0:" << range << "]" << endl;
189  pltNetwork << "set nokey" << endl;
190 
191  // open point file
192  filename = oss.str() + ".point";
193  pltData.open(filename.c_str(), std::ios::out);
194 
195  pltNetwork << "plot '" << filename << "' using 1:2 with points pointtype 7,\\" << endl;
196 
197  // open vector file
198  filename = oss.str() + ".arrow";
199  pltVector.open(filename.c_str(), std::ios::out);
200 
201  pltNetwork << " '" << filename << "' using 1:2:3:4 with vectors linetype 1" << endl;
202  pltNetwork.close();
203 
204  // write point data file
205  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
206  pltData << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << endl;
207  }
208  pltData.close();
209 
210  //write arrow data file
211  if(!plotMissing) {
212  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
213  for(SiteMap::iterator itSites = itTopology->second.getModule()->Sites.begin(); itSites != itTopology->second.getModule()->Sites.end(); ++itSites) {
214  if(plotConnections) {
215  VTopology::iterator destNode = Topology.find(itSites->second->addr.getKey());
216  if(destNode != Topology.end()) {
217  Vector2D relPos = destNode->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
218  pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
219  << relPos.x << "\t" << relPos.y << endl;
220  }
221  }
222  else {
223  Vector2D relPos = itSites->second->coord - itTopology->second.getModule()->getPosition();
224  pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
225  << relPos.x << "\t" << relPos.y << endl;
226  }
227  }
228  }
229  } else {
230  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
231  double AOIWidthSqr = itTopology->second.getModule()->getAOI();
232  AOIWidthSqr *= AOIWidthSqr;
233  Vector2D vastPosition = itTopology->second.getModule()->getPosition();
234  for(VTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
235  if(itI != itTopology && vastPosition.distanceSqr(itI->second.getModule()->getPosition()) <= AOIWidthSqr) {
236  SiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getHandle());
237  if(currentSite == itTopology->second.getModule()->Sites.end()) {
238  Vector2D relPos = itI->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
239  pltVector << itTopology->second.getModule()->getPosition().x << "\t"
240  << itTopology->second.getModule()->getPosition().y << "\t"
241  << relPos.x << "\t" << relPos.y << "\t"
242  << itTopology->second.getModule()->getParentModule()->getParentModule()->getFullName() << ":"
243  << itTopology->second.getModule()->thisSite.addr.getKey().toString(16) << "\t"
244  << itI->second.getModule()->getParentModule()->getParentModule()->getFullName() << ":"
245  << itI->second.getModule()->thisSite.addr.getKey().toString(16) << endl;
246  }
247  }
248  }
249  }
250  }
251  pltVector.close();
252  }
253  }
254  Topology.clear();
255 }
256 
258 {
259  for(int i=0; i<=simulation.getLastModuleId(); i++) {
260  cModule* module = simulation.getModule(i);
261  if(module && dynamic_cast<Vast*>(module)) {
262  Vast* vast = check_and_cast<Vast*>(module);
263  if(vast->getState() == BaseOverlay::READY) {
264  VTopologyNode temp(i);
265  Topology.insert(std::make_pair(vast->getHandle().getKey(), temp));
266  }
267  }
268  }
269 }
270 
272 {
273  for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
274  itTopology->second.visited = false;
275  }
276 }
277 
279 {
280  VTopology::iterator itEntry = Topology.find(key);
281  if(itEntry != Topology.end() && itEntry->second.visited == false) {
282  int count = 1;
283  itEntry->second.visited = true;
284  Vast* vast = itEntry->second.getModule();
285  for(SiteMap::iterator itSites = vast->Sites.begin(); itSites != vast->Sites.end(); ++itSites) {
286  count += getComponentSize(itSites->first.getKey());
287  }
288  return count;
289  }
290  return 0;
291 }
292 
294 {
295  // destroy self timer messages
296  cancelAndDelete(probeTimer);
297  cancelAndDelete(plotTimer);
298 }