Software /
code /
verse
Annotate
init.lua @ 124:3659ba3b8dfa
verse: Log error when connection fails instantly
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 12 Sep 2010 17:49:01 +0100 |
parent | 98:1dccff7df2d5 |
child | 125:b46921de1e03 |
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; | |
13 | |
14 local stream = {}; | |
15 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
16 stream_mt = stream; |
0 | 17 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
18 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
19 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 t:set_logger(logger, true); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
24 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
|
25 return t; |
0 | 26 end |
27 | |
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
|
28 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
|
29 |
0 | 30 function verse.loop() |
31 return server.loop(); | |
32 end | |
33 | |
45
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
34 function verse.quit() |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
35 return server.setquitting(true); |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
36 end |
50a2e4fb0a16
verse: Add verse.quit() to exit the event loop
Matthew Wild <mwild1@gmail.com>
parents:
44
diff
changeset
|
37 |
44
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
38 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
|
39 |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
40 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
|
41 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
|
42 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
43 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
44 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
|
45 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
46 connect_port = tonumber(connect_port) or 5222; |
0 | 47 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
49 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
51 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
|
52 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 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
|
54 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
|
55 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
57 |
59
829a0a495dd1
verse: Remove some useless comments
Matthew Wild <mwild1@gmail.com>
parents:
55
diff
changeset
|
58 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
0 | 59 if not conn then |
124
3659ba3b8dfa
verse: Log error when connection fails instantly
Matthew Wild <mwild1@gmail.com>
parents:
98
diff
changeset
|
60 self:warn("connection initialisation failed: %s", err); |
95
d67ad403ca55
verse: Return false rather than nil from connect() on error, for consistency
Matthew Wild <mwild1@gmail.com>
parents:
94
diff
changeset
|
61 return false, err; |
0 | 62 end |
63 | |
64 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
65 local w, t = conn.write, tostring; |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
66 self.send = function (_, data) return w(conn, t(data)); end |
96
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
67 return true; |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
68 end |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
69 |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
70 function stream:close() |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
71 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
|
72 self.conn:close(); |
f0aec10023aa
verse: Return true from connect() on success, add stream:close()
Matthew Wild <mwild1@gmail.com>
parents:
95
diff
changeset
|
73 on_disconnect(conn, reason); |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
74 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
75 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
76 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
81 end |
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 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
|
84 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
|
85 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
|
86 end |
0 | 87 end |
88 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
89 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
|
90 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
|
91 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
|
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 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
94 |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
95 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
|
96 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
|
97 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
|
98 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
|
99 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
104 self.log = {}; |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
105 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
|
106 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
|
107 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
108 end |
37396504de5f
verse: Multiple changes to allow controlling logging for both verse and streams
Matthew Wild <mwild1@gmail.com>
parents:
42
diff
changeset
|
109 return old_logger; |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
110 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
111 |
94
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
112 function stream_mt:set_log_levels(levels) |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
113 self:set_logger(nil, levels); |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
114 end |
b40465267fb5
verse: Add stream:set_loglevels()
Matthew Wild <mwild1@gmail.com>
parents:
64
diff
changeset
|
115 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
116 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
117 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
118 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
|
119 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
120 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
121 |
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
|
122 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
|
123 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
|
124 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
125 |
53
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
126 function stream:unhook(name, handler) |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
127 return self.events.remove_handler(name, handler); |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
128 end |
091ff10eb51c
verse: Add stream:unhook(event_name, handler)
Matthew Wild <mwild1@gmail.com>
parents:
45
diff
changeset
|
129 |
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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
140 function stream:add_plugin(name) |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
141 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
142 local ok, err = verse.plugins[name](self); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
143 if ok then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
144 self:debug("Loaded %s plugin", name); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
145 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
146 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
|
147 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
148 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
149 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
150 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
151 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
152 -- Listener factory |
0 | 153 function new_listener(stream) |
154 local conn_listener = {}; | |
155 | |
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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
162 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
|
163 stream:event("incoming-raw", data); |
0 | 164 end |
165 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
166 function conn_listener.ondisconnect(conn, err) |
0 | 167 stream.connected = false; |
168 stream:event("disconnected", { reason = err }); | |
169 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
|
170 |
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
|
171 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
|
172 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
|
173 end |
0 | 174 |
60
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
175 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
|
176 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
|
177 end |
1f47ddab3499
verse: Fire "status" event for connection status changes (notably SSL handshake complete)
Matthew Wild <mwild1@gmail.com>
parents:
59
diff
changeset
|
178 |
0 | 179 return conn_listener; |
180 end | |
181 | |
182 | |
183 local log = require "util.logger".init("verse"); | |
184 | |
185 return verse; |