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

通讯编程

开发平台:

Visual C++

  1. /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (c) Xerox Corporation 1997. All rights reserved.
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the
  7.  * Free Software Foundation; either version 2 of the License, or (at your
  8.  * option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * 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.  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18.  *
  19.  * Linking this file statically or dynamically with other modules is making
  20.  * a combined work based on this file.  Thus, the terms and conditions of
  21.  * the GNU General Public License cover the whole combination.
  22.  *
  23.  * In addition, as a special exception, the copyright holders of this file
  24.  * give you permission to combine this file with free software programs or
  25.  * libraries that are released under the GNU LGPL and with code included in
  26.  * the standard release of ns-2 under the Apache 2.0 license or under
  27.  * otherwise-compatible licenses with advertising requirements (or modified
  28.  * versions of such code, with unchanged license).  You may copy and
  29.  * distribute such a system following the terms of the GNU GPL for this
  30.  * file and the licenses of the other code concerned, provided that you
  31.  * include the source code of that other code when and as the GNU GPL
  32.  * requires distribution of source code.
  33.  *
  34.  * Note that people who make modified versions of this file are not
  35.  * obligated to grant this special exception for their modified versions;
  36.  * it is their choice whether to do so.  The GNU General Public License
  37.  * gives permission to release a modified version without this exception;
  38.  * this exception also makes it possible to release a modified version
  39.  * which carries forward this exception.
  40.  */
  41. /*
  42.  * Copyright (c) 1994 Regents of the University of California.
  43.  * All rights reserved.
  44.  *
  45.  * Redistribution and use in source and binary forms, with or without
  46.  * modification, are permitted provided that the following conditions
  47.  * are met:
  48.  * 1. Redistributions of source code must retain the above copyright
  49.  *    notice, this list of conditions and the following disclaimer.
  50.  * 2. Redistributions in binary form must reproduce the above copyright
  51.  *    notice, this list of conditions and the following disclaimer in the
  52.  *    documentation and/or other materials provided with the distribution.
  53.  * 3. All advertising materials mentioning features or use of this software
  54.  *    must display the following acknowledgement:
  55.  * This product includes software developed by the Computer Systems
  56.  * Engineering Group at Lawrence Berkeley Laboratory.
  57.  * 4. Neither the name of the University nor of the Laboratory may be used
  58.  *    to endorse or promote products derived from this software without
  59.  *    specific prior written permission.
  60.  *
  61.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  62.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  64.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  65.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  66.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  67.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  68.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  69.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  70.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  71.  * SUCH DAMAGE.
  72.  */
  73. #ifndef lint
  74. static const char rcsid[] =
  75.     "@(#) $Header: /cvsroot/nsnam/ns-2/adc/simple-intserv-sched.cc,v 1.7 2005/08/26 05:05:28 tomh Exp $ (LBL)";
  76. #endif
  77. //Simple scheduler with 2 service priority levels and protects signalling
  78. //ctrl  packets
  79. #include "config.h"
  80. #include "queue.h"
  81. #define CLASSES 2
  82. class SimpleIntServ : public Queue {
  83. public:
  84. SimpleIntServ() {
  85. int i;
  86. char buf[10];
  87. for (i=0;i<CLASSES;i++) {
  88. q_[i] = new PacketQueue;
  89. qlimit_[i] = 0;
  90. sprintf(buf,"qlimit%d_",i);
  91. bind(buf,&qlimit_[i]);
  92. }
  93. }
  94. protected :
  95. void enque(Packet *);
  96. Packet *deque();
  97. PacketQueue *q_[CLASSES];
  98. int qlimit_[CLASSES];
  99. };
  100. static class SimpleIntServClass : public TclClass {
  101. public:
  102. SimpleIntServClass() : TclClass("Queue/SimpleIntServ") {}
  103. TclObject* create(int, const char*const*) {
  104. return (new SimpleIntServ);
  105. }
  106. } class_simple_intserv;
  107. void SimpleIntServ::enque(Packet* p)
  108. {
  109. hdr_ip* iph=hdr_ip::access(p);
  110. int cl=(iph->flowid()) ? 1:0;
  111. if (q_[cl]->length() >= (qlimit_[cl]-1)) {
  112. hdr_cmn* ch=hdr_cmn::access(p);
  113. packet_t ptype = ch->ptype();
  114. if ( (ptype != PT_REQUEST) && (ptype != PT_REJECT) && (ptype != PT_ACCEPT) && (ptype != PT_CONFIRM) && (ptype != PT_TEARDOWN) ) {
  115. drop(p);
  116. }
  117. else {
  118. q_[cl]->enque(p);
  119. }
  120. }
  121. else {
  122. q_[cl]->enque(p);
  123. }
  124. }
  125. Packet *SimpleIntServ::deque()
  126. {
  127. int i;
  128. for (i=CLASSES-1;i>=0;i--)
  129. if (q_[i]->length())
  130. return q_[i]->deque();
  131. return 0;
  132. }