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>
33
34
35namespace itpp
36{
37
39{
40 parameters_ok = false;
41 keep_running = false;
42}
43
44Packet_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
52void 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
72void 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
94void 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
102void 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
110void 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
122void 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
142ACK_Channel::ACK_Channel(const double Pr, const Ttype Delay)
143{
144 set_parameters(Pr, Delay);
145}
146
147
149
150void 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
163void 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
184void 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
void set_parameters(const double Pr, const Ttype Delay)
ADD DOCUMENTATION HERE.
ACK_Channel()
ADD DOCUMENTATION HERE.
Slot< ACK_Channel, ACK * > input
ADD DOCUMENTATION HERE.
~ACK_Channel()
ADD DOCUMENTATION HERE.
void set_errors(const ivec &Lost)
ADD DOCUMENTATION HERE.
Signal< ACK * > output
ADD DOCUMENTATION HERE.
static void add(Base_Event *e)
Add event to Queue.
Definition: events.cpp:45
Packet_Channel()
ADD DOCUMENTATION HERE.
Signal< int > input_request
ADD DOCUMENTATION HERE.
~Packet_Channel()
ADD DOCUMENTATION HERE.
Slot< Packet_Channel, Link_Packet * > input
ADD DOCUMENTATION HERE.
void set_errors(const ivec &Lost)
ADD DOCUMENTATION HERE.
void set_parameters(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
ADD DOCUMENTATION HERE.
Slot< Packet_Channel, int > nof_inputs
ADD DOCUMENTATION HERE.
Slot< Packet_Channel, bool > start
ADD DOCUMENTATION HERE.
Signal< Link_Packet * > output
ADD DOCUMENTATION HERE.
Signal< void * > get_nof_inputs
ADD DOCUMENTATION HERE.
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
double randu(void)
Generates a random uniform (0,1) number.
Definition: random.h:804
Minimum and maximum functions on vectors and matrices.
itpp namespace
Definition: itmex.h:37
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
double Ttype
64-bit floating point time
Definition: events.h:54
Definition of a Packet channel class.
Definition of classes for random number generators.
Sorting functions.
SourceForge Logo

Generated on Tue Jan 24 2023 00:00:00 for IT++ by Doxygen 1.9.6