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 #include "Timer.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 Timer::Timer(JString strData, long ms)
00034 {
00035 endedAt.setInvalid();
00036 data = strData;
00037 shouldContinue = true;
00038 msRuntime = ms;
00039 object = NULL;
00040 stringCallback = NULL;
00041 objectCallback = NULL;
00042 timerReceiver = NULL;
00043 timerObjectReceiver = NULL;
00044
00045 }
00046
00047 Timer::Timer(Object* obj, long ms) {
00048 endedAt.setInvalid();
00049 object = obj;
00050 shouldContinue = true;
00051 msRuntime = ms;
00052 stringCallback = NULL;
00053 objectCallback = NULL;
00054 timerReceiver = NULL;
00055 timerObjectReceiver = NULL;
00056 }
00057
00058 Timer::Timer(JString strData, long ms, void (*function)(JString string))
00059 {
00060 endedAt.setInvalid();
00061 data = strData;
00062 shouldContinue = true;
00063 stringCallback = function;
00064 msRuntime = ms;
00065 object = NULL;
00066 objectCallback = NULL;
00067 timerReceiver = NULL;
00068 timerObjectReceiver = NULL;
00069
00070 }
00071
00072 Timer::Timer(Object* obj, long ms, void (*function)(Object* object)) {
00073 endedAt.setInvalid();
00074 object = obj;
00075 shouldContinue = true;
00076 objectCallback = function;
00077 msRuntime = ms;
00078 stringCallback = NULL;
00079 timerReceiver = NULL;
00080 timerObjectReceiver = NULL;
00081 }
00082
00083 Timer::Timer(JString strData, long ms, TimerReceiver* receiver) {
00084 endedAt.setInvalid();
00085 data = strData;
00086 shouldContinue = true;
00087 msRuntime = ms;
00088 object = NULL;
00089 timerReceiver = receiver;
00090 timerObjectReceiver = NULL;
00091 stringCallback = NULL;
00092 objectCallback = NULL;
00093
00094 }
00095
00096 Timer::Timer(Object* obj, long ms, TimerObjectReceiver* receiver) {
00097 endedAt.setInvalid();
00098 object = obj;
00099 shouldContinue = true;
00100 msRuntime = ms;
00101 timerReceiver = NULL;
00102 timerObjectReceiver = receiver;
00103 stringCallback = NULL;
00104 objectCallback = NULL;
00105 }
00106
00107
00108
00109 Timer::~Timer() {
00110
00111 cancel();
00112
00113 }
00114
00115
00116
00117 void Timer::run() {
00118
00119
00120
00121 endedAt.setInvalid();
00122 startedAt = JTime();
00123
00124 timerSem.wait(msRuntime);
00125
00126
00127 if (shouldContinue) {
00128 if (object != NULL) {
00129 if (objectCallback != NULL) {
00130 (*objectCallback)(object);
00131 object = NULL;
00132 }
00133 else if (timerObjectReceiver != NULL) {
00134 timerObjectReceiver->receiveTimerObject(object);
00135 object = NULL;
00136 }
00137 }
00138 else {
00139 if (stringCallback != NULL)
00140 (*stringCallback)(data);
00141 else if (timerReceiver != NULL)
00142 timerReceiver->receiveTimerString(data);
00143 }
00144 }
00145
00146
00147 extSem.post();
00148 endedAt = JTime();
00149
00150
00151
00152
00153
00154
00155
00156 }
00157
00158 long Timer::msSinceEnded() {
00159 JTime now;
00160 if (!hasEnded())
00161 return -1;
00162 else
00163 return (now - endedAt);
00164 }
00165
00166 bool Timer::hasEnded() {
00167 return endedAt.isValid();
00168 }
00169
00170 void Timer::cancel() {
00171
00172 shouldContinue = false;
00173 stringCallback = NULL;
00174 objectCallback = NULL;
00175 timerReceiver = NULL;
00176 timerObjectReceiver = NULL;
00177 if (object != NULL) {
00178 if (DEBUGLEVEL(DETAILEDSTATUS))
00179 printf("Deleting Unused Timer Object... "); fflush(stdout);
00180
00181 delete(object);
00182 object = NULL;
00183 if (DEBUGLEVEL(DETAILEDSTATUS))
00184 printf("Done\n"); fflush(stdout);
00185
00186
00187 }
00188
00189 if (!hasEnded()) {
00190 timerSem.post();
00191 extSem.wait(100);
00192 }
00193
00194
00195
00196 this->terminate();
00197
00198 }
00199
00200
00201 long Timer::msLeft() {
00202
00203
00204 JTime now;
00205
00206
00207 long left = msRuntime - (now - startedAt);
00208
00209
00210 if (left < -1000) {
00211 if (DEBUGLEVEL(KITCHENSINK)) {
00212 printf("Timer ran out long ago... ERROR!\n");
00213 }
00214 shouldContinue = false;
00215 timerSem.post();
00216 }
00217
00218 return left;
00219 }
00220
00221 long Timer::waitFor() {
00222
00223 JTime start;
00224
00225
00226 long ms = -1;
00227
00228 while ((ms=msLeft()) > 0) {
00229
00230 extSem.wait(ms/2);
00231
00232
00233 }
00234
00235 ms = start.getAge();
00236
00237
00238
00239 return ms;
00240 }
00241
00242
00243 void Timer::OnTimer() {
00244
00245 timerSem.post();
00246 }
00247
00248 Object* Timer::getAndRemoveObject() {
00249 Object* obj = object;
00250 object = NULL;
00251 return obj;
00252 }
00253
00254
00255 }