Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

timeout.cpp

Go to the documentation of this file.
00001 // $Id$
00002 
00003 //**********************************************************************
00004 //                                                                     *
00005 //                                                                     *
00006 //    Description:   Timeout class.  Invokes a callback function on    *
00007 //                   expiration.                                       *
00008 //                                                                     *
00009 //    Author:        Chris White (whitecf@bcs.org.uk)                  *
00010 //                                                                     *
00011 //    Copyright (C) 2003  Monitor Computing Services Ltd.              *
00012 //                                                                     *
00013 //    This program is free software; you can redistribute it and/or    *
00014 //    modify it under the terms of the GNU General Public License      *
00015 //    as published by the Free Software Foundation; either version 2   *
00016 //    of the License, or any later version.                            *
00017 //                                                                     *
00018 //    This program is distributed in the hope that it will be useful,  *
00019 //    but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00020 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00021 //    GNU General Public License for more details.                     *
00022 //                                                                     *
00023 //    You should have received a copy of the GNU General Public        *
00024 //    License (http://www.gnu.org/copyleft/gpl.html) along with this   *
00025 //    program; if not, write to:                                       *
00026 //       The Free Software Foundation Inc.,                            *
00027 //       59 Temple Place - Suite 330,                                  *
00028 //       Boston, MA  02111-1307,                                       *
00029 //       USA.                                                          *
00030 //                                                                     *
00031 //**********************************************************************
00032 //                                                                     *
00033 //    Notes:                                                           *
00034 //                                                                     *
00035 //**********************************************************************
00036 
00037 #include <mytrace.h>
00038 #include <tmoutlst.h>
00039 
00040 #include "timeout.h"
00041 
00042 
00043 using namespace Monitor::Utility;
00044 
00045 
00046 
00047 Timeout::Timeout(const CallbackBase&    newWatch,
00048                  unsigned               newPeriod,
00049                  const string&          newName) :
00050     Watchable(),
00051     period(newPeriod), timeoutName(newName), expiredFlag(0)
00052 {
00053 #ifdef _DOENTRYTRACE_
00054     string    traceString("Timeout::Timeout[");
00055 
00056     traceString += timeoutName;
00057     traceString += "](newWatch, newPeriod, newName)";
00058 
00059     _USEMYTRACE_(traceString.c_str())
00060 #endif
00061     addWatch(newWatch);
00062 
00063     reset(newPeriod);
00064 
00065     TimeoutList::addTimeout(*this);
00066 }
00067 
00068 
00069 Timeout::Timeout(unsigned               newPeriod,
00070                  const string&          newName) :
00071     Watchable(),
00072     period(newPeriod), timeoutName(newName), expiredFlag(0)
00073 {
00074 #ifdef _DOENTRYTRACE_
00075     string    traceString("Timeout::Timeout[");
00076 
00077     traceString += timeoutName;
00078     traceString += "](newPeriod, newName)";
00079 
00080     _USEMYTRACE_(traceString.c_str())
00081 #endif
00082 
00083     reset(newPeriod);
00084 }
00085 
00086 
00087 Timeout::~Timeout()
00088 {
00089 #ifdef _DOENTRYTRACE_
00090     string    traceString("Timeout::~Timeout[");
00091 
00092     traceString += timeoutName;
00093     traceString += "]()";
00094 
00095     _USEMYTRACE_(traceString.c_str())
00096 #endif
00097     TimeoutList::removeTimeout(*this);
00098 }
00099 
00100 
00101 int
00102 Timeout::operator==(const Timeout&    other)
00103 {
00104     return (&other == this);
00105 }
00106 
00107 
00108 
00109 void
00110 Timeout::reset(unsigned    newPeriod)
00111 {
00112 #ifdef _DOENTRYTRACE_
00113     string    traceString("Timeout::reset[");
00114 
00115     traceString += timeoutName;
00116     traceString += "](newPeriod)";
00117 
00118     _USEMYTRACE_(traceString.c_str())
00119 #endif
00120     if (0 < newPeriod)
00121     {
00122         period = newPeriod;
00123     }
00124 
00125     endTime = period;
00126 
00127     if (0 < endTime)
00128     {
00129         endTime *= CLK_TCK;
00130         endTime /= 1000;
00131     }
00132     
00133     endTime += clock();
00134 
00135     expiredFlag = 0;
00136 }
00137 
00138 
00139 
00140 int
00141 Timeout::hasExpired() const
00142 {
00143     return expiredFlag;
00144 }
00145 
00146 
00147 
00148 int
00149 Timeout::run()
00150 {
00151     int    retVal = 0;
00152 
00153     if (canRun())
00154     {
00155         retVal = !0;
00156 
00157         if (clock() >= endTime)
00158         {
00159             retVal = 0;
00160 
00161             expire();
00162         }
00163     }
00164 
00165     return retVal;
00166 }
00167 
00168 
00169 
00170 const string&
00171 Timeout::getTimeoutName() const
00172 {
00173     return timeoutName;
00174 }
00175 
00176 
00177 
00178 int
00179 Timeout::canRun()
00180 {
00181     return ((0 < endTime) &&
00182             !expiredFlag);
00183 }
00184 
00185 
00186 
00187 void
00188 Timeout::expire()
00189 {
00190 #ifdef _DOENTRYTRACE_
00191     string    traceString("Timeout::expire[");
00192 
00193     traceString += timeoutName;
00194     traceString += "]()";
00195 
00196     _USEMYTRACE_(traceString.c_str())
00197 #endif
00198     updated();
00199 
00200     expiredFlag = !0;
00201 }
00202 

Generated on Wed Oct 29 20:51:59 2003 for Utility BC Common by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002