User Manual, Developers Guide and API Documentation

Main.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/ldk/fun/Main.hpp>
00029 
00030 #include <WNS/ldk/Layer.hpp>
00031 #include <WNS/ldk/FunctionalUnit.hpp>
00032 #include <WNS/ldk/CommandPool.hpp>
00033 #include <WNS/ldk/CommandProxy.hpp>
00034 #include <WNS/ldk/Group.hpp>
00035 #include <WNS/ldk/LinkHandlerInterface.hpp>
00036 #include <WNS/ldk/PyConfigCreator.hpp>
00037 
00038 #include <WNS/logger/Logger.hpp>
00039 #include <WNS/pyconfig/Parser.hpp>
00040 #include <WNS/StaticFactory.hpp>
00041 
00042 #include <iostream>
00043 
00044 using namespace ::wns::ldk;
00045 using namespace ::wns::ldk::fun;
00046 
00047 Main::Main(ILayer* _layer) :
00048         layer(_layer),
00049         proxy(new CommandProxy),
00050         fuMap(),
00051         linkHandler(NULL),
00052         logger("default","default")
00053 {
00054     wns::pyconfig::Parser pyco;
00055     pyco.loadString("import openwns.logger\n"
00056             "class LinkHandler:\n"
00057             "  type = \"wns.ldk.SimpleLinkHandler\"\n"
00058             "  isAcceptingLogger = openwns.logger.Logger(\"WNS\", \"LinkHandler\", False)\n"
00059             "  isAcceptingLogger.level = 3\n"
00060             "  sendDataLogger = openwns.logger.Logger(\"WNS\", \"LinkHandler\", False)\n"
00061             "  sendDataLogger.level = 3\n"
00062             "  wakeupLogger = openwns.logger.Logger(\"WNS\", \"LinkHandler\", False)\n"
00063             "  wakeupLogger.level = 3\n"
00064             "  onDataLogger = openwns.logger.Logger(\"WNS\", \"LinkHandler\", False)\n"
00065             "  onDataLogger.level = 3\n"
00066             "  traceCompoundJourney = False\n"
00067             "linkHandler = LinkHandler()\n");
00068     wns::pyconfig::View view(pyco, "linkHandler");
00069 
00070     linkHandler = wns::StaticFactory< wns::ldk::PyConfigCreator<LinkHandlerInterface> >
00071         ::creator(view.get<std::string>("type"))->create(view);
00072 } // Main
00073 
00074 
00075 Main::Main(ILayer* _layer, const wns::pyconfig::View& _config) :
00076         layer(_layer),
00077         proxy(new CommandProxy(_config.get("fun.commandProxy"))),
00078         fuMap(),
00079         linkHandler(NULL),
00080         logger(_config.get("fun.logger"))
00081 {
00082     wns::pyconfig::View view = _config.get<wns::pyconfig::View>("linkHandler");
00083 
00084     linkHandler = wns::StaticFactory< wns::ldk::PyConfigCreator<LinkHandlerInterface> >
00085         ::creator(view.get<std::string>("type"))
00086         ->create(view);
00087 } // Main
00088 
00089 void
00090 Main::onFUNCreated(wns::logger::Logger* logger)
00091 {
00092     for(FunctionalUnitMap::iterator it = fuMap.begin();
00093         it != fuMap.end();
00094         ++it) {
00095         if (logger)
00096             MESSAGE_SINGLE(NORMAL, (*logger), "Post-FUN-Creation for:" << it->second->getName());
00097         it->second->onFUNCreated();
00098     }
00099 }
00100 
00101 Main::~Main()
00102 {
00103     for(FunctionalUnitMap::iterator it = fuMap.begin();
00104         it != fuMap.end();
00105         ++it)
00106     {
00107         delete it->second;
00108     }
00109     delete linkHandler;
00110     delete proxy;
00111 } // ~Main
00112 
00113 
00114 CommandProxy*
00115 Main::getProxy() const
00116 {
00117     return this->proxy;
00118 } // getProxy
00119 
00120 
00121 void
00122 Main::addFunctionalUnit(const std::string& commandName,
00123             const std::string& functionalUnitName,
00124             FunctionalUnit* functionalUnit)
00125 {
00126     assure(functionalUnit, "tried to add invalid functionalUnit.");
00127 
00128     if(this->knowsFunctionalUnit(functionalUnitName))
00129         throw Exception("FunctionalUnit " + functionalUnitName + " already added.");
00130 
00131     functionalUnit->setName(functionalUnitName);
00132 
00133     this->fuMap[functionalUnitName] = functionalUnit;
00134     this->getProxy()->addFunctionalUnit(commandName, functionalUnit);
00135 
00136     MESSAGE_BEGIN(VERBOSE, logger, m, "");
00137     m << getName() << ": added FU '" << functionalUnit->getName() 
00138       << "', commandName: '" << commandName<< "', got PCIID: " << functionalUnit->getPCIID();
00139     MESSAGE_END();
00140 } // addFunctionalUnit
00141 
00142 
00143 
00144 
00145 void
00146 Main::addFunctionalUnit(const std::string& name, FunctionalUnit* functionalUnit)
00147 {
00148     // Backward compatibility Function, assuming that fuName and commandName
00149     // are equal
00150     this->addFunctionalUnit(name, name, functionalUnit);
00151 }
00152 
00153 void
00154 Main::removeFunctionalUnit(const std::string& name)
00155 {
00156     if(!this->knowsFunctionalUnit(name))
00157         throw Exception("FunctionalUnit " + name + " is not registered.");
00158 
00159     // remove command of FU from command pool
00160     this->getProxy()->removeFunctionalUnit(name);
00161 
00162     this->fuMap.erase(name);
00163 } // removeFunctionalUnit
00164 
00165 
00166 FunctionalUnit*
00167 Main::getFunctionalUnit(const std::string& name) const
00168 {
00169     FunctionalUnitMap::const_iterator it = this->fuMap.find(name);
00170 
00171     if(it == this->fuMap.end())
00172     {
00173         Exception e;
00174         e << "FunctionalUnit '" << name << "' not found in " << getName() << ".\n\n"
00175           << "Available FUs are: \n";
00176 
00177         for(FunctionalUnitMap::const_iterator iter = fuMap.begin();
00178             iter != fuMap.end();
00179             ++iter)
00180         {
00181             e << " * " << (iter->second)->getName() << "\n";
00182         }
00183 
00184         throw e;
00185     }
00186 
00187     return it->second;
00188 } // getFunctionalUnit
00189 
00190 
00191 bool
00192 Main::knowsFunctionalUnit(const std::string& name) const
00193 {
00194     return this->fuMap.find(name) != this->fuMap.end();
00195 } // knowsFunctionalUnit
00196 
00197 
00198 void
00199 Main::connectFunctionalUnit(const std::string& upperName, const std::string& lowerName,
00200                             const std::string& srcPort, const std::string& dstPort)
00201 
00202 {
00203     FunctionalUnit* upper = this->getFunctionalUnit(upperName);
00204     FunctionalUnit* lower = this->getFunctionalUnit(lowerName);
00205 
00206         upper->connect(lower, srcPort, dstPort);
00207 } // connectFunctionalUnit
00208 
00209 
00210 void
00211 Main::upConnectFunctionalUnit(const std::string& upperName, const std::string& lowerName,
00212                               const std::string& srcPort, const std::string& dstPort)
00213 {
00214     FunctionalUnit* upper = this->getFunctionalUnit(upperName);
00215     FunctionalUnit* lower = this->getFunctionalUnit(lowerName);
00216 
00217         upper->upConnect(lower, srcPort, dstPort);
00218 } // connectFunctionalUnit
00219 
00220 
00221 void
00222 Main::downConnectFunctionalUnit(const std::string& upperName, const std::string& lowerName,
00223                                 const std::string& srcPort, const std::string& dstPort)
00224 {
00225     FunctionalUnit* upper = this->getFunctionalUnit(upperName);
00226     FunctionalUnit* lower = this->getFunctionalUnit(lowerName);
00227 
00228         upper->downConnect(lower, srcPort, dstPort);
00229 } // connectFunctionalUnit
00230 
00231 /*
00232 void
00233 Main::reconfigureFUN(const wns::pyconfig::View& reconfig)
00234 {
00235     // pointer to FU to be replaced
00236     std::string oldFUName = reconfig.get<std::string>("replaceFU");
00237     FunctionalUnit* oldFU = getFunctionalUnit(oldFUName);
00238 
00239     // create new FU
00240     wns::pyconfig::View FUconfig(reconfig, "newFUConfig");
00241     std::string pluginName = FUconfig.get<std::string>("__plugin__");
00242     FunctionalUnit* newFU = FunctionalUnitFactory::creator(pluginName)
00243         ->create(oldFU->getFUN(), FUconfig);
00244     addFunctionalUnit(reconfig.get<std::string>("newFUName"), newFU);
00245 
00246     // remove command of old FU from command pool
00247     getProxy()->removeFunctionalUnit(oldFUName);
00248 
00249     // if old FU contains a SubFUN remove all commands of FUs inside the
00250     // SubFUN from command pool
00251     Group* groupFU = dynamic_cast<Group*>(oldFU);
00252     if (groupFU)
00253         groupFU->getSubFUN()->removeFUsFromCommandPool();
00254 
00255     // change links of upper connectors, lower deliverers and lower receptors
00256     // to point to new FU
00257     for(FunctionalUnitMap::iterator it = fuMap.begin();
00258         it != fuMap.end();
00259         ++it)
00260     {
00261         // upper connector links
00262         Link::ExchangeContainer connectorLink = it->second->getConnector()->get();
00263         for (unsigned int i = 0; i < connectorLink.size(); ++i)
00264         {
00265             if (connectorLink[i] == oldFU)
00266             {
00267                 connectorLink[i] = newFU;
00268                 it->second->getConnector()->set(connectorLink);
00269                 break;
00270             }
00271         }
00272 
00273         // lower deliverer links
00274         Link::ExchangeContainer delivererLink = it->second->getDeliverer()->get();
00275         for (unsigned int i = 0; i < delivererLink.size(); ++i)
00276         {
00277             if (delivererLink[i] == oldFU)
00278             {
00279                 delivererLink[i] = newFU;
00280                 it->second->getDeliverer()->set(delivererLink);
00281                 break;
00282             }
00283         }
00284 
00285         // lower receptor links
00286         Link::ExchangeContainer receptorLink = it->second->getReceptor()->get();
00287         for (unsigned int i = 0; i < receptorLink.size(); ++i)
00288         {
00289             if (receptorLink[i] == oldFU)
00290             {
00291                 receptorLink[i] = newFU;
00292                 it->second->getReceptor()->set(receptorLink);
00293                 break;
00294             }
00295         }
00296     }
00297     // copy upper receptor link set from old FU to new FU
00298     newFU->getReceptor()->set(oldFU->getReceptor()->get());
00299 
00300     // copy upper deliverer link set from old FU to new FU
00301     newFU->getDeliverer()->set(oldFU->getDeliverer()->get());
00302 
00303     // copy lower connector link set from old FU to new FU
00304     newFU->getConnector()->set(oldFU->getConnector()->get());
00305 
00306     // delete old FU
00307     fuMap.erase(oldFUName);
00308     delete oldFU;
00309 
00310     // find new friends
00311     onFUNCreated();
00312 } // reconfigureFUN
00313 */
00314 
00315 //
00316 // Layer delegations
00317 //
00318 
00319 
00320 ILayer*
00321 Main::getLayer() const
00322 {
00323     return this->layer;
00324 } // getLayer
00325 
00326 
00327 std::string
00328 Main::getName() const
00329 {
00330     return this->getLayer()->getName();
00331 } // getName
00332 
00333 LinkHandlerInterface*
00334 Main::getLinkHandler() const
00335 {
00336     return linkHandler;
00337 }
00338 
00339 CommandReaderInterface*
00340 Main::getCommandReader(const std::string& commandName) const
00341 {
00342     return proxy->getCommandReader(commandName);
00343 }
00344 
00345 
00346 

Generated on Sun May 27 03:31:43 2012 for openWNS by  doxygen 1.5.5