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

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * hdr_qs.cc
  3.  * Copyright (C) 2001 by the University of Southern California
  4.  * $Id: hdr_qs.cc,v 1.9 2007/01/22 05:33:57 tom_henderson Exp $
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License,
  8.  * version 2, as published by the Free Software Foundation.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  *
  20.  * The copyright of this module includes the following
  21.  * linking-with-specific-other-licenses addition:
  22.  *
  23.  * In addition, as a special exception, the copyright holders of
  24.  * this module give you permission to combine (via static or
  25.  * dynamic linking) this module with free software programs or
  26.  * libraries that are released under the GNU LGPL and with code
  27.  * included in the standard release of ns-2 under the Apache 2.0
  28.  * license or under otherwise-compatible licenses with advertising
  29.  * requirements (or modified versions of such code, with unchanged
  30.  * license).  You may copy and distribute such a system following the
  31.  * terms of the GNU GPL for this module and the licenses of the
  32.  * other code concerned, provided that you include the source code of
  33.  * that other code when and as the GNU GPL requires distribution of
  34.  * source code.
  35.  *
  36.  * Note that people who make modified versions of this module
  37.  * are not obligated to grant this special exception for their
  38.  * modified versions; it is their choice whether to do so.  The GNU
  39.  * General Public License gives permission to release a modified
  40.  * version without this exception; this exception also makes it
  41.  * possible to release a modified version which carries forward this
  42.  * exception.
  43.  *
  44.  */
  45. /*
  46.  * Quick Start for TCP and IP.
  47.  * A scheme for transport protocols to dynamically determine initial 
  48.  * congestion window size.
  49.  *
  50.  * http://www.ietf.org/internet-drafts/draft-amit-quick-start-02.ps
  51.  *
  52.  * Packet header definition for Quick Start implementation
  53.  * hdr_qs.h
  54.  *
  55.  * Srikanth Sundarrajan, 2002
  56.  * sundarra@usc.edu
  57.  */
  58. #include <stdio.h>
  59. #include "hdr_qs.h"
  60. #include "qsagent.h"
  61. #include <math.h>
  62. #define Epsilon 0.000001
  63. int hdr_qs::offset_;
  64. static class QSHeaderClass : public PacketHeaderClass {
  65. public:
  66. QSHeaderClass() : PacketHeaderClass("PacketHeader/QS", sizeof(hdr_qs)) {
  67. bind_offset(&hdr_qs::offset_);
  68. }
  69. } class_qshdr;
  70. /*
  71.  * Helper function contributed by Olivier Dalle to solve portability
  72.  * problems in the Bps_to_rate function.  
  73.  * Choose Epsilon such that any floating point in range [(n - Epsilon); n]
  74.  * will be converted to int value n.
  75.  */
  76. int fuzzy_cast(double f) {
  77.         int temp = (int)f;
  78.         if ( (f- (float)temp) >= (1.0-Epsilon) )         
  79.                 /*
  80.                  * Adding a small value such as 2*Epsilon may be risky
  81.                  * if Epsilon is very small and f is big, while 0.5 should
  82.                  * work in any case
  83.                  */
  84.                 return (int)(f+0.5);    
  85.         else
  86.                 return temp;
  87. }
  88. /*
  89.  * These two functions convert rate in QS packet to 10 KBps units
  90.  *   and vice versa.
  91.  */
  92. double hdr_qs::rate_to_Bps(int rate)
  93. {
  94.        if (rate == 0)
  95.                return 0;
  96.        switch(QSAgent::rate_function_) {
  97.        case 1:
  98.        default:
  99.                return rate * 10240; // rate unit: 10 kilobytes per sec
  100.        case 2:
  101.                return pow(2, rate) * 5000; // exponential, base 2
  102.        }
  103. }
  104. int hdr_qs::Bps_to_rate(double Bps)
  105. {
  106.        if (Bps == 0)
  107.                return 0;
  108.        switch(QSAgent::rate_function_) {
  109.        case 1:
  110.        default:
  111.                return (int) Bps / 10240; // rate unit: 10 kilobytes per sec
  112.        case 2:
  113.        // Add an option for rounding either up or down.
  114.        int bpstorate = fuzzy_cast(log(Bps / 5000) / log(2));
  115.                // return (bpstorate >= 1 ? bpstorate : 0);
  116.        if (bpstorate >= 1) 
  117. return bpstorate;
  118.        else 
  119. return 0;
  120.        }
  121. }
  122. // Local Variables:
  123. // mode:c++
  124. // c-basic-offset: 8
  125. // End: