Debian Codegraph @

samples/limit_searcher

raw
1 #!/opt/perl/bin/perl
2 use strict;
3 use utf8;
4 use AnyEvent;
5 use AnyEvent::XMPP::Client;
6 use AnyEvent::XMPP::Util qw/simxml/;
7 use AnyEvent::XMPP::Ext::Disco;
8
9 my $j = AnyEvent->condvar;
10 my $cl = AnyEvent::XMPP::Client->new;
11
12 $cl->add_account ('[email protected]', 'test');
13
14 my $max_size = 100000;
15 my $first_size = $max_size;
16 my $last_nok_size = $max_size;
17 my $last_ok_size = 0;
18 my $delta = 10;
19
20 $cl->reg_cb (
21 session_ready => sub {
22 my ($cl, $acc) = @_;
23 my $con = $acc->connection;
24
25 if (($last_nok_size - $last_ok_size) < $delta) {
26 print "Found limit in delta range: $last_ok_size for ".$acc->jid."\n";
27 $con->disconnect ("found limit");
28 return 0;
29 }
30
31 $con->send_iq (set => sub {
32 my ($w) = @_;
33 simxml ($w,
34 defns => 'jabber:iq:private',
35 node => {
36 name => 'query',
37 ns => 'jabber:iq:private',
38 childs => [
39 { name => "test", dns => "test:fe", childs => [ "A" x $first_size ] },
40 ]
41 }
42 );
43 print "Trying $first_size...\n";
44 }, sub {
45 my ($n, $e) = @_;
46 if ($e) {
47 die "iq private error: " . $e->string . "\n";
48 } else {
49 $con->send_iq (get => sub {
50 my ($w) = @_;
51 simxml ($w,
52 defns => 'jabber:iq:private',
53 node => {
54 name => 'query',
55 ns => 'jabber:iq:private',
56 childs => [ { name => 'test', dns => 'test:fe' } ]
57 }
58 );
59 }, sub {
60 my ($n, $e) = @_;
61 if ($e) {
62 $con->disconnect ("bad iq reply");
63 } else {
64 my ($q) = $n->find_all ([qw/jabber:iq:private query/],
65 [qw/test:fe test/]);
66 my $len = length $q->text;
67 if ($len == $first_size) {
68 print "$len seems to be ok!\n";
69 $last_ok_size = $first_size;
70 $first_size = $last_ok_size + ($last_nok_size - $last_ok_size) / 2;
71 $first_size = int ($first_size);
72 $con->disconnect ("retry");
73 } else {
74 $con->disconnect ("too short iq reply");
75 }
76 }
77 });
78 }
79 }, timeout => 1000000);
80
81 1
82 },
83 stream_error => sub {
84 my ($cl, $acc, $err) = @_;
85 print "STREAM ERROR: [" . $err->string . "] at $first_size, retry...\n";
86 1
87 },
88 connect_error => sub {
89 my ($cl, $acc, $err) = @_;
90 print "Connect error ".$acc->jid.": $err\n";
91 1
92 },
93 disconnect => sub {
94 my ($cl, $acc, $host, $port, $msg) = @_;
95 if ($msg eq 'found limit') { $j->broadcast }
96 elsif ($msg ne 'retry') {
97 $last_nok_size = $first_size;
98 $first_size = $last_ok_size + ($last_nok_size - $last_ok_size) / 2;
99 $first_size = int ($first_size);
100 print "disconnect got ($msg), retry with $first_size\n";
101 }
102 $cl->update_connections; # reconnect !
103 1
104 },
105 message => sub {
106 my ($cl, $acc, $msg) = @_;
107 print "message from: " . $msg->from . ": " . $msg->any_body . "\n";
108 1
109 }
110 );
111
112 $cl->start;
113 $j->wait;
114