scansion/objects/client.lua

changeset 23
af7a51d78b7b
parent 22
b1fa55d17057
child 24
3c572d238d9e
--- a/scansion/objects/client.lua	Sat Sep 19 21:45:23 2015 +0100
+++ b/scansion/objects/client.lua	Sat Sep 19 21:48:26 2015 +0100
@@ -26,18 +26,22 @@
 return {
 	_validate = function (client)
 		assert(client.jid, "No JID specified");
-		client.stream = verse.new();
-		client.stream:hook("stanza", function (stanza) print("Stanza:", stanza) end);
+		client.stream = verse.new(verse.new_logger(client.name));
+	
+		function client.log(fmt, ...)
+			return client.stream:info(fmt, ...);
+		end
 
 		-- This one prints all received data
-		client.stream:hook("incoming-raw", print, 1000);
-		client.stream:hook("outgoing-raw", print, 1000);
-
+		client.stream:hook("incoming-raw", function (s) client.log("Data in:  %s", s); end, 1000);
+		client.stream:hook("outgoing-raw", function (s) client.log("Data out: %s", s); end, 1000);
+		-- And incoming, parsed, stanzas
+		client.stream:hook("stanza", function (s) client.log("Stanza: %s", s) end);
 	end;
 
 	connects = function (client)
 		local wait, done = async.waiter();
-		client.stream:hook("ready", function () print"aha" done() end);
+		client.stream:hook("ready", function () client.log"ready" done() client.log("ready done") end);
 		client.stream:connect_client(client.jid, client.password);
 		wait();
 		client.full_jid = client.stream.jid;
@@ -52,17 +56,19 @@
 		local expected_stanza = fill_vars(client.script, parse_xml(table.concat(data)));
 		local function stanza_handler(received_stanza)
 			if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
-				print("NOT IT!")
 				verse.quit();
+				client.log("NOT IT!")
+				client.log("Expected: %s", expected_stanza);
+				client.log("Received: %s", received_stanza);
 			else
-				print("YES!")
+				client.log("YES! %s", expected_stanza)
 			end
 			expected_stanza = nil;
 		end
 		client.stream:hook("stanza", stanza_handler, 100);
 		verse.add_task(stanza_timeout, function ()
 			if not expected_stanza then return; end
-			print("TIMEOUT")
+			client.log("TIMEOUT waiting for %s", expected_stanza)
 		end);
 	end;
 

mercurial