IT++ Logo
selective_repeat.cpp
Go to the documentation of this file.
1 
30 #include <cstdlib>
31 
33 
34 namespace itpp
35 {
36 
37 bool in_sequence(const int a, const int b, const int L)
38 {
39  it_assert(a >= 0 && a < L, "in_sequence(): ");
40  it_assert(b >= 0 && b < L, "in_sequence(): ");
41  return ((b - a + L) % L) < L / 2;
42 }
43 
45 {
46  parameters_ok = false;
47  packet_input.set_name("Selective_Repeat_ARQ_Sender packet_input Slot");
48  packet_input.forward(this, &Selective_Repeat_ARQ_Sender::handle_packet_input);
49  ack_input.set_name("Selective_Repeat_ARQ_Sender ack_input Slot");
50  ack_input.forward(this, &Selective_Repeat_ARQ_Sender::handle_ack_input);
51  query_nof_ready_packets.set_name("Selective_Repeat_ARQ_Sender query_nof_ready_packets Slot");
52  query_nof_ready_packets.forward(this, &Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets);
53  packet_output_request.set_name("Selective_Repeat_ARQ_Sender packet_output_request Slot");
54  packet_output_request.forward(this, &Selective_Repeat_ARQ_Sender::handle_packet_output_request);
55 
56 }
57 
58 Selective_Repeat_ARQ_Sender::Selective_Repeat_ARQ_Sender(const int Seq_no_size, const int Buffer_size_factor, const int Link_packet_size, const Ttype Time_out)
59 {
60  set_parameters(Seq_no_size, Buffer_size_factor, Link_packet_size, Time_out);
61  packet_input.set_name("Selective_Repeat_ARQ_Sender packet_input Slot");
62  packet_input.forward(this, &Selective_Repeat_ARQ_Sender::handle_packet_input);
63  ack_input.set_name("Selective_Repeat_ARQ_Sender ack_input Slot");
64  ack_input.forward(this, &Selective_Repeat_ARQ_Sender::handle_ack_input);
65  query_nof_ready_packets.set_name("Selective_Repeat_ARQ_Sender query_nof_ready_packets Slot");
66  query_nof_ready_packets.forward(this, &Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets);
67  packet_output_request.set_name("Selective_Repeat_ARQ_Sender packet_output_request Slot");
68  packet_output_request.forward(this, &Selective_Repeat_ARQ_Sender::handle_packet_output_request);
69 }
70 
72 {
73  std::cout << "no_retransmit = " << no_retransmit << std::endl;
74 }
75 
76 void Selective_Repeat_ARQ_Sender::set_parameters(const int Seq_no_size,
77  const int Buffer_size_factor,
78  const int Link_packet_size,
79  const Ttype Time_out)
80 {
81  it_assert((0 < Seq_no_size) && (Seq_no_size <= 30),
82  "Selective_Repeat_ARQ_Sender::set_parameters(): ");
83  it_assert((0 < Buffer_size_factor) && (Buffer_size_factor <= 10),
84  "Selective_Repeat_ARQ_Sender::set_parameters(): ");
85  it_assert(Link_packet_size > 0, "Selective_Repeat_ARQ_Sender::set_parameters(): ");
86  it_assert(Time_out > 0, "Selective_Repeat_ARQ_Sender::set_parameters(): ");
87  seq_no_size = Seq_no_size;
88  link_packet_size = Link_packet_size;
89  seq_no_max = 1 << Seq_no_size;
90  input_buffer_size = seq_no_max * Buffer_size_factor;
91  input_buffer.set_size(input_buffer_size);
92  for (int l = 0; l < input_buffer_size; input_buffer(l++) = NULL);
93  input_free_space = input_buffer_size;
94  input_next = 0;
95  tx_next = 0;
96  tx_last = 0;
97  time_out = Time_out;
98  timer.set_size(seq_no_max);
99  for (int l = 0; l < seq_no_max; timer(l++).forward(this, &Selective_Repeat_ARQ_Sender::retransmit));
100  outstanding = 0;
101  seq_no = 0;
102  output_indexes.set_size(seq_no_max);
103  output_indexes.ones();
104  output_indexes *= -1;
105  retransmission_indexes.set_size(seq_no_max);
106  retransmission_indexes.ones();
107  retransmission_indexes *= -1;
108  rd_pos = 0;
109  rt_pos = 0;
110  scheduled_total = 0;
111  scheduled_retransmissions = 0;
112  no_retransmit = 0;
113  parameters_ok = true;
114  ip_pkt_queue.set_max_byte_size(1500*32);
115  id = 0;
116 }
117 
118 void Selective_Repeat_ARQ_Sender::handle_ack_input(Array<Packet*> packet_array)
119 {
120  Packet *packet = packet_array(0);
121  ACK *A = (ACK *) packet;
122 
123  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::handle_ack_input(): ");
124  it_assert(A, "Selective_Repeat_ARQ_Sender::handle_ack_input(): ");
125  it_assert(A->seq_no >= 0 && A->seq_no < seq_no_max, "Selective_Repeat_ARQ_Sender::handle_ack_input(): ");
126  if (outstanding) {
127  if (in_sequence(tx_last % seq_no_max, A->seq_no, seq_no_max))
128  remove(A->seq_no);
129  while (!input_buffer(tx_last) && outstanding) {
130  outstanding--;
131  input_free_space++;
132  tx_last = (tx_last + 1) % input_buffer_size;
133  }
134  }
135  delete A;
136  fill_output();
137 }
138 
139 void Selective_Repeat_ARQ_Sender::handle_packet_input(Packet *packet)
140 {
141  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::handle_packet_input(): ");
142  it_assert(packet, "Selective_Repeat_ARQ_Sender::handle_packet_input(): ");
143  ip_pkt_queue.push(packet);
144 
145 }
146 
147 // The number of blocks in the ip_pkt_queue that can be scheduled to be
148 // transmitted (in the tx buffer)
149 int Selective_Repeat_ARQ_Sender::feasable_blocks()
150 {
151  div_t q = div(ip_pkt_queue.byte_size(), link_packet_size);
152  int blocks_in_ip_queue = (q.rem) ? q.quot + 1 : q.quot;
153  return std::min(free_sequence_numbers(),
154  buffered_non_outstanding() +
155  std::min(blocks_in_ip_queue, input_free_space));
156 }
157 
158 
159 void Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets(void*)
160 {
161  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets(): ");
162  nof_ready_packets(scheduled_total + feasable_blocks());
163 }
164 
165 void Selective_Repeat_ARQ_Sender::handle_packet_output_request(const int nbr_blocks_requested)
166 {
167  int nbr_blocks_to_tx;
168  int feasable_blks = feasable_blocks();
169  if (nbr_blocks_requested <= scheduled_total + feasable_blks) {
170  nbr_blocks_to_tx = nbr_blocks_requested;
171  }
172  else {
173  it_warning("Number of requested blocks is more than what is possible to transmitt");
174  nbr_blocks_to_tx = scheduled_total + feasable_blks;
175  }
176 
177  //int nbr_ip_pkts_in_q = ip_pkt_queue.size();
178  while (nbr_blocks_to_tx > scheduled_total) {
179  it_assert(!ip_pkt_queue.empty(), "Selective_Repeat_ARQ_Sender::handle_packet_output_request(): ");
180  Packet *packet = ip_pkt_queue.front();
181  ip_pkt_queue.pop();
182  push_packet_on_tx_buffer(packet);
183  }
184 
185  Array<Packet*> tmp;
186  get_link_packets(nbr_blocks_requested, tmp);
187  packet_output(tmp);
188 }
189 
190 void Selective_Repeat_ARQ_Sender::push_packet_on_tx_buffer(Packet *packet)
191 {
192  L3_Packet_Info *pkt_info = new L3_Packet_Info(packet);
193  int packet_byte_size = pkt_info->pkt_pointer->bit_size() / 8;
194  int nbr_blocks = packet_byte_size / link_packet_size;
195  if (nbr_blocks*link_packet_size != packet_byte_size)
196  nbr_blocks++;
197  if (input_free_space >= nbr_blocks) {
198  pkt_info->timestamp = Event_Queue::now();
199  for (int n = nbr_blocks - 1; n >= 0; n--) {
200  input_buffer(input_next) = new Link_Packet(-1, n, pkt_info);
201  input_free_space--;
202  input_next = (input_next + 1) % input_buffer_size;
203  }
204  }
205  else {
206  buffer_overflow(0);
207  it_error("Selective_Repeat_ARQ_Sender::push_packet_on_tx_buffer(): "
208  "Stopped due to buffer overflow");
209  }
210  fill_output();
211 
212 }
213 
214 void Selective_Repeat_ARQ_Sender::fill_output()
215 {
216  int packets_2_output = std::min(free_sequence_numbers(), buffered_non_outstanding());
217  while (packets_2_output) {
218  input_buffer(tx_next)->seq_no = seq_no;
219  outstanding++;
220  schedule_output(tx_next, seq_no, false);
221  seq_no = (seq_no + 1) % seq_no_max;
222  tx_next = (tx_next + 1) % input_buffer_size;
223  packets_2_output--;
224  }
225 }
226 
227 void Selective_Repeat_ARQ_Sender::schedule_output(const int Buffer_index, const int Sequence_number, const bool Retransmission)
228 {
229  it_assert(input_buffer(Buffer_index) != NULL, "Selective_Repeat_ARQ_Sender::schedule_output(): ");
230  if (output_indexes(Sequence_number) == -1)
231  scheduled_total++;
232  output_indexes(Sequence_number) = Buffer_index;
233  if (Retransmission) {
234  if (retransmission_indexes(Sequence_number) != 1) // This is a new retransmission.
235  scheduled_retransmissions++;
236  retransmission_indexes(Sequence_number) = 1; // Mark packet (index) for retransmission.
237  }
238  else // Mark packet (index) for first time transmission.
239  retransmission_indexes(Sequence_number) = 0;
240 }
241 
242 void Selective_Repeat_ARQ_Sender::get_link_packets(const int K, Array<Packet*> &pa)
243 {
244  int packets_2_retransmit = std::min(K, scheduled_retransmissions);
245  int new_packets_2_transmit = std::min(K, scheduled_total) - packets_2_retransmit;
246  scheduled_retransmissions -= packets_2_retransmit;
247  scheduled_total -= packets_2_retransmit + new_packets_2_transmit;
248  pa.set_size(packets_2_retransmit + new_packets_2_transmit);
249  int l = 0;
250  while (packets_2_retransmit) { // Retransmissions have priority over ...
251  if (retransmission_indexes(rt_pos) == 1) {
252  timer(rt_pos).set(rt_pos, time_out);
253  pa(l++) = (Packet *) new Link_Packet(*input_buffer(output_indexes(rt_pos)));
254  output_indexes(rt_pos) = -1;
255  retransmission_indexes(rt_pos) = -1;
256  packets_2_retransmit--;
257  }
258  rt_pos = (rt_pos + 1) % seq_no_max;
259  }
260  while (new_packets_2_transmit) { // new packets.
261  if (output_indexes(rd_pos) != -1) {
262  timer(rd_pos).set(rd_pos, time_out);
263  pa(l++) = (Packet *) new Link_Packet(*input_buffer(output_indexes(rd_pos)));
264  output_indexes(rd_pos) = -1;
265  new_packets_2_transmit--;
266  }
267  rd_pos = (rd_pos + 1) % seq_no_max;
268  }
269 }
270 
271 void Selective_Repeat_ARQ_Sender::remove(const int Sequence_number)
272 {
273  if (output_indexes(Sequence_number) != -1) {
274  output_indexes(Sequence_number) = -1;
275  scheduled_total--;
276  if (retransmission_indexes(Sequence_number) == 1)
277  scheduled_retransmissions--;
278  retransmission_indexes(Sequence_number) = -1;
279  }
280  const int i = sequence_number_2_buffer_index(Sequence_number);
281  if (input_buffer(i)) {
282  timer(Sequence_number).cancel(); // Cancel the retransmission timer.
283  it_assert(input_buffer(i)->seq_no == Sequence_number, "Selective_Repeat_ARQ_Sender::remove(): ");
284  delete input_buffer(i);
285  input_buffer(i) = NULL;
286  }
287 }
288 
289 void Selective_Repeat_ARQ_Sender::retransmit(const int Sequence_number)
290 {
291  no_retransmit++;
292  const int buffer_index = sequence_number_2_buffer_index(Sequence_number);
293  schedule_output(buffer_index, Sequence_number, true);
294 }
295 
296 int Selective_Repeat_ARQ_Sender::buffered_non_outstanding()
297 {
298  return input_buffer_size - input_free_space - outstanding;
299 }
300 
301 int Selective_Repeat_ARQ_Sender::free_sequence_numbers()
302 {
303  return seq_no_max / 2 - outstanding;
304 }
305 
306 int Selective_Repeat_ARQ_Sender::sequence_number_2_buffer_index(const int Sequence_number)
307 {
308  it_assert(input_buffer(tx_last), "Selective_Repeat_ARQ_Sender::sequence_number_2_buffer_index(): ");
309  it_assert(input_buffer(tx_last)->seq_no != -1, "Selective_Repeat_ARQ_Sender::sequence_number_2_buffer_index(): ");
310  return (tx_last + (Sequence_number - input_buffer(tx_last)->seq_no + seq_no_max) % seq_no_max) % input_buffer_size;
311 }
312 
314 {
315  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::link_packets_buffered(): ");
316  return input_buffer_size - input_free_space;
317 }
318 
320 {
321  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::nof_ready_link_packets(): ");
322  return scheduled_total + feasable_blocks();
323 }
324 
326 {
327  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::link_packets_queued_waiting_for_transmission(): ");
328  div_t q = div(ip_pkt_queue.byte_size(), link_packet_size);
329  int blocks_in_ip_queue = (q.rem) ? q.quot + 1 : q.quot;
330  return buffered_non_outstanding() + scheduled_total + blocks_in_ip_queue;
331 }
332 
333 // int Selective_Repeat_ARQ_Sender::time_stamp_HOL_packet(){
334 // assert(parameters_ok);
335 // return buffered_non_outstanding()+feasable_blocks();
336 // }
337 
339 {
340  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::buffer_size(): ");
341  return input_buffer_size;
342 }
343 
345 {
346  it_assert(parameters_ok, "Selective_Repeat_ARQ_Sender::link_packets_max_queuing_time(): ");
347  it_assert(input_buffer(tx_last), "Selective_Repeat_ARQ_Sender::link_packets_max_queuing_time(): ");
348  return Event_Queue::now() - input_buffer(tx_last)->l3_pkt_info_p->timestamp;
349 }
350 
353 {
354  parameters_ok = false;
355  packet_input.forward(this, &Selective_Repeat_ARQ_Receiver::handle_packet_input);
356  packet_input.set_name("Selective_Repeat_ARQ_Receiver packet_input Slot");
357 }
358 
360 {
361  set_parameters(Seq_no_size);
362  packet_input.forward(this, &Selective_Repeat_ARQ_Receiver::handle_packet_input);
363  packet_input.set_name("Selective_Repeat_ARQ_Receiver packet_input Slot");
364 }
365 
367 
368 void Selective_Repeat_ARQ_Receiver::set_parameters(const int Seq_no_size)
369 {
370  seq_no_size = Seq_no_size;
371  seq_no_max = 1 << seq_no_size;
372  rx_buffer.set_size(seq_no_max);
373  for (int l = 0; l < seq_no_max; rx_buffer(l++) = NULL);
374  Rnext = 0;
375  id = 0;
376  parameters_ok = true;
377 }
378 
379 void Selective_Repeat_ARQ_Receiver::handle_packet_input(Array<Packet*> packet_array)
380 {
381  it_assert(parameters_ok, "Selective_Repeat_ARQ_Receiver::handle_packet_input(): ");
382 
383  int nbr_pkts = packet_array.length();
384  Link_Packet *packet;
385  for (int i = 0;i < nbr_pkts;i++) {
386  packet = (Link_Packet *) packet_array(i);
387  it_assert(packet, "Selective_Repeat_ARQ_Receiver::handle_packet_input(): ");
388  it_assert(packet->seq_no >= 0 && packet->seq_no < seq_no_max, "Selective_Repeat_ARQ_Receiver::handle_packet_input(): ");
389  Array<Packet*> ack_pkt;
390  ack_pkt.set_size(1);
391  ack_pkt(0) = (Packet *) new ACK(packet->seq_no, id++);
392  ack_output(ack_pkt); // Acknowledge the receipt of this packet.
393  if (in_sequence(Rnext, packet->seq_no, seq_no_max) && !rx_buffer(packet->seq_no)) // Is this a new packet in-sequence packet?
394  rx_buffer(packet->seq_no) = packet; // This is a new in-sequence packet.
395  else // This either is a duplicate packet or an out-of-sequence packet.
396  delete packet;
397  while (rx_buffer(Rnext)) { // Is there an unbroken sequence of packets that we can output?
398 
399  if (rx_buffer(Rnext)->link_packet_id == 0) {
400  packet_output(rx_buffer(Rnext)->l3_pkt_info_p->pkt_pointer);
401  delete rx_buffer(Rnext)->l3_pkt_info_p;
402  }
403  delete rx_buffer(Rnext);
404  rx_buffer(Rnext) = NULL;
405  Rnext = (Rnext + 1) % seq_no_max;
406  }
407  }
408 }
409 
410 
411 } //namespace itpp
412 
SourceForge Logo

Generated on Sat May 25 2013 16:32:23 for IT++ by Doxygen 1.8.2