cbr_traffic.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) Xerox Corporation 1997. All rights reserved.
00004  *  
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by the
00007  * Free Software Foundation; either version 2 of the License, or (at your
00008  * option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * Linking this file statically or dynamically with other modules is making
00020  * a combined work based on this file.  Thus, the terms and conditions of
00021  * the GNU General Public License cover the whole combination.
00022  *
00023  * In addition, as a special exception, the copyright holders of this file
00024  * give you permission to combine this file with free software programs or
00025  * libraries that are released under the GNU LGPL and with code included in
00026  * the standard release of ns-2 under the Apache 2.0 license or under
00027  * otherwise-compatible licenses with advertising requirements (or modified
00028  * versions of such code, with unchanged license).  You may copy and
00029  * distribute such a system following the terms of the GNU GPL for this
00030  * file and the licenses of the other code concerned, provided that you
00031  * include the source code of that other code when and as the GNU GPL
00032  * requires distribution of source code.
00033  *
00034  * Note that people who make modified versions of this file are not
00035  * obligated to grant this special exception for their modified versions;
00036  * it is their choice whether to do so.  The GNU General Public License
00037  * gives permission to release a modified version without this exception;
00038  * this exception also makes it possible to release a modified version
00039  * which carries forward this exception.
00040  */
00041 
00042 #include <stdlib.h>
00043  
00044 #include "random.h"
00045 #include "trafgen.h"
00046 #include "ranvar.h"
00047 
00048 
00049 /* 
00050  * Constant bit rate traffic source.   Parameterized by interval, (optional)
00051  * random noise in the interval, and packet size.  
00052  */
00053 
00054 class CBR_Traffic : public TrafficGenerator {
00055  public:
00056     CBR_Traffic();
00057     virtual double next_interval(int&);
00058     //HACK so that udp agent knows interpacket arrival time within a burst
00059     inline double interval() { return (interval_); }
00060  protected:
00061     virtual void start();
00062     void init();
00063     double rate_;     /* send rate during on time (bps) */
00064     double interval_; /* packet inter-arrival time during burst (sec) */
00065     double random_;
00066     int seqno_;
00067     int maxpkts_;
00068 };
00069 
00070 
00071 static class CBRTrafficClass : public TclClass {
00072  public:
00073     CBRTrafficClass() : TclClass("Application/Traffic/CBR") {}
00074     TclObject* create(int, const char*const*) {
00075         return (new CBR_Traffic());
00076     }
00077 } class_cbr_traffic;
00078 
00079 CBR_Traffic::CBR_Traffic() : seqno_(0)
00080 {
00081     bind_bw("rate_", &rate_);
00082     bind("random_", &random_);
00083     bind("packetSize_", &size_);
00084     bind("maxpkts_", &maxpkts_);
00085 }
00086 
00087 void CBR_Traffic::init()
00088 {
00089         // compute inter-packet interval 
00090     interval_ = (double)(size_ << 3)/(double)rate_;
00091     if (agent_)
00092         if (agent_->get_pkttype() != PT_TCP &&
00093             agent_->get_pkttype() != PT_TFRC)
00094             agent_->set_pkttype(PT_CBR);
00095 }
00096 
00097 void CBR_Traffic::start()
00098 {
00099         init();
00100         running_ = 1;
00101         timeout();
00102 }
00103 
00104 double CBR_Traffic::next_interval(int& size)
00105 {
00106     // Recompute interval in case rate_ or size_ has changes
00107     interval_ = (double)(size_ << 3)/(double)rate_;
00108     double t = interval_;
00109     if (random_)
00110         t += interval_ * Random::uniform(-0.5, 0.5);    
00111     size = size_;
00112     if (++seqno_ < maxpkts_)
00113         return(t);
00114     else
00115         return(-1); 
00116 }
00117 

Generated on Tue Mar 6 16:47:43 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6