[Ns-developers] Timer API problem

Gustavo Carneiro gjcarneiro at gmail.com
Mon May 19 10:20:29 PDT 2008


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;

-- 
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