User Manual, Developers Guide and API Documentation

Node.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. 16, 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/node/Node.hpp>
00029 #include <WNS/node/Registry.hpp>
00030 #include <WNS/service/Service.hpp>
00031 #include <WNS/node/component/Interface.hpp>
00032 #include <WNS/Functor.hpp>
00033 #include <WNS/Assure.hpp>
00034 
00035 #include <WNS/probe/bus/ContextProviderCollection.hpp>
00036 #include <WNS/probe/bus/ContextProvider.hpp>
00037 
00038 using namespace wns::node;
00039 
00040 std::list<unsigned long int> Node::ids;
00041 
00042 Node::Node(Registry* registry, const pyconfig::View& pyco) :
00043     localServices(),
00044     contextProviderRegistry(),
00045     name(pyco.get<std::string>("name")),
00046     nodeID(pyco.get<unsigned long int>("nodeID")),
00047     config(pyco),
00048     log(pyco.get("logger")),
00049     globalNodes(registry),
00050     localComponents()
00051 {
00052     assure(name != "", "No name given!");
00053     assure(globalNodes,  "must be non-NULL");
00054 
00055     // Check if nodeID was not allocated yet
00056     if (std::find(ids.begin(), ids.end(), nodeID) != ids.end())
00057     {
00058         // We throw an error now. Unfortunately we need to cleanup our
00059         // allocated data, the destructor will not be called
00060         // if you throw an exception within a constructor.
00061         // @todo dbn : apply auto_ptr to IDProvider Registries
00062         throw wns::Exception("Duplicate Node ID detected!");
00063     }
00064 
00065     // All went well, mark this nodeID allocated
00066     Node::ids.insert(ids.begin(),nodeID);
00067 
00068     for (int ii = 0; ii < pyco.len("contextProviders"); ++ii)
00069     {
00070         // for now only ConstantContextProviders are allowed
00071         wns::pyconfig::View providerView = pyco.get("contextProviders", ii);
00072         getContextProviderCollection().addProvider(wns::probe::bus::contextprovider::Constant(providerView));
00073     }
00074 
00075     // add Node to registry
00076     globalNodes->insert(name, this);
00077 }
00078 
00079 void
00080 Node::startup()
00081 {
00082     // loop over the list to create all components
00083     for(int ii = 0;
00084         ii < config.len("components");
00085         ++ii)
00086     {
00087         pyconfig::View componentConfig = config.get<pyconfig::View>("components", ii);
00088 
00089         // get the type of the Component and ask the ComponentFactory
00090         std::string plugin = componentConfig.get<std::string>("nameInComponentFactory");
00091 
00092         component::Creator* cc = component::Factory::creator(plugin);
00093 
00094         // create and add the Component to this Node
00095         component::Interface* ci = cc->create(this, componentConfig);
00096         localComponents.push_back(ci);
00097         ci->startup();
00098     }
00099 
00100     // tell all Components that we're ready with construction
00101     for(ComponentContainer::iterator itr = localComponents.begin();
00102         itr != localComponents.end();
00103         ++itr)
00104     {
00105         (*itr)->onNodeCreated();
00106     }
00107 } // Node
00108 
00109 
00110 Node::~Node()
00111 {
00112     // delete all Components
00113     for(ComponentContainer::iterator it = localComponents.begin();
00114         it != localComponents.end();
00115         ++it)
00116     {
00117         delete *it;
00118     }
00119 
00120     // clear the container
00121     localComponents.clear();
00122 } // ~Node
00123 
00124 
00125 void
00126 Node::addService(const std::string& name, service::Service* si)
00127 {
00128     assure(si, "must be non-NULL");
00129     localServices.insert(name, si);
00130 }
00131 
00132 void
00133 Node::onWorldCreated()
00134 {
00135     std::for_each(localComponents.begin(),
00136               localComponents.end(),
00137               std::mem_fun(&wns::node::component::Interface::onWorldCreated));
00138 }
00139 
00140 void
00141 Node::onShutdown()
00142 {
00143     std::for_each(localComponents.begin(),
00144               localComponents.end(),
00145               std::mem_fun(&wns::node::component::Interface::onShutdown));
00146 }
00147 
00148 wns::probe::bus::ContextProviderCollection&
00149 Node::getContextProviderCollection()
00150 {
00151     return contextProviderRegistry;
00152 }
00153 
00154 std::string
00155 Node::getName() const
00156 {
00157     assure(name != "", "No name given!");
00158     return name;
00159 }
00160 
00161 unsigned int
00162 Node::getNodeID() const
00163 {
00164     return nodeID;
00165 }
00166 
00167 
00168 
00169 wns::service::Service*
00170 Node::getAnyService(const component::FQSN& fqsn) const
00171 {
00172     if(fqsn.getNodeName() == getName())
00173     {
00174         // we can answer the question
00175         return localServices.find(fqsn.getServiceName());
00176     }
00177     else
00178     {
00179         // ask someone else
00180         node::Interface* node =
00181             globalNodes->find(fqsn.getNodeName());
00182         return node->getService<wns::service::Service*>(fqsn.getServiceName());
00183     }
00184 }
00185 
00186 
00187 /*
00188   Local Variables:
00189   mode: c++
00190   fill-column: 80
00191   c-basic-offset: 8
00192   c-tab-always-indent: t
00193   indent-tabs-mode: t
00194   tab-width: 8
00195   End:
00196 */

Generated on Fri May 25 03:31:48 2012 for openWNS by  doxygen 1.5.5