Annotate

net/server.lua @ 2968:fdd049a4e551

Merge 0.6->0.7
author Matthew Wild <mwild1@gmail.com>
date Fri, 09 Apr 2010 12:31:51 +0100
parent 2925:692b3c6c5bd2
parent 2967:c39ad633b26d
child 3461:447634564356
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2925
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
1 -- Prosody IM
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
4 --
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
6 -- COPYING file in the source package for more information.
692b3c6c5bd2 Merge 0.6->0.7
Matthew Wild <mwild1@gmail.com>
parents: 2645 2923
diff changeset
7 --
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
8
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local use_luaevent = require "core.configmanager".get("*", "core", "use_libevent");
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
11 if use_luaevent then
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
12 use_luaevent = pcall(require, "luaevent.core");
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
13 if not use_luaevent then
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
14 log("error", "libevent not found, falling back to select()");
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
15 end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
16 end
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
17
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 local server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
2162
22b6b1899a55 net.server: Log an error when libevent is requested, but luaevent is unavailable, and don't load luaevent when not requested.
Waqas Hussain <waqas20@gmail.com>
parents: 2136
diff changeset
20 if use_luaevent then
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 server = require "net.server_event";
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
22 -- util.timer requires "net.server", so instead of having
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
23 -- Lua look for, and load us again (causing a loop) - set this here
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
24 -- (usually it isn't set until we return, look down there...)
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 package.loaded["net.server"] = server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 -- Backwards compatibility for timers, addtimer
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 -- called a function roughly every second
2105
fecf33cb2913 net.server: Small fix for addtimer() compatibility code
Matthew Wild <mwild1@gmail.com>
parents: 2094
diff changeset
29 local add_task = require "util.timer".add_task;
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 function server.addtimer(f)
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return add_task(1, function (...) f(...); return 1; end);
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
2435
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
33
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
34 -- Overwrite signal.signal() because we need to ask libevent to
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
35 -- handle them instead
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
36 local ok, signal = pcall(require, "util.signal");
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
37 if ok and signal then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
38 local _signal_signal = signal.signal;
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
39 function signal.signal(signal_id, handler)
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
40 if type(signal_id) == "string" then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
41 signal_id = signal[signal_id:upper()];
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
42 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
43 if type(signal_id) ~= "number" then
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
44 return false, "invalid-signal";
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
45 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
46 return server.hook_signal(signal_id, handler);
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
47 end
1ab73691b58e net.server: When libevent backend is in use, override signal.signal to use server.hook_signal
Matthew Wild <mwild1@gmail.com>
parents: 2281
diff changeset
48 end
2094
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 else
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 server = require "net.server_select";
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 package.loaded["net.server"] = server;
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end
c69cb5c171e0 net.server: New net.server to choose the appropriate library from server_select/server_event based on the availability of luaevent and the use_libevent config option
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
2136
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
54 -- require "net.server" shall now forever return this,
23c687039652 net.server: Add some comments to explain to waqas how it all works :)
Matthew Wild <mwild1@gmail.com>
parents: 2105
diff changeset
55 -- ie. server_select or server_event as chosen above.
2645
625d02b2a1a0 net.server: Remove redundant commented code and some trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2435
diff changeset
56 return server;