Annotate

net/connlisteners.lua @ 416:7cc2d8a8ae97

Added util-src/hashes.c - support for sha1, sha256 and md5 hashes
author Waqas Hussain <waqas20@gmail.com>
date Wed, 26 Nov 2008 01:46:16 +0500
parent 380:2b22b8eee939
child 467:66f145f5c932
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
26 function get(name)
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
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
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
31 end
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
32 return h;
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
33 end
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
34
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
35 function start(name, udata)
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
36 local h = get(name);
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
37 if not h then
93f3c6b94c75 Initial s2s stuff
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
38 error("No such connection module: "..name, 0);
99
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return server_add(h,
380
2b22b8eee939 Small fix for connlisteners to accept nil for userdata
Matthew Wild <mwild1@gmail.com>
parents: 145
diff changeset
41 (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
42 (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
43 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return _M;