Software /
code /
verse
Annotate
init.lua @ 236:d75a209e57fc
verse: verse.logger() -> verse.new_logger()
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 27 Nov 2011 22:01:59 +0000 |
parent | 234:a597da99810c |
child | 237:d158c97556b4 |
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"; |
0 | 10 |
11 module("verse", package.seeall); | |
12 local verse = _M; | |
205
cbcd45be11a8
verse: Expose net.server as verse.server
Matthew Wild <mwild1@gmail.com>
parents:
173
diff
changeset
|
13 _M.server = server; |
0 | 14 |
15 local stream = {}; | |
16 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
17 stream_mt = stream; |
0 | 18 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
19 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
20 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
21 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
|
22 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
|
23 t.id = tostring(t):match("%x*$"); |
234
a597da99810c
verse: Connections use global Verse logger by default
Matthew Wild <mwild1@gmail.com>
parents:
205
diff
changeset
|
24 t:set_logger(logger or verse.log, true); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
25 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
|
26 t.plugins = {}; |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
27 return t; |
0 | 28 end |
29 | |
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
|
30 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
|
31 |
236
d75a209e57fc
verse: verse.logger() -> verse.new_logger()
Matthew Wild <mwild1@gmail.com>
parents:
234
diff
changeset
|
32 verse.logger = logger.init; -- Deprecated |
d75a209e57fc
verse: verse.logger() -> verse.new_logger()
Matthew Wild <mwild1@gmail.com>
parents:
234
diff
changeset
|
33 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
|
34 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
|
35 |
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
|
36 function verse.set_logger(logger) |
141
15706f6492ae
verse: Fix set_logger() to set verse.log() correctly
Matthew Wild <mwild1@gmail.com>
parents:
135
diff
changeset
|
37 verse.log = logger; |
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
|
38 server.setlogger(logger); |
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 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
|
40 |
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
|
41 function verse.filter_log(levels, logger) |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
42 local level_set = {}; |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
43 for _, level in ipairs(levels) do |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
44 level_set[level] = true; |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
45 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
|
46 return function (level, name, ...) |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
47 if level_set[level] then |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
48 return logger(level, name, ...); |
ea753bc81a33
verse: Add verse.filter_log(levels, logger) to filter a logger for certain levels
Matthew Wild <mwild1@gmail.com>
parents:
141
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 |
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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 |
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
|
58 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
|
59 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
|
60 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
|
61 |
0 | 62 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
|
63 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
|
64 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
|
65 |
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
|
66 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
|
67 return xpcall(server.step, error_handler); |
0 | 68 end |
69 | |
45
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
70 function verse.quit() |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
71 return server.setquitting(true); |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
72 end |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
73 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
74 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
|
75 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
76 connect_port = tonumber(connect_port) or 5222; |
0 | 77 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
78 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
79 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
80 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
81 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
|
82 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
87 |
59
829a0a495dd1
verse: Remove some useless comments
Matthew Wild <mwild1@gmail.com>
parents:
55
diff
changeset
|
88 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
0 | 89 if not conn then |
124
3659ba3b8dfa
verse: Log error when connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
90 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
|
91 return self:event("disconnected", { reason = err }) or false, err; |
0 | 92 end |
93 | |
94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 end; |
96
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
101 return true; |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
102 end |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
103 |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 return; |
b38397163737
verse: Log error and return when trying to close a closed connection
Matthew Wild <mwild1@gmail.com>
parents:
126
diff
changeset
|
108 end |
96
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
109 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
|
110 self.conn:close(); |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
111 on_disconnect(conn, reason); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
112 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
113 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
114 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
119 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
120 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
121 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
|
122 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
|
123 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
|
124 end |
0 | 125 end |
126 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
131 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
132 |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 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
|
137 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
142 self.log = {}; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
143 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
|
144 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
|
145 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
146 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
147 return old_logger; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
148 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
149 |
94
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
150 function stream_mt:set_log_levels(levels) |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
151 self:set_logger(nil, levels); |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
152 end |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
153 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
154 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
155 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
156 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
|
157 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
158 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
159 |
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
|
160 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
|
161 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
|
162 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
163 |
53
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
164 function stream:unhook(name, handler) |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
165 return self.events.remove_handler(name, handler); |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
166 end |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
167 |
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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 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
|
177 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
178 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
|
179 if self.plugins[name] then return true; end |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
180 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
181 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
|
182 if ok ~= false then |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
183 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
|
184 self.plugins[name] = true; |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
185 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
186 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
|
187 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
188 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
189 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
190 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
191 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
192 -- Listener factory |
0 | 193 function new_listener(stream) |
194 local conn_listener = {}; | |
195 | |
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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
201 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
|
202 stream:event("incoming-raw", data); |
0 | 203 end |
204 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
205 function conn_listener.ondisconnect(conn, err) |
0 | 206 stream.connected = false; |
207 stream:event("disconnected", { reason = err }); | |
208 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
|
209 |
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
|
210 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
|
211 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
|
212 end |
0 | 213 |
60
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
214 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
|
215 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
|
216 end |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
217 |
0 | 218 return conn_listener; |
219 end | |
220 | |
221 | |
222 local log = require "util.logger".init("verse"); | |
223 | |
224 return verse; |