IT++ Logo

packet_generator.cpp

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

Generated on Sun Sep 14 18:52:35 2008 for IT++ by Doxygen 1.5.6