How to add timer in aodv using ns2

2019-09-10 11:48发布

问题:

At aodv When the node receive route request it will check if it has valid route to the destination, if it has no valid route it will rebroadcast the route request. I want to add timer before the node rebroadcast the route request.During the timer time if the node receive RREQ with the same ID (that means the node receive the RREQ twice ) then discard the RREQ otherwise rebroadcast the RREQ. I don’t know how to write the code of this part. The code of timer 1. The timer was defined in aodv.h

    class RouteRequestTimer : public Handler {
    public: 
    RouteRequestTimer(AODV* a) : agent(a) { busy_ = 0; }
    void handle(Event*);
    void start(double time);
    void stop(void);
    inline int busy(void) { return busy_; }
    private:
    AODV *agent;
    Event intr;
    int busy_;
    };
  1. The timer was declared in the routing agent aodv.h

    friend class  RouteRequestTimer;
    RouteRequestTimer rrtimer;
    
  2. In aodv.cc, implement the handle function

    void RouteRequestTimer::handle(Event*) {
    busy_ = 0;
    #define interval 0.5
    fprintf (stderr, "This is a test for the usage of timer.\n");
    Scheduler::instance().schedule(this, &intr, interval);
    }
    void RouteRequestTimer::start(double time) {
    Scheduler &s = Scheduler::instance();
    assert(busy_ == 0);
    busy_ = 1;
    s.schedule(this, &intr, time);
    }
    void RouteRequestTimer::stop(void) {
    Scheduler &s = Scheduler::instance();
    assert(busy_);
    s.cancel(&intr);
    busy_ = 0;
    }
    
  3. The timer was initialized at aodv.cc

    AODV::AODV(nsaddr_t id) : ..., rrtimer(this), ... {
    }
    
  4. The timer was used at the function that receive the route request

    void
    AODV::recvRequest(packet *p){
    …
    …
    …
    Scheduler::instance().schedule(&rrtimer, p->copy(), inerval);  
    …
    }
    

Then I recompile ns2 and the compilation completed with no error. When I run the tcl code for the network using aodv, this error appear

scheduler: Event UID not valid

please how to solve this error and how to check the received route request id on the timer, if a RREQ with the same id is received then discard the packet otherwise forward it.

Thanks in advance

回答1:

You must follow these steps to implement a timer in ns2 :

1) Define your timer class at the aodv.h file as follow

class MyTimer : public Handler {
  public:
    MyTimer(AODV* a) : agent(a) {}
    void handle(Event*);
  private:
    AODV *agent;
    Event intr;
};

2) Define an object of this class as a property of the AODV class in the aodv.h :

MyTimer mtimer;

3) Make MyTimer class as a friend class of the AODV in aodv.h

friend class MyTimer;

4)In the AODV.cc you must define the functionality of MyTimer. In the handle function write the code that you want to execute at timer expiration :

void MovePacketToSendingBufferTimer::handle(Event* p) {

}

5) In the AODV.cc find the constructor of AODV class and call the constructor of the MyTimer class :

AODV::AODV(nsaddr_t id) : Agent(PT_AODV),
          mtimer(this){
            ///body of constructor
}

6)Now Your timer is ready to use. Based on the timer usage you can call the handler at any arbitrary place of your code with this line of code :

Scheduler::instance().schedule(&mtimer, new Event(), 0.5);

with execution of above line handler of MyTimer class will execute at 0.5 second after this is a useful example of ns2 timers :ns2-timers



标签: timer adhoc ns2