Software /
code /
verse
Annotate
init.lua @ 60:1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 11 May 2010 22:40:13 +0100 |
parent | 59:829a0a495dd1 |
child | 64:a28540d4117a |
rev | line source |
---|---|
0 | 1 |
17
ec6b0b94826c
verse: Include LuaRocks packages if we can
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
2 -- Use LuaRocks if available |
ec6b0b94826c
verse: Include LuaRocks packages if we can
Matthew Wild <mwild1@gmail.com>
parents:
16
diff
changeset
|
3 pcall(require, "luarocks.require"); |
0 | 4 |
30
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
5 local server = require "net.server"; |
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
6 local events = require "util.events"; |
0 | 7 |
8 module("verse", package.seeall); | |
9 local verse = _M; | |
10 | |
11 local stream = {}; | |
12 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
13 stream_mt = stream; |
0 | 14 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
15 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
16 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
17 function verse.new(logger, base) |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
18 local t = setmetatable(base or {}, stream); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
19 t.id = tostring(t):match("%x*$"); |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
20 t:set_logger(logger, true); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
21 t.events = events.new(); |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
22 return t; |
0 | 23 end |
24 | |
42
6006e6bb1c28
verse: Add verse.add_task(delay, callback) to add timer functions using util.timer
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
25 verse.add_task = require "util.timer".add_task; |
6006e6bb1c28
verse: Add verse.add_task(delay, callback) to add timer functions using util.timer
Matthew Wild <mwild1@gmail.com>
parents:
30
diff
changeset
|
26 |
0 | 27 function verse.loop() |
28 return server.loop(); | |
29 end | |
30 | |
45
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
31 function verse.quit() |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
32 return server.setquitting(true); |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
33 end |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
34 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
35 verse.logger = logger.init; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
36 |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
37 function verse.set_logger(logger) |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
38 server.setlogger(logger); |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
39 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
40 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
41 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
|
42 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
43 connect_port = tonumber(connect_port) or 5222; |
0 | 44 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
45 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
46 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
47 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 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
|
49 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 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
|
51 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
|
52 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
54 |
59
829a0a495dd1
verse: Remove some useless comments
Matthew Wild <mwild1@gmail.com>
parents:
55
diff
changeset
|
55 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
0 | 56 if not conn then |
57 return nil, err; | |
58 end | |
59 | |
60 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
61 local w, t = conn.write, tostring; |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
62 self.send = function (_, data) return w(conn, t(data)); end |
1
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 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 function stream:debug(...) |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
67 if self.logger and self.log.debug then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
68 return self.logger("debug", ...); |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
69 end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
70 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
71 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
72 function stream:warn(...) |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
73 if self.logger and self.log.warn then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
74 return self.logger("warn", ...); |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
75 end |
0 | 76 end |
77 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
78 function stream:error(...) |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
79 if self.logger and self.log.error then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
80 return self.logger("error", ...); |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
81 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
82 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
83 |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
84 function stream:set_logger(logger, levels) |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
85 local old_logger = self.logger; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
86 if logger then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
87 self.logger = logger; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
88 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
89 if levels then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
90 if levels == true then |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
91 levels = { "debug", "info", "warn", "error" }; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
92 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
93 self.log = {}; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
94 for _, level in ipairs(levels) do |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
95 self.log[level] = true; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
96 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
97 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
98 return old_logger; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
99 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
100 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
101 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
102 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
103 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
|
104 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
105 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
106 |
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
|
107 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
|
108 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
|
109 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
110 |
53
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
111 function stream:unhook(name, handler) |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
112 return self.events.remove_handler(name, handler); |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
113 end |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
114 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
115 function stream:add_plugin(name) |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
116 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
117 local ok, err = verse.plugins[name](self); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
118 if ok then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
119 self:debug("Loaded %s plugin", name); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
120 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
121 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
|
122 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
123 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
124 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
125 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
126 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
127 -- Listener factory |
0 | 128 function new_listener(stream) |
129 local conn_listener = {}; | |
130 | |
54
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
131 function conn_listener.onconnect(conn) |
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
132 stream.connected = true; |
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
133 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn:write(tostring(data)); end; |
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
134 stream:event("connected"); |
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
135 end |
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
136 |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
137 function conn_listener.onincoming(conn, data) |
54
1a2a3d598254
verse: Take advantage of server.lua's new onconnect callback for a more robust "connected" event
Matthew Wild <mwild1@gmail.com>
parents:
53
diff
changeset
|
138 stream:event("incoming-raw", data); |
0 | 139 end |
140 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
141 function conn_listener.ondisconnect(conn, err) |
0 | 142 stream.connected = false; |
143 stream:event("disconnected", { reason = err }); | |
144 end | |
55
163beb198646
verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
145 |
163beb198646
verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
146 function conn_listener.ondrain(conn) |
163beb198646
verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
147 stream:event("drained"); |
163beb198646
verse: Add "drained" event to signal when send buffer is empty (new server.lua feature)
Matthew Wild <mwild1@gmail.com>
parents:
54
diff
changeset
|
148 end |
0 | 149 |
60
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
150 function conn_listener.onstatus(conn, new_status) |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
151 stream:event("status", new_status); |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
152 end |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
153 |
0 | 154 return conn_listener; |
155 end | |
156 | |
157 | |
158 local log = require "util.logger".init("verse"); | |
159 | |
160 return verse; |