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().key);
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         );
00140         cOV_AverageMissingNeighbors.record(mnAverage);
00141         cOV_MaxMissingNeighbors.record((double)mnMax);
00142         cOV_AverageDrift.record(drift);
00143     }
00144     else if(msg->isName("plotTimer")) {
00145          //reset timer
00146         cancelEvent(plotTimer);
00147         scheduleAt(simTime() + plotIntervall, msg);
00148 
00149         int range = (int)Topology.begin()->second.getModule()->getAreaDimension();
00150         std::stringstream oss;
00151         std::string filename;
00152         int simTimeInt, stellen = 1;
00153         simTimeInt = (int)SIMTIME_DBL(simTime());
00154         oss << "plot";
00155         for(int i=0; i<6; i++) {
00156             if(!(simTimeInt / stellen)) {
00157                 oss << "0";
00158             }
00159             stellen *= 10;
00160         }
00161         oss << simTimeInt;
00162 
00163         // open/ write plot file
00164         filename = oss.str() + ".plot";
00165         pltNetwork.open(filename.c_str(), std::ios::out);
00166         pltNetwork << "set xrange [0:" << range << "]" << endl;
00167         pltNetwork << "set yrange [0:" << range << "]" << endl;
00168         pltNetwork << "set nokey" << endl;
00169 
00170         // open point file
00171         filename = oss.str() + ".point";
00172         pltData.open(filename.c_str(), std::ios::out);
00173 
00174         pltNetwork << "plot '" << filename << "' using 1:2 with points pointtype 7,\\" << endl;
00175 
00176         // open vector file
00177         filename = oss.str() + ".arrow";
00178         pltVector.open(filename.c_str(), std::ios::out);
00179 
00180         pltNetwork << "     '" << filename << "' using 1:2:3:4 with vectors linetype 1" << endl;
00181         pltNetwork.close();
00182 
00183         // write point data file
00184         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00185             pltData << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << endl;
00186         }
00187         pltData.close();
00188 
00189         //write arrow data file
00190         for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00191             for(SiteMap::iterator itSites = itTopology->second.getModule()->Sites.begin(); itSites != itTopology->second.getModule()->Sites.end(); ++itSites) {
00192                 if(plotConnections) {
00193                     VTopology::iterator destNode = Topology.find(itSites->second->addr.key);
00194                     if(destNode != Topology.end()) {
00195                         Vector2D relPos = destNode->second.getModule()->getPosition() - itTopology->second.getModule()->getPosition();
00196                         pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00197                                   << relPos.x << "\t" << relPos.y << endl;
00198                     }
00199                 }
00200                 else {
00201                     Vector2D relPos = itSites->second->coord - itTopology->second.getModule()->getPosition();
00202                     pltVector << itTopology->second.getModule()->getPosition().x << "\t" << itTopology->second.getModule()->getPosition().y << "\t"
00203                               << relPos.x << "\t" << relPos.y << endl;
00204                 }
00205             }
00206         }
00207         pltVector.close();
00208     }
00209     Topology.clear();
00210 }
00211 
00212 void ConnectivityProbe::extractTopology()
00213 {
00214     for(int i=0; i<=simulation.getLastModuleId(); i++) {
00215         cModule* module = simulation.getModule(i);
00216         if(module && dynamic_cast<Vast*>(module)) {
00217             Vast* vast = check_and_cast<Vast*>(module);
00218             if(vast->getState() == BaseOverlay::READY) {
00219                 VTopologyNode temp(i);
00220                 Topology.insert(std::make_pair(vast->getHandle().key, temp));
00221             }
00222         }
00223     }
00224 }
00225 
00226 void ConnectivityProbe::resetTopologyNodes()
00227 {
00228     for(VTopology::iterator itTopology = Topology.begin(); itTopology != Topology.end(); ++itTopology) {
00229         itTopology->second.visited = false;
00230     }
00231 }
00232 
00233 unsigned int ConnectivityProbe::getComponentSize(OverlayKey key)
00234 {
00235     VTopology::iterator itEntry = Topology.find(key);
00236     if(itEntry != Topology.end() && itEntry->second.visited == false) {
00237         int count = 1;
00238         itEntry->second.visited = true;
00239         Vast* vast = itEntry->second.getModule();
00240         for(SiteMap::iterator itSites = vast->Sites.begin(); itSites != vast->Sites.end(); ++itSites) {
00241             count += getComponentSize(itSites->first.key);
00242         }
00243         return count;
00244     }
00245     return 0;
00246 }
00247 
00248 ConnectivityProbe::~ConnectivityProbe()
00249 {
00250     // destroy self timer messages
00251     cancelAndDelete(probeTimer);
00252     cancelAndDelete(plotTimer);
00253 }

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