Sun, 30 Nov 2008 17:18:31 +0100
Automerge with waqas.
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | |
471
727d7bd97cd2
Fix for loading connlisteners when running without CFG_SOURCEDIR
Matthew Wild <mwild1@gmail.com>
parents:
467
diff
changeset
|
2 | local listeners_dir = (CFG_SOURCEDIR or ".").."/net/"; |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local server_add = require "net.server".add; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local log = require "util.logger".init("connlisteners"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local dofile, pcall, error = |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | dofile, pcall, error |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | module "connlisteners" |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local listeners = {}; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | function register(name, listener) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | if listeners[name] and listeners[name] ~= listener then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | log("warning", "Listener %s is already registered, not registering any more", name); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | return false; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | listeners[name] = listener; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | log("info", "Registered connection listener %s", name); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | return true; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | function deregister(name) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | listeners[name] = nil; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | |
127 | 27 | function get(name) |
28 | local h = listeners[name]; | |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | if not h then |
467
66f145f5c932
Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents:
380
diff
changeset
|
30 | pcall(dofile, listeners_dir..name:gsub("[^%w%-]", "_").."_listener.lua"); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | h = listeners[name]; |
127 | 32 | end |
33 | return h; | |
34 | end | |
35 | ||
36 | function start(name, udata) | |
37 | local h = get(name); | |
38 | if not h then | |
39 | error("No such connection module: "..name, 0); | |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | return server_add(h, |
380
2b22b8eee939
Small fix for connlisteners to accept nil for userdata
Matthew Wild <mwild1@gmail.com>
parents:
145
diff
changeset
|
42 | (udata and udata.port) or h.default_port or error("Can't start listener "..name.." because no port was specified, and it has no default port", 0), |
2b22b8eee939
Small fix for connlisteners to accept nil for userdata
Matthew Wild <mwild1@gmail.com>
parents:
145
diff
changeset
|
43 | (udata and udata.interface) or "*", (udata and udata.mode) or h.default_mode or 1, (udata and udata.ssl) or nil ); |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | |
467
66f145f5c932
Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents:
380
diff
changeset
|
46 | return _M; |