Software /
code /
verse
Annotate
init.lua @ 42:6006e6bb1c28
verse: Add verse.add_task(delay, callback) to add timer functions using util.timer
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 03 Jan 2010 18:01:54 +0000 |
parent | 30:9c96318913f7 |
child | 44:37396504de5f |
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 |
30
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
5 local server = require "net.server"; |
9c96318913f7
Revert module names throughout to their Prosody equivalents
Matthew Wild <mwild1@gmail.com>
parents:
23
diff
changeset
|
6 local events = require "util.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 |
23
33e346283135
verse: Have verse.new() accept a prototype table on which to base the connection
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
17 function verse.new(base) |
33e346283135
verse: Have verse.new() accept a prototype table on which to base the connection
Matthew Wild <mwild1@gmail.com>
parents:
20
diff
changeset
|
18 local t = base or {}; |
1
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 | |
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
|
25 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
|
26 |
0 | 27 function verse.loop() |
28 return server.loop(); | |
29 end | |
30 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
31 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
|
32 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
33 connect_port = tonumber(connect_port) or 5222; |
0 | 34 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
35 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
36 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
37 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
38 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
|
39 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
40 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
|
41 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
|
42 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
43 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
44 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
45 --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
|
46 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); --, hosts[from_host].ssl_ctx, false ); |
0 | 47 if not conn then |
48 return nil, err; | |
49 end | |
50 | |
51 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
52 local w, t = conn.write, tostring; |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
53 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
|
54 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
55 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
57 function stream:debug(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
58 return self.logger("debug", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
59 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
60 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
61 function stream:warn(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
62 return self.logger("warn", ...); |
0 | 63 end |
64 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
65 function stream:error(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 return self.logger("error", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
67 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
68 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
69 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
70 function stream:event(name, ...) |
4
0ef21511c7ff
Log debug message when firing an event
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
71 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
|
72 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
73 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
74 |
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
|
75 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
|
76 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
|
77 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
78 |
3
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
79 function stream:add_plugin(name) |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
80 if require("verse.plugins."..name) then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
81 local ok, err = verse.plugins[name](self); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
82 if ok then |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
83 self:debug("Loaded %s plugin", name); |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
84 else |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
85 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
|
86 end |
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 return self; |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
89 end |
372ddb5900d3
verse: Support for loading plugins
Matthew Wild <mwild1@gmail.com>
parents:
2
diff
changeset
|
90 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
91 -- Listener factory |
0 | 92 function new_listener(stream) |
93 local conn_listener = {}; | |
94 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
95 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
|
96 stream:debug("Data"); |
0 | 97 if not stream.connected then |
98 stream.connected = true; | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
99 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn:write(tostring(data)); end; |
0 | 100 stream:event("connected"); |
101 end | |
102 if data then | |
103 stream:event("incoming-raw", data); | |
104 end | |
105 end | |
106 | |
20
972066e06f4c
verse: Update for new server connection API
Matthew Wild <mwild1@gmail.com>
parents:
17
diff
changeset
|
107 function conn_listener.ondisconnect(conn, err) |
0 | 108 stream.connected = false; |
109 stream:event("disconnected", { reason = err }); | |
110 end | |
111 | |
112 return conn_listener; | |
113 end | |
114 | |
115 | |
116 local log = require "util.logger".init("verse"); | |
117 | |
118 return verse; |