IT++ Logo
packet_generator.cpp
Go to the documentation of this file.
1 
30 
31 
32 namespace itpp
33 {
34 
35 Packet_Generator::Packet_Generator(const int Packet_size, const unsigned long int Max_packets)
36 {
37  keep_running = false;
38  start.forward(this, &Packet_Generator::handle_start);
39  next.forward(this, &Packet_Generator::handle_next);
40  output.connect(&next);
41  set_parameters(Packet_size, Max_packets);
42 }
43 
45 
46 void Packet_Generator::set_parameters(const int Packet_size, const unsigned long int Max_packets)
47 {
48  it_assert(Packet_size > 0, "Packet_Generator::set_parameters(): ");
49  packet_size = Packet_size;
50  max_packets = Max_packets;
51  id = 0;
52 }
53 
55 {
56  return packet_size;
57 }
58 
60 {
61  return max_packets;
62 }
63 
64 void Packet_Generator::handle_next(Packet*)
65 {
66  if (keep_running) {
67  output(new Packet(8*packet_size), delta_t());
68  id++;
69  if (max_packets && id >= max_packets)
70  start(false);
71  }
72 }
73 
74 void Packet_Generator::handle_start(const bool run)
75 {
76  if (run && !keep_running) {
77  keep_running = run;
78  handle_next(NULL);
79  }
80  keep_running = run;
81 }
82 
83 
84 // ---------------------------- Poisson_Packet_Generator -------------------------------------------------
85 
87  const int Packet_size,
88  const unsigned long int Max_packets): Packet_Generator(Packet_size, Max_packets)
89 {
90  set_parameters(Avg_bit_rate, Packet_size, Max_packets);
91 }
92 
94 
95 void Poisson_Packet_Generator::set_parameters(const double Avg_bit_rate,
96  const int Packet_size,
97  const unsigned long int Max_packets)
98 {
99  Packet_Generator::set_parameters(Packet_size, Max_packets);
100  it_assert(Avg_bit_rate > 0.0, "Packet_Generator::set_parameters(): ");
101  avg_bit_rate = Avg_bit_rate;
103  ee.setup(1.0);
104 }
105 
107 {
108  return avg_bit_rate;
109 }
110 
111 
113 {
114  return ee()*avg_delta_t;
115 }
116 
117 
118 // ---------------------------- Constant_Rate_Packet_Generator -------------------------------------------------
119 
120 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) {}
121 
123 
125 {
126  return avg_delta_t;
127 }
128 
129 
130 // ---------------------------- Burst_WWW_Packet_Generator -------------------------------------------------
131 
132 
133 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)
134 {
135  Navg = 50; // Average number of packets per burst [packets].
136  Ti = 1.1960e-4; // Average inter-arrival time between packets in burst [s].
137  Tr = Navg * Packet_size * 8.0 / Avg_bit_rate - Ti * (Navg - 1); // Average time between bursts.
138  N = 0;
139 }
140 
142 {
143 
144 }
145 
147 {
148  if (N == 0) { // Start of a new burst.
149  N = Navg;
150  N--; // First packet is triggered at ...
151  return ee()*Tr; // ... start time of next burst.
152  }
153  else { // Within a burst.
154  N--; // One packet less in the burst ...
155  return ee()*Ti; // ... arrival time for next packet within the burst.
156  }
157 }
158 
159 
160 // ----------------------------Sink -------------------------------------------------
161 
162 Sink::Sink(const unsigned long int Max_packets)
163 {
164  it_assert(Max_packets > 0, "Sink::Sink(): ");
165  max_packets = Max_packets;
166  Ncp = 0;
167  Nbytes = 0;
168  packet_input.forward(this, &Sink::handle_packet_input);
169  start_time = Event_Queue::now();
170 }
171 
173 {
174  std::cout << "Time = " << Event_Queue::now() << ", Sink : " << std::endl;
175  std::cout << "Received " << Ncp << " packets in sequence." << std::endl;
176  std::cout << "Receive average bit rate = " << Nbytes*8.0 / (Event_Queue::now() - start_time) << " [bits/second]." << std::endl;
177 }
178 
179 
180 void Sink::handle_packet_input(Packet *P)
181 {
182  it_assert(P != NULL, "Sink::handle_packet_input(): ");
183  Ncp++;
184  Nbytes += (P->bit_size() / 8);
185  delete P;
186  if (Ncp >= max_packets) {
187  std::cout << "Time = " << Event_Queue::now() << ", Sink : " << std::endl;
188  std::cout << "Simulation stopped because : Ncp > max_packets" << std::endl;
190  }
191 }
192 
193 
194 } // namespace itpp
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:24 for IT++ by Doxygen 1.8.2