![]() |
User Manual, Developers Guide and API Documentation |
![]() |
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 <CONSTANZE/Listener.hpp> 00029 #include <WNS/module/Base.hpp> 00030 #include <iomanip> // for setprecision 00031 00032 using namespace constanze; 00033 00034 Listener::Listener( const wns::pyconfig::View& config, wns::node::component::Interface* component ): 00035 pyco(config), 00036 log(pyco.get("logger")), 00037 domainName(pyco.get<std::string>("domainName")), 00038 windowSize(config.get<double>("probeWindow")), // time in seconds 00039 doMMPPestimation(config.get<bool>("doMMPPestimation")), 00040 sizeProbe(new wns::probe::bus::ContextCollector(wns::probe::bus::ContextProviderCollection(&component->getContextProviderCollection()), 00041 "traffic.endToEnd.packet.incoming.size")), 00042 bitsRateProbe(new wns::probe::bus::ContextCollector(wns::probe::bus::ContextProviderCollection(&component->getContextProviderCollection()), 00043 "traffic.endToEnd.window.incoming.bitThroughput")), 00044 packetRateProbe(new wns::probe::bus::ContextCollector(wns::probe::bus::ContextProviderCollection(&component->getContextProviderCollection()), 00045 "traffic.endToEnd.window.incoming.packetThroughput")), 00046 bitsWindowedRateProbe(), 00047 packetWindowedRateProbe(), 00048 delayProbe(new wns::probe::bus::ContextCollector(wns::probe::bus::ContextProviderCollection(&component->getContextProviderCollection()), 00049 "traffic.endToEnd.packet.incoming.delay")), 00050 bitsWindowedResult("traffic.windowedEndToEndIncomingBitThroughputTrace"), 00051 packetWindowedResult("traffic.windowedEndToEndIncomingPacketThroughputTrace"), 00052 packetCounter(0), 00053 bitCounter(0), 00054 windowedPacketCounter(0), 00055 windowedBitCounter(0), 00056 currentWindowEnd(windowSize) 00057 { 00058 // The names of the probes must match those in Probes.py (no automatism) 00059 wns::probe::bus::ContextProviderCollection localContext( 00060 &component->getContextProviderCollection()); 00061 localContext.addProvider(wns::probe::bus::contextprovider::Container(&this->bitsWindowedResult)); 00062 localContext.addProvider(wns::probe::bus::contextprovider::Container(&this->packetWindowedResult)); 00063 bitsWindowedRateProbe = wns::probe::bus::ContextCollectorPtr( 00064 new wns::probe::bus::ContextCollector( 00065 localContext, 00066 "traffic.windowedEndToEndIncomingBitThroughputTrace")); 00067 packetWindowedRateProbe = wns::probe::bus::ContextCollectorPtr( 00068 new wns::probe::bus::ContextCollector 00069 (localContext, 00070 "traffic.windowedEndToEndIncomingPacketThroughputTrace")); 00071 // if doMMPPestimation is true, measure data flow 00072 if (doMMPPestimation) { 00073 wns::pyconfig::View measurementConfig = config.get("measurement"); 00074 measurement = new Measurement(measurementConfig); 00076 observationVector = new std::vector<int>(); 00077 } else { 00078 measurement = NULL; 00079 } 00080 MESSAGE_SINGLE(NORMAL, log, "Listener created (domainName="<<domainName<<")."); 00081 } 00082 00083 Listener::~Listener() 00084 { 00085 MESSAGE_BEGIN(NORMAL, log, m, "Received a total of "); 00086 m << packetCounter << " packets with " << bitCounter << " bits"; 00087 m << " at my socket=" << domainName; 00088 MESSAGE_END(); 00089 00090 if (doMMPPestimation) { 00091 // apply baum-welch algorithm 00093 HMM *hmm = measurement->calculate( 00094 measurement->getEstimatorNumberOfStates(), 00095 observationVector); 00096 measurement->output_python( 00097 hmm->getTransitionsMatrix(), 00098 hmm->getNumberOfStates(), 00099 measurement->getMMPPestimationResultFileName()); 00100 // set memory for free 00101 delete hmm; 00103 delete observationVector; 00104 delete measurement; 00105 } 00106 } 00107 00108 void Listener::onData(const wns::osi::PDUPtr& _data) 00109 { 00110 assure(_data , "data is empty."); 00111 assureType(_data.getPtr(), ConstanzePDU*); 00112 int bits = _data->getLengthInBits(); 00113 00114 sizeProbe->put(bits); 00115 00116 const simTimeType now = wns::simulator::getEventScheduler()->getTime(); 00117 const simTimeType packetCreationTime = ConstanzePDU::castPDU2ConstanzePDU( 00118 _data.getPtr())->getTimeStamp(); 00119 bitCounter += bits; 00120 packetCounter ++; 00121 00122 // window mechanism: 00123 // now may be lagging several windowSizes after currentWindowEnd, 00124 // so roll-up each missing window one by one: 00125 while (now >= currentWindowEnd) { // need to shift window 00126 // write probes for old (outdated) window 00127 double bitRate = static_cast<double>(windowedBitCounter) / windowSize; 00128 00129 if (doMMPPestimation) { 00131 // measurement->update(bitRate); 00132 00133 bool notProperStateLevel = true; 00134 00135 stateLevel = 0; // state(Level) 00136 00137 for(unsigned int i = 0; notProperStateLevel == true && i < measurement->boundaryVector->size(); i++){ 00138 00139 if(bitRate <= measurement->boundaryVector->at(i)){ 00140 stateLevel = i; 00141 notProperStateLevel = false; 00142 } 00143 } 00144 if(notProperStateLevel) 00145 stateLevel = measurement->boundaryVector->size(); 00146 00147 00148 observationVector->push_back(stateLevel); 00149 } 00150 00151 double packetRate = static_cast<double>(windowedPacketCounter) / windowSize; 00152 00153 bitsRateProbe->put(bitRate); 00154 packetRateProbe->put(packetRate); 00155 double dummy = 0.0; 00156 { 00157 // Prepare string with result 00158 std::ostringstream o; 00159 o << std::fixed << std::setprecision( 6 ) << currentWindowEnd 00160 << "\t" << std::setprecision( 2 ) << bitRate; 00161 00162 // Instruct the context provider to provide the string next time it 00163 // is being asked 00164 bitsWindowedResult.set(o.str()); 00165 // Put a dummy value 00166 bitsWindowedRateProbe->put(dummy); 00167 } 00168 { 00169 // Prepare string with result 00170 std::ostringstream o; 00171 o << std::fixed << std::setprecision( 6 ) << currentWindowEnd 00172 << "\t" << std::setprecision( 2 ) << bitRate; 00173 00174 // Instruct the context provider to provide the string next time it 00175 // is being asked 00176 packetWindowedResult.set(o.str()); 00177 // Put a dummy value 00178 packetWindowedRateProbe->put(dummy); 00179 } 00180 // initialize next window: 00181 currentWindowEnd += windowSize; // may still be < now 00182 windowedBitCounter = 0; 00183 windowedPacketCounter = 0; 00184 } 00185 windowedBitCounter += bits; 00186 windowedPacketCounter ++; 00187 // delay/size probes 00188 double travelTime = now - packetCreationTime; 00189 assure(travelTime > 0.0, "packet with travel time <=0."); 00190 delayProbe->put(travelTime); 00191 } 00192 00193 void Listener::registerBinding(constanze::Binding* _binding) 00194 { 00195 binding = _binding; 00196 binding->registerListener(this); 00197 } 00198 00199 std::string Listener::printAddress() const 00200 { 00201 std::ostringstream tmp; 00202 tmp << domainName; 00203 return tmp.str(); 00204 }
1.5.5