Software /
code /
verse
Annotate
init.lua @ 20:972066e06f4c
verse: Update for new server connection API
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 01 Dec 2009 04:14:52 +0000 |
parent | 17:ec6b0b94826c |
child | 23:33e346283135 |
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 |
5 local server = require "server"; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
6 local events = require "events"; |
0 | 7 |
8 module("verse", package.seeall); | |
9 local verse = _M; | |
10 | |
11 local stream = {}; | |
12 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
13 stream_mt = stream; |
0 | 14 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
15 verse.plugins = {}; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
16 |
0 | 17 function verse.new() |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
18 local t = {}; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
19 t.id = tostring(t):match("%x*$"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
20 t.logger = logger.init(t.id); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
21 t.events = events.new(); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
22 return setmetatable(t, stream); |
0 | 23 end |
24 | |
25 function verse.loop() | |
26 return server.loop(); | |
27 end | |
28 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
29 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
|
30 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
31 connect_port = tonumber(connect_port) or 5222; |
0 | 32 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
33 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
34 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
35 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
36 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
|
37 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
38 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
|
39 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
|
40 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
41 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
42 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
43 --local conn, err = server.addclient(self.connect_host or self.host, tonumber(self.connect_port) or 5222, new_listener(self), "*a"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
44 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); --, hosts[from_host].ssl_ctx, false ); |
0 | 45 if not conn then |
46 return nil, err; | |
47 end | |
48 | |
49 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 local w, t = conn.write, tostring; |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
51 self.send = function (_, data) return w(conn, t(data)); end |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
52 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
54 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
55 function stream:debug(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 return self.logger("debug", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
57 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
58 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
59 function stream:warn(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
60 return self.logger("warn", ...); |
0 | 61 end |
62 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
63 function stream:error(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
64 return self.logger("error", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
65 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
67 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
68 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
69 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
|
70 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
71 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
72 |
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
|
73 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
|
74 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
|
75 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
76 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
77 function stream:add_plugin(name) |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
78 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
79 local ok, err = verse.plugins[name](self); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
80 if ok then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
81 self:debug("Loaded %s plugin", name); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
82 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
83 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
|
84 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
85 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
86 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
87 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
88 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
89 -- Listener factory |
0 | 90 function new_listener(stream) |
91 local conn_listener = {}; | |
92 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
93 function conn_listener.onincoming(conn, data) |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
94 stream:debug("Data"); |
0 | 95 if not stream.connected then |
96 stream.connected = true; | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
97 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn:write(tostring(data)); end; |
0 | 98 stream:event("connected"); |
99 end | |
100 if data then | |
101 stream:event("incoming-raw", data); | |
102 end | |
103 end | |
104 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
105 function conn_listener.ondisconnect(conn, err) |
0 | 106 stream.connected = false; |
107 stream:event("disconnected", { reason = err }); | |
108 end | |
109 | |
110 return conn_listener; | |
111 end | |
112 | |
113 | |
114 local log = require "util.logger".init("verse"); | |
115 | |
116 return verse; |