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

Generated on Sat Jul 9 2011 15:21:32 for IT++ by Doxygen 1.7.4