User Manual, Developers Guide and API Documentation

Session.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 <APPLICATIONS/session/Session.hpp>
00029 #include <boost/tuple/tuple.hpp>
00030 
00031 using namespace applications::session;
00032 
00033 Session::Session(const wns::pyconfig::View& _pyco) :
00034   wns::events::MultipleTimeout<Timeout>(),
00035   wns::IOutputStreamable(),
00036   lastPacket(false),
00037   pyco(_pyco),
00038   logger(_pyco.get("logger")),
00039   state(idle),
00040   component(NULL),
00041   connection(NULL),
00042   listener(NULL),
00043   sessionIndex(0),
00044   incomingPacketSizeCounter(0),
00045   incomingPacketCounter(0),
00046   incomingPacketSize(0),
00047   outgoingPacketSizeCounter(0),
00048   outgoingPacketCounter(0),
00049   outgoingPacketSize(0),
00050   packetDelay(0.0),
00051   maxDelay(10.0),
00052   iatStart(0.0),
00053   iatEnd(0.0),
00054   settlingTime(0.0),
00055   probeEndTime(_pyco.get<wns::simulator::Time>("probeEndTime")),
00056   firstIatProbe(true),
00057   measuringDuration(0.0),
00058   windowedIncomingBitThroughput(0.0),
00059   windowedIncomingPacketThroughput(0),
00060   windowedOutgoingBitThroughput(0.0),
00061   windowedOutgoingPacketThroughput(0),
00062   windowSize(1.0),
00063   /* WARNING: sessionStartTime should not be greater than settlingTime */
00064   sessionStartTime(wns::simulator::getEventScheduler()->getTime()),
00065   bitRate(0),
00066   packetSize(0),
00067   iat(0.0),
00068   now(0.0),
00069   packetNumber(0),
00070   lastPacketNumber(0),
00071   packetLossCounter(0),
00072   packetsDuringSettlingTime(0),
00073   receivedPackets(0),
00074   receivedOnTimePackets(0),
00075   packetLossRatio(0.0),
00076   maxLossRatio(1.0),
00077   receivedPacketNumber(0),
00078   packetFrom("Session"),
00079   senderId(-1),
00080   applId(-1)
00081 {
00082 }
00083 
00084 Session::~Session()
00085 {
00086   listener = NULL;
00087   component = NULL;
00088   connection = NULL;
00089 }
00090 
00091 void
00092 Session::onTimeout(const Timeout& _t)
00093 {
00094   now = wns::simulator::getEventScheduler()->getTime();
00095 
00096   if(now > settlingTime)
00097     {
00098       windowIncomingBitThroughputProbe->put(windowedIncomingBitThroughput, 
00099             boost::make_tuple("Appl.SenderId", senderId,
00100                 "Appl.CellId", getCellId(senderId)));
00101       windowIncomingPacketThroughputProbe->put(windowedIncomingPacketThroughput, 
00102             boost::make_tuple("Appl.SenderId", senderId,
00103                 "Appl.CellId", getCellId(senderId)));
00104 
00105       windowOutgoingBitThroughputProbe->put(windowedOutgoingBitThroughput, 
00106             boost::make_tuple("Appl.SenderId", senderId,
00107                 "Appl.CellId", getCellId(senderId)));
00108       windowOutgoingPacketThroughputProbe->put(windowedOutgoingPacketThroughput, 
00109             boost::make_tuple("Appl.SenderId", senderId,
00110                 "Appl.CellId", getCellId(senderId)));
00111 
00112       windowedIncomingBitThroughput = 0.0;
00113       windowedIncomingPacketThroughput = 0;
00114 
00115       windowedOutgoingBitThroughput = 0.0;
00116       windowedOutgoingPacketThroughput = 0;
00117 
00118       setTimeout(probetimeout, windowSize);
00119     }
00120   else
00121     {
00122       MESSAGE_SINGLE(NORMAL, logger, "APPL: Not probing during settling time!");
00123     }
00124 }
00125 
00126 
00127 void
00128 Session::registerBinding(const applications::session::BindingPtr& _binding)
00129 {
00130   binding = _binding;
00131 }
00132 
00133 void
00134 Session::registerListener(applications::session::Binding* _listener)
00135 {
00136   listener = _listener;
00137 }
00138 
00139 void
00140 Session::registerComponent(applications::node::component::Component* _component,
00141                int _sessionIndex)
00142 {
00143   component = _component;
00144   sessionIndex = _sessionIndex;
00145 
00146   //TEST
00147   intWert = component->getNode()->getNodeID();
00148   strings << intWert;
00149   packetFromID = strings.str();
00150 
00151   wns::probe::bus::ContextProviderCollection localContext(&component->getContextProviderCollection());
00152 
00153   MESSAGE_BEGIN(NORMAL, logger, m, "APPL: Setting probe context, ");
00154   m << "Appl.Id=" << component->getNode()->getNodeID();
00155   m << ", Appl.StationType=" << this->stationType;
00156   m << ", Appl.SessionType=" << this->sessionType;
00157   MESSAGE_END();
00158 
00159   applId = component->getNode()->getNodeID();
00160   localContext.addProvider(wns::probe::bus::contextprovider::Constant("Appl.Id", applId));
00161   localContext.addProvider(wns::probe::bus::contextprovider::Constant("Appl.StationType", this->stationType));
00162   localContext.addProvider(wns::probe::bus::contextprovider::Constant("Appl.SessionType", this->sessionType));
00163 
00164   /* The names of the probes must match those in default.py (no automatism) */
00165   incomingPacketSizeProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.packet.incoming.size"));
00166 
00167   outgoingPacketSizeProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.packet.outgoing.size"));
00168 
00169   packetDelayProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.packet.incoming.delay"));
00170 
00171   windowIncomingBitThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.window.incoming.bitThroughput"));
00172 
00173   windowIncomingPacketThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.window.incoming.packetThroughput"));
00174 
00175   windowOutgoingBitThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.window.outgoing.bitThroughput"));
00176 
00177   windowOutgoingPacketThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.window.outgoing.packetThroughput"));
00178 
00179   sessionIncomingBitThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.incoming.bitThroughput"));
00180 
00181   sessionIncomingPacketThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.incoming.packetThroughput"));
00182 
00183   sessionOutgoingBitThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.outgoing.bitThroughput"));
00184 
00185   sessionOutgoingPacketThroughputProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.outgoing.packetThroughput"));
00186 
00187   measuringDurationProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.duration"));
00188 
00189   iatProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.packet.outgoing.iat"));
00190 
00191   packetLossProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.incoming.packetLoss"));
00192 
00193   userSatisfactionProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.session.userSatisfaction"));
00194 
00195   connectionProbe = wns::probe::bus::ContextCollectorPtr(new wns::probe::bus::ContextCollector(localContext, "applications.connectionEstablished"));
00196 }
00197 
00198 
00199 void
00200 Session::onConnectionEstablished(wns::service::tl::Connection* _connection)
00201 {
00202   connection = _connection;
00203 }
00204 
00205 void
00206 Session::incomingProbesCalculation(const wns::osi::PDUPtr& _pdu)
00207 {
00208 
00209   now = wns::simulator::getEventScheduler()->getTime();
00210 
00211   if(now > settlingTime)
00212     {
00213 
00214       assureType(_pdu.getPtr(), applications::session::PDU*);
00215 
00216       incomingPacketCounter++;
00217 
00218       incomingPacketSize = _pdu.getPtr()->getLengthInBits();
00219       incomingPacketSizeCounter += incomingPacketSize;
00220 
00221       windowedIncomingPacketThroughput++;
00222       windowedIncomingBitThroughput += incomingPacketSize;
00223 
00224       assure(now >= static_cast<applications::session::PDU*>(_pdu.getPtr())->getCreationTime(), "Living in the past!");
00225 
00226       packetDelay = now - static_cast<applications::session::PDU*>(_pdu.getPtr())->getCreationTime();
00227 
00228       applications::session::Session* sender = static_cast<applications::session::PDU*>(_pdu.getPtr())->getSender();
00229       if(sender != NULL)
00230       {
00231         sender->onPDUReceivedByPeer(_pdu);
00232         assure(senderId == -1 || senderId == sender->getId(), "Receiving from different peers.");
00233         senderId = sender->getId();
00234       }
00235 
00236       packetDelayProbe->put(packetDelay, boost::make_tuple("Appl.SenderId", senderId,
00237                 "Appl.CellId", getCellId(senderId)));
00238       incomingPacketSizeProbe->put(incomingPacketSize, boost::make_tuple("Appl.SenderId", senderId,
00239                 "Appl.CellId", getCellId(senderId)));
00240 
00241       MESSAGE_SINGLE(NORMAL, logger, "APPL: PDU received with a size of " << _pdu.getPtr()->getLengthInBits()
00242              << " bits and a delay of " << packetDelay << ". Now recieved a total of "
00243              << incomingPacketCounter << " packets and " << incomingPacketSizeCounter << " bits.");
00244 
00245       /* packetLoss */
00246       if(receivedPacketNumber > (lastPacketNumber + 1))
00247     {
00248       long int counter = receivedPacketNumber - (lastPacketNumber + 1);
00249 
00250       packetLossCounter += counter;
00251 
00252       MESSAGE_SINGLE(NORMAL, logger, "APPL: " << counter << " packets lost. Now lost a total of "
00253              << packetLossCounter << " packets out of " << receivedPacketNumber << ".\n");
00254 
00255       lastPacketNumber = receivedPacketNumber;
00256     }
00257       else
00258     {
00259       lastPacketNumber = receivedPacketNumber;
00260     }
00261 
00262     }
00263   else
00264     {
00265       packetsDuringSettlingTime++;
00266       MESSAGE_SINGLE(NORMAL, logger, "APPL: Not probing during settling time!" << packetsDuringSettlingTime
00267              << " packets received during settling time.");
00268       lastPacketNumber = receivedPacketNumber;
00269     }
00270 }
00271 
00272 void
00273 Session::onPDUReceivedByPeer(const wns::osi::PDUPtr& _pdu)
00274 {
00275   now = wns::simulator::getEventScheduler()->getTime();
00276   wns::simulator::Time txTime;
00277   txTime = static_cast<applications::session::PDU*>(_pdu.getPtr())->getCreationTime();
00278 
00279   if(txTime <= probeEndTime && txTime > settlingTime)
00280   {
00281     wns::simulator::Time delay = now - txTime;
00282 
00283     receivedPackets++;
00284     if(delay < maxDelay)
00285     {
00286        receivedOnTimePackets++;
00287     }
00288   }   
00289 }
00290 
00291 int
00292 Session::getId()
00293 {
00294   return applId;
00295 }
00296 
00297 void
00298 Session::outgoingProbesCalculation(const wns::osi::PDUPtr& pdu)
00299 {
00300   now = wns::simulator::getEventScheduler()->getTime();
00301 
00302   static_cast<applications::session::PDU*>(pdu.getPtr())->setSender(this);
00303 
00304   if(now > settlingTime && now < probeEndTime)
00305     {
00306       outgoingPacketCounter++;
00307 
00308       outgoingPacketSize = pdu->getLengthInBits();
00309       outgoingPacketSizeCounter += outgoingPacketSize;
00310 
00311       windowedOutgoingPacketThroughput++;
00312       windowedOutgoingBitThroughput += outgoingPacketSize;
00313 
00314       outgoingPacketSizeProbe->put(outgoingPacketSize, 
00315         boost::make_tuple("Appl.SenderId", senderId,
00316                 "Appl.CellId", getCellId(senderId)));
00317     }
00318   else
00319     {
00320       MESSAGE_SINGLE(NORMAL, logger, "APPL: Not probing during settling time!");
00321     }
00322 }
00323 
00324 void
00325 Session::sessionProbesCalculation()
00326 {
00327   measuringDuration = wns::simulator::getEventScheduler()->getTime() - settlingTime;
00328   MESSAGE_SINGLE(NORMAL, logger, "APPL: SessionDuration = " << measuringDuration << ".");
00329   measuringDurationProbe->put(measuringDuration, boost::make_tuple("Appl.SenderId", senderId,
00330     "Appl.CellId", getCellId(senderId)));
00331 
00332   sessionIncomingBitThroughputProbe->put(incomingPacketSizeCounter / measuringDuration, 
00333     boost::make_tuple("Appl.SenderId", senderId,
00334                 "Appl.CellId", getCellId(senderId)));
00335   sessionIncomingPacketThroughputProbe->put(incomingPacketCounter / measuringDuration, 
00336     boost::make_tuple("Appl.SenderId", senderId,
00337                 "Appl.CellId", getCellId(senderId)));
00338 
00339   sessionOutgoingBitThroughputProbe->put(outgoingPacketSizeCounter / measuringDuration, 
00340     boost::make_tuple("Appl.SenderId", senderId,
00341                 "Appl.CellId", getCellId(senderId)));
00342   sessionOutgoingPacketThroughputProbe->put(outgoingPacketCounter / measuringDuration, 
00343     boost::make_tuple("Appl.SenderId", senderId,
00344                 "Appl.CellId", getCellId(senderId)));
00345 
00346   long int lost = outgoingPacketCounter - receivedOnTimePackets;
00347 
00348   if(outgoingPacketCounter > 0 && double(lost) / double(outgoingPacketCounter) < maxLossRatio)
00349     userSatisfactionProbe->put(1, boost::make_tuple("Appl.SenderId", senderId, 
00350         "Appl.CellId", getCellId(senderId)));
00351   else
00352   {
00353     userSatisfactionProbe->put(0, boost::make_tuple("Appl.SenderId", senderId, 
00354         "Appl.CellId", getCellId(senderId)));
00355   }
00356 
00357   if(receivedPacketNumber > 0)
00358   {
00359     MESSAGE_SINGLE(NORMAL, logger, "APPL: At the end a total of " << packetLossCounter
00360         << " packets out of " << (receivedPacketNumber-packetsDuringSettlingTime) << " are lost.\n");
00361 
00362     if(outgoingPacketCounter > 0)
00363     {
00364         packetLossProbe->put(double(lost) / double(outgoingPacketCounter), 
00365             boost::make_tuple("Appl.SenderId", senderId,
00366                 "Appl.CellId", getCellId(senderId)));
00367     }
00368     else
00369     {
00370         packetLossProbe->put(1.0, 
00371             boost::make_tuple("Appl.SenderId", senderId,
00372                 "Appl.CellId", getCellId(senderId)));
00373     }
00374   }
00375 }
00376 
00377 void
00378 Session::iatProbesCalculation()
00379 {
00380   now = wns::simulator::getEventScheduler()->getTime();
00381 
00382   if(now > settlingTime)
00383     {
00384       if(firstIatProbe == false)
00385     {
00386       iatEnd = wns::simulator::getEventScheduler()->getTime() - iatStart;
00387 
00388       MESSAGE_SINGLE(NORMAL, logger, "APPL: iat = " << iatEnd << ".");
00389 
00390       iatProbe->put(iatEnd, 
00391             boost::make_tuple("Appl.SenderId", senderId,
00392                 "Appl.CellId", getCellId(senderId)));
00393 
00394       iatStart = wns::simulator::getEventScheduler()->getTime();
00395     }
00396       else
00397     {
00398       iatStart = wns::simulator::getEventScheduler()->getTime();
00399 
00400       firstIatProbe = false;
00401     }
00402     }
00403   else
00404     {
00405       MESSAGE_SINGLE(NORMAL, logger, "APPL: Not probing during settling time!");
00406     }
00407 }
00408 
00409 
00410 void
00411 Session::onShutdown()
00412 {
00413 }
00414 
00415 
00416 std::string
00417 Session::doToString() const
00418 {
00419     std::stringstream ss;
00420     ss << "session = " << this->sessionType << std::endl;
00421     return ss.str();
00422 }
00423 
00424 int
00425 Session::getCellId(int senderId)
00426 {
00427     if(wns::simulator::getRegistry()->knows("DLLAssoc"))
00428     {
00429         std::map<int, int> assoc;
00430         assoc = wns::simulator::getRegistry()->find<std::map<int, int> >("DLLAssoc");
00431         if(assoc.find(senderId) != assoc.end())
00432         {
00433             return assoc[senderId];
00434         }
00435         else
00436         if(assoc.find(applId) != assoc.end())
00437         {
00438             return assoc[applId];
00439         }
00440     }
00441     return -1;
00442 }
00443 

Generated on Fri May 25 03:32:17 2012 for openWNS by  doxygen 1.5.5