37 bool in_sequence(
const int a,
const int b,
const int L)
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;
46 parameters_ok =
false;
60 set_parameters(Seq_no_size, Buffer_size_factor, Link_packet_size, Time_out);
73 std::cout <<
"no_retransmit = " << no_retransmit << std::endl;
77 const int Buffer_size_factor,
78 const int Link_packet_size,
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;
99 for (
int l = 0; l < seq_no_max; timer(l++).forward(
this, &Selective_Repeat_ARQ_Sender::retransmit));
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;
111 scheduled_retransmissions = 0;
113 parameters_ok =
true;
118 void Selective_Repeat_ARQ_Sender::handle_ack_input(Array<Packet*> packet_array)
120 Packet *packet = packet_array(0);
121 ACK *A = (ACK *) packet;
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(): ");
127 if (in_sequence(tx_last % seq_no_max, A->seq_no, seq_no_max))
129 while (!input_buffer(tx_last) && outstanding) {
132 tx_last = (tx_last + 1) % input_buffer_size;
139 void Selective_Repeat_ARQ_Sender::handle_packet_input(Packet *packet)
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);
149 int Selective_Repeat_ARQ_Sender::feasable_blocks()
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));
159 void Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets(
void*)
161 it_assert(parameters_ok,
"Selective_Repeat_ARQ_Sender::handle_query_nof_ready_packets(): ");
165 void Selective_Repeat_ARQ_Sender::handle_packet_output_request(
const int nbr_blocks_requested)
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;
173 it_warning(
"Number of requested blocks is more than what is possible to transmitt");
174 nbr_blocks_to_tx = scheduled_total + feasable_blks;
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();
182 push_packet_on_tx_buffer(packet);
190 void Selective_Repeat_ARQ_Sender::push_packet_on_tx_buffer(Packet *packet)
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)
197 if (input_free_space >= nbr_blocks) {
199 for (
int n = nbr_blocks - 1; n >= 0; n--) {
200 input_buffer(input_next) =
new Link_Packet(-1, n, pkt_info);
202 input_next = (input_next + 1) % input_buffer_size;
207 it_error(
"Selective_Repeat_ARQ_Sender::push_packet_on_tx_buffer(): "
208 "Stopped due to buffer overflow");
214 void Selective_Repeat_ARQ_Sender::fill_output()
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;
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;
227 void Selective_Repeat_ARQ_Sender::schedule_output(
const int Buffer_index,
const int Sequence_number,
const bool Retransmission)
229 it_assert(input_buffer(Buffer_index) != NULL,
"Selective_Repeat_ARQ_Sender::schedule_output(): ");
230 if (output_indexes(Sequence_number) == -1)
232 output_indexes(Sequence_number) = Buffer_index;
233 if (Retransmission) {
234 if (retransmission_indexes(Sequence_number) != 1)
235 scheduled_retransmissions++;
236 retransmission_indexes(Sequence_number) = 1;
239 retransmission_indexes(Sequence_number) = 0;
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);
250 while (packets_2_retransmit) {
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--;
258 rt_pos = (rt_pos + 1) % seq_no_max;
260 while (new_packets_2_transmit) {
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--;
267 rd_pos = (rd_pos + 1) % seq_no_max;
271 void Selective_Repeat_ARQ_Sender::remove(
const int Sequence_number)
273 if (output_indexes(Sequence_number) != -1) {
274 output_indexes(Sequence_number) = -1;
276 if (retransmission_indexes(Sequence_number) == 1)
277 scheduled_retransmissions--;
278 retransmission_indexes(Sequence_number) = -1;
280 const int i = sequence_number_2_buffer_index(Sequence_number);
281 if (input_buffer(i)) {
282 timer(Sequence_number).cancel();
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;
289 void Selective_Repeat_ARQ_Sender::retransmit(
const int Sequence_number)
292 const int buffer_index = sequence_number_2_buffer_index(Sequence_number);
293 schedule_output(buffer_index, Sequence_number,
true);
296 int Selective_Repeat_ARQ_Sender::buffered_non_outstanding()
298 return input_buffer_size - input_free_space - outstanding;
301 int Selective_Repeat_ARQ_Sender::free_sequence_numbers()
303 return seq_no_max / 2 - outstanding;
306 int Selective_Repeat_ARQ_Sender::sequence_number_2_buffer_index(
const int Sequence_number)
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;
315 it_assert(parameters_ok,
"Selective_Repeat_ARQ_Sender::link_packets_buffered(): ");
316 return input_buffer_size - input_free_space;
321 it_assert(parameters_ok,
"Selective_Repeat_ARQ_Sender::nof_ready_link_packets(): ");
322 return scheduled_total + feasable_blocks();
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;
340 it_assert(parameters_ok,
"Selective_Repeat_ARQ_Sender::buffer_size(): ");
341 return input_buffer_size;
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;
354 parameters_ok =
false;
370 seq_no_size = Seq_no_size;
371 seq_no_max = 1 << seq_no_size;
373 for (
int l = 0; l < seq_no_max; rx_buffer(l++) = NULL);
376 parameters_ok =
true;
379 void Selective_Repeat_ARQ_Receiver::handle_packet_input(Array<Packet*> packet_array)
381 it_assert(parameters_ok,
"Selective_Repeat_ARQ_Receiver::handle_packet_input(): ");
383 int nbr_pkts = packet_array.length();
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;
391 ack_pkt(0) = (Packet *)
new ACK(packet->seq_no,
id++);
393 if (in_sequence(Rnext, packet->seq_no, seq_no_max) && !rx_buffer(packet->seq_no))
394 rx_buffer(packet->seq_no) = packet;
397 while (rx_buffer(Rnext)) {
399 if (rx_buffer(Rnext)->link_packet_id == 0) {
401 delete rx_buffer(Rnext)->l3_pkt_info_p;
403 delete rx_buffer(Rnext);
404 rx_buffer(Rnext) = NULL;
405 Rnext = (Rnext + 1) % seq_no_max;