Software /
code /
verse
Annotate
init.lua @ 247:7c58c16efa3e
verse: Remove silly logger line I just noticed at the end of the file
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 30 Nov 2011 18:01:53 +0000 |
parent | 242:ab4773b0ef5e |
child | 257:816b8ccb3082 |
child | 258:94b1d9863dd9 |
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 |
64
a28540d4117a
verse: Load LuaSec if possible
Matthew Wild <mwild1@gmail.com>
parents:
60
diff
changeset
|
5 -- Load LuaSec if available |
a28540d4117a
verse: Load LuaSec if possible
Matthew Wild <mwild1@gmail.com>
parents:
60
diff
changeset
|
6 pcall(require, "ssl"); |
a28540d4117a
verse: Load LuaSec if possible
Matthew Wild <mwild1@gmail.com>
parents:
60
diff
changeset
|
7 |
30
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
8 local server = require "net.server"; |
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
9 local events = require "util.events"; |
237
d158c97556b4
verse: require('util.logger') (besides being correct, also fixes a silly bug when loading verse twice)
Matthew Wild <mwild1@gmail.com>
parents:
236
diff
changeset
|
10 local logger = require "util.logger"; |
0 | 11 |
12 module("verse", package.seeall); | |
13 local verse = _M; | |
205
cbcd45be11a8
verse: Expose net.server as verse.server
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
14 _M.server = server; |
0 | 15 |
16 local stream = {}; | |
17 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
18 stream_mt = stream; |
0 | 19 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
20 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
21 |
239
65a0d3fcbbad
verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents:
238
diff
changeset
|
22 local max_id = 0; |
65a0d3fcbbad
verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents:
238
diff
changeset
|
23 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
24 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
|
25 local t = setmetatable(base or {}, stream); |
239
65a0d3fcbbad
verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents:
238
diff
changeset
|
26 max_id = max_id + 1; |
65a0d3fcbbad
verse: Switch connection ids to be a simple incrementing integer
Matthew Wild <mwild1@gmail.com>
parents:
238
diff
changeset
|
27 t.id = tostring(max_id); |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
28 t.logger = logger or verse.new_logger("stream"..t.id); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
29 t.events = events.new(); |
173
3112a9e450ac
verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
30 t.plugins = {}; |
247
7c58c16efa3e
verse: Remove silly logger line I just noticed at the end of the file
Matthew Wild <mwild1@gmail.com>
parents:
242
diff
changeset
|
31 t.verse = verse; |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
32 return t; |
0 | 33 end |
34 | |
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
|
35 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
|
36 |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
37 verse.logger = logger.init; -- COMPAT: Deprecated |
236
d75a209e57fc
verse: verse.logger() -> verse.new_logger()
Matthew Wild <mwild1@gmail.com>
parents:
234
diff
changeset
|
38 verse.new_logger = logger.init; |
125
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
39 verse.log = verse.logger("verse"); |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
40 |
241
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
41 local function format(format, ...) |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
42 local n, arg, maxn = 0, { ... }, select('#', ...); |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
43 return (format:gsub("%%(.)", function (c) if n <= maxn then n = n + 1; return tostring(arg[n]); end end)); |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
44 end |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
45 |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
46 function verse.set_log_handler(log_handler, levels) |
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
47 levels = levels or { "debug", "info", "warn", "error" }; |
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
48 logger.reset(); |
241
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
49 local function _log_handler(name, level, message, ...) |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
50 return log_handler(name, level, format(message, ...)); |
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
51 end |
240
e27bf761bea6
verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents:
239
diff
changeset
|
52 if log_handler then |
e27bf761bea6
verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents:
239
diff
changeset
|
53 for i, level in ipairs(levels) do |
241
931bf313c691
verse: Format messages before passing them to custom log handler
Matthew Wild <mwild1@gmail.com>
parents:
240
diff
changeset
|
54 logger.add_level_sink(level, _log_handler); |
240
e27bf761bea6
verse: set_log_handler(): Allow nil to be passed for log_handler to disable logging (the default).
Matthew Wild <mwild1@gmail.com>
parents:
239
diff
changeset
|
55 end |
142
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
56 end |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
57 end |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
58 |
242
ab4773b0ef5e
verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents:
241
diff
changeset
|
59 function _default_log_handler(name, level, message) |
ab4773b0ef5e
verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents:
241
diff
changeset
|
60 return io.stderr:write(name, "\t", level, "\t", message, "\n"); |
ab4773b0ef5e
verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents:
241
diff
changeset
|
61 end |
ab4773b0ef5e
verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents:
241
diff
changeset
|
62 verse.set_log_handler(_default_log_handler, { "error" }); |
ab4773b0ef5e
verse: Add default log handler for errors
Matthew Wild <mwild1@gmail.com>
parents:
241
diff
changeset
|
63 |
125
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
64 local function error_handler(err) |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
65 verse.log("error", "Error: %s", err); |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
66 verse.log("error", "Traceback: %s", debug.traceback()); |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
67 end |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
68 |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
69 function verse.set_error_handler(new_error_handler) |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
70 error_handler = new_error_handler; |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
71 end |
b46921de1e03
verse: Add verse.log() to log a message using the default logger. Also add verse.set_error_handler() to, er, set an error handler.
Matthew Wild <mwild1@gmail.com>
parents:
124
diff
changeset
|
72 |
0 | 73 function verse.loop() |
126
fa3ddadb8364
verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents:
125
diff
changeset
|
74 return xpcall(server.loop, error_handler); |
fa3ddadb8364
verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents:
125
diff
changeset
|
75 end |
fa3ddadb8364
verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents:
125
diff
changeset
|
76 |
fa3ddadb8364
verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents:
125
diff
changeset
|
77 function verse.step() |
fa3ddadb8364
verse: verse.loop() and new verse.step() use the new error handler to, er, handle errors.
Matthew Wild <mwild1@gmail.com>
parents:
125
diff
changeset
|
78 return xpcall(server.step, error_handler); |
0 | 79 end |
80 | |
45
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
81 function verse.quit() |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
82 return server.setquitting(true); |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
83 end |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
84 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
85 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
|
86 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
87 connect_port = tonumber(connect_port) or 5222; |
0 | 88 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
89 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
90 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
91 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
92 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
|
93 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
94 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
|
95 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); |
135
8adf5fb27ce3
verse: Fire disconnected event even if connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents:
132
diff
changeset
|
96 return self:event("disconnected", { reason = err }) or false, err; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
97 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
98 |
59
829a0a495dd1
verse: Remove some useless comments
Matthew Wild <mwild1@gmail.com>
parents:
55
diff
changeset
|
99 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
0 | 100 if not conn then |
124
3659ba3b8dfa
verse: Log error when connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
101 self:warn("connection initialisation failed: %s", err); |
135
8adf5fb27ce3
verse: Fire disconnected event even if connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents:
132
diff
changeset
|
102 return self:event("disconnected", { reason = err }) or false, err; |
0 | 103 end |
104 | |
105 self.conn = conn; | |
163
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
106 self.send = function (stream, data) |
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
107 self:event("outgoing", data); |
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
108 data = tostring(data); |
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
109 self:event("outgoing-raw", data); |
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
110 return conn:write(data); |
48a47bca9a63
verse: Change to set conn:send in only one place, and fire new events 'outgoing' and 'outgoing-raw'
Matthew Wild <mwild1@gmail.com>
parents:
162
diff
changeset
|
111 end; |
96
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
112 return true; |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
113 end |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
114 |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
115 function stream:close() |
132
b38397163737
verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents:
126
diff
changeset
|
116 if not self.conn then |
b38397163737
verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents:
126
diff
changeset
|
117 verse.log("error", "Attempt to close disconnected connection - possibly a bug"); |
b38397163737
verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents:
126
diff
changeset
|
118 return; |
b38397163737
verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents:
126
diff
changeset
|
119 end |
96
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
120 local on_disconnect = self.conn.disconnect(); |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
121 self.conn:close(); |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
122 on_disconnect(conn, reason); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
123 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
124 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
125 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
126 function stream:debug(...) |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
127 return self.logger("debug", ...); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
128 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
129 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
130 function stream:warn(...) |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
131 return self.logger("warn", ...); |
0 | 132 end |
133 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
134 function stream:error(...) |
238
9b6fca5d9a02
verse, squishy: Port to Prosody's util.logger. Adds new function, verse.set_log_handler(handler, levels).
Matthew Wild <mwild1@gmail.com>
parents:
237
diff
changeset
|
135 return self.logger("error", ...); |
94
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
136 end |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
137 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
138 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
139 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
140 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
|
141 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
142 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
143 |
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
|
144 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
|
145 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
|
146 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
147 |
53
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
148 function stream:unhook(name, handler) |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
149 return self.events.remove_handler(name, handler); |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
150 end |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
151 |
98
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
152 function verse.eventable(object) |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
153 object.events = events.new(); |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
154 object.hook, object.unhook = stream.hook, stream.unhook; |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
155 local fire_event = object.events.fire_event; |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
156 function object:event(name, ...) |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
157 return fire_event(name, ...); |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
158 end |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
159 return object; |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
160 end |
1dccff7df2d5
verse: Add verse.eventable() to give a passed object :event() and :hook()/:unhook() methods and return it again
Matthew Wild <mwild1@gmail.com>
parents:
96
diff
changeset
|
161 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
162 function stream:add_plugin(name) |
173
3112a9e450ac
verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
163 if self.plugins[name] then return true; end |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
164 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
165 local ok, err = verse.plugins[name](self); |
162
2950079d4319
verse: Consider a plugin as failed to load only if it returns false
Matthew Wild <mwild1@gmail.com>
parents:
142
diff
changeset
|
166 if ok ~= false then |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
167 self:debug("Loaded %s plugin", name); |
173
3112a9e450ac
verse: Make add_plugin only load a plugin once per stream
Matthew Wild <mwild1@gmail.com>
parents:
163
diff
changeset
|
168 self.plugins[name] = true; |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
169 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
170 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
|
171 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
172 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
173 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
174 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
175 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
176 -- Listener factory |
0 | 177 function new_listener(stream) |
178 local conn_listener = {}; | |
179 | |
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
|
180 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
|
181 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
|
182 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
|
183 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
|
184 |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
185 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
|
186 stream:event("incoming-raw", data); |
0 | 187 end |
188 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
189 function conn_listener.ondisconnect(conn, err) |
0 | 190 stream.connected = false; |
191 stream:event("disconnected", { reason = err }); | |
192 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
|
193 |
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
|
194 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
|
195 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
|
196 end |
0 | 197 |
60
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
198 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
|
199 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
|
200 end |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
201 |
0 | 202 return conn_listener; |
203 end | |
204 | |
205 return verse; |