定时器使用(for NS2)
2013-10-26 14:16
239 查看
AODV method: 1. define your timer in the .h file such as in the aodv.h file class PrintTimer : public Handler { public: PrintTimer(AODV* a) : agent(a) {} void handle(Event*); private: AODV *agent; Event intr; }; 2. declare the timer in your routing agent in the .h file class AODV : public Agent { ...... ...... PrintTimer ptimer;; ..... friend class PrintTimer;; .... }; 3. In the aodv.cc file, implement your handle function which will do what you want to do when the timer expires void PrintTimer::handle(Event*) { fprintf (stderr, "This is a test for the usage of timer.\n"); //if you want to schedule this timer periodically //#define DEFINED_DELAY 1.0 //sec // Scheduler::instance().schedule(this, &intr, DEFINED_DELAY); } 4. remember to initialize it AODV::AODV(nsaddr_t id) : ..., ptimer(this), ... { } 5. Then you can use it. For example, in void AODV::recvReply(Packet *p) { .... .... //when it receives a reply, schedule the print timer for 0.5 s //Scheduler::instance().schedule(&ptimer, p->copy(), 0.5); //if want to process the packet Scheduler::instance().schedule(&ptimer, new Event(), 0.5); //nothing needs to be processed // you can also directly call the handle function, no delay //ptimer.handle((Event*) 0); // or //Scheduler::instance().schedule(&ptimer, new Event(), 0.0); .... .... } | Another method: 1. define your timer in your .h file such as in my sensor-agent.h file class WakeupTimer : public TimerHandler { public: WakeupTimer(SensorAgent* agent) : TimerHandler() { agent_ = agent; } void expire(Event* event); private: SensorAgent* agent_; }; 2. declare the timer in your agent as in my sensorAgent in the .h file class sensorAgent : public Agent { ...... ...... WakeupTimer wakeupTimer_; ..... friend class WakeupTimer; .... }; 3. In sensor-agent.cc file, implement your expire function which will do what you want to do after the delay void WakeupTimer::expire(Event* event) { // wakeup timer has expired. switch state to probing and determine if // this node needs to start working. agent_->switchState(SENSOR_PROBING); } 4. remember to initialize it sensorAgent::sensorAgent() : ..., wakeupTimer_(this), ... { } 5. Then you can use it wakeupTimer_.sched(sleeping_duration); wakeupTimer_.cancel(); |
With stop, restart in .h class helloTimer : public Handler { public: helloTimer(OppFwd* a) : agent(a) { busy_ = 0; } void handle(Event*); void start(double time); void stop(void); inline int busy(void) { return busy_; } private: OppFwd *agent; Event intr; int busy_; }; in .cc void helloTimer::handle(Event*) { busy_ = 0; agent->sendHello(); double interval = MinHelloInterval + ((MaxHelloInterval - MinHelloInterval) * Random::uniform()); assert(interval >= 0); Scheduler::instance().schedule(this, &intr, interval); } void helloTimer::start(double time) { Scheduler &s = Scheduler::instance(); assert(busy_ == 0); busy_ = 1; s.schedule(this, &intr, time); } void helloTimer::stop(void) { Scheduler &s = Scheduler::instance(); assert(busy_); s.cancel(&intr); busy_ = 0; } | |
"Couldn't schedule timer", check common/timer-handler.cc |
相关文章推荐
- 【转】ns2中定时器的使用
- for循环当中使用定时器的注意事项
- 在Weblogic中使用定时器(commonj Timer for weblogic server)
- ArcGIS Runtime SDK for iOS(二) ---使用定时器绘制由point组成的polyLine,并显示位置坐标
- IAR FOR AVR 定时器中断的使用
- IAR For AVR 定时器溢出中断 (使用小结)
- [技巧]go语言使用select{}阻塞main函数和for range 做定时器服务
- ns2中定时器的使用详解
- IAR For AVR 定时器溢出中断 (使用小结)
- ns2中定时器的使用详解
- 每天学习一算法系列(12) (求1+2+…+n,不能使用乘除法,for、while、if 、else、switch、case 等关键字以及条件判断语句)
- JavaScript 使用for(…in…)实现反射机制
- Go 使用for range time.Tick() 设置定时器
- java当中的定时器的4种使用方式
- 【版本控制】Github for Windows客户端的使用
- class.forname()的使用
- 使用jQuery for Asp.Net 我的开发环境配置
- ARM 7 用户模式下禁止/使能中断的一种方法--使用软中断 for Keil MDK
- SpringBoot学习记录2------定时器的使用
- java 定时器的使用