Annotate

net/connlisteners.lua @ 99:ba08b8a4eeef

Abstract connections with "connection listeners" - Added connlistener for xmppclient - SASL/TLS now use a new session:reset_stream() method - main.lua on its way to being a bit neater
author Matthew Wild <mwild1@gmail.com>
date Wed, 22 Oct 2008 17:36:21 +0100
child 127:93f3c6b94c75
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
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 function start(name, udata)
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local h = listeners[name]
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];
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not h then
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 error("No such connection module: "..name, 0);
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return server_add(h,
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 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
37 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
38 end
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
ba08b8a4eeef Abstract connections with "connection listeners"
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return _M;