File: packet_generator.cpp

package info (click to toggle)
libitpp 4.3.1-13
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (194 lines) | stat: -rw-r--r-- 5,710 bytes parent folder | download | duplicates (7)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*!
 * \file
 * \brief Implementation of a Packet generator class
 * \author Anders Persson and Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ 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 General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/protocol/packet_generator.h>


namespace itpp
{

Packet_Generator::Packet_Generator(const int Packet_size, const unsigned long int Max_packets)
{
  keep_running = false;
  start.forward(this, &Packet_Generator::handle_start);
  next.forward(this, &Packet_Generator::handle_next);
  output.connect(&next);
  set_parameters(Packet_size, Max_packets);
}

Packet_Generator::~Packet_Generator() { }

void Packet_Generator::set_parameters(const int Packet_size, const unsigned long int Max_packets)
{
  it_assert(Packet_size > 0, "Packet_Generator::set_parameters(): ");
  packet_size = Packet_size;
  max_packets = Max_packets;
  id = 0;
}

int Packet_Generator::get_packet_size()
{
  return packet_size;
}

int Packet_Generator::get_max_packets()
{
  return max_packets;
}

void Packet_Generator::handle_next(Packet*)
{
  if (keep_running) {
    output(new Packet(8*packet_size), delta_t());
    id++;
    if (max_packets && id >= max_packets)
      start(false);
  }
}

void Packet_Generator::handle_start(const bool run)
{
  if (run && !keep_running) {
    keep_running = run;
    handle_next(NULL);
  }
  keep_running = run;
}


// ---------------------------- Poisson_Packet_Generator -------------------------------------------------

Poisson_Packet_Generator::Poisson_Packet_Generator(const double Avg_bit_rate,
    const int Packet_size,
    const unsigned long int Max_packets): Packet_Generator(Packet_size, Max_packets)
{
  set_parameters(Avg_bit_rate, Packet_size, Max_packets);
}

Poisson_Packet_Generator::~Poisson_Packet_Generator() {}

void Poisson_Packet_Generator::set_parameters(const double Avg_bit_rate,
    const int Packet_size,
    const unsigned long int Max_packets)
{
  Packet_Generator::set_parameters(Packet_size, Max_packets);
  it_assert(Avg_bit_rate > 0.0, "Packet_Generator::set_parameters(): ");
  avg_bit_rate = Avg_bit_rate;
  avg_delta_t = 8.0 * get_packet_size() / avg_bit_rate;
  ee.setup(1.0);
}

double Poisson_Packet_Generator::get_avg_bit_rate()
{
  return avg_bit_rate;
}


Ttype Poisson_Packet_Generator::delta_t()
{
  return ee()*avg_delta_t;
}


// ---------------------------- Constant_Rate_Packet_Generator -------------------------------------------------

Constant_Rate_Packet_Generator::Constant_Rate_Packet_Generator(const double Avg_rate, const int Packet_size, const unsigned long int Max_packets): Poisson_Packet_Generator(Avg_rate, Packet_size, Max_packets) {}

Constant_Rate_Packet_Generator::~Constant_Rate_Packet_Generator() {}

Ttype Constant_Rate_Packet_Generator::delta_t()
{
  return avg_delta_t;
}


// ---------------------------- Burst_WWW_Packet_Generator -------------------------------------------------


Burst_WWW_Packet_Generator::Burst_WWW_Packet_Generator(const double Avg_bit_rate, const int Packet_size, const int Max_packets): Poisson_Packet_Generator(Avg_bit_rate, Packet_size, Max_packets)
{
  Navg = 50; // Average number of packets per burst [packets].
  Ti = 1.1960e-4; // Average inter-arrival time between packets in burst [s].
  Tr = Navg * Packet_size * 8.0 / Avg_bit_rate - Ti * (Navg - 1); // Average time between bursts.
  N = 0;
}

Burst_WWW_Packet_Generator::~Burst_WWW_Packet_Generator()
{

}

Ttype Burst_WWW_Packet_Generator::delta_t()
{
  if (N == 0) { // Start of a new burst.
    N = Navg;
    N--; // First packet is triggered at ...
    return ee()*Tr; // ... start time of next burst.
  }
  else { // Within a burst.
    N--; // One packet less in the burst ...
    return ee()*Ti; // ... arrival time for next packet within the burst.
  }
}


// ----------------------------Sink -------------------------------------------------

Sink::Sink(const unsigned long int Max_packets)
{
  it_assert(Max_packets > 0, "Sink::Sink(): ");
  max_packets = Max_packets;
  Ncp = 0;
  Nbytes = 0;
  packet_input.forward(this, &Sink::handle_packet_input);
  start_time = Event_Queue::now();
}

Sink::~Sink()
{
  std::cout << "Time = " << Event_Queue::now() << ", Sink : " << std::endl;
  std::cout << "Received " << Ncp << " packets in sequence." << std::endl;
  std::cout << "Receive average bit rate = " << Nbytes*8.0 / (Event_Queue::now() - start_time) << " [bits/second]." << std::endl;
}


void Sink::handle_packet_input(Packet *P)
{
  it_assert(P != NULL, "Sink::handle_packet_input(): ");
  Ncp++;
  Nbytes += (P->bit_size() / 8);
  delete P;
  if (Ncp >= max_packets) {
    std::cout << "Time = " << Event_Queue::now() << ", Sink : " << std::endl;
    std::cout << "Simulation stopped because : Ncp > max_packets" << std::endl;
    Event_Queue::stop();
  }
}


} // namespace itpp