User Manual, Developers Guide and API Documentation

VideoTrace.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/VideoTrace.hpp>
00029 
00030 using namespace applications::session::server;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR(applications::session::server::VideoTrace,
00033                      applications::session::Session,
00034                      "server.VideoTrace", wns::PyConfigViewCreator);
00035 
00036 VideoTrace::VideoTrace(const wns::pyconfig::View& _pyco) :
00037   Session(_pyco),
00038   videoFrameRate(25),
00039   videoFile(NULL),
00040   movieChoice(""),
00041   firstPacket(true),
00042   firstPacketNumber(true)
00043 {
00044   iat = 1 / double(videoFrameRate);
00045 
00046   settlingTime = _pyco.get<wns::simulator::Time>("settlingTime");
00047 
00048   /* only for probing */
00049   sessionType = videotrace;
00050 
00051   packetFrom = "server.VideoTrace";
00052 }
00053 
00054 
00055 VideoTrace::~VideoTrace()
00056 {
00057 }
00058 
00059 
00060 void
00061 VideoTrace::onData(const wns::osi::PDUPtr& _pdu)
00062 {
00063   state = running;
00064 
00065   assureType(_pdu.getPtr(), applications::session::PDU*);
00066 
00067   receivedPacketNumber = static_cast<applications::session::PDU*>(_pdu.getPtr())->getPacketNumber();
00068 
00069   MESSAGE_SINGLE(NORMAL, logger, "APPL: receivedPacketNumber = " << receivedPacketNumber << ".");
00070 
00071   applications::session::Session::incomingProbesCalculation(_pdu);
00072 
00073   if(receivedPacketNumber == 1)
00074     {
00075       firstPacketDelay = packetDelay;
00076 
00077       movieChoice = static_cast<applications::session::PDU*>(_pdu.getPtr())->getMovieChoice();
00078     }
00079   onTimeout(statetimeout);
00080 }
00081 
00082 
00083 void
00084 VideoTrace::onTimeout(const Timeout& _t)
00085 {
00086   if(_t == statetimeout)
00087     {
00088       /* Sending requested videofile. */
00089 
00090       if(firstPacket == true)
00091     {
00092       firstPacket = false;
00093 
00094       MESSAGE_SINGLE(NORMAL, logger, "APPL: MovieChoice = " << movieChoice << ".");
00095 
00096       videoFile = new VideoFile(movieChoice);
00097     }
00098 
00099       lastPacket = videoFile->VideoFile::lastPacket();
00100 
00101       if(lastPacket == false)
00102     {
00103       applications::session::Session::iatProbesCalculation();
00104 
00105       packetSize = videoFile->VideoFile::getNextPacket();
00106 
00107       applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
00108       applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());
00109 
00110       if(firstPacketNumber == true)
00111         {
00112           packetNumber = 1;
00113           applicationPDU->setPacketNumber(packetNumber, packetFrom);
00114           MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00115 
00116           firstPacketNumber = false;
00117         }
00118       else
00119         {
00120           ++packetNumber;
00121           applicationPDU->setPacketNumber(packetNumber, packetFrom);
00122           MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
00123         }
00124 
00125       wns::osi::PDUPtr pdu(applicationPDU);
00126       applications::session::Session::outgoingProbesCalculation(pdu);
00127 
00128       connection->sendData(pdu);
00129 
00130       setTimeout(statetimeout, iat);
00131     }
00132       else
00133     {
00134       /* Last packet sent, now wait till session ends. */
00135       MESSAGE_SINGLE(NORMAL, logger, "APPL: Last packet of tracefile sent. Wait till session ends. ");
00136     }
00137     }
00138   else if(_t == probetimeout)
00139     {
00140       applications::session::Session::onTimeout(_t);
00141     }
00142   else
00143     {
00144       assure(false, "APPL: Unknown timout type =" << _t);
00145     }
00146 }
00147 
00148 
00149 
00150 VideoFile::VideoFile(const char* _movie) :
00151   lastIndex(false)
00152 {
00153   file.open(_movie, std::ios::out);
00154 
00155   assure(file != NULL, "APPL: Video file was not opened. Please check if it is available!");
00156 }
00157 
00158 
00159 VideoFile::~VideoFile()
00160 {
00161 }
00162 
00163 
00164 int
00165 VideoFile::getNextPacket()
00166 {
00167   int packetSize = 1;
00168   char line[100];
00169 
00170   if(!file.eof())
00171     {
00172       file.getline(line, 100);
00173       if (isdigit(line[0]))
00174     {
00175       sscanf(line, "%d %*f", &packetSize);
00176     }
00177       else
00178     {
00179       lastIndex = true;
00180       file.close();
00181     }
00182     }
00183   else
00184     {
00185       lastIndex = true;
00186       file.close();
00187     }
00188   return packetSize;
00189 }
00190 
00191 
00192 bool
00193 VideoFile::lastPacket()
00194 {
00195   return lastIndex;
00196 }
00197 
00198 
00199 int
00200 VideoFile::getNumberOfFrames()
00201 {
00202   int count = 0;
00203   int p;
00204   char line[100];
00205 
00206   while(!file.eof())
00207     {
00208       file.getline(line, 100);
00209       if(isdigit(line[0]))
00210     {
00211       sscanf(line, "%d %*f", &p);
00212       count = count + 1;
00213     }
00214     }
00215   return count;
00216 }

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