scansion/objects/client.lua

Wed, 28 Oct 2015 02:26:15 +0100

author
Kim Alvefur <zash@zash.se>
date
Wed, 28 Oct 2015 02:26:15 +0100
changeset 49
69c329681c3b
parent 48
6450aea6c564
child 50
0ce9c9a0c2a0
permissions
-rw-r--r--

scansion.objects.client: And the port too

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
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
4 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
5
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
6 local stanza_timeout = 5;
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 stanzacmp = require "scansion.stanzacmp";
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
9
37
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
10 local function filter_expression(script, s)
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
11 local expr = s:match("^%$%{(.+)%}$");
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
12 if not expr then return s end
46
Matthew Wild <mwild1@gmail.com>
parents: 44 38
diff changeset
13 local name, value_name = expr:match("^(.+)'s (.+)$");
37
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
14 assert(name, "Unable to parse expression: "..expr);
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
15 local key = value_name:lower():gsub(" ", "_");
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
16 assert(script.objects[name], "Unknown object called "..name);
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
17 local value = script.objects[name][key];
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
18 assert(value ~= nil, "Unknown attribute (of "..name.."): "..value_name);
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
19 return value;
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
20 end
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
21
16
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
22 local function fill_vars(script, stanza)
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
23 for k, v in pairs(stanza.attr) do
37
b2036129ccd0 client: Split variable expansion into a function so that it may be reused more easily
Kim Alvefur <zash@zash.se>
parents: 34
diff changeset
24 stanza.attr[k] = filter_expression(script, v);
16
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
25 end
38
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
26 for i, child in ipairs(stanza) do
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
27 if type(child) == "string" then
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
28 stanza[i] = filter_expression(script, child);
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
29 elseif type(child) == "table" then
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
30 fill_vars(script, child);
68458d0c50a0 client: Apply variable expansion to text nodes
Kim Alvefur <zash@zash.se>
parents: 37
diff changeset
31 end
34
946c7d13faac client: Apply variable expansion recursively to child nodes
Kim Alvefur <zash@zash.se>
parents: 29
diff changeset
32 end
16
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
33 return stanza;
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
34 end
59f176aa3465 client: Allow simple variable substitution in XML in scripts
Matthew Wild <mwild1@gmail.com>
parents: 15
diff changeset
35
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 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
37 _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
38 assert(client.jid, "No JID specified");
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
39 client.stream = verse.new(verse.new_logger(client.name));
48
6450aea6c564 scansion.objects.client: Pass on 'connect_host' to the stream
Kim Alvefur <zash@zash.se>
parents: 46
diff changeset
40 client.stream.connect_host = client.connect_host
49
69c329681c3b scansion.objects.client: And the port too
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
41 client.stream.connect_port = client.connect_port
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
42
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
43 function client.log(fmt, ...)
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
44 return client.stream:info(fmt, ...);
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
45 end
10
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
46
75bf4c021461 client: ALL the debugging on
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
47 -- This one prints all received data
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
48 client.stream:hook("incoming-raw", function (s) client.log("Data in: %s", s); end, 1000);
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
49 client.stream:hook("outgoing-raw", function (s) client.log("Data out: %s", s); end, 1000);
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
50 -- And incoming, parsed, stanzas
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
51 client.stream:hook("stanza", function (s) client.log("Stanza: %s", s) 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
52 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
53
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 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
55 local wait, done = async.waiter();
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
56 client.stream:hook("ready", function () client.log"ready" done() client.log("ready done") end);
4
c54194d8cc30 client: verse improvements
Matthew Wild <mwild1@gmail.com>
parents: 1
diff changeset
57 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
58 wait();
15
0441673df075 client: Add full JID as property of clients
Matthew Wild <mwild1@gmail.com>
parents: 10
diff changeset
59 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
60 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
61
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
62 sends = function (client, data)
24
3c572d238d9e client: More flexible handling of whitespace in stanzas in script
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
63 local stanza = fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " ")))));
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
64 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
65 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
66
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
67 receives = function (client, data)
26
c32334d33438 client: Wait for received stanzas using async waiter
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
68 local wait, done = async.waiter();
24
3c572d238d9e client: More flexible handling of whitespace in stanzas in script
Matthew Wild <mwild1@gmail.com>
parents: 23
diff changeset
69 local expected_stanza = fill_vars(client.script, assert(parse_xml((table.concat(data):gsub("\t", " ")))));
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
70 local function stanza_handler(received_stanza)
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
71 if not stanzacmp.stanzas_match(expected_stanza, received_stanza) then
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
72 client.log("NOT IT!")
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
73 client.log("Expected: %s", expected_stanza);
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
74 client.log("Received: %s", received_stanza);
25
9f8166238993 client: Error out when test fails, instead of directly calling verse.quit()
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
75 error("Received unexpected stanza");
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
76 else
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
77 client.log("YES! %s", expected_stanza)
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
78 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
79 expected_stanza = nil;
27
34d405303242 client: Remove stanza handler when stanza comes in
Matthew Wild <mwild1@gmail.com>
parents: 26
diff changeset
80 client.stream:unhook("stanza", stanza_handler);
26
c32334d33438 client: Wait for received stanzas using async waiter
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
81 done();
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
82 end
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
83 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
84 verse.add_task(stanza_timeout, function ()
28
2dd867c31214 client: Add comment
Matthew Wild <mwild1@gmail.com>
parents: 27
diff changeset
85 if not expected_stanza then return; end -- Stanza already received
23
af7a51d78b7b client: Improve stream logging, now prefixed by client name (taken from script)
Matthew Wild <mwild1@gmail.com>
parents: 22
diff changeset
86 client.log("TIMEOUT waiting for %s", expected_stanza)
25
9f8166238993 client: Error out when test fails, instead of directly calling verse.quit()
Matthew Wild <mwild1@gmail.com>
parents: 24
diff changeset
87 error("Timed out waiting for stanza");
26
c32334d33438 client: Wait for received stanzas using async waiter
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
88 done();
6
0c94ea0cabec client: Implement send/receive, including new stanzacmp library
Matthew Wild <mwild1@gmail.com>
parents: 4
diff changeset
89 end);
26
c32334d33438 client: Wait for received stanzas using async waiter
Matthew Wild <mwild1@gmail.com>
parents: 25
diff changeset
90 wait();
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
91 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
92
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
93 disconnects = function (client)
29
9dcdea04601c client: Implement 'disconnects' action
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
94 client.stream:close();
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
95 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
96 }

mercurial