2008/04/14

NS2 週期性執行某個工作

1. 設計 Handler Class


[cpp]
class CustomHandler : public Handler {
public:
CustomHandler(JobClass* j, double itv) {
this->job = j;
this->interval = itv;
}

void start() {
Scheduler::instance().schedule(this, &intr, interval);
}

void handle(Event *intr);
private:
Event intr;

JobClass* job;
double interval;
};
[/cpp]

2. 實做 handle(Event *intr)
[cpp]
CustomHandler::handle(Event *intr) {

job->doSomething();
Scheduler::instance().schedule(this, intr, interval);

}
[/cpp]

3. 使用方法
[cpp]
JobClass job;
double itv = 10;
CustomHandler ch(&job, itv);

ch.start();
[/cpp]

No comments:

Post a Comment