Annotate

util/timer.lua @ 2883:adbca5af8536

mod_compression: Don't tell client compression succeeded when it didn't.
author Waqas Hussain <waqas20@gmail.com>
date Tue, 09 Mar 2010 17:52:18 +0500
parent 1871:838d1317bca4
child 2095:3256c5d00901
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 -- Prosody IM
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 --
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7 --
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local ns_addtimer = require "net.server".addtimer;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local get_time = os.time;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 local t_insert = table.insert;
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
13 local t_remove = table.remove;
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
14 local ipairs, pairs = ipairs, pairs;
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local type = type;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 local data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 local new_data = {};
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 module "timer"
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 local function _add_task(delay, func)
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local current_time = get_time();
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 delay = delay + current_time;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 if delay >= current_time then
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 t_insert(new_data, {delay, func});
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 else func(); end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 add_task = _add_task;
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 ns_addtimer(function()
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 local current_time = get_time();
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
34 if #new_data > 0 then
842
4932f94d35c1 util.timer: More small fixes I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 841
diff changeset
35 for _, d in pairs(new_data) do
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
36 t_insert(data, d);
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
37 end
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
38 new_data = {};
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 end
841
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
40
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
41 for i, d in pairs(data) do
77ff000c2055 util.timer: Fix crash when loaded but no tasks set, fix skipping some tasks when multiple set, and one removed
Matthew Wild <mwild1@gmail.com>
parents: 832
diff changeset
42 local t, func = d[1], d[2];
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 if t <= current_time then
842
4932f94d35c1 util.timer: More small fixes I forgot to commit
Matthew Wild <mwild1@gmail.com>
parents: 841
diff changeset
44 data[i] = nil;
1871
838d1317bca4 util.timer: Pass current_time to timer callbacks
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
45 local r = func(current_time);
832
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 if type(r) == "number" then _add_task(r, func); end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 end
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end);
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
282ae70db19f Added util/timer.lua - a timer API
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 return _M;