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