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 "TimerManager.h"
00026
00027 namespace cmlabs {
00028
00029
00030
00031
00032
00033 TimerManager::TimerManager()
00034 {
00035 timerList = new ObjectDictionary();
00036 timerList->doDelete();
00037 }
00038
00039 TimerManager::~TimerManager()
00040 {
00041 cancelAll();
00042
00043
00044 delete(timerList);
00045
00046 }
00047
00048 JString TimerManager::createTimer(JString strData, long ms) {
00049 doMaintenance();
00050 JString id = createUniqueID("timer");
00051 Timer* t = new Timer(strData, ms);
00052
00053 timerList->put(id, t);
00054 t->name = id;
00055 t->start();
00056 return id;
00057 }
00058
00059 JString TimerManager::createTimer(Object* obj, long ms) {
00060 doMaintenance();
00061 JString id = createUniqueID("timer");
00062 Timer* t = new Timer(obj, ms);
00063
00064 timerList->put(id, t);
00065 t->name = id;
00066 t->start();
00067 return id;
00068 }
00069
00070 JString TimerManager::createTimer(JString strData, long ms, void (*function)(JString id)) {
00071 doMaintenance();
00072 JString id = createUniqueID("timer");
00073 Timer* t = new Timer(strData, ms, function);
00074
00075 timerList->put(id, t);
00076 t->name = id;
00077 t->start();
00078 return id;
00079 }
00080
00081 JString TimerManager::createTimer(Object* obj, long ms, void (*function)(Object* object)) {
00082 doMaintenance();
00083 JString id = createUniqueID("timer");
00084 Timer* t = new Timer(obj, ms, function);
00085
00086 timerList->put(id, t);
00087 t->name = id;
00088 t->start();
00089 return id;
00090 }
00091
00092 JString TimerManager::createTimer(JString strData, long ms, TimerReceiver* receiver) {
00093 doMaintenance();
00094 JString id = createUniqueID("timer");
00095 Timer* t = new Timer(strData, ms, receiver);
00096
00097 timerList->put(id, t);
00098 t->name = id;
00099 t->start();
00100 return id;
00101 }
00102
00103 JString TimerManager::createTimer(Object* obj, long ms, TimerObjectReceiver* receiver) {
00104 doMaintenance();
00105 JString id = createUniqueID("timer");
00106 Timer* t = new Timer(obj, ms, receiver);
00107
00108 timerList->put(id, t);
00109 t->name = id;
00110 t->start();
00111 return id;
00112 }
00113
00114 void TimerManager::doMaintenance() {
00115
00116
00117 Collection removes;
00118
00119 JString key;
00120 Timer* timer;
00121 for (int n=0; n<timerList->getCount(); n++) {
00122 key = timerList->getKey(n);
00123 timer = (Timer*) timerList->get(key);
00124 if (timer != NULL) {
00125 if (timer->msSinceEnded() > 100) {
00126
00127 removes.add(key);
00128 }
00129 else {
00130
00131 }
00132 }
00133 }
00134
00135 for (int m=0; m<removes.getCount(); m++) {
00136
00137 key = removes.get(m);
00138 if (!cancel(key)) {
00139 if (DEBUGLEVEL(DEBUG))
00140 printf("Failed Removed Timer %s!\n", (char*) key);
00141 }
00142
00143
00144 }
00145
00146
00147 }
00148
00149 bool TimerManager::cancel(JString id) {
00150
00151 Timer* t = (Timer*) timerList->get(id);
00152 if (t != NULL) {
00153
00154
00155
00156
00157
00158
00159
00160 timerList->remove(id);
00161 return true;
00162 }
00163
00164 return false;
00165 }
00166
00167 bool TimerManager::cancelAll() {
00168
00169 JString key;
00170 Timer* timer;
00171
00172 for (int n=0; n<timerList->getCount(); n++) {
00173 key = timerList->getKey(n);
00174 timer = (Timer*) timerList->get(key);
00175 if (timer != NULL) {
00176 if (timer->isRunning())
00177 if (DEBUGLEVEL(DEBUG))
00178 printf(" Timer %s is active: %d / %d!\n", (char*) key, timer->msLeft(), timer->msSinceEnded());
00179
00180
00181 timer->cancel();
00182 wait(0);
00183 }
00184 }
00185
00186
00187
00188 doMaintenance();
00189
00190 return true;
00191 }
00192
00193 long TimerManager::waitFor(JString id) {
00194 Timer* t = getTimer(id);
00195 long res = -22222;
00196 if (t != NULL) {
00197 res = t->waitFor();
00198 if (t->msLeft() == 0)
00199 timerList->remove(id);
00200 }
00201 return res;
00202 }
00203
00204 long TimerManager::msLeft(JString id) {
00205 Timer* t = getTimer(id);
00206 if (t != NULL)
00207 return t->msLeft();
00208 return -11111;
00209 }
00210
00211 Timer* TimerManager::getTimer(JString id) {
00212 Timer* t = NULL;
00213
00214 t = (Timer*) timerList->get(id);
00215
00216
00217
00218 return t;
00219 }
00220
00221
00222 int TimerManager::getActiveTimerCount() {
00223 return timerList->getCount();
00224 }
00225
00226 Collection TimerManager::getActiveTimerIDs() {
00227 return timerList->getAllKeys();
00228 }
00229
00230
00231
00232 bool TimerManager::unitTest() {
00233
00234 int base = 500;
00235
00236 #if defined(_WIN32_WCE)
00237 base = 1000;
00238 #endif
00239
00240 int max = 6;
00241 JSemaphore** sems = new JSemaphore*[max];
00242 int n;
00243
00244 this->addUnitTestLog("Starting test...");
00245
00246 for (n=0; n<max; n++) {
00247 printf("%d\b", n); fflush(stdout);
00248 sems[n] = new JSemaphore(JString(n));
00249 this->createTimer(sems[n], (n+1)*base, this);
00250 }
00251
00252 for (n=0; n<max; n++) {
00253 printf("O%d\b\b", n); fflush(stdout);
00254 if (sems[n]->tryWait()) {
00255
00256 this->addUnitTestLog(JString::format("FAILED: Semaphore %d triggered early 1...", n));
00257
00258 }
00259 }
00260
00261
00262 for (n=0; n<max; n++) {
00263 printf("OO%d\b\b\b", n); fflush(stdout);
00264 wait(base+10);
00265
00266 if (!sems[n]->tryWait()) {
00267 printf("FAILED: Semaphore %d should have been triggered by now...\n", n);
00268 this->addUnitTestLog(JString::format("FAILED: Semaphore %d should have been triggered by now...", n));
00269 return false;
00270 }
00271 else {
00272
00273 }
00274 if (n == (max/2)) {
00275
00276 this->createTimer(sems[0], (max-n+1)*base, this);
00277 this->createTimer(sems[1], (max-n+2)*base, this);
00278 this->createTimer(sems[2], (max-n+3)*base, this);
00279
00280 }
00281 }
00282
00283 printf("OOO.\b\b\b\b"); fflush(stdout);
00284 if (sems[0]->tryWait()) {
00285 this->addUnitTestLog("Semaphore 0 triggered early 2");
00286 return false;
00287 }
00288
00289 printf("OOO0\b\b\b\b"); fflush(stdout);
00290 if (!sems[0]->wait(5000)) {
00291 printf("FAILED: Last Semaphore should have been triggered by now...\n");
00292 this->addUnitTestLog("Semaphore 0 should have been done");
00293 return false;
00294 }
00295
00296 printf("OOO1\b\b\b\b"); fflush(stdout);
00297 if (!sems[1]->wait(5000)) {
00298 this->addUnitTestLog("Semaphore 1 should have been done");
00299 return false;
00300 }
00301
00302 printf("OOO2\b\b\b\b"); fflush(stdout);
00303 if (!sems[2]->wait(5000)) {
00304 this->addUnitTestLog("Semaphore 2 should have been done");
00305 return false;
00306 }
00307
00308 wait(base);
00309
00310
00311 this->doMaintenance();
00312
00313 printf("OOOC\b\b\b\b"); fflush(stdout);
00314 for (n=0; n<max; n++)
00315 delete(sems[n]);
00316
00317 delete [] sems;
00318 printf("OOOO\b\b\b\b"); fflush(stdout);
00319 return true;
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397 }
00398
00399
00400
00401 bool TimerManager::receiveTimerObject(Object* obj) {
00402
00403 JSemaphore* sem = (JSemaphore*) obj;
00404 if (obj == NULL) {
00405 printf("Received NULL object instead of semaphore...\n");
00406 }
00407 else {
00408
00409 sem->post();
00410
00411 }
00412 return true;
00413 }
00414
00415
00416 }