IT++ Logo

packet_channel.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/protocol/packet_channel.h>
00031 #include <itpp/base/random.h>
00032 #include <itpp/base/sort.h>
00033 #include <itpp/base/math/min_max.h>
00034 
00035 
00036 namespace itpp
00037 {
00038 
00039 Packet_Channel::Packet_Channel()
00040 {
00041   parameters_ok = false;
00042   keep_running = false;
00043 }
00044 
00045 Packet_Channel::Packet_Channel(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots)
00046 {
00047   set_parameters(Pr, Delay, Block_rate, Max_slots);
00048 }
00049 
00050 
00051 Packet_Channel::~Packet_Channel() {}
00052 
00053 void Packet_Channel::set_parameters(const double Pr,  const Ttype Delay,  const double Block_rate, const int Max_slots)
00054 {
00055   it_assert(Delay >= 0, "Packet_Channel::set_parameters(): ");
00056   it_assert(Pr >= 0.0 && Pr <= 1.0, "Packet_Channel::set_parameters(): ");
00057   it_assert(Block_rate > 0, "Packet_Channel::set_parameters(): ");
00058   it_assert(Max_slots >= 0, "Packet_Channel::set_parameters(): ");
00059   delay = Delay;
00060   pr = Pr;
00061   block_time = 1.0 / Block_rate;
00062   max_slots = Max_slots;
00063   input.forward(this, &Packet_Channel::handle_input);
00064   nof_inputs.forward(this, &Packet_Channel::handle_nof_inputs);
00065   start.forward(this, &Packet_Channel::handle_start);
00066   keep_running = false;
00067   explicit_errors = false;
00068   K = 0;
00069   k = 0;
00070   parameters_ok = true;
00071 }
00072 
00073 void Packet_Channel::handle_input(Link_Packet* M)
00074 {
00075   it_assert(parameters_ok, "Packet_Channel::handle_input(): ");
00076   it_assert(M != NULL, "Packet_Channel::handle_input(): ");
00077   if (explicit_errors) {
00078     if (k < L) {
00079       lose = lost(k) == K;
00080       if (lose)
00081         k++;
00082     }
00083     K++;
00084   }
00085   else
00086     lose = randu() < pr;
00087   if (lose) {
00088     delete M;
00089   }
00090   else
00091     output(M, delay);
00092   lose = false;
00093 }
00094 
00095 void Packet_Channel::block_rate_loop()
00096 {
00097   it_assert(parameters_ok, "Packet_Channel::block_rate_loop(): ");
00098   get_nof_inputs(NULL);
00099   if (keep_running)
00100     Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
00101 }
00102 
00103 void Packet_Channel::handle_start(const bool run)
00104 {
00105   it_assert(parameters_ok, "Packet_Channel::handle_start(): ");
00106   if (run && !keep_running)// Channel is in 'stop' state. Start it and keep running.
00107     Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time));
00108   keep_running = run;
00109 }
00110 
00111 void Packet_Channel::handle_nof_inputs(const int Nof_ready_messages)
00112 {
00113   it_assert(Nof_ready_messages >= 0, "Packet_Channel::handle_nof_inputs(): ");
00114   int L = 0;
00115   if (max_slots > 0)
00116     L = std::min(Nof_ready_messages, round_i(randu() * max_slots));
00117   else
00118     L = std::min(Nof_ready_messages, 1);
00119   if (L > 0)
00120     input_request(L);
00121 }
00122 
00123 void Packet_Channel::set_errors(const ivec &Lost)
00124 {
00125   L = Lost.length();
00126   if (L > 0) {
00127     it_assert(min(Lost) >= 0, "Packet_Channel::set_errors(): ");
00128     lost = Lost;
00129     sort(lost);
00130     explicit_errors = true;
00131   }
00132 }
00133 
00134 
00135 // ----------------------------- Ack_Channel --------------------------------
00136 
00137 
00138 ACK_Channel::ACK_Channel()
00139 {
00140   parameters_ok = false;
00141 }
00142 
00143 ACK_Channel::ACK_Channel(const double Pr, const Ttype Delay)
00144 {
00145   set_parameters(Pr, Delay);
00146 }
00147 
00148 
00149 ACK_Channel::~ACK_Channel() {}
00150 
00151 void ACK_Channel::set_parameters(const double Pr, const Ttype Delay)
00152 {
00153   it_assert(Delay >= 0, "ACK_Channel::set_parameters(): ");
00154   it_assert(Pr >= 0.0 && Pr <= 1.0, "ACK_Channel::set_parameters(): ");
00155   delay = Delay;
00156   pr = Pr;
00157   input.forward(this, &ACK_Channel::handle_input);
00158   explicit_errors = false;
00159   K = 0;
00160   k = 0;
00161   parameters_ok = true;
00162 }
00163 
00164 void ACK_Channel::handle_input(ACK* M)
00165 {
00166   it_assert(parameters_ok, "ACK_Channel::handle_input(): ");
00167   it_assert(M != NULL, "ACK_Channel::handle_input(): ");
00168   if (explicit_errors) {
00169     if (k < L) {
00170       lose = lost(k) == K;
00171       if (lose)
00172         k++;
00173     }
00174     K++;
00175   }
00176   else
00177     lose = randu() < pr;
00178   if (lose)
00179     delete M;
00180   else
00181     output(M, delay);
00182   lose = false;
00183 }
00184 
00185 void ACK_Channel::set_errors(const ivec& Lost)
00186 {
00187   L = Lost.length();
00188   if (L > 0) {
00189     it_assert(min(Lost) >= 0, "ACK_Channel::set_errors(): ");
00190     lost = Lost;
00191     sort(lost);
00192     explicit_errors = true;
00193   }
00194 }
00195 
00196 } // namespace itpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SourceForge Logo

Generated on Sat Feb 26 2011 16:06:34 for IT++ by Doxygen 1.7.3