[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ns] timer




I tried the example in ns manual - "Creating a new agent" in chapter 10,
where an echo agent periodically sends out a packet. I tried the way which
is shown in the manual, but I got segmentation fault in execution. It
seems that the error occurred at proxy_timer_->resched(...).

Did any of you have similar problem? I really appreciate if you could take
some time to see what could be the problem. (the main code is attached
below). The version I'm using is 2.1b7a.

Thanks a lot,

Jingwen

--------------------------------
class ProxyTimer;

class ProxyAgent : public Agent {
 public:
  ProxyAgent();
  virtual int command(int argc, const char*const* argv);
  void timeout(int);
 protected:
  void sendit();
  ProxyTimer *proxy_timer_;
};

class ProxyTimer : public TimerHandler {
 public:
  ProxyTimer(ProxyAgent *a) : TimerHandler() {a_=a;}
 protected:
  virtual void expire(Event *e);
  ProxyAgent *a_;
};
--------------------------------

int ProxyAgent::command(int argc, const char*const* argv)
{
  if (argc == 2) {
    if (strcmp(argv[1], "initialize") == 0) {
      timeout(0);
      return(TCL_OK);
    }
  }
  return (Agent::command(argc, argv));
}

void ProxyAgent::timeout(int){
  sendit();
  proxy_timer_->resched(2);
}

void ProxyAgent::sendit(){
  Packet* p = allocpkt();
  hdr_proxy *ph = hdr_proxy::access(p);
  strcpy(ph->message, "hello");
  send(p,0);
}

void ProxyTimer::expire(Event *e){
  a_->timeout(0);
}

-------------------------------------