IT++ Logo Newcom Logo

packet_generator.cpp

Go to the documentation of this file.
00001 
00033 #include <itpp/protocol/packet_generator.h>
00034 
00035 
00036 namespace itpp {
00037 
00038   Packet_Generator::Packet_Generator(const int Packet_size, const unsigned long int Max_packets){
00039     keep_running = false;
00040     start.forward(this, &Packet_Generator::handle_start);
00041     next.forward(this, &Packet_Generator::handle_next);
00042     output.connect(&next);
00043     set_parameters(Packet_size, Max_packets);
00044   }
00045 
00046   Packet_Generator::~Packet_Generator() { }
00047 
00048   void Packet_Generator::set_parameters(const int Packet_size, const unsigned long int Max_packets){
00049     it_assert(Packet_size>0,"Packet_Generator::set_parameters(): ");
00050     packet_size = Packet_size;
00051     max_packets = Max_packets;
00052     id = 0;
00053   }
00054 
00055   int Packet_Generator::get_packet_size(){
00056     return packet_size;
00057   }
00058 
00059   int Packet_Generator::get_max_packets(){
00060     return max_packets;
00061   }
00062 
00063   void Packet_Generator::handle_next(Packet*){
00064     if(keep_running){    
00065       output(new Packet(8*packet_size), delta_t());
00066       id++;
00067       if(max_packets && id>=max_packets)
00068         start(false);
00069     }
00070   }
00071 
00072   void Packet_Generator::handle_start(const bool run){
00073     if(run&&!keep_running){
00074       keep_running = run;
00075       handle_next(NULL);
00076     }
00077     keep_running = run;
00078   }
00079 
00080 
00081   // ---------------------------- Poisson_Packet_Generator -------------------------------------------------
00082 
00083   Poisson_Packet_Generator::Poisson_Packet_Generator(const double Avg_bit_rate, 
00084                                                      const int Packet_size, 
00085                                                      const unsigned long int Max_packets):Packet_Generator(Packet_size, Max_packets){
00086     set_parameters(Avg_bit_rate, Packet_size, Max_packets);
00087   }
00088 
00089   Poisson_Packet_Generator::~Poisson_Packet_Generator(){}
00090 
00091   void Poisson_Packet_Generator::set_parameters(const double Avg_bit_rate, 
00092                                                 const int Packet_size, 
00093                                                 const unsigned long int Max_packets){
00094     Packet_Generator::set_parameters(Packet_size, Max_packets);  
00095     it_assert(Avg_bit_rate > 0.0,"Packet_Generator::set_parameters(): ");
00096     avg_bit_rate = Avg_bit_rate;
00097     avg_delta_t = 8.0*get_packet_size()/avg_bit_rate;
00098     ee.setup(1.0);
00099   }
00100 
00101   double Poisson_Packet_Generator::get_avg_bit_rate(){
00102     return avg_bit_rate;
00103   }
00104 
00105 
00106   Ttype Poisson_Packet_Generator::delta_t(){
00107     return ee()*avg_delta_t;
00108   }
00109 
00110 
00111   // ---------------------------- Constant_Rate_Packet_Generator -------------------------------------------------
00112 
00113   Constant_Rate_Packet_Generator::Constant_Rate_Packet_Generator(const double Avg_rate, const int Packet_size, const unsigned long int Max_packets):Poisson_Packet_Generator(Avg_rate, Packet_size, Max_packets){}
00114 
00115   Constant_Rate_Packet_Generator::~Constant_Rate_Packet_Generator(){}
00116 
00117   Ttype Constant_Rate_Packet_Generator::delta_t(){
00118     return avg_delta_t;
00119   }
00120 
00121 
00122   // ---------------------------- Burst_WWW_Packet_Generator -------------------------------------------------
00123 
00124 
00125   Burst_WWW_Packet_Generator::Burst_WWW_Packet_Generator(const double Avg_bit_rate, const int Packet_size, const int Max_packets):Poisson_Packet_Generator(Avg_bit_rate, Packet_size, Max_packets) {
00126     Navg = 50; // Average number of packets per burst [packets].
00127     Ti = 1.1960e-4; // Average inter-arrival time between packets in burst [s].
00128     Tr = Navg*Packet_size*8.0/Avg_bit_rate-Ti*(Navg-1); // Average time between bursts.
00129     N = 0;
00130   }
00131 
00132   Burst_WWW_Packet_Generator::~Burst_WWW_Packet_Generator() {
00133 
00134   }
00135 
00136   Ttype Burst_WWW_Packet_Generator::delta_t() {
00137     if(N==0){ // Start of a new burst.
00138       N = Navg;
00139       N--; // First packet is triggered at ...
00140       return ee()*Tr; // ... start time of next burst.
00141     }
00142     else{ // Within a burst.
00143       N--; // One packet less in the burst ...
00144       return ee()*Ti; // ... arrival time for next packet within the burst.
00145     }
00146   }
00147 
00148 
00149   // ----------------------------Sink -------------------------------------------------
00150 
00151   Sink::Sink(const unsigned long int Max_packets){
00152     it_assert(Max_packets>0,"Sink::Sink(): ");
00153     max_packets = Max_packets;
00154     Ncp = 0;
00155     Nbytes = 0;
00156     packet_input.forward(this, &Sink::handle_packet_input);
00157     start_time = Event_Queue::now();
00158   }
00159 
00160   Sink::~Sink(){
00161     std::cout << "Time = "<<Event_Queue::now()<<", Sink : " << std::endl;
00162     std::cout << "Received "<<Ncp<<" packets in sequence." << std::endl;
00163     std::cout << "Receive average bit rate = "<<Nbytes*8.0/(Event_Queue::now()-start_time)<<" [bits/second]." << std::endl;
00164   }
00165 
00166 
00167   void Sink::handle_packet_input(Packet *P){
00168     it_assert(P!=NULL,"Sink::handle_packet_input(): ");
00169     Ncp++;
00170     Nbytes+=(P->bit_size()/8);
00171     delete P;
00172     if(Ncp >= max_packets){
00173       std::cout << "Time = "<<Event_Queue::now()<<", Sink : " << std::endl;
00174       std::cout << "Simulation stopped because : Ncp > max_packets" << std::endl;
00175       Event_Queue::stop();
00176     }
00177   }
00178 
00179 
00180 } // namespace itpp
SourceForge Logo

Generated on Fri Jan 11 08:51:39 2008 for IT++ by Doxygen 1.3.9.1