line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Client; |
2
|
10
|
|
|
10
|
|
4162
|
use warnings; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
298
|
|
3
|
10
|
|
|
10
|
|
44
|
use strict; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
162
|
|
4
|
10
|
|
|
10
|
|
36
|
use utf8; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
48
|
|
5
|
10
|
|
|
10
|
|
170
|
use Carp; |
|
10
|
|
|
|
|
12
|
|
|
10
|
|
|
|
|
524
|
|
6
|
10
|
|
|
10
|
|
44
|
use JSON::Parse '0.61', 'valid_json'; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
920
|
|
7
|
10
|
|
|
10
|
|
56
|
use JSON::Create '0.35'; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
660
|
|
8
|
10
|
|
|
10
|
|
54
|
use JSON::Server; |
|
10
|
|
|
|
|
10
|
|
|
10
|
|
|
|
|
310
|
|
9
|
10
|
|
|
10
|
|
54
|
use IO::Socket; |
|
10
|
|
|
|
|
10
|
|
|
10
|
|
|
|
|
60
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
4
|
|
|
4
|
1
|
3018748
|
my ($class, %options) = @_; |
15
|
4
|
|
|
|
|
91
|
my $port = $options{port}; |
16
|
4
|
50
|
|
|
|
177
|
if (! $port) { |
17
|
0
|
|
|
|
|
0
|
carp "No port specified"; |
18
|
0
|
|
|
|
|
0
|
return undef; |
19
|
|
|
|
|
|
|
} |
20
|
4
|
|
|
|
|
52
|
delete $options{port}; |
21
|
4
|
|
|
|
|
52
|
my $verbose = $options{verbose}; |
22
|
4
|
50
|
|
|
|
66
|
if ($verbose) { |
23
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "->new - messages on.\n"; |
24
|
|
|
|
|
|
|
} |
25
|
4
|
|
|
|
|
51
|
delete $options{verbose}; |
26
|
4
|
|
|
|
|
136
|
for my $k (keys %options) { |
27
|
0
|
|
|
|
|
0
|
carp "Unknown option '$k'"; |
28
|
0
|
|
|
|
|
0
|
delete $options{$k}; |
29
|
|
|
|
|
|
|
} |
30
|
4
|
|
|
|
|
217
|
my $client = bless { |
31
|
|
|
|
|
|
|
port => $port, |
32
|
|
|
|
|
|
|
verbose => $verbose, |
33
|
|
|
|
|
|
|
jc => JSON::Create->new (downgrade_utf8 => 1,), |
34
|
|
|
|
|
|
|
jp => JSON::Parse->new (), |
35
|
|
|
|
|
|
|
}; |
36
|
4
|
|
|
|
|
793
|
$client->{jp}->upgrade_utf8 (1); |
37
|
4
|
|
|
|
|
66
|
return $client; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub JSON::Client::send |
41
|
|
|
|
|
|
|
{ |
42
|
7
|
|
|
7
|
1
|
2004205
|
my ($jcl, $input) = @_; |
43
|
7
|
50
|
|
|
|
92
|
if (! $input) { |
44
|
0
|
|
|
|
|
0
|
carp "Nothing to send"; |
45
|
0
|
|
|
|
|
0
|
return {error => 'empty input'}; |
46
|
|
|
|
|
|
|
} |
47
|
7
|
|
|
|
|
137
|
my $json_msg = $jcl->{jc}->run ($input); |
48
|
7
|
|
|
|
|
390
|
my $sock = make_sock ($jcl->{port}); |
49
|
7
|
100
|
|
|
|
47
|
if (! $sock) { |
50
|
3
|
|
|
|
|
59
|
return {error => 'make_sock failed'}; |
51
|
|
|
|
|
|
|
} |
52
|
4
|
50
|
|
|
|
63
|
if ($jcl->{verbose}) { |
53
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - sending $json_msg\n"; |
54
|
|
|
|
|
|
|
} |
55
|
4
|
|
|
|
|
71
|
my ($got, $ok) = get ($sock, $json_msg); |
56
|
4
|
50
|
|
|
|
33
|
if (! $ok) { |
57
|
0
|
|
|
|
|
0
|
carp "Error reading from server: $@"; |
58
|
0
|
|
|
|
|
0
|
return {error => "Error reading from server: $@"}; |
59
|
|
|
|
|
|
|
} |
60
|
4
|
50
|
|
|
|
34
|
if ($jcl->{verbose}) { |
61
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - got reply '$got'\n"; |
62
|
|
|
|
|
|
|
} |
63
|
4
|
50
|
|
|
|
77
|
if (! valid_json ($got)) { |
64
|
0
|
0
|
|
|
|
0
|
if ($jcl->{verbose}) { |
65
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - not valid JSON\n"; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
|
|
|
|
0
|
return {error => 'invalid JSON'}; |
68
|
|
|
|
|
|
|
} |
69
|
4
|
|
|
|
|
139
|
return $jcl->{jp}->parse ($got); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub make_sock |
73
|
|
|
|
|
|
|
{ |
74
|
8
|
|
|
8
|
0
|
1003663
|
my ($port) = @_; |
75
|
8
|
|
|
|
|
207
|
my %so = JSON::Server::so (); |
76
|
8
|
|
|
|
|
164
|
%so = ( |
77
|
|
|
|
|
|
|
%so, |
78
|
|
|
|
|
|
|
PeerPort => $port, |
79
|
|
|
|
|
|
|
PeerHost => 'localhost', |
80
|
|
|
|
|
|
|
); |
81
|
8
|
|
|
|
|
184
|
my $sock = IO::Socket->new (%so); |
82
|
8
|
100
|
|
|
|
12061
|
if (! $sock) { |
83
|
3
|
|
|
|
|
351
|
warn "IO::Socket->new failed: $!"; |
84
|
|
|
|
|
|
|
} |
85
|
8
|
|
|
|
|
85
|
return $sock; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get |
89
|
|
|
|
|
|
|
{ |
90
|
6
|
|
|
6
|
0
|
102
|
my ($sock, $json_msg) = @_; |
91
|
6
|
|
|
|
|
186
|
$sock->send ($json_msg . chr (0)); |
92
|
6
|
|
|
|
|
721
|
my $got = ''; |
93
|
6
|
|
|
|
|
33
|
my ($ok) = eval { |
94
|
6
|
|
|
|
|
13
|
my $data; |
95
|
6
|
|
|
|
|
26
|
my $max = 1000; |
96
|
6
|
|
33
|
|
|
77
|
while (! defined $data || length ($data) == $max) { |
97
|
6
|
|
|
|
|
83
|
$data = ''; |
98
|
6
|
|
|
|
|
148
|
$sock->recv ($data, $max); |
99
|
6
|
|
|
|
|
8214
|
$got .= $data; |
100
|
6
|
50
|
|
|
|
95
|
if ($got =~ s/\x00//) { |
101
|
6
|
|
|
|
|
26
|
last; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
6
|
|
|
|
|
30
|
1; |
105
|
|
|
|
|
|
|
}; |
106
|
6
|
|
|
|
|
34
|
return ($got, $ok); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |