WvStreams
wvtimestream.cc
1 /*
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4  *
5  * See wvtimestream.h.
6  */
7 #include "wvtimestream.h"
8 
9 WvTimeStream::WvTimeStream():
10  last(wvtime_zero), next(wvtime_zero), ms_per_tick(0)
11 {
12 }
13 
14 
15 void WvTimeStream::set_timer(time_t msec)
16 {
17  WvTime now = wvstime();
18 
19  ms_per_tick = msec > 0 ? msec : 0;
20  next = msecadd(now, ms_per_tick);
21  last = now;
22 }
23 
24 
25 bool WvTimeStream::isok() const
26 {
27  return true;
28 }
29 
30 
32 {
33  WvTime now;
34  time_t diff;
36 
37  //fprintf(stderr, "%p: timestream pre_select mspt=%ld msto=%ld\n",
38  // this, ms_per_tick, si.msec_timeout);
39 
40  if (ms_per_tick)
41  {
42  now = wvstime();
43 
44  /* Are we going back in time? If so, adjust the due time. */
45  if (now < last)
46  next = tvdiff(next, tvdiff(last, now));
47 
48  last = now;
49 
50  if (next <= now)
51  {
52  si.msec_timeout = 0;
53  return;
54  }
55 
56  diff = msecdiff(next, now);
57  diff = diff < 0 ? 0 : diff;
58  if (diff < si.msec_timeout || si.msec_timeout < 0)
59  si.msec_timeout = diff;
60  }
61 }
62 
63 
65 {
66  WvTime now = wvstime();
67 
68  return WvStream::post_select(si) || (ms_per_tick && next <= now);
69 }
70 
71 
73 {
75 
76  /* Schedule our next timer event, unless alarm_is_ticking, which
77  * would mean that we're here because someone used alarm() rather
78  * than because our timer expired. */
79  if (!alarm_was_ticking)
80  {
81  WvTime now = wvstime();
82 
83  next = msecadd(next, ms_per_tick);
84 
85  if (msecdiff(next, now) > ms_per_tick * 100 ||
86  msecdiff(now, next) > ms_per_tick * 100)
87  {
88  // reset if we fall forward or behind WAY too excessively
89  // This is usually due to a change in system time
90  last = now;
91  next = msecadd(last, ms_per_tick);
92  }
93  else if (msecdiff(next, now) > ms_per_tick * 10)
94  // compensate if we fall behind too excessively
95  next = msecadd(now, ms_per_tick);
96  }
97 }
WvStream::execute
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
Definition: wvstream.h:652
WvTimeStream::pre_select
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling ::select().
Definition: wvtimestream.cc:31
WvStream::alarm_was_ticking
bool alarm_was_ticking
This will be true during callback execution if the callback was triggered by the alarm going off.
Definition: wvstream.h:54
WvTime
Based on (and interchangeable with) struct timeval.
Definition: wvtimeutils.h:17
WvTimeStream::post_select
virtual bool post_select(SelectInfo &si)
post_select() is called after ::select(), and returns true if this object is now ready.
Definition: wvtimestream.cc:64
WvTimeStream::set_timer
void set_timer(time_t msec)
Every 'msec' milliseconds, select() will return true on this stream.
Definition: wvtimestream.cc:15
IWvStream::SelectInfo
the data structure used by pre_select()/post_select() and internally by select().
Definition: iwvstream.h:50
WvStream::post_select
virtual bool post_select(SelectInfo &si)
post_select() is called after ::select(), and returns true if this object is now ready.
Definition: wvstream.cc:875
WvTimeStream::isok
virtual bool isok() const
return true if the stream is actually usable right now
Definition: wvtimestream.cc:25
WvStream::pre_select
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling ::select().
Definition: wvstream.cc:844
WvTimeStream::execute
virtual void execute()
The callback() function calls execute(), and then calls the user- specified callback if one is define...
Definition: wvtimestream.cc:72