User Manual, Developers Guide and API Documentation

VoIP.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/server/VoIP.hpp>
00029 
00030 using namespace applications::session::server;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR(applications::session::server::VoIP,
00033                      applications::session::Session,
00034                      "server.VoIP", wns::PyConfigViewCreator);
00035 
00036 VoIP::VoIP(const wns::pyconfig::View& _pyco) :
00037   Session(_pyco),
00038   stateTransitionDistribution(NULL),
00039   firstPacketNumber(true),
00040   cnCounter(0),
00041   trafficStartDelay(_pyco.get<wns::simulator::Time>("trafficStartDelay")),
00042   receivedFirst(false)
00043 {
00044   wns::pyconfig::View sTVConfig(_pyco, "stateTransition");
00045   std::string sTVName = sTVConfig.get<std::string>("__plugin__");
00046   stateTransitionDistribution = wns::distribution::DistributionFactory::creator(sTVName)->create(sTVConfig);
00047 
00048   settlingTime = _pyco.get<wns::simulator::Time>("settlingTime");
00049 
00050   iat = _pyco.get<wns::simulator::Time>("packetIat");
00051   voicePacketSize = _pyco.get<Bit>("voicePacketSize");
00052   /* Packet size includes 12 bytes RTP header. */
00053   voicePacketSize = voicePacketSize + 96;
00054 
00055   comfortNoisePacketSize = _pyco.get<Bit>("comfortNoisePacketSize");
00056   /* Packet size includes 12 bytes RTP header. */
00057   comfortNoisePacketSize = comfortNoisePacketSize + 96;
00058 
00059   comfortNoise = _pyco.get<bool>("comfortNoise");
00060 
00061   MESSAGE_BEGIN(NORMAL, logger, m, "APPL: New VoIP session started with: ");
00062   m << "voicePacketSize = " << voicePacketSize;
00063   m << ", iat = " << iat;
00064   m << ", comfortNoisePacketSize = " << comfortNoisePacketSize;
00065   MESSAGE_END();
00066 
00067   maxDelay = _pyco.get<wns::simulator::Time>("maxDelay");
00068   maxLossRatio = _pyco.get<double>("maxLossRatio");
00069 
00070   state = running;
00071 
00072   /* only for probing */
00073   sessionType = voip;
00074 
00075   packetFrom = "server.VoIP";
00076 }
00077 
00078 VoIP::~VoIP()
00079 {
00080   if (stateTransitionDistribution != NULL)
00081     delete stateTransitionDistribution;
00082   stateTransitionDistribution = NULL;
00083 }
00084 
00085 void
00086 VoIP::onData(const wns::osi::PDUPtr& _pdu)
00087 {
00088   assureType(_pdu.getPtr(), applications::session::PDU*);
00089 
00090   receivedPacketNumber = static_cast<applications::session::PDU*>(_pdu.getPtr())->getPacketNumber();
00091 
00092   MESSAGE_SINGLE(NORMAL, logger, "APPL: receivedPacketNumber = " << receivedPacketNumber << ".");
00093 
00094   applications::session::Session::incomingProbesCalculation(_pdu);
00095 
00096   if((!receivedFirst) && (state != sessionended))
00097     {
00098       receivedFirst = true;
00099       firstPacketDelay = packetDelay;
00100 
00101       MESSAGE_SINGLE(NORMAL, logger, "APPL: Traffic will start in " 
00102         << trafficStartDelay << "s.");
00103 
00104       setTimeout(sendtimeout, trafficStartDelay);
00105       voIPState = inactive;
00106       cnCounter = 1;
00107       setTimeout(statetransitiontimeout, 0.02 + trafficStartDelay);
00108     }
00109 }
00110 
00111 void
00112 VoIP::onTimeout(const Timeout& _t)
00113 {
00114   if(_t == sendtimeout)
00115     {
00116       MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending voicepacket!");
00117 
00118       applications::session::Session::iatProbesCalculation();
00119 
00120       packetSize = voicePacketSize;
00121 
00122       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(voicePacketSize), pyco);
00123       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00124 
00125       ++packetNumber;
00126       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00127       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00128 
00129       wns::osi::PDUPtr pdu(applicationPDU);
00130       applications::session::Session::outgoingProbesCalculation(pdu);
00131 
00132       connection->sendData(pdu);
00133     }
00134   else if(_t == receivetimeout)
00135     {
00136       /* Comfort noise packets will be send to fill the silence */
00137       MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending comfort noise packet!");
00138 
00139       applications::session::Session::iatProbesCalculation();
00140 
00141       packetSize = comfortNoisePacketSize;
00142 
00143       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(comfortNoisePacketSize), pyco);
00144       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00145 
00146       ++packetNumber;
00147       applicationPDU->setPacketNumber(packetNumber, packetFrom);
00148       MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00149 
00150       wns::osi::PDUPtr pdu(applicationPDU);
00151       applications::session::Session::outgoingProbesCalculation(pdu);
00152 
00153       connection->sendData(pdu);
00154     }
00155   else if(_t == statetransitiontimeout)
00156     {
00157       /* Transition between active (speech) state and inactive (silent/pause) state. */
00158       double stateTransitionValue = (*stateTransitionDistribution)();
00159 
00160       MESSAGE_SINGLE(NORMAL, logger, "APPL: State transition. TransitionValue = " << stateTransitionValue << ".");
00161 
00162       if(stateTransitionValue <= 0.01 && voIPState == inactive)
00163     {
00164       cnCounter = 0;
00165       onTimeout(sendtimeout);
00166 
00167       voIPState = active;
00168     }
00169       else if(stateTransitionValue > 0.01 && voIPState == inactive)
00170     {
00171       if(cnCounter == 8)
00172         {
00173           cnCounter = 1;
00174           onTimeout(receivetimeout);
00175         }
00176       else
00177         {
00178           cnCounter += 1;
00179         }
00180     }
00181       else if(stateTransitionValue <= 0.01 && voIPState == active)
00182     {
00183       cnCounter = 1;
00184       onTimeout(receivetimeout);
00185 
00186       voIPState = inactive;
00187     }
00188       else if(stateTransitionValue > 0.01 && voIPState == active)
00189     {
00190       onTimeout(sendtimeout);
00191     }
00192 
00193       setTimeout(statetransitiontimeout, 0.02);
00194     }
00195   else if(_t == probetimeout)
00196     {
00197       applications::session::Session::onTimeout(_t);
00198     }
00199   else
00200     {
00201       assure(false, "APPL: Unknown timout type =" << _t);
00202     }
00203 }

Generated on Sun May 27 03:32:16 2012 for openWNS by  doxygen 1.5.5