Thu, 25 Dec 2014 10:48:06 +0000
Initial commit. Tortoises are fun.
0
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | local timer = require "util.timer"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local setmetatable = setmetatable; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local os_time = os.time; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | module "watchdog" |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | local watchdog_methods = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | local watchdog_mt = { __index = watchdog_methods }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | function new(timeout, callback) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local watchdog = setmetatable({ timeout = timeout, last_reset = os_time(), callback = callback }, watchdog_mt); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | timer.add_task(timeout+1, function (current_time) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | local last_reset = watchdog.last_reset; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | if not last_reset then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | return; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | local time_left = (last_reset + timeout) - current_time; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | if time_left < 0 then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | return watchdog:callback(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | return time_left + 1; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | end); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | return watchdog; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | function watchdog_methods:reset() |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | self.last_reset = os_time(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | function watchdog_methods:cancel() |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | self.last_reset = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | return _M; |