~comnets/openwns-app/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 * This file is part of openWNS (open Wireless Network Simulator)
 * _____________________________________________________________________________
 *
 * Copyright (C) 2004-2007
 * Chair of Communication Networks (ComNets)
 * Kopernikusstr. 5, D-52074 Aachen, Germany
 * phone: ++49-241-80-27910,
 * fax: ++49-241-80-22242
 * email: info@openwns.org
 * www: http://www.openwns.org
 * _____________________________________________________________________________
 *
 * openWNS is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 2 as published by the
 * Free Software Foundation;
 *
 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

#include <APPLICATIONS/session/server/wimax/Video.hpp>

using namespace applications::session::server::wimax;

STATIC_FACTORY_REGISTER_WITH_CREATOR(applications::session::server::wimax::Video,
				     applications::session::Session,
				     "server.WiMAXVideo", wns::PyConfigViewCreator);


using namespace applications::session::server::wimax;

Video::Video(const wns::pyconfig::View& _pyco) :
  Session(_pyco),
  framePacketCounter(0),
  firstPacketNumber(true)
{
  wns::pyconfig::View pSDConfig(_pyco, "packetSize");
  std::string pSDName = pSDConfig.get<std::string>("__plugin__");
  packetSizeDistribution = wns::distribution::DistributionFactory::creator(pSDName)->create(pSDConfig);

  wns::pyconfig::View iATConfig(_pyco, "packetIat");
  std::string iATName = iATConfig.get<std::string>("__plugin__");
  packetIatDistribution = wns::distribution::DistributionFactory::creator(iATName)->create(iATConfig);

  settlingTime = _pyco.get<wns::simulator::Time>("settlingTime");

  videoFrameRate = _pyco.get<double>("frameRate");
  videoFrameIat = 1 / double(videoFrameRate);
  numberOfPacketsPerFrame = _pyco.get<double>("numberOfPacketsPerFrame");

  MESSAGE_BEGIN(NORMAL, logger, m, "APPL: Starting new session with: ");
  m << "videoFrameRate = " << videoFrameRate;
  m << ", videoFrameIat = " << videoFrameIat;
  m << ", numberOfPackets = " << numberOfPacketsPerFrame;
  MESSAGE_END();

  state = idle;

  /* only for probing */
  sessionType = wimaxvideo;

  packetFrom = "server.WiMAXVideo";
}

Video::~Video()
{
  if(packetSizeDistribution != NULL)
    delete packetSizeDistribution;
  packetSizeDistribution = NULL;

  if(packetIatDistribution != NULL)
    delete packetIatDistribution;
  packetIatDistribution = NULL;
}

void
Video::onData(const wns::osi::PDUPtr& _pdu)
{
  state = running;

  assureType(_pdu.getPtr(), applications::session::PDU*);

  receivedPacketNumber = static_cast<applications::session::PDU*>(_pdu.getPtr())->getPacketNumber();

  MESSAGE_SINGLE(NORMAL, logger, "APPL: receivedPacketNumber = " << receivedPacketNumber << ".");

  applications::session::Session::incomingProbesCalculation(_pdu);

  onTimeout(frametimeout);
}

void
Video::onTimeout(const Timeout& _t)
{
  if(_t == statetimeout)
    {
      if(framePacketCounter < numberOfPacketsPerFrame)
	{
	  framePacketCounter += 1;

	  packetSize = (*packetSizeDistribution)();
	  /* The distributed value is in byte, so it has to be converted to bit. */
	  packetSize *= 8.0;

	  MESSAGE_SINGLE(NORMAL, logger, "APPL: Sending video packet!");

	  applications::session::Session::iatProbesCalculation();

	  applications::session::PDU* applicationPDU = new applications::session::PDU(Bit(packetSize), pyco);
	  applicationPDU->setCreationTime(wns::simulator::getEventScheduler()->getTime());

	  if(firstPacketNumber == true)
	    {
	      packetNumber = 1;
	      applicationPDU->setPacketNumber(packetNumber, packetFrom);
	      MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");

	      firstPacketNumber = false;
	    }
	  else
	    {
	      ++packetNumber;
	      applicationPDU->setPacketNumber(packetNumber, packetFrom);
	      MESSAGE_SINGLE(NORMAL, logger, "APPL: PacketNumber = " << packetNumber << ".");
	    }

	  wns::osi::PDUPtr pdu(applicationPDU);
      applications::session::Session::outgoingProbesCalculation(pdu);

	  connection->sendData(pdu);

	  iat = (*packetIatDistribution)();
	  /* The distributed value is in ms, so it has to be converted to sec. */
	  iat /= 1000.0;

	  setTimeout(statetimeout, iat);
	}
      else
	{
	  framePacketCounter = 0;
	}
    }
  else if(_t == frametimeout)
    {
      /* Starting new frame! */
      setTimeout(frametimeout, videoFrameIat);
      onTimeout(statetimeout);
    }
  else if(_t == probetimeout)
    {
      applications::session::Session::onTimeout(_t);
    }
  else
    {
      assure(false, "APPL: Unknown timout type =" << _t);
    }
}