![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /****************************************************************************** 00002 * TCP 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/TcpServerListenerBinding.hpp> 00013 #include <CONSTANZE/TcpServerBinding.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::TcpServerListenerBinding, 00024 constanze::Binding, 00025 "TcpServerListenerBinding", 00026 wns::PyConfigViewCreator); 00027 00028 TcpServerListenerBinding::TcpServerListenerBinding(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 { 00035 c = GeneratorBase::Factory::creator(generatorConfig.get<std::string>("__plugin__")); 00036 00037 // who sets ownAddress??? [rs] 00038 MESSAGE_BEGIN(NORMAL, log, m, "New TcpServerListenerBinding created on my socket="); 00039 m << ownAddress << ":" << listenPort; 00040 MESSAGE_END(); 00041 } 00042 00043 TcpServerListenerBinding::~TcpServerListenerBinding() 00044 { 00045 for(std::list<GeneratorBase*>::const_iterator iter = generatorList.begin(); iter != generatorList.end(); ++iter) { 00046 delete (*iter); 00047 } 00048 } 00049 00050 void TcpServerListenerBinding::sendData(const wns::osi::PDUPtr&) 00051 { 00052 // use registry<Generator, Connection> to send the data 00053 } 00054 00055 void TcpServerListenerBinding::registerComponent(wns::node::component::Component* _component) 00056 { 00057 component = _component; 00058 tcpService = 00059 component->getService<wns::service::tl::Service*>( 00060 pyco.get<std::string>("tcpService")); 00061 tcpService->listenOnPort(listenPort, this); 00062 dns = component->getService<wns::service::nl::DNSService*>(pyco.get<std::string>("dnsService")); 00063 } 00064 00065 void 00066 TcpServerListenerBinding::onConnectionEstablished(wns::service::nl::Address _address, wns::service::tl::Connection* _connection) 00067 { 00068 // _address is not my address, but the one of the peer node; 00069 // my address can be found in connection's flow ID. 00070 // Create a Generator on top of this Binding 00071 // Of course handling of all connection pointers is necessary. 00072 // A registry with mapping of Generator to Connection# 00073 // Generator has to get its parameters either from here or from python 00074 peerAddress = _address; 00075 _connection->registerDataHandler(this); 00076 00077 //generate the traffic generator: 00078 GeneratorBase* generator = c->create(generatorConfig); 00079 00080 MESSAGE_BEGIN(NORMAL, log, m, ""); 00081 m << "onConnectionEstablished() called from peer "<<peerAddress; 00082 MESSAGE_END(); 00083 00084 // save (generator, connection): 00085 //generatorToConnectionRegistry.insert(generator, _connection); 00086 00087 // generate the TcpServerBinding for the generator: 00088 TcpServerBinding* serverBinding = new constanze::TcpServerBinding(_address, _connection, log); 00089 00090 assure(dns!=NULL,"dns needed"); 00091 //ownAddress = serverBinding->???(); 00092 //ownAddress = listener->printAddress(); 00093 ownAddress = dns->lookup(listener->printAddress()); 00094 00095 MESSAGE_BEGIN(NORMAL, log, m, ""); 00096 m << "onConnectionEstablished(): me="<<ownAddress<<":"<<listenPort; 00097 MESSAGE_END(); 00098 00099 // Tell the generator which binding to use: 00100 generator->registerBinding(serverBinding); 00101 generator->bindingReady(); 00102 // keep a list of all generated generators 00103 generatorList.push_back(generator); 00104 } 00105 00106 void TcpServerListenerBinding::onData(const wns::osi::PDUPtr& _data) 00107 { 00108 assure(listener, "No listener set."); 00109 MESSAGE_BEGIN(NORMAL, log, m, "Received " << _data->getLengthInBits()/8 << " bytes"); // "from ?" 00110 m << " on my socket=" << ownAddress << ":" << listenPort; 00111 MESSAGE_END(); 00112 listener->onData(_data); 00113 } 00114 00115 void TcpServerListenerBinding::registerListener(constanze::Listener* _listener) 00116 { 00117 listener = _listener; 00118 } 00119 00120 void TcpServerListenerBinding::initBinding(constanze::StartTrigger* ) 00121 { 00122 // intentionally left empty 00123 } 00124 00125 void TcpServerListenerBinding::releaseBinding(constanze::StopTrigger*) 00126 { 00128 } 00129 00130 void TcpServerListenerBinding::onConnectionClosed(wns::service::tl::Connection*) 00131 { 00132 // TCP has no connection that can be closed 00133 // intentionally left empty 00134 } 00135 00136 void TcpServerListenerBinding::onConnectionClosedByPeer(wns::service::tl::Connection*) 00137 { 00138 // TCP has no connection that can be closed 00139 // intentionally left empty 00140 } 00141 00142 void TcpServerListenerBinding::onConnectionLost(wns::service::tl::Connection*) 00143 { 00144 // TCP has no connection that can be closed 00145 // intentionally left empty 00146 } 00147 00148 std::string 00149 TcpServerListenerBinding::printAddress() const 00150 { 00151 std::ostringstream tmp; 00152 //tmp << ownAddress << ":" << listenPort; 00153 tmp << peerAddress; 00154 return tmp.str(); 00155 } 00156 00157
1.5.5