00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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