User Manual, Developers Guide and API Documentation

UdpServerListenerBinding.cpp

Go to the documentation of this file.
00001  /******************************************************************************
00002  * UDP ServerListener Binding                                                 *
00003  * __________________________________________________________________________ *
00004  *                                                                            *
00005  * Copyright (C) 2005                                                         *
00006  * Lehrstuhl fuer Kommunikationsnetze (ComNets)                               *
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany                                 *
00008  * phone: ++49-241-80-27910 (phone), fax: ++49-241-80-22242                   *
00009  * email: wns@comnetsrwth-aachen.de, www: http://wns.comnets.rwth-aachen.de/  *
00010  *****************************************************************************/
00011 
00012 #include <CONSTANZE/UdpServerListenerBinding.hpp>
00013 #include <CONSTANZE/UdpServerBinding.hpp>
00014 
00015 #include <WNS/StaticFactory.hpp>
00016 #include <WNS/service/nl/Address.hpp>
00017 #include <WNS/service/tl/PortPool.hpp>
00018 #include <WNS/module/Base.hpp>
00019 
00020 using namespace constanze;
00021 
00022 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00023     constanze::UdpServerListenerBinding,
00024     constanze::Binding,
00025     "UdpServerListenerBinding",
00026     wns::PyConfigViewCreator);
00027 
00028 UdpServerListenerBinding::UdpServerListenerBinding(const wns::pyconfig::View& _pyco):
00029     pyco(_pyco),
00030     log(pyco.get("logger")),
00031     dns(NULL),
00032     listenPort(_pyco.get<int>("listenPort")),
00033     generatorConfig(pyco.get("generator")),
00034     nodeID(0)
00035 {
00036     c = GeneratorBase::Factory::creator(generatorConfig.get<std::string>("__plugin__"));
00037 
00038     // who sets ownAddress??? [rs]
00039     MESSAGE_BEGIN(NORMAL, log, m, "New UdpServerListenerBinding created on my socket=");
00040     m << ownAddress << ":" << listenPort;
00041     MESSAGE_END();
00042 }
00043 
00044 UdpServerListenerBinding::~UdpServerListenerBinding()
00045 {
00046   for(std::list<GeneratorBase*>::const_iterator iter = generatorList.begin(); iter != generatorList.end(); ++iter) {
00047       delete (*iter);
00048   }
00049 }
00050 
00051 void UdpServerListenerBinding::sendData(const wns::osi::PDUPtr&)
00052 {
00053     // use registry<Generator, Connection> to send the data
00054 }
00055 
00056 void UdpServerListenerBinding::registerComponent(wns::node::component::Component* _component)
00057 {
00058     component = _component;
00059     udpService =
00060         component->getService<wns::service::tl::Service*>(
00061             pyco.get<std::string>("udpService"));
00062     udpService->listenOnPort(listenPort, this);
00063     dns = component->getService<wns::service::nl::DNSService*>(pyco.get<std::string>("dnsService"));
00064 
00065     nodeID = component->getNode()->getNodeID();
00066 }
00067 
00068 void
00069 UdpServerListenerBinding::onConnectionEstablished(wns::service::nl::Address _address, wns::service::tl::Connection* _connection)
00070 {
00071     // _address is not my address, but the one of the peer node;
00072     // my address can be found in connection's flow ID.
00073     // Create a Generator on top of this Binding
00074     // Of course handling of all connection pointers is necessary.
00075     // A registry with mapping of Generator to Connection#
00076     // Generator has to get its parameters either from here or from python
00077     peerAddress = _address;
00078     _connection->registerDataHandler(this);
00079 
00080     //generate the traffic generator:
00081     GeneratorBase* generator = c->create(generatorConfig);
00082 
00083     MESSAGE_BEGIN(NORMAL, log, m, "");
00084     m << "onConnectionEstablished() called from peer "<<peerAddress;
00085     MESSAGE_END();
00086 
00087     // save (generator, connection):
00088     //generatorToConnectionRegistry.insert(generator, _connection);
00089 
00090     // generate the UdpServerBinding for the generator:
00091     UdpServerBinding* serverBinding = new constanze::UdpServerBinding(_address, _connection, log);
00092 
00093     assure(dns!=NULL,"dns needed");
00094     //ownAddress = serverBinding->???();
00095     //ownAddress = listener->printAddress();
00096     ownAddress = dns->lookup(listener->printAddress());
00097 
00098     MESSAGE_BEGIN(NORMAL, log, m, "");
00099     m << "onConnectionEstablished(): me="<<ownAddress<<":"<<listenPort;
00100     MESSAGE_END();
00101 
00102     // Tell the generator which binding to use:
00103     generator->registerBinding(serverBinding);
00104     generator->bindingReady();
00105     // keep a list of all generated generators
00106     generatorList.push_back(generator);
00107 }
00108 
00109 void UdpServerListenerBinding::onData(const wns::osi::PDUPtr& _data)
00110 {
00111     assure(listener, "No listener set.");
00112     MESSAGE_BEGIN(NORMAL, log, m, "Received " << _data->getLengthInBits()/8 << " bytes"); // "from ?"
00113     m << " on my socket=" << ownAddress << ":" << listenPort;
00114     MESSAGE_END();
00115     listener->onData(_data);
00116 }
00117 
00118 void UdpServerListenerBinding::registerListener(constanze::Listener* _listener)
00119 {
00120     listener = _listener;
00121 }
00122 
00123 void UdpServerListenerBinding::initBinding(constanze::StartTrigger* )
00124 {
00125     // intentionally left empty
00126 }
00127 
00128 void UdpServerListenerBinding::releaseBinding(constanze::StopTrigger*)
00129 {
00131 }
00132 
00133 void UdpServerListenerBinding::onConnectionClosed(wns::service::tl::Connection*)
00134 {
00135     // UDP has no connection that can be closed
00136     // intentionally left empty
00137 }
00138 
00139 void UdpServerListenerBinding::onConnectionClosedByPeer(wns::service::tl::Connection*)
00140 {
00141     // UDP has no connection that can be closed
00142     // intentionally left empty
00143 }
00144 
00145 void UdpServerListenerBinding::onConnectionLost(wns::service::tl::Connection*)
00146 {
00147     // UDP has no connection that can be closed
00148     // intentionally left empty
00149 }
00150 
00151 std::string
00152 UdpServerListenerBinding::printAddress() const
00153 {
00154     std::ostringstream tmp;
00155     //tmp << ownAddress << ":" << listenPort;
00156     tmp << peerAddress;
00157     return tmp.str();
00158 }
00159 
00160 

Generated on Sat May 26 03:32:14 2012 for openWNS by  doxygen 1.5.5