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

通讯编程

开发平台:

Visual C++

  1. /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
  2. /*
  3.  * Copyright (C) 1997 by the University of Southern California
  4.  * $Id: timer-handler.h,v 1.9 2005/08/25 18:58:02 johnh 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.  * @(#) $Header: /cvsroot/nsnam/ns-2/common/timer-handler.h,v 1.9 2005/08/25 18:58:02 johnh Exp $ (USC/ISI)
  47.  */
  48. #ifndef timer_handler_h
  49. #define timer_handler_h
  50. #include "scheduler.h"
  51. /*
  52.  * Abstract base class to deal with timer-style handlers.
  53.  *
  54.  *
  55.  * To define a new timer, subclass this function and define handle:
  56.  *
  57.  * class MyTimer : public TimerHandler {
  58.  * public:
  59.  * MyTimer(MyAgentClass *a) : AgentTimerHandler() { a_ = a; }
  60.  * virtual double expire(Event *e);
  61.  * protected:
  62.  * MyAgentClass *a_;
  63.  * };
  64.  *
  65.  * Then define expire.
  66.  *
  67.  * Often MyTimer will be a friend of MyAgentClass,
  68.  * or expire() will only call a function of MyAgentClass.
  69.  *
  70.  * See tcp-rbp.{cc,h} for a real example.
  71.  */
  72. #define TIMER_HANDLED -1.0 // xxx: should be const double in class?
  73. class TimerHandler : public Handler {
  74. public:
  75. TimerHandler() : status_(TIMER_IDLE) { }
  76. void sched(double delay); // cannot be pending
  77. void resched(double delay); // may or may not be pending
  78. // if you don't know the pending status
  79. // call resched()
  80. void cancel(); // must be pending
  81. inline void force_cancel() { // cancel!
  82. if (status_ == TIMER_PENDING) {
  83. _cancel();
  84. status_ = TIMER_IDLE;
  85. }
  86. }
  87. enum TimerStatus { TIMER_IDLE, TIMER_PENDING, TIMER_HANDLING };
  88. int status() { return status_; };
  89. protected:
  90. virtual void expire(Event *) = 0;  // must be filled in by client
  91. // Should call resched() if it wants to reschedule the interface.
  92. virtual void handle(Event *);
  93. int status_;
  94. Event event_;
  95. private:
  96. inline void _sched(double delay) {
  97. (void)Scheduler::instance().schedule(this, &event_, delay);
  98. }
  99. inline void _cancel() {
  100. (void)Scheduler::instance().cancel(&event_);
  101. // no need to free event_ since it's statically allocated
  102. }
  103. };
  104. // Local Variables:
  105. // mode:c++
  106. // End:
  107. #endif /* timer_handler_h */