00001 #include "RepeatTimerCore.h"
00002 #include "RepeatTimer.h"
00003
00004 #include <assert.h>
00005
00006
00007
00008 void RepeatTimerCore::checkExists(unsigned int index)
00009 {
00010 if (timer_map->find(index)==timer_map->end())
00011 error(" timer index %u doesn't exist", index);
00012 }
00013
00014 void RepeatTimerCore::handleMessage(cMessage* msg)
00015 {
00016 assert(msg->isSelfMessage());
00017 unsigned int index = msg->getKind();
00018 (*timer_map)[index].count --;
00019 if ((*timer_map)[index].count > 0) {
00020 cMessage *timer = (*timer_map)[index].timer;
00021 double when = (*timer_map)[index].when;
00022 scheduleAt(simTime() + when, timer);
00023 }
00024
00025 timer->handleRepeatTimer(msg->getKind());
00026 }
00027
00028 void RepeatTimerCore::init(RepeatTimer *owner)
00029 {
00030 timer = owner;
00031 timer_map = new std::map<unsigned int,TInfo>();
00032 }
00033
00034 unsigned int RepeatTimerCore::setRepeatTimer(double when, int repeats)
00035 {
00036 unsigned int key = timer_map->size();
00037 while (timer_map->find(key)!=timer_map->end())
00038 key++;
00039 setRepeatTimer(key,when,repeats);
00040 return key;
00041 }
00042
00043 void RepeatTimerCore::setRepeatTimer(unsigned int index, double when, int repeats)
00044 {
00045 Enter_Method_Silent();
00046 cMessage *timer;
00047 if (timer_map->find(index)==timer_map->end()) {
00048 timer = new cMessage("timer");
00049 timer->setKind(index);
00050 (*timer_map)[index].timer = timer;
00051 (*timer_map)[index].destructor = NULL;
00052 } else {
00053 timer = (*timer_map)[index].timer;
00054 if (timer->isScheduled())
00055 cancelEvent(timer);
00056 }
00057 (*timer_map)[index].count = (*timer_map)[index].repeats = repeats;
00058 (*timer_map)[index].when = when;
00059
00060 scheduleAt(simTime() + when, timer);
00061 }
00062
00063 void RepeatTimerCore::cancelRepeatTimer(unsigned int index)
00064 {
00065 Enter_Method_Silent();
00066 checkExists(index);
00067 if ((*timer_map)[index].timer->isScheduled())
00068 cancelEvent((*timer_map)[index].timer);
00069 }
00070
00071 float RepeatTimerCore::remainingRepeatTimer(unsigned int index)
00072 {
00073 checkExists(index);
00074 if ((*timer_map)[index].timer->isScheduled())
00075 return SIMTIME_DBL((*timer_map)[index].timer->getArrivalTime()-simTime());
00076 else
00077 return -1;
00078 }
00079
00080 void RepeatTimerCore::setContextDestructor(unsigned int index, cleanup *c)
00081 {
00082 checkExists(index);
00083 (*timer_map)[index].destructor = c;
00084 }
00085
00086 RepeatTimerCore::~RepeatTimerCore()
00087 {
00088 for (std::map<unsigned int,TInfo>::const_iterator p=timer_map->begin();p!=timer_map->end();p++) {
00089 unsigned int index = p->second.timer->getKind();
00090 checkExists(index);
00091 if ((*timer_map)[index].timer->isScheduled()) {
00092 if ((*timer_map)[index].destructor!=NULL)
00093 (*timer_map)[index].destructor(contextPointer(index));
00094 cancelEvent((*timer_map)[index].timer);
00095 }
00096 delete p->second.timer;
00097 }
00098 delete timer_map;
00099 }
00100
00101 void RepeatTimerCore::setContextPointer(unsigned int index,void * data)
00102 {
00103 checkExists(index);
00104 (*timer_map)[index].timer->setContextPointer(data);
00105 }
00106
00107 void * RepeatTimerCore::contextPointer(unsigned int index)
00108 {
00109 checkExists(index);
00110 return (*timer_map)[index].timer->getContextPointer();
00111 }
00112
00113 void RepeatTimerCore::allocateRepeatTimers(unsigned int count)
00114 {
00115 Enter_Method_Silent();
00116 for (unsigned int i=0;i<count;i++) {
00117 cMessage *timer;
00118 if (timer_map->find(i)==timer_map->end()) {
00119 timer = new cMessage("timer");
00120 timer->setKind(i);
00121 (*timer_map)[i].timer = timer;
00122 }
00123 }
00124 }
00125
00126 void RepeatTimerCore::deleteRepeatTimer(unsigned int index)
00127 {
00128 checkExists(index);
00129 cancelRepeatTimer(index);
00130 if ((*timer_map)[index].destructor)
00131 (*timer_map)[index].destructor(contextPointer(index));
00132 delete timer_map->find(index)->second.timer;
00133 timer_map->erase(timer_map->find(index));
00134 }
00135
00136 void RepeatTimerCore::resetRepeatTimer(unsigned int index)
00137 {
00138 Enter_Method_Silent();
00139 checkExists(index);
00140 (*timer_map)[index].count = (*timer_map)[index].repeats;
00141 cMessage *timer = (*timer_map)[index].timer;
00142 if (timer->isScheduled())
00143 cancelEvent(timer);
00144 double when = (*timer_map)[index].when;
00145 scheduleAt(simTime() + when, timer);
00146 }
00147
00148 void RepeatTimerCore::resetAllRepeatTimers(void)
00149 {
00150 std::map <unsigned int, TInfo>::const_iterator p;
00151 for (p = timer_map->begin(); p != timer_map->end(); p++) {
00152 resetRepeatTimer(p->second.timer->getKind());
00153 }
00154 }
00155
00156 bool RepeatTimerCore::timerExists(unsigned int index)
00157 {
00158 return timer_map->find(index) != timer_map->end();
00159 }
00160