[Ns-developers] Timer API problem

Gustavo Carneiro gjcarneiro at gmail.com
Wed May 21 14:25:48 PDT 2008


2008/5/21 Mathieu Lacage <mathieu.lacage at sophia.inria.fr>:

> On Mon, 2008-05-19 at 18:20 +0100, Gustavo Carneiro wrote:
> > Just for the kicks (actually hoping to make simpler code), I decided
> > to use
> > Timer for a small periodic callback:
> >
> > void LoadReset ()
> > {
> >   std::cout << " load=" << (g_busyTime/(g_idleTime +
> > g_busyTime)).GetDouble
> > () << std::endl;
> >   g_idleTime = g_busyTime = Seconds (0);
> > }
> >
> > main()
> > {
> >   Timer loadResetTimer;
> >   loadResetTimer.SetFunction (LoadReset);
> >   loadResetTimer.SetDelay (Seconds (1));
> >   loadResetTimer.Schedule ();
> > }
> >
> > Problem is that the timer callback is only invoked once.  That kind of
> > kills
> > the purpose.  I hadn't noticed this problem in OlsrAgentImpl because
> > it's a
> > slightly different scenario (timer schedules are triggered by network
> > events), and because the class has access to all timers as members of
> > the
> > class.
> >
> > To solve this API usability issue I can offer three alternatives (not
> > necessarily mutually exclusive):
> >
> > 1. the callback returns true to signal that it wants to be called
> > again:
> > 2. the callback receives the timer object as first parameter and calls
> > Schedule () on it;
> > 3. the timer setup code adds a TIMER_REPEAT flag to make the callback
> > re-schedule automatically;
>
> Another option would be something quite similar to 2 which would not
> require API changes:
>
> void
> MyFunction (Timer *timer)
> {
>  timer->Schedule ();
> }
>
> Timer timer;
> timer.SetFunction (&MyFunction);
> timer.SetDelay (...);
> timer.SetArguments (&timer);
>

That works for me.  I was hoping for something more convenient, but this is
acceptable I guess.

Thanks.

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert


More information about the Ns-developers mailing list