scansion/objects/client.lua

Sat, 05 Sep 2015 23:24:15 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 05 Sep 2015 23:24:15 +0100
changeset 6
0c94ea0cabec
parent 4
c54194d8cc30
child 8
42b4e73c0d30
permissions
-rw-r--r--

client: Implement send/receive, including new stanzacmp library

4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
1 local verse = require "verse".init("client");
1
017c5809d537 client: Enable verse log output
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
2
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
3 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
4
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
5 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
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
7 local stanza_timeout = 5;
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
8
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
9 local stanzacmp = require "scansion.stanzacmp";
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
10
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
11 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
12 _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
13 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
14 client.stream = verse.new();
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 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
16
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
17 connects = function (client)
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
18 client.stream:connect_client(client.jid, client.password);
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
19 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
20
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
21 sends = function (client, data)
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
22 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
23 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
24 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
25
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
26 receives = function (client, data)
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
27 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
28 local function stanza_handler(received_stanza)
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
29 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
30 print("NOT IT!")
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
31 verse.quit();
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
32 else
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
33 print("YES!")
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
34 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
35 expected_stanza = nil;
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
36 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
37 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
38 verse.add_task(stanza_timeout, function ()
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
39 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
40 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
41 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
42
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
43 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
44 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
45 }

mercurial