ConnectivityProbe.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 "ConnectivityProbe.h"
00025 
00026 Define_Module(ConnectivityProbe);
00027 
00028 VTopologyNode::VTopologyNode(int moduleID)
00029 {
00030     this->moduleID = moduleID;
00031     visited = false;
00032 }
00033 
00034 Vast* VTopologyNode::getModule() const
00035 {
00036     return check_and_cast<Vast*>(simulation.getModule(moduleID));
00037 }
00038 
00039 void ConnectivityProbe::initialize()
00040 {
00041     globalStatistics = GlobalStatisticsAccess().get();
00042     probeIntervall = par("connectivityProbeIntervall");
00043     plotIntervall = par("visualizeNetworkIntervall");
00044     probeTimer = new cMessage("probeTimer");
00045     plotTimer = new cMessage("plotTimer");
00046     plotConnections = par("plotConnections");
00047 
00048     if(probeIntervall > 0.0) {
00049         scheduleAt(simTime() + probeIntervall, probeTimer);
00050 
00051         cOV_NodeCount.setName("total node count");
00052         cOV_MaximumComponent.setName("largest connected component");
00053         cOV_MaxConnectivity.setName("connectivity in percent");
00054         cOV_ZeroMissingNeighbors.setName("neighbor-error free nodes");
00055         cOV_AverageMissingNeighbors.setName("average missing neighbors per node");
00056         cOV_MaxMissingNeighbors.setName("largest missing neighbors count");
00057         cOV_AverageDrift.setName("average drift");
00058     }
00059 
00060     if(plotIntervall > 0.0) {
00061         scheduleAt(simTime() + plotIntervall, plotTimer);
00062     }
00063 }
00064 
00065 void ConnectivityProbe::handleMessage(cMessage* msg)
00066 {
00067     // fill topology with all VAST modules
00068     extractTopology();
00069 
00070     if(Topology.size() == 0) {
00071         return;
00072     }
00073 
00074     // catch self timer messages
00075     if(msg->isName("probeTimer")) {
00076         //reset timer
00077         cancelEvent(probeTimer);
00078         scheduleAt(simTime() + probeIntervall, msg);
00079 
00080         unsigned int maxComponent = 0;
00081         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00082             unsigned int count = getComponentSize(itTopology->second.getModule()->getHandle().getKey());
00083             if(count > maxComponent) {
00084                 maxComponent = count;
00085             }
00086             resetTopologyNodes();
00087             if(count == Topology.size()) {
00088                 break;
00089             }
00090         }
00091 
00092         cOV_NodeCount.record((double)Topology.size());
00093         cOV_MaximumComponent.record((double)maxComponent);
00094         cOV_MaxConnectivity.record((double)maxComponent * 100.0 / (double)Topology.size());
00095         RECORD_STATS (
00096             globalStatistics->addStdDev("ConnectivityProbe: max connectivity", (double)maxComponent * 100.0 / (double)Topology.size());
00097         );
00098 
00099         int mnMax = 0;
00100         int mnZero = 0;
00101         int driftCount = 0;
00102         double mnAverage = 0.0;
00103         double drift = 0.0;
00104 
00105         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00106             double AOIWidthSqr = itTopology->second.getModule()->getAOI();
00107             AOIWidthSqr *= AOIWidthSqr;
00108             Vector2D vastPosition = itTopology->second.getModule()->getPosition();
00109             int missing = 0;
00110             for(VTopology::iterator itI = Topology.begin(); itI != Topology.end(); ++itI) {
00111                 if(itI != itTopology && vastPosition.distanceSqr(itI->second.getModule()->getPosition()) <= AOIWidthSqr) {
00112                     SiteMap::iterator currentSite = itTopology->second.getModule()->Sites.find(itI->second.getModule()->getHandle());
00113                     if(currentSite == itTopology->second.getModule()->Sites.end()) {
00114                         ++missing;
00115                     }
00116                     else {
00117                         drift += sqrt(currentSite->second->coord.distanceSqr(itI->second.getModule()->getPosition()));
00118                         ++driftCount;
00119                     }
00120                 }
00121             }
00122 
00123             mnAverage += missing;
00124             if(mnMax < missing) {
00125                 mnMax = missing;
00126             }
00127             if(missing == 0) {
00128                 ++mnZero;
00129             }
00130         }
00131         mnAverage /= (double)Topology.size();
00132         if(driftCount > 0) {
00133             drift /= (double)driftCount;
00134         }
00135 
00136         cOV_ZeroMissingNeighbors.record((double)mnZero);
00137         RECORD_STATS (
00138             globalStatistics->addStdDev("ConnectivityProbe: percentage zero missing neighbors", (double)mnZero * 100.0 / (double)Topology.size());
00139             globalStatistics->addStdDev("ConnectivityProbe: average drift", drift);
00140         );
00141         cOV_AverageMissingNeighbors.record(mnAverage);
00142         cOV_MaxMissingNeighbors.record((double)mnMax);
00143         cOV_AverageDrift.record(drift);
00144     }
00145     else if(msg->isName("plotTimer")) {
00146          //reset timer
00147         cancelEvent(plotTimer);
00148         scheduleAt(simTime() + plotIntervall, msg);
00149 
00150         int range = (int)Topology.begin()->second.getModule()->getAreaDimension();
00151         std::stringstream oss;
00152         std::string filename;
00153         int simTimeInt, stellen = 1;
00154         simTimeInt = (int)SIMTIME_DBL(simTime());
00155         oss << "plot";
00156         for(int i=0; i<6; i++) {
00157             if(!(simTimeInt / stellen)) {
00158                 oss << "0";
00159             }
00160             stellen *= 10;
00161         }
00162         oss << simTimeInt;
00163 
00164         // open/ write plot file
00165         filename = oss.str() + ".plot";
00166         pltNetwork.open(filename.c_str(), std::ios::out);
00167         pltNetwork << "set xrange [0:" << range << "]" << endl;
00168         pltNetwork << "set yrange [0:" << range << "]" << endl;
00169         pltNetwork << "set nokey" << endl;
00170 
00171         // open point file
00172         filename = oss.str() + ".point";
00173         pltData.open(filename.c_str(), std::ios::out);
00174 
00175         pltNetwork << "plot '" << filename << "' using 1:2 with points pointtype 7,\\" << endl;
00176 
00177         // open vector file
00178         filename = oss.str() + ".arrow";
00179         pltVector.open(filename.c_str(), std::ios::out);
00180 
00181         pltNetwork << "     '" << filename << "' using 1:2:3:4 with vectors linetype 1" << endl;
00182         pltNetwork.close();
00183 
00184         // write point data file
00185         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00186             pltData << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << endl;
00187         }
00188         pltData.close();
00189 
00190         //write arrow data file
00191         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00192             for(SiteMap::iterator itSites = itTopology->second.getModule()->Sites.begin(); itSites != itTopology->second.getModule()->Sites.end(); ++itSites) {
00193                 if(plotConnections) {
00194                     VTopology::iterator destNode = Topology.find(itSites->second->addr.getKey());
00195                     if(destNode != Topology.end()) {
00196                         Vector2D relPos = destNode->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
00197                         pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00198                                   << relPos.x << "\t" << relPos.y << endl;
00199                     }
00200                 }
00201                 else {
00202                     Vector2D relPos = itSites->second->coord - itTopology->second.getModule()->getPosition();
00203                     pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00204                               << relPos.x << "\t" << relPos.y << endl;
00205                 }
00206             }
00207         }
00208         pltVector.close();
00209     }
00210     Topology.clear();
00211 }
00212 
00213 void ConnectivityProbe::extractTopology()
00214 {
00215     for(int i=0; i<=simulation.getLastModuleId(); i++) {
00216         cModule* module = simulation.getModule(i);
00217         if(module && dynamic_cast<Vast*>(module)) {
00218             Vast* vast = check_and_cast<Vast*>(module);
00219             if(vast->getState() == BaseOverlay::READY) {
00220                 VTopologyNode temp(i);
00221                 Topology.insert(std::make_pair(vast->getHandle().getKey(), temp));
00222             }
00223         }
00224     }
00225 }
00226 
00227 void ConnectivityProbe::resetTopologyNodes()
00228 {
00229     for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00230         itTopology->second.visited = false;
00231     }
00232 }
00233 
00234 unsigned int ConnectivityProbe::getComponentSize(OverlayKey key)
00235 {
00236     VTopology::iterator itEntry = Topology.find(key);
00237     if(itEntry != Topology.end() && itEntry->second.visited == false) {
00238         int count = 1;
00239         itEntry->second.visited = true;
00240         Vast* vast = itEntry->second.getModule();
00241         for(SiteMap::iterator itSites = vast->Sites.begin(); itSites != vast->Sites.end(); ++itSites) {
00242             count += getComponentSize(itSites->first.getKey());
00243         }
00244         return count;
00245     }
00246     return 0;
00247 }
00248 
00249 ConnectivityProbe::~ConnectivityProbe()
00250 {
00251     // destroy self timer messages
00252     cancelAndDelete(probeTimer);
00253     cancelAndDelete(plotTimer);
00254 }

Generated on Tue Sep 8 17:26:52 2009 for OverSim by  doxygen 1.5.8