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

Generated on Fri Aug 14 15:28:25 2009 for IT++ by Doxygen 1.5.9