IT++ Logo
packet_channel.cpp
Go to the documentation of this file.
1 
30 #include <itpp/base/random.h>
31 #include <itpp/base/sort.h>
32 #include <itpp/base/math/min_max.h>
33 
34 
35 namespace itpp
36 {
37 
39 {
40  parameters_ok = false;
41  keep_running = false;
42 }
43 
44 Packet_Channel::Packet_Channel(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
45 {
46  set_parameters(Pr, Delay, Block_rate, Max_slots);
47 }
48 
49 
51 
52 void Packet_Channel::set_parameters(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
53 {
54  it_assert(Delay >= 0, "Packet_Channel::set_parameters(): ");
55  it_assert(Pr >= 0.0 && Pr <= 1.0, "Packet_Channel::set_parameters(): ");
56  it_assert(Block_rate > 0, "Packet_Channel::set_parameters(): ");
57  it_assert(Max_slots >= 0, "Packet_Channel::set_parameters(): ");
58  delay = Delay;
59  pr = Pr;
60  block_time = 1.0 / Block_rate;
61  max_slots = Max_slots;
62  input.forward(this, &Packet_Channel::handle_input);
63  nof_inputs.forward(this, &Packet_Channel::handle_nof_inputs);
64  start.forward(this, &Packet_Channel::handle_start);
65  keep_running = false;
66  explicit_errors = false;
67  K = 0;
68  k = 0;
69  parameters_ok = true;
70 }
71 
72 void Packet_Channel::handle_input(Link_Packet* M)
73 {
74  it_assert(parameters_ok, "Packet_Channel::handle_input(): ");
75  it_assert(M != NULL, "Packet_Channel::handle_input(): ");
76  if (explicit_errors) {
77  if (k < L) {
78  lose = lost(k) == K;
79  if (lose)
80  k++;
81  }
82  K++;
83  }
84  else
85  lose = randu() < pr;
86  if (lose) {
87  delete M;
88  }
89  else
90  output(M, delay);
91  lose = false;
92 }
93 
94 void Packet_Channel::block_rate_loop()
95 {
96  it_assert(parameters_ok, "Packet_Channel::block_rate_loop(): ");
97  get_nof_inputs(NULL);
98  if (keep_running)
99  Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
100 }
101 
102 void Packet_Channel::handle_start(const bool run)
103 {
104  it_assert(parameters_ok, "Packet_Channel::handle_start(): ");
105  if (run && !keep_running)// Channel is in 'stop' state. Start it and keep running.
106  Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
107  keep_running = run;
108 }
109 
110 void Packet_Channel::handle_nof_inputs(const int Nof_ready_messages)
111 {
112  it_assert(Nof_ready_messages >= 0, "Packet_Channel::handle_nof_inputs(): ");
113  int L = 0;
114  if (max_slots > 0)
115  L = std::min(Nof_ready_messages, round_i(randu() * max_slots));
116  else
117  L = std::min(Nof_ready_messages, 1);
118  if (L > 0)
119  input_request(L);
120 }
121 
122 void Packet_Channel::set_errors(const ivec &Lost)
123 {
124  L = Lost.length();
125  if (L > 0) {
126  it_assert(min(Lost) >= 0, "Packet_Channel::set_errors(): ");
127  lost = Lost;
128  sort(lost);
129  explicit_errors = true;
130  }
131 }
132 
133 
134 // ----------------------------- Ack_Channel --------------------------------
135 
136 
138 {
139  parameters_ok = false;
140 }
141 
142 ACK_Channel::ACK_Channel(const double Pr, const Ttype Delay)
143 {
144  set_parameters(Pr, Delay);
145 }
146 
147 
149 
150 void ACK_Channel::set_parameters(const double Pr, const Ttype Delay)
151 {
152  it_assert(Delay >= 0, "ACK_Channel::set_parameters(): ");
153  it_assert(Pr >= 0.0 && Pr <= 1.0, "ACK_Channel::set_parameters(): ");
154  delay = Delay;
155  pr = Pr;
156  input.forward(this, &ACK_Channel::handle_input);
157  explicit_errors = false;
158  K = 0;
159  k = 0;
160  parameters_ok = true;
161 }
162 
163 void ACK_Channel::handle_input(ACK* M)
164 {
165  it_assert(parameters_ok, "ACK_Channel::handle_input(): ");
166  it_assert(M != NULL, "ACK_Channel::handle_input(): ");
167  if (explicit_errors) {
168  if (k < L) {
169  lose = lost(k) == K;
170  if (lose)
171  k++;
172  }
173  K++;
174  }
175  else
176  lose = randu() < pr;
177  if (lose)
178  delete M;
179  else
180  output(M, delay);
181  lose = false;
182 }
183 
184 void ACK_Channel::set_errors(const ivec& Lost)
185 {
186  L = Lost.length();
187  if (L > 0) {
188  it_assert(min(Lost) >= 0, "ACK_Channel::set_errors(): ");
189  lost = Lost;
190  sort(lost);
191  explicit_errors = true;
192  }
193 }
194 
195 } // namespace itpp
SourceForge Logo

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