realaudio.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. //
  3. /*
  4.  * realaudio.cc
  5.  * Copyright (C) 2000 by the University of Southern California
  6.  * $Id: realaudio.cc,v 1.6 2005/08/25 18:58:11 johnh Exp $
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License,
  10.  * version 2, as published by the Free Software Foundation.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20.  *
  21.  *
  22.  * The copyright of this module includes the following
  23.  * linking-with-specific-other-licenses addition:
  24.  *
  25.  * In addition, as a special exception, the copyright holders of
  26.  * this module give you permission to combine (via static or
  27.  * dynamic linking) this module with free software programs or
  28.  * libraries that are released under the GNU LGPL and with code
  29.  * included in the standard release of ns-2 under the Apache 2.0
  30.  * license or under otherwise-compatible licenses with advertising
  31.  * requirements (or modified versions of such code, with unchanged
  32.  * license).  You may copy and distribute such a system following the
  33.  * terms of the GNU GPL for this module and the licenses of the
  34.  * other code concerned, provided that you include the source code of
  35.  * that other code when and as the GNU GPL requires distribution of
  36.  * source code.
  37.  *
  38.  * Note that people who make modified versions of this module
  39.  * are not obligated to grant this special exception for their
  40.  * modified versions; it is their choice whether to do so.  The GNU
  41.  * General Public License gives permission to release a modified
  42.  * version without this exception; this exception also makes it
  43.  * possible to release a modified version which carries forward this
  44.  * exception.
  45.  *
  46.  */
  47. //
  48. // RealAudio traffic model that simulates RealAudio traffic based on a set of
  49. // traces collected from Broadcast.com
  50. //
  51. #ifndef lint
  52. static const char rcsid[] =
  53.     "@(#) $Header: /cvsroot/nsnam/ns-2/realaudio/realaudio.cc,v 1.6 2005/08/25 18:58:11 johnh Exp $ (USC/ISI)";
  54. #endif
  55. #ifndef WIN32 
  56. #include <sys/time.h>
  57. #endif
  58. #include "random.h"
  59. #include "trafgen.h"
  60. #include "ranvar.h"
  61. class RA_Traffic : public TrafficGenerator {
  62.  public:
  63. RA_Traffic();
  64. int loadCDF(const char* filename);
  65.         int lookup(double u);
  66. virtual double value();
  67. virtual double interpolate(double u, double x1, double y1, double x2, double y2);
  68. virtual double next_interval(int&);
  69.  protected:
  70. void init();
  71. double ontime_;  /* average length of burst (sec) */
  72. double offtime_; /* average idle period (sec) */
  73. double rate_;    /* send rate during burst (bps) */
  74. double interval_; /* inter-packet time at burst rate */
  75. double burstlen_; /* average # packets/burst */
  76. unsigned int rem_; /* number of packets remaining in current burst */
  77.         double minCDF_;         // min value of the CDF (default to 0)
  78. double maxCDF_;         // max value of the CDF (default to 1)
  79. int interpolation_;     // how to interpolate data (INTER_DISCRETE...)
  80. int numEntry_;          // number of entries in the CDF table
  81. int maxEntry_;          // size of the CDF table (mem allocation)
  82. CDFentry* table_;       // CDF table of (val_, cdf_)
  83. RNG* rng_;
  84. //        EmpiricalRandomVariable Offtime_ ;
  85. //      EmpiricalRandomVariable Ontime_ ;
  86. };
  87. static class RATrafficClass : public TclClass {
  88.  public:
  89. RATrafficClass() : TclClass("Application/Traffic/RealAudio") {}
  90.   TclObject* create(int, const char*const*) {
  91. return (new RA_Traffic());
  92. }
  93. } class_ra_traffic;
  94. RA_Traffic::RA_Traffic() : minCDF_(0), maxCDF_(1), maxEntry_(32), table_(0)
  95. {
  96. bind("minCDF_", &minCDF_);
  97. bind("maxCDF_", &maxCDF_);
  98. bind("interpolation_", &interpolation_);
  99. bind("maxEntry_", &maxEntry_);
  100. bind_time("burst_time_", &ontime_);
  101. bind_time("idle_time_", &offtime_);
  102. bind_bw("rate_", &rate_);
  103. bind("packetSize_", &size_);
  104. rng_ = RNG::defaultrng();
  105. }
  106. void RA_Traffic::init()
  107. {
  108.         int res = loadCDF("offtimecdf");
  109. //      int res1 = Ontime_.loadCDF("ontimecdf");
  110.         timeval tv;
  111. gettimeofday(&tv, 0);
  112.         if (res < 0)  printf("error:unable to load offtimecdf");
  113. interval_ = ontime_ ;
  114. burstlen_ = (double) ( rate_ * (ontime_ + offtime_))/ (double)(size_ << 3);
  115. rem_ = 0;
  116. if (agent_)
  117. agent_->set_pkttype(PT_REALAUDIO);
  118. }
  119. double RA_Traffic::next_interval(int& size)
  120. {
  121. // double t = Ontime_.value() ;
  122. double o = value() ; //off-time is taken from CDF
  123. double t = ontime_ ; //fixed on-time
  124. // double o = offtime_ ;
  125. // o = o * ran ;
  126. if (rem_ == 0) {
  127. /* compute number of packets in next burst */
  128. rem_ = int(burstlen_ + .5);
  129. //recalculate the number of packet sent during ON-period if
  130. // off-time is mutliple of 1.8s
  131. if (o > offtime_ ) {
  132.            double b = 
  133.    // (double) ( rate_ * (ontime_ + o))/ (double)(size_ << 3);
  134.    (double) ( rate_ * (t + o))/ (double)(size_ << 3);
  135.    rem_ = int(b);
  136. }
  137. // if (ran > 0.8 ) rem_++ ;
  138. // if (ran < 0.2 ) rem_-- ;
  139. /* make sure we got at least 1 */
  140. if (rem_<= 0)
  141. rem_ = 1;
  142. /* start of an idle period, compute idle time */
  143. t += o ;
  144. }
  145. rem_--;
  146. size = size_;
  147. return(t);
  148. }
  149. int RA_Traffic::loadCDF(const char* filename)
  150. {
  151. FILE* fp;
  152. char line[256];
  153. CDFentry* e;
  154. fp = fopen(filename, "r");
  155. if (fp == 0)
  156. return 0;
  157. if (table_ == 0)
  158. table_ = new CDFentry[maxEntry_];
  159. for (numEntry_=0;  fgets(line, 256, fp);  numEntry_++) {
  160. if (numEntry_ >= maxEntry_) { // resize the CDF table
  161. maxEntry_ *= 2;
  162. e = new CDFentry[maxEntry_];
  163. for (int i=numEntry_-1; i >= 0; i--)
  164. e[i] = table_[i];
  165. delete table_;
  166. table_ = e;
  167. }
  168. e = &table_[numEntry_];
  169. // Use * and l together raises a warning
  170. sscanf(line, "%lf %*f %lf", &e->val_, &e->cdf_);
  171. }
  172. return numEntry_;
  173. }
  174. double RA_Traffic::value()
  175. {
  176. if (numEntry_ <= 0)
  177. return 0;
  178. double u = rng_->uniform(minCDF_, maxCDF_);
  179. int mid = lookup(u);
  180. if (mid && interpolation_ && u < table_[mid].cdf_)
  181. return interpolate(u, table_[mid-1].cdf_, table_[mid-1].val_,
  182.    table_[mid].cdf_, table_[mid].val_);
  183. return table_[mid].val_;
  184. }
  185. double RA_Traffic::interpolate(double x, double x1, double y1, double x2, double y2)
  186. {
  187. double value = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
  188. if (interpolation_ == INTER_INTEGRAL) // round up
  189. return ceil(value);
  190. return value;
  191. }
  192. int RA_Traffic::lookup(double u)
  193. {
  194. // always return an index whose value is >= u
  195. int lo, hi, mid;
  196. if (u <= table_[0].cdf_)
  197. return 0;
  198. for (lo=1, hi=numEntry_-1;  lo < hi; ) {
  199. mid = (lo + hi) / 2;
  200. if (u > table_[mid].cdf_)
  201. lo = mid + 1;
  202. else hi = mid;
  203. }
  204. return lo;
  205. }