Software /
code /
verse
Comparison
init.lua @ 417:d46a502955c0
verse: Remove use of deprecated module() function
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 20 May 2018 01:16:22 +0200 |
parent | 411:db462d4feb44 |
child | 424:eaaaf4495e06 |
comparison
equal
deleted
inserted
replaced
416:92ce569b9b73 | 417:d46a502955c0 |
---|---|
9 | 9 |
10 local server = require "net.server"; | 10 local server = require "net.server"; |
11 local events = require "util.events"; | 11 local events = require "util.events"; |
12 local logger = require "util.logger"; | 12 local logger = require "util.logger"; |
13 | 13 |
14 module("verse", package.seeall); | 14 local verse = {}; |
15 local verse = _M; | 15 verse.server = server; |
16 _M.server = server; | |
17 | 16 |
18 local stream = {}; | 17 local stream = {}; |
19 stream.__index = stream; | 18 stream.__index = stream; |
20 stream_mt = stream; | 19 verse.stream_mt = stream; |
21 | 20 |
22 verse.plugins = {}; | 21 verse.plugins = {}; |
23 | 22 |
24 function verse.init(...) | 23 function verse.init(...) |
25 for i=1,select("#", ...) do | 24 for i=1,select("#", ...) do |
73 logger.add_level_sink(level, _log_handler); | 72 logger.add_level_sink(level, _log_handler); |
74 end | 73 end |
75 end | 74 end |
76 end | 75 end |
77 | 76 |
78 function _default_log_handler(name, level, message) | 77 function verse._default_log_handler(name, level, message) |
79 return io.stderr:write(name, "\t", level, "\t", message, "\n"); | 78 return io.stderr:write(name, "\t", level, "\t", message, "\n"); |
80 end | 79 end |
81 verse.set_log_handler(_default_log_handler, { "error" }); | 80 verse.set_log_handler(verse._default_log_handler, { "error" }); |
82 | 81 |
83 local function error_handler(err) | 82 local function error_handler(err) |
84 verse.log("error", "Error: %s", err); | 83 verse.log("error", "Error: %s", err); |
85 verse.log("error", "Traceback: %s", debug.traceback()); | 84 verse.log("error", "Traceback: %s", debug.traceback()); |
86 end | 85 end |
102 end | 101 end |
103 | 102 |
104 function stream:listen(host, port) | 103 function stream:listen(host, port) |
105 host = host or "localhost"; | 104 host = host or "localhost"; |
106 port = port or 0; | 105 port = port or 0; |
107 local conn, err = server.addserver(host, port, new_listener(self, "server"), "*a"); | 106 local conn, err = server.addserver(host, port, verse.new_listener(self, "server"), "*a"); |
108 if conn then | 107 if conn then |
109 self:debug("Bound to %s:%s", host, port); | 108 self:debug("Bound to %s:%s", host, port); |
110 self.server = conn; | 109 self.server = conn; |
111 end | 110 end |
112 return conn, err; | 111 return conn, err; |
125 if not success and err ~= "timeout" then | 124 if not success and err ~= "timeout" then |
126 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); | 125 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); |
127 return self:event("disconnected", { reason = err }) or false, err; | 126 return self:event("disconnected", { reason = err }) or false, err; |
128 end | 127 end |
129 | 128 |
130 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); | 129 local conn = server.wrapclient(conn, connect_host, connect_port, verse.new_listener(self), "*a"); |
131 if not conn then | 130 if not conn then |
132 self:warn("connection initialisation failed: %s", err); | 131 self:warn("connection initialisation failed: %s", err); |
133 return self:event("disconnected", { reason = err }) or false, err; | 132 return self:event("disconnected", { reason = err }) or false, err; |
134 end | 133 end |
135 self:set_conn(conn); | 134 self:set_conn(conn); |
210 end | 209 end |
211 return self; | 210 return self; |
212 end | 211 end |
213 | 212 |
214 -- Listener factory | 213 -- Listener factory |
215 function new_listener(stream) | 214 function verse.new_listener(stream) |
216 local conn_listener = {}; | 215 local conn_listener = {}; |
217 | 216 |
218 function conn_listener.onconnect(conn) | 217 function conn_listener.onconnect(conn) |
219 if stream.server then | 218 if stream.server then |
220 local client = verse.new(); | 219 local client = verse.new(); |
221 conn:setlistener(new_listener(client)); | 220 conn:setlistener(verse.new_listener(client)); |
222 client:set_conn(conn); | 221 client:set_conn(conn); |
223 stream:event("connected", { client = client }); | 222 stream:event("connected", { client = client }); |
224 else | 223 else |
225 stream.connected = true; | 224 stream.connected = true; |
226 stream:event("connected"); | 225 stream:event("connected"); |