97 |
97 |
98 function verse.quit() |
98 function verse.quit() |
99 return server.setquitting(true); |
99 return server.setquitting(true); |
100 end |
100 end |
101 |
101 |
|
102 function stream:listen(host, port) |
|
103 host = host or "localhost"; |
|
104 port = port or 0; |
|
105 local conn, err = server.addserver(host, port, new_listener(self, "server"), "*a"); |
|
106 if conn then |
|
107 self:debug("Bound to %s:%s", host, port); |
|
108 self.server = conn; |
|
109 end |
|
110 return conn, err; |
|
111 end |
|
112 |
102 function stream:connect(connect_host, connect_port) |
113 function stream:connect(connect_host, connect_port) |
103 connect_host = connect_host or "localhost"; |
114 connect_host = connect_host or "localhost"; |
104 connect_port = tonumber(connect_port) or 5222; |
115 connect_port = tonumber(connect_port) or 5222; |
105 |
116 |
106 -- Create and initiate connection |
117 -- Create and initiate connection |
116 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
127 local conn = server.wrapclient(conn, connect_host, connect_port, new_listener(self), "*a"); |
117 if not conn then |
128 if not conn then |
118 self:warn("connection initialisation failed: %s", err); |
129 self:warn("connection initialisation failed: %s", err); |
119 return self:event("disconnected", { reason = err }) or false, err; |
130 return self:event("disconnected", { reason = err }) or false, err; |
120 end |
131 end |
121 |
132 self:set_conn(conn); |
|
133 return true; |
|
134 end |
|
135 |
|
136 function stream:set_conn(conn) |
122 self.conn = conn; |
137 self.conn = conn; |
123 self.send = function (stream, data) |
138 self.send = function (stream, data) |
124 self:event("outgoing", data); |
139 self:event("outgoing", data); |
125 data = tostring(data); |
140 data = tostring(data); |
126 self:event("outgoing-raw", data); |
141 self:event("outgoing-raw", data); |
127 return conn:write(data); |
142 return conn:write(data); |
128 end; |
143 end; |
129 return true; |
|
130 end |
144 end |
131 |
145 |
132 function stream:close() |
146 function stream:close() |
133 if not self.conn then |
147 if not self.conn then |
134 verse.log("error", "Attempt to close disconnected connection - possibly a bug"); |
148 verse.log("error", "Attempt to close disconnected connection - possibly a bug"); |
197 -- Listener factory |
211 -- Listener factory |
198 function new_listener(stream) |
212 function new_listener(stream) |
199 local conn_listener = {}; |
213 local conn_listener = {}; |
200 |
214 |
201 function conn_listener.onconnect(conn) |
215 function conn_listener.onconnect(conn) |
202 stream.connected = true; |
216 if stream.server then |
203 stream:event("connected"); |
217 stream:debug("foo"); |
|
218 local client = verse.new(); |
|
219 conn:setlistener(new_listener(client)); |
|
220 client:set_conn(conn); |
|
221 stream:event("connected", { client = client }); |
|
222 else |
|
223 stream.connected = true; |
|
224 stream:event("connected"); |
|
225 end |
204 end |
226 end |
205 |
227 |
206 function conn_listener.onincoming(conn, data) |
228 function conn_listener.onincoming(conn, data) |
207 stream:event("incoming-raw", data); |
229 stream:event("incoming-raw", data); |
208 end |
230 end |