INET Framework for OMNeT++/OMNEST
CriticalnessAnalyzer.cc
Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2010 Martin Danko
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 
00019 #include "CriticalnessAnalyzer.h"
00020 
00021 
00022 Define_Module(CriticalnessAnalyzer);
00023 
00024 void CriticalnessAnalyzer::initialize()
00025 {
00026 
00027 if(par("analyze"))
00028 {
00029   cTopology topology;
00030   
00031   topology.extractByNedTypeName(cStringTokenizer("inet.ansa.ANSARouter").asVector());
00032   
00033   routers.clear();
00034   
00035   for(int i = 0; i < topology.getNumNodes(); ++i)
00036   {
00037     cTopology::Node *node = topology.getNode(i);
00038     
00039     CA::RouterRecord router(node->getModule()->getName(), node->getModule());
00040     
00041     for(int j = 0; j < node->getNumOutLinks(); ++j)
00042     {
00043       cTopology::LinkOut  *link = node->getLinkOut(j);
00044       double cost = (100000000 / link->getLocalGate()->getTransmissionChannel()->par("datarate").doubleValue());
00045       std::string name = link->getRemoteNode()->getModule()->getName();
00046       
00047       CA::NeighborRecord neigh(name, cost);
00048       router.addNeighbor(neigh);
00049     }
00050     
00051     routers.push_back(router);
00052     
00053   }
00054 
00055   startNode = (const char *) par("startNode");
00056   if(!existRouter(startNode))
00057     error("Error - Start node %s doesn't exist", startNode.c_str());
00058   
00059   destNode = (const char *) par("destNode");
00060   if(!existRouter(destNode))
00061     error("Error - Destination node %s doesn't exist", destNode.c_str());
00062   
00063   tempPath.addNode(startNode, 0.0);
00064   generatePaths(startNode);
00065   sortByCost();
00066   calculateCriticalness();
00067   updateDisplayString();
00068   writeResultsToFile();
00069   
00070   WATCH_VECTOR(paths);
00071   WATCH_VECTOR(routers);
00072 }  
00073 }
00074 
00075 void CriticalnessAnalyzer::handleMessage(cMessage *msg)
00076 {
00077     ASSERT(false);
00078 }
00079 
00080 std::vector<CA::RouterRecord>::iterator  CriticalnessAnalyzer::getRouterRecord(std::string &name)
00081 {
00082   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00083   {
00084      if(name == it->getName())
00085       return it;
00086   }
00087   return routers.end();
00088 }
00089 
00090 
00091 void CriticalnessAnalyzer::generatePaths(std::string &name)
00092 {
00093   std::vector<CA::RouterRecord>::iterator it = getRouterRecord(name);
00094   
00095   if(it !=routers.end())
00096   {
00097     for(int i = 0; i < it->getNeighborsNum(); ++i)
00098     {
00099       std::string neighName = it->getNeighbor(i).getName();
00100       double cost = it->getNeighbor(i).getCost();
00101       double actCost = tempPath.getCost(); 
00102       
00103       if (neighName == destNode)
00104       {
00105         tempPath.addNode(neighName, cost);
00106         paths.push_back(tempPath);
00107         tempPath.removeLastNode();
00108         tempPath.setCost(actCost);
00109       }
00110       else if (!tempPath.isInPath(neighName))
00111       {
00112         tempPath.addNode(neighName, cost);
00113         generatePaths(neighName);
00114         tempPath.removeLastNode();
00115         tempPath.setCost(actCost);
00116       }
00117     }
00118   }
00119   
00120 }
00121 
00122 void CriticalnessAnalyzer::sortByCost()
00123 {
00124   std::vector<CA::Path> tmpPaths;
00125   
00126   while (paths.size() > 0)
00127   {
00128     int i = 0;
00129     int index = 0;
00130     double cost = paths.at(0).getCost();
00131     
00132     for (std::vector<CA::Path>::iterator it = paths.begin(); it!=paths.end(); ++it)
00133     {
00134       if(it->getCost() < cost)
00135       {
00136         cost = it->getCost();
00137         index = i;
00138       }
00139       ++i;
00140     }
00141     tmpPaths.push_back(paths.at(index));
00142     paths.erase(paths.begin() + index);   
00143   }
00144   
00145   paths = tmpPaths;
00146 }
00147 
00148 void CriticalnessAnalyzer::calculateCriticalness()
00149 {
00150   int pathNum = paths.size();
00151   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00152   {
00153     int p = 0;
00154     std::string name = it->getName();
00155     
00156     
00157     for (std::vector<CA::Path>::iterator pit = paths.begin(); pit!=paths.end(); ++pit)
00158     {
00159       if(pit->isInPath(name))
00160         ++p;
00161     }
00162     it->setCriticalness((int)((p*100)/pathNum));
00163   }  
00164 }
00165 
00166 bool CriticalnessAnalyzer::existRouter(std::string &name)
00167 {
00168   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00169   {
00170     if(it->getName() == name)
00171       return true;
00172   }
00173   return false;
00174 }
00175 
00176 void CriticalnessAnalyzer::updateDisplayString()
00177 {
00178   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00179   {  
00180       int cr = it->getCriticalness();
00181       char buf[80];
00182       sprintf(buf, "Criticalness: %d%%", cr);
00183        
00184       if(cr == 100 )
00185       {
00186         it->getModule()->getDisplayString().setTagArg("i",1,"red");
00187       }
00188       else if (cr == 0)
00189       {
00190         it->getModule()->getDisplayString().setTagArg("i",1,"yellow");
00191       }
00192       it->getModule()->getDisplayString().setTagArg("t",0,buf);
00193   }
00194 
00195 }
00196 
00197 void CriticalnessAnalyzer::writeResultsToFile()
00198 { 
00199   std::ofstream outFile;
00200   outFile.open ("results/CPAnalysis.csv");
00201   
00202   outFile << ";TotCost";
00203   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00204   {
00205     outFile << ";"<< it->getName();
00206   }
00207    outFile << "\n";
00208   
00209   int i = 1;
00210   
00211   for (std::vector<CA::Path>::iterator pit = paths.begin(); pit!=paths.end(); ++pit)
00212   {
00213     outFile << i << ";" << pit->getCost();
00214     for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00215     {
00216       std::string name = it->getName();
00217       if(pit->isInPath(name))
00218         outFile << ";x";
00219       else
00220         outFile << ";";
00221     }
00222     outFile << "\n";
00223     ++i;
00224   }
00225   
00226   outFile << "CP;";
00227   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00228   {
00229     if(it->getCriticalness() == 100)
00230       outFile << ";x";
00231     else
00232       outFile << ";";
00233   }
00234   outFile << "\n";
00235   
00236   outFile << "UP;";
00237   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00238   {
00239     if(it->getCriticalness() == 0)
00240       outFile << ";x";
00241     else
00242       outFile << ";";
00243   }
00244   outFile << "\n";
00245   
00246   outFile << "Criticalness;";
00247   for (std::vector<CA::RouterRecord>::iterator it = routers.begin(); it!=routers.end(); ++it)
00248   {
00249     outFile << ";" << it->getCriticalness() << "%";
00250   }
00251   outFile << "\n";
00252   
00253   outFile.close();
00254 }
00255 
00256 
00257 bool CA::Path::isInPath(std::string &node)
00258 {
00259   for (std::vector<std::string>::iterator it = path.begin(); it!=path.end(); ++it)
00260   {
00261      if(node == *it)
00262       return true;
00263   }
00264   return false;
00265 }
00266 
00267