realaudio.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 //
00003 
00004 /*
00005  * realaudio.cc
00006  * Copyright (C) 2000 by the University of Southern California
00007  * $Id: realaudio.cc,v 1.6 2005/08/25 18:58:11 johnh Exp $
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License,
00011  * version 2, as published by the Free Software Foundation.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License along
00019  * with this program; if not, write to the Free Software Foundation, Inc.,
00020  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021  *
00022  *
00023  * The copyright of this module includes the following
00024  * linking-with-specific-other-licenses addition:
00025  *
00026  * In addition, as a special exception, the copyright holders of
00027  * this module give you permission to combine (via static or
00028  * dynamic linking) this module with free software programs or
00029  * libraries that are released under the GNU LGPL and with code
00030  * included in the standard release of ns-2 under the Apache 2.0
00031  * license or under otherwise-compatible licenses with advertising
00032  * requirements (or modified versions of such code, with unchanged
00033  * license).  You may copy and distribute such a system following the
00034  * terms of the GNU GPL for this module and the licenses of the
00035  * other code concerned, provided that you include the source code of
00036  * that other code when and as the GNU GPL requires distribution of
00037  * source code.
00038  *
00039  * Note that people who make modified versions of this module
00040  * are not obligated to grant this special exception for their
00041  * modified versions; it is their choice whether to do so.  The GNU
00042  * General Public License gives permission to release a modified
00043  * version without this exception; this exception also makes it
00044  * possible to release a modified version which carries forward this
00045  * exception.
00046  *
00047  */
00048 //
00049 // RealAudio traffic model that simulates RealAudio traffic based on a set of
00050 // traces collected from Broadcast.com
00051 //
00052 
00053 #ifndef lint
00054 static const char rcsid[] =
00055     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/realaudio/realaudio.cc,v 1.6 2005/08/25 18:58:11 johnh Exp $ (USC/ISI)";
00056 #endif
00057 
00058 #ifndef WIN32 
00059 #include <sys/time.h>
00060 #endif
00061 #include "random.h"
00062 #include "trafgen.h"
00063 #include "ranvar.h"
00064 
00065 class RA_Traffic : public TrafficGenerator {
00066  public:
00067     RA_Traffic();
00068     int loadCDF(const char* filename);
00069         int lookup(double u);
00070     virtual double value();
00071     virtual double interpolate(double u, double x1, double y1, double x2, double y2);
00072     virtual double next_interval(int&);
00073 
00074  protected:
00075     void init();
00076     double ontime_;  /* average length of burst (sec) */
00077     double offtime_; /* average idle period (sec) */
00078     double rate_;    /* send rate during burst (bps) */
00079     double interval_; /* inter-packet time at burst rate */
00080     double burstlen_; /* average # packets/burst */
00081     unsigned int rem_; /* number of packets remaining in current burst */
00082         double minCDF_;         // min value of the CDF (default to 0)
00083     double maxCDF_;         // max value of the CDF (default to 1)
00084     int interpolation_;     // how to interpolate data (INTER_DISCRETE...)
00085     int numEntry_;          // number of entries in the CDF table
00086     int maxEntry_;          // size of the CDF table (mem allocation)
00087     CDFentry* table_;       // CDF table of (val_, cdf_)
00088     RNG* rng_;
00089 
00090 //        EmpiricalRandomVariable Offtime_ ;
00091 //      EmpiricalRandomVariable Ontime_ ;
00092 };
00093 
00094 
00095 static class RATrafficClass : public TclClass {
00096  public:
00097     RATrafficClass() : TclClass("Application/Traffic/RealAudio") {}
00098     TclObject* create(int, const char*const*) {
00099         return (new RA_Traffic());
00100     }
00101 } class_ra_traffic;
00102 
00103 RA_Traffic::RA_Traffic() : minCDF_(0), maxCDF_(1), maxEntry_(32), table_(0)
00104 {
00105     bind("minCDF_", &minCDF_);
00106     bind("maxCDF_", &maxCDF_);
00107     bind("interpolation_", &interpolation_);
00108     bind("maxEntry_", &maxEntry_);
00109     bind_time("burst_time_", &ontime_);
00110     bind_time("idle_time_", &offtime_);
00111     bind_bw("rate_", &rate_);
00112     bind("packetSize_", &size_);
00113 
00114     rng_ = RNG::defaultrng();
00115 }
00116 
00117 void RA_Traffic::init()
00118 {
00119         int res = loadCDF("offtimecdf");
00120 //      int res1 = Ontime_.loadCDF("ontimecdf");
00121         timeval tv;
00122     gettimeofday(&tv, 0);
00123 
00124         if (res < 0)  printf("error:unable to load offtimecdf");
00125 
00126     interval_ = ontime_ ;
00127     burstlen_ = (double) ( rate_ * (ontime_ + offtime_))/ (double)(size_ << 3);
00128     rem_ = 0;
00129     if (agent_)
00130         agent_->set_pkttype(PT_REALAUDIO);
00131 }
00132 
00133 double RA_Traffic::next_interval(int& size)
00134 {
00135 //  double t = Ontime_.value() ;
00136     double o = value() ; //off-time is taken from CDF
00137 
00138     double t = ontime_ ; //fixed on-time
00139 //  double o = offtime_ ;
00140 
00141 //  o = o * ran ;
00142 
00143     if (rem_ == 0) {
00144         /* compute number of packets in next burst */
00145         rem_ = int(burstlen_ + .5);
00146 
00147         //recalculate the number of packet sent during ON-period if
00148         // off-time is mutliple of 1.8s
00149         if (o > offtime_ ) {
00150                double b = 
00151            // (double) ( rate_ * (ontime_ + o))/ (double)(size_ << 3);
00152            (double) ( rate_ * (t + o))/ (double)(size_ << 3);
00153            rem_ = int(b);
00154         }
00155 
00156 //      if (ran > 0.8 ) rem_++ ;
00157 //      if (ran < 0.2 ) rem_-- ;
00158 
00159         /* make sure we got at least 1 */
00160         if (rem_<= 0)
00161             rem_ = 1;   
00162         /* start of an idle period, compute idle time */
00163         t += o ;
00164     }   
00165     rem_--;
00166 
00167     size = size_;
00168     return(t);
00169 
00170 }
00171 
00172 int RA_Traffic::loadCDF(const char* filename)
00173 {
00174     FILE* fp;
00175     char line[256];
00176     CDFentry* e;
00177 
00178     fp = fopen(filename, "r");
00179     if (fp == 0)
00180         return 0;
00181 
00182     if (table_ == 0)
00183         table_ = new CDFentry[maxEntry_];
00184     for (numEntry_=0;  fgets(line, 256, fp);  numEntry_++) {
00185         if (numEntry_ >= maxEntry_) {   // resize the CDF table
00186             maxEntry_ *= 2;
00187             e = new CDFentry[maxEntry_];
00188             for (int i=numEntry_-1; i >= 0; i--)
00189                 e[i] = table_[i];
00190             delete table_;
00191             table_ = e;
00192         }
00193         e = &table_[numEntry_];
00194         // Use * and l together raises a warning
00195         sscanf(line, "%lf %*f %lf", &e->val_, &e->cdf_);
00196     }
00197     return numEntry_;
00198 }
00199 
00200 double RA_Traffic::value()
00201 {
00202     if (numEntry_ <= 0)
00203         return 0;
00204     double u = rng_->uniform(minCDF_, maxCDF_);
00205     int mid = lookup(u);
00206     if (mid && interpolation_ && u < table_[mid].cdf_)
00207         return interpolate(u, table_[mid-1].cdf_, table_[mid-1].val_,
00208                    table_[mid].cdf_, table_[mid].val_);
00209     return table_[mid].val_;
00210 }
00211 
00212 double RA_Traffic::interpolate(double x, double x1, double y1, double x2, double y2)
00213 {
00214     double value = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
00215     if (interpolation_ == INTER_INTEGRAL)   // round up
00216         return ceil(value);
00217     return value;
00218 }
00219 
00220 int RA_Traffic::lookup(double u)
00221 {
00222     // always return an index whose value is >= u
00223     int lo, hi, mid;
00224     if (u <= table_[0].cdf_)
00225         return 0;
00226     for (lo=1, hi=numEntry_-1;  lo < hi; ) {
00227         mid = (lo + hi) / 2;
00228         if (u > table_[mid].cdf_)
00229             lo = mid + 1;
00230         else hi = mid;
00231     }
00232     return lo;
00233 }

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