Annotate

net/connlisteners.lua @ 378:47fdbd074641

Fix dns for poor Windows users, who have never seen a resolv.conf
author Matthew Wild <mwild1@gmail.com>
date Sun, 23 Nov 2008 02:50:57 +0000
parent 145:fbb3a4ff9cf1
child 380:2b22b8eee939
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,
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 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
42 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
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;