00001 00033 #include <itpp/protocol/packet_channel.h> 00034 #include <itpp/base/random.h> 00035 #include <itpp/base/sort.h> 00036 #include <itpp/base/stat.h> 00037 00038 00039 namespace itpp { 00040 00041 Packet_Channel::Packet_Channel() { 00042 parameters_ok = false; 00043 keep_running = false; 00044 } 00045 00046 Packet_Channel::Packet_Channel(const double Pr, const Ttype Delay, const double Block_rate, const int Max_slots) { 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 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 it_assert(parameters_ok,"Packet_Channel::handle_input(): "); 00074 it_assert(M!=NULL,"Packet_Channel::handle_input(): "); 00075 if(explicit_errors){ 00076 if(k<L){ 00077 lose = lost(k)==K; 00078 if(lose) 00079 k++; 00080 } 00081 K++; 00082 } 00083 else 00084 lose = randu() < pr; 00085 if(lose){ 00086 delete M; 00087 } 00088 else 00089 output(M, delay); 00090 lose = false; 00091 } 00092 00093 void Packet_Channel::block_rate_loop() { 00094 it_assert(parameters_ok,"Packet_Channel::block_rate_loop(): "); 00095 get_nof_inputs(NULL); 00096 if(keep_running) 00097 Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time)); 00098 } 00099 00100 void Packet_Channel::handle_start(const bool run) { 00101 it_assert(parameters_ok,"Packet_Channel::handle_start(): "); 00102 if(run&&!keep_running)// Channel is in 'stop' state. Start it and keep running. 00103 Event_Queue::add(new Event<Packet_Channel>(this, &Packet_Channel::block_rate_loop, block_time)); 00104 keep_running = run; 00105 } 00106 00107 void Packet_Channel::handle_nof_inputs(const int Nof_ready_messages) { 00108 it_assert(Nof_ready_messages>=0,"Packet_Channel::handle_nof_inputs(): "); 00109 int L = 0; 00110 if(max_slots>0) 00111 L = std::min(Nof_ready_messages, round_i(randu()*max_slots)); 00112 else 00113 L = std::min(Nof_ready_messages, 1); 00114 if(L>0) 00115 input_request(L); 00116 } 00117 00118 void Packet_Channel::set_errors(const ivec &Lost) { 00119 L = Lost.length(); 00120 if(L>0){ 00121 it_assert(min(Lost)>=0,"Packet_Channel::set_errors(): "); 00122 lost = Lost; 00123 sort(lost); 00124 explicit_errors = true; 00125 } 00126 } 00127 00128 00129 // ----------------------------- Ack_Channel -------------------------------- 00130 00131 00132 ACK_Channel::ACK_Channel() { 00133 parameters_ok = false; 00134 } 00135 00136 ACK_Channel::ACK_Channel(const double Pr, const Ttype Delay) { 00137 set_parameters(Pr, Delay); 00138 } 00139 00140 00141 ACK_Channel::~ACK_Channel(){} 00142 00143 void ACK_Channel::set_parameters(const double Pr, const Ttype Delay) { 00144 it_assert(Delay >= 0,"ACK_Channel::set_parameters(): "); 00145 it_assert(Pr>=0.0 && Pr<=1.0,"ACK_Channel::set_parameters(): "); 00146 delay = Delay; 00147 pr = Pr; 00148 input.forward(this, &ACK_Channel::handle_input); 00149 explicit_errors = false; 00150 K = 0; 00151 k = 0; 00152 parameters_ok = true; 00153 } 00154 00155 void ACK_Channel::handle_input(ACK* M) { 00156 it_assert(parameters_ok,"ACK_Channel::handle_input(): "); 00157 it_assert(M!=NULL,"ACK_Channel::handle_input(): "); 00158 if(explicit_errors){ 00159 if(k<L){ 00160 lose = lost(k)==K; 00161 if(lose) 00162 k++; 00163 } 00164 K++; 00165 } 00166 else 00167 lose = randu() < pr; 00168 if(lose) 00169 delete M; 00170 else 00171 output(M, delay); 00172 lose = false; 00173 } 00174 00175 void ACK_Channel::set_errors(const ivec& Lost) { 00176 L = Lost.length(); 00177 if(L>0){ 00178 it_assert(min(Lost)>=0,"ACK_Channel::set_errors(): "); 00179 lost = Lost; 00180 sort(lost); 00181 explicit_errors = true; 00182 } 00183 } 00184 00185 } // namespace itpp
Generated on Thu Apr 19 14:43:45 2007 for IT++ by Doxygen 1.5.1