Fri, 24 Oct 2008 01:06:54 +0500
Moved some code and removed unnecessary checks.
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local server_add = require "net.server".add; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local log = require "util.logger".init("connlisteners"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | local dofile, pcall, error = |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | dofile, pcall, error |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | module "connlisteners" |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local listeners = {}; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | function register(name, listener) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | if listeners[name] and listeners[name] ~= listener then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | 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
|
15 | return false; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | listeners[name] = listener; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | log("info", "Registered connection listener %s", name); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | return true; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | function deregister(name) |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | listeners[name] = nil; |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | end |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | |
127 | 26 | function get(name) |
27 | local h = listeners[name]; | |
99
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | if not h then |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | pcall(dofile, "net/"..name:gsub("[^%w%-]", "_").."_listener.lua"); |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | h = listeners[name]; |
127 | 31 | |
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, |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | 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), |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | udata.interface or "*", udata.mode or h.default_mode or 1, udata.ssl ); |
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 | |
ba08b8a4eeef
Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | return _M; |