scansion/objects/client.lua

changeset 23
af7a51d78b7b
parent 22
b1fa55d17057
child 24
3c572d238d9e
equal deleted inserted replaced
22:b1fa55d17057 23:af7a51d78b7b
24 end 24 end
25 25
26 return { 26 return {
27 _validate = function (client) 27 _validate = function (client)
28 assert(client.jid, "No JID specified"); 28 assert(client.jid, "No JID specified");
29 client.stream = verse.new(); 29 client.stream = verse.new(verse.new_logger(client.name));
30 client.stream:hook("stanza", function (stanza) print("Stanza:", stanza) end); 30
31 function client.log(fmt, ...)
32 return client.stream:info(fmt, ...);
33 end
31 34
32 -- This one prints all received data 35 -- This one prints all received data
33 client.stream:hook("incoming-raw", print, 1000); 36 client.stream:hook("incoming-raw", function (s) client.log("Data in: %s", s); end, 1000);
34 client.stream:hook("outgoing-raw", print, 1000); 37 client.stream:hook("outgoing-raw", function (s) client.log("Data out: %s", s); end, 1000);
35 38 -- And incoming, parsed, stanzas
39 client.stream:hook("stanza", function (s) client.log("Stanza: %s", s) end);
36 end; 40 end;
37 41
38 connects = function (client) 42 connects = function (client)
39 local wait, done = async.waiter(); 43 local wait, done = async.waiter();
40 client.stream:hook("ready", function () print"aha" done() end); 44 client.stream:hook("ready", function () client.log"ready" done() client.log("ready done") end);
41 client.stream:connect_client(client.jid, client.password); 45 client.stream:connect_client(client.jid, client.password);
42 wait(); 46 wait();
43 client.full_jid = client.stream.jid; 47 client.full_jid = client.stream.jid;
44 end; 48 end;
45 49
50 54
51 receives = function (client, data) 55 receives = function (client, data)
52 local expected_stanza = fill_vars(client.script, parse_xml(table.concat(data))); 56 local expected_stanza = fill_vars(client.script, parse_xml(table.concat(data)));
53 local function stanza_handler(received_stanza) 57 local function stanza_handler(received_stanza)
54 if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then 58 if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
55 print("NOT IT!")
56 verse.quit(); 59 verse.quit();
60 client.log("NOT IT!")
61 client.log("Expected: %s", expected_stanza);
62 client.log("Received: %s", received_stanza);
57 else 63 else
58 print("YES!") 64 client.log("YES! %s", expected_stanza)
59 end 65 end
60 expected_stanza = nil; 66 expected_stanza = nil;
61 end 67 end
62 client.stream:hook("stanza", stanza_handler, 100); 68 client.stream:hook("stanza", stanza_handler, 100);
63 verse.add_task(stanza_timeout, function () 69 verse.add_task(stanza_timeout, function ()
64 if not expected_stanza then return; end 70 if not expected_stanza then return; end
65 print("TIMEOUT") 71 client.log("TIMEOUT waiting for %s", expected_stanza)
66 end); 72 end);
67 end; 73 end;
68 74
69 disconnects = function (client) 75 disconnects = function (client)
70 end; 76 end;

mercurial