scansion/objects/client.lua

Mon, 07 Sep 2015 14:33:09 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 07 Sep 2015 14:33:09 +0100
changeset 15
0441673df075
parent 10
75bf4c021461
child 16
59f176aa3465
permissions
-rw-r--r--

client: Add full JID as property of clients

8
42b4e73c0d30 client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
1 local async = require "scansion.async";
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
2 local verse = require "verse".init("client");
1
017c5809d537 client: Enable verse log output
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
3
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
4 verse.set_log_handler(verse._default_log_handler, { "debug", "info", "warn", "error" });
1
017c5809d537 client: Enable verse log output
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
5
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local parse_xml = require "scansion.xml".parse;
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
7
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
8 local stanza_timeout = 5;
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
9
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
10 local stanzacmp = require "scansion.stanzacmp";
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
11
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return {
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 _validate = function (client)
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 assert(client.jid, "No JID specified");
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 client.stream = verse.new();
10
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
16 client.stream:hook("stanza", function (stanza) print("Stanza:", stanza) end);
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
17
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
18 -- This one prints all received data
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
19 client.stream:hook("incoming-raw", print, 1000);
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
20 client.stream:hook("outgoing-raw", print, 1000);
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
21
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 connects = function (client)
8
42b4e73c0d30 client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
25 local wait, done = async.waiter();
42b4e73c0d30 client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
26 client.stream:hook("ready", function () print"aha" done() end);
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
27 client.stream:connect_client(client.jid, client.password);
8
42b4e73c0d30 client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
28 print("waiting")
42b4e73c0d30 client: Wait for login to complete before continuing past the 'connects' action
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
29 wait();
15
0441673df075 client: Add full JID as property of clients
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
30 client.full_jid = client.stream.jid;
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 sends = function (client, data)
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
34 local stanza = parse_xml(table.concat(data));
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
35 client.stream:send(stanza);
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 receives = function (client, data)
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
39 local expected_stanza = parse_xml(table.concat(data));
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
40 local function stanza_handler(received_stanza)
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
41 if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
42 print("NOT IT!")
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
43 verse.quit();
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
44 else
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
45 print("YES!")
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
46 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
47 expected_stanza = nil;
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
48 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
49 client.stream:hook("stanza", stanza_handler, 100);
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
50 verse.add_task(stanza_timeout, function ()
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
51 if not expected_stanza then return; end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
52 end);
0
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 disconnects = function (client)
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end;
2e31b584f8d9 It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 }

mercurial