packet_channel.cpp
Go to the documentation of this file.
#include <itpp/protocol/packet_channel.h>
#include <itpp/base/random.h>
#include <itpp/base/sort.h>
#include <itpp/base/math/min_max.h>
namespace itpp
{
Packet_Channel::Packet_Channel()
{
parameters_ok = false;
keep_running = false;
}
Packet_Channel::Packet_Channel(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
{
set_parameters(Pr, Delay, Block_rate, Max_slots);
}
Packet_Channel::~Packet_Channel() {}
void Packet_Channel::set_parameters(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
{
it_assert(Delay >= 0, "Packet_Channel::set_parameters(): ");
it_assert(Pr >= 0.0 && Pr <= 1.0, "Packet_Channel::set_parameters(): ");
it_assert(Block_rate > 0, "Packet_Channel::set_parameters(): ");
it_assert(Max_slots >= 0, "Packet_Channel::set_parameters(): ");
delay = Delay;
pr = Pr;
block_time = 1.0 / Block_rate;
max_slots = Max_slots;
input.forward(this, &Packet_Channel::handle_input);
nof_inputs.forward(this, &Packet_Channel::handle_nof_inputs);
start.forward(this, &Packet_Channel::handle_start);
keep_running = false;
explicit_errors = false;
K = 0;
k = 0;
parameters_ok = true;
}
void Packet_Channel::handle_input(Link_Packet* M)
{
it_assert(parameters_ok, "Packet_Channel::handle_input(): ");
it_assert(M != NULL, "Packet_Channel::handle_input(): ");
if (explicit_errors) {
if (k < L) {
lose = lost(k) == K;
if (lose)
k++;
}
K++;
}
else
lose = randu() < pr;
if (lose) {
delete M;
}
else
output(M, delay);
lose = false;
}
void Packet_Channel::block_rate_loop()
{
it_assert(parameters_ok, "Packet_Channel::block_rate_loop(): ");
get_nof_inputs(NULL);
if (keep_running)
Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
}
void Packet_Channel::handle_start(const bool run)
{
it_assert(parameters_ok, "Packet_Channel::handle_start(): ");
if (run && !keep_running)// Channel is in 'stop' state. Start it and keep running.
Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
keep_running = run;
}
void Packet_Channel::handle_nof_inputs(const int Nof_ready_messages)
{
it_assert(Nof_ready_messages >= 0, "Packet_Channel::handle_nof_inputs(): ");
int L = 0;
if (max_slots > 0)
L = std::min(Nof_ready_messages, round_i(randu() * max_slots));
else
L = std::min(Nof_ready_messages, 1);
if (L > 0)
input_request(L);
}
void Packet_Channel::set_errors(const ivec &Lost)
{
L = Lost.length();
if (L > 0) {
it_assert(min(Lost) >= 0, "Packet_Channel::set_errors(): ");
lost = Lost;
sort(lost);
explicit_errors = true;
}
}
// ----------------------------- Ack_Channel --------------------------------
ACK_Channel::ACK_Channel()
{
parameters_ok = false;
}
ACK_Channel::ACK_Channel(const double Pr, const Ttype Delay)
{
set_parameters(Pr, Delay);
}
ACK_Channel::~ACK_Channel() {}
void ACK_Channel::set_parameters(const double Pr, const Ttype Delay)
{
it_assert(Delay >= 0, "ACK_Channel::set_parameters(): ");
it_assert(Pr >= 0.0 && Pr <= 1.0, "ACK_Channel::set_parameters(): ");
delay = Delay;
pr = Pr;
input.forward(this, &ACK_Channel::handle_input);
explicit_errors = false;
K = 0;
k = 0;
parameters_ok = true;
}
void ACK_Channel::handle_input(ACK* M)
{
it_assert(parameters_ok, "ACK_Channel::handle_input(): ");
it_assert(M != NULL, "ACK_Channel::handle_input(): ");
if (explicit_errors) {
if (k < L) {
lose = lost(k) == K;
if (lose)
k++;
}
K++;
}
else
lose = randu() < pr;
if (lose)
delete M;
else
output(M, delay);
lose = false;
}
void ACK_Channel::set_errors(const ivec& Lost)
{
L = Lost.length();
if (L > 0) {
it_assert(min(Lost) >= 0, "ACK_Channel::set_errors(): ");
lost = Lost;
sort(lost);
explicit_errors = true;
}
}
} // namespace itpp
itpp::Packet_Channel::nof_inputs
Slot< Packet_Channel, int > nof_inputs
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:69
itpp::round_i
ITPP_EXPORT int round_i(double x)
Round to nearest integer.
itpp::Ttype
double Ttype
64-bit floating point time
Definition: events.h:54
packet_channel.h
Definition of a Packet channel class.
itpp::randu
double randu(void)
Generates a random uniform (0,1) number.
Definition: random.h:804
itpp::Packet_Channel::input
Slot< Packet_Channel, Link_Packet * > input
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:67
itpp::Packet_Channel::~Packet_Channel
~Packet_Channel()
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:50
itpp::ACK_Channel::ACK_Channel
ACK_Channel()
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:137
it_assert
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
itpp::Packet_Channel::output
Signal< Link_Packet * > output
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:73
sort.h
Sorting functions.
min_max.h
Minimum and maximum functions on vectors and matrices.
itpp::Packet_Channel::set_parameters
void set_parameters(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:52
itpp::min
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
itpp::Packet_Channel::Packet_Channel
Packet_Channel()
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:38
itpp::Packet_Channel::set_errors
void set_errors(const ivec &Lost)
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:122
itpp::Packet_Channel::input_request
Signal< int > input_request
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:75
itpp::ACK_Channel::set_errors
void set_errors(const ivec &Lost)
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:184
itpp::Event
An Event class that executes a function when the event expires.
Definition: events.h:164
itpp
itpp namespace
Definition: itmex.h:36
itpp::Packet_Channel::get_nof_inputs
Signal< void * > get_nof_inputs
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:77
itpp::ACK_Channel::~ACK_Channel
~ACK_Channel()
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:148
itpp::ACK
Definition: packet.h:117
random.h
Definition of classes for random number generators.
itpp::Link_Packet
Definition: packet.h:97
itpp::Packet_Channel::start
Slot< Packet_Channel, bool > start
ADD DOCUMENTATION HERE.
Definition: packet_channel.h:65
itpp::Event_Queue::add
static void add(Base_Event *e)
Add event to Queue.
Definition: events.cpp:45
itpp::ACK_Channel::set_parameters
void set_parameters(const double Pr, const Ttype Delay)
ADD DOCUMENTATION HERE.
Definition: packet_channel.cpp:150

Generated on Sat Sep 30 2017 07:04:09 for IT++ by Doxygen 1.8.13