Software /
code /
verse
Annotate
init.lua @ 2:9e9f3be09131
Remove some XMPP client-specific requires from init.lua
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 04 Aug 2009 18:32:25 +0100 |
parent | 1:7c8d0a2fc004 |
child | 3:372ddb5900d3 |
rev | line source |
---|---|
0 | 1 |
2 | |
3 local server = require "server"; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
4 local events = require "events"; |
0 | 5 |
6 module("verse", package.seeall); | |
7 local verse = _M; | |
8 | |
9 local stream = {}; | |
10 stream.__index = stream; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
11 stream_mt = stream; |
0 | 12 |
13 function verse.new() | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
14 local t = {}; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
15 t.id = tostring(t):match("%x*$"); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
16 t.logger = logger.init(t.id); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
17 t.events = events.new(); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
18 return setmetatable(t, stream); |
0 | 19 end |
20 | |
21 function verse.loop() | |
22 return server.loop(); | |
23 end | |
24 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
25 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
|
26 connect_host = connect_host or "localhost"; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
27 connect_port = tonumber(connect_port) or 5222; |
0 | 28 |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
29 -- Create and initiate connection |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
30 local conn = socket.tcp() |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
31 conn:settimeout(0); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
32 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
|
33 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
34 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
|
35 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
|
36 return false, err; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
37 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
38 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
39 --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
|
40 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); --, hosts[from_host].ssl_ctx, false ); |
0 | 41 if not conn then |
42 return nil, err; | |
43 end | |
44 | |
45 self.conn = conn; | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
46 local w, t = conn.write, tostring; |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
47 self.send = function (_, data) return w(t(data)); end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
48 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
49 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
50 -- Logging functions |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
51 function stream:debug(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
52 return self.logger("debug", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
53 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
54 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
55 function stream:warn(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
56 return self.logger("warn", ...); |
0 | 57 end |
58 | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
59 function stream:error(...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
60 return self.logger("error", ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
61 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
62 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
63 -- Event handling |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
64 function stream:event(name, ...) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
65 return self.events.fire_event(name, ...); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
66 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
67 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
68 function stream:hook(name, callback) |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
69 return self.events.add_handler(name, callback); |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
70 end |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
71 |
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
72 -- Listener factory |
0 | 73 function new_listener(stream) |
74 local conn_listener = {}; | |
75 | |
76 function conn_listener.incoming(conn, data) | |
1
7c8d0a2fc004
Break client-specific code into verse.client module
Matthew Wild <mwild1@gmail.com>
parents:
0
diff
changeset
|
77 stream:debug("Data"); |
0 | 78 if not stream.connected then |
79 stream.connected = true; | |
80 stream.send = function (stream, data) stream:debug("Sending data: "..tostring(data)); return conn.write(tostring(data)); end; | |
81 stream:event("connected"); | |
82 end | |
83 if data then | |
84 stream:event("incoming-raw", data); | |
85 end | |
86 end | |
87 | |
88 function conn_listener.disconnect(conn, err) | |
89 stream.connected = false; | |
90 stream:event("disconnected", { reason = err }); | |
91 end | |
92 | |
93 return conn_listener; | |
94 end | |
95 | |
96 | |
97 local log = require "util.logger".init("verse"); | |
98 | |
99 return verse; |