Software /
code /
verse
Annotate
init.lua @ 16:13444ae6e3c4
verse: Fix stream:hook() to pass additional parameters to the underlying hook(), so we don't strip priority
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 30 Nov 2009 15:53:44 +0000 |
parent | 4:0ef21511c7ff |
child | 17:ec6b0b94826c |
rev | line source |
---|---|
0 | 1 |
2 | |
3 local server = require "server"; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
4 local events = require "events"; |
0 | 5 |
6 module("verse", package.seeall); | |
7 local verse = _M; | |
8 | |
9 local stream = {}; | |
10 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
11 stream_mt = stream; |
0 | 12 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
13 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
14 |
0 | 15 function verse.new() |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
16 local t = {}; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
17 t.id = tostring(t):match("%x*$"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
18 t.logger = logger.init(t.id); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
19 t.events = events.new(); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
20 return setmetatable(t, stream); |
0 | 21 end |
22 | |
23 function verse.loop() | |
24 return server.loop(); | |
25 end | |
26 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
27 function stream:connect(connect_host, connect_port) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
28 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
29 connect_port = tonumber(connect_port) or 5222; |
0 | 30 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
31 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
32 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
33 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
34 local success, err = conn:connect(connect_host, connect_port); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
35 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
36 if not success and err ~= "timeout" then |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
37 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
38 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
39 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
40 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
41 --local conn, err = server.addclient(self.connect_host or self.host, tonumber(self.connect_port) or 5222, new_listener(self), "*a"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
42 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); --, hosts[from_host].ssl_ctx, false ); |
0 | 43 if not conn then |
44 return nil, err; | |
45 end | |
46 | |
47 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 local w, t = conn.write, tostring; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
49 self.send = function (_, data) return w(t(data)); end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
51 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
52 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 function stream:debug(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
54 return self.logger("debug", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
55 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
57 function stream:warn(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
58 return self.logger("warn", ...); |
0 | 59 end |
60 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
61 function stream:error(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
62 return self.logger("error", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
63 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
64 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
65 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
67 self:debug("Firing event: "..tostring(name)); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
68 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
69 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
70 |
16
13444ae6e3c4
verse: Fix stream:hook() to pass additional parameters to the underlying hook(), so we don't strip priority
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
71 function stream:hook(name, ...) |
13444ae6e3c4
verse: Fix stream:hook() to pass additional parameters to the underlying hook(), so we don't strip priority
Matthew Wild <mwild1@gmail.com>
parents:
4
diff
changeset
|
72 return self.events.add_handler(name, ...); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
73 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
74 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
75 function stream:add_plugin(name) |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
76 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
77 local ok, err = verse.plugins[name](self); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
78 if ok then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
79 self:debug("Loaded %s plugin", name); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
80 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
81 self:warn("Failed to load %s plugin: %s", name, err); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
82 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
83 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
84 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
85 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
86 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
87 -- Listener factory |
0 | 88 function new_listener(stream) |
89 local conn_listener = {}; | |
90 | |
91 function conn_listener.incoming(conn, data) | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
92 stream:debug("Data"); |
0 | 93 if not stream.connected then |
94 stream.connected = true; | |
95 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn.write(tostring(data)); end; | |
96 stream:event("connected"); | |
97 end | |
98 if data then | |
99 stream:event("incoming-raw", data); | |
100 end | |
101 end | |
102 | |
103 function conn_listener.disconnect(conn, err) | |
104 stream.connected = false; | |
105 stream:event("disconnected", { reason = err }); | |
106 end | |
107 | |
108 return conn_listener; | |
109 end | |
110 | |
111 | |
112 local log = require "util.logger".init("verse"); | |
113 | |
114 return verse; |