line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Client; |
2
|
10
|
|
|
10
|
|
3918
|
use warnings; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
290
|
|
3
|
10
|
|
|
10
|
|
44
|
use strict; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
156
|
|
4
|
10
|
|
|
10
|
|
32
|
use utf8; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
42
|
|
5
|
10
|
|
|
10
|
|
156
|
use Carp; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
442
|
|
6
|
10
|
|
|
10
|
|
42
|
use JSON::Parse '0.61', 'valid_json'; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
888
|
|
7
|
10
|
|
|
10
|
|
158
|
use JSON::Create '0.35'; |
|
10
|
|
|
|
|
14
|
|
|
10
|
|
|
|
|
678
|
|
8
|
10
|
|
|
10
|
|
48
|
use JSON::Server; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
256
|
|
9
|
10
|
|
|
10
|
|
46
|
use IO::Socket; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
44
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.02_01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
4
|
|
|
4
|
1
|
3013367
|
my ($class, %options) = @_; |
15
|
4
|
|
|
|
|
54
|
my $port = $options{port}; |
16
|
4
|
50
|
|
|
|
99
|
if (! $port) { |
17
|
0
|
|
|
|
|
0
|
carp "No port specified"; |
18
|
0
|
|
|
|
|
0
|
return undef; |
19
|
|
|
|
|
|
|
} |
20
|
4
|
|
|
|
|
37
|
delete $options{port}; |
21
|
4
|
|
|
|
|
38
|
my $verbose = $options{verbose}; |
22
|
4
|
50
|
|
|
|
72
|
if ($verbose) { |
23
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "->new - messages on.\n"; |
24
|
|
|
|
|
|
|
} |
25
|
4
|
|
|
|
|
34
|
delete $options{verbose}; |
26
|
4
|
|
|
|
|
59
|
for my $k (keys %options) { |
27
|
0
|
|
|
|
|
0
|
carp "Unknown option '$k'"; |
28
|
0
|
|
|
|
|
0
|
delete $options{$k}; |
29
|
|
|
|
|
|
|
} |
30
|
4
|
|
|
|
|
139
|
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
|
|
|
|
|
505
|
$client->{jp}->upgrade_utf8 (1); |
37
|
4
|
|
|
|
|
37
|
return $client; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub JSON::Client::send |
41
|
|
|
|
|
|
|
{ |
42
|
7
|
|
|
7
|
1
|
2002551
|
my ($jcl, $input) = @_; |
43
|
7
|
50
|
|
|
|
73
|
if (! $input) { |
44
|
0
|
|
|
|
|
0
|
carp "Nothing to send"; |
45
|
0
|
|
|
|
|
0
|
return {error => 'empty input'}; |
46
|
|
|
|
|
|
|
} |
47
|
7
|
|
|
|
|
102
|
my $json_msg = $jcl->{jc}->run ($input); |
48
|
7
|
|
|
|
|
255
|
my $sock = make_sock ($jcl->{port}); |
49
|
7
|
100
|
|
|
|
33
|
if (! $sock) { |
50
|
2
|
|
|
|
|
28
|
return {error => 'make_sock failed'}; |
51
|
|
|
|
|
|
|
} |
52
|
5
|
50
|
|
|
|
42
|
if ($jcl->{verbose}) { |
53
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - sending $json_msg\n"; |
54
|
|
|
|
|
|
|
} |
55
|
5
|
|
|
|
|
21
|
my ($got, $ok) = get ($sock, $json_msg); |
56
|
5
|
50
|
|
|
|
21
|
if (! $ok) { |
57
|
0
|
|
|
|
|
0
|
carp "Error reading from server: $@"; |
58
|
0
|
|
|
|
|
0
|
return {error => "Error reading from server: $@"}; |
59
|
|
|
|
|
|
|
} |
60
|
5
|
50
|
|
|
|
20
|
if ($jcl->{verbose}) { |
61
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - got reply '$got'\n"; |
62
|
|
|
|
|
|
|
} |
63
|
5
|
100
|
|
|
|
56
|
if (! valid_json ($got)) { |
64
|
1
|
50
|
|
|
|
9
|
if ($jcl->{verbose}) { |
65
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - not valid JSON\n"; |
66
|
|
|
|
|
|
|
} |
67
|
1
|
|
|
|
|
30
|
return {error => 'invalid JSON'}; |
68
|
|
|
|
|
|
|
} |
69
|
4
|
|
|
|
|
89
|
return $jcl->{jp}->parse ($got); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub make_sock |
73
|
|
|
|
|
|
|
{ |
74
|
8
|
|
|
8
|
0
|
1002946
|
my ($port) = @_; |
75
|
8
|
|
|
|
|
88
|
my %so = JSON::Server::so (); |
76
|
8
|
|
|
|
|
122
|
%so = ( |
77
|
|
|
|
|
|
|
%so, |
78
|
|
|
|
|
|
|
PeerPort => $port, |
79
|
|
|
|
|
|
|
PeerHost => 'localhost', |
80
|
|
|
|
|
|
|
); |
81
|
8
|
|
|
|
|
133
|
my $sock = IO::Socket->new (%so); |
82
|
8
|
100
|
|
|
|
7333
|
if (! $sock) { |
83
|
2
|
|
|
|
|
147
|
warn "IO::Socket->new failed: $!"; |
84
|
|
|
|
|
|
|
} |
85
|
8
|
|
|
|
|
42
|
return $sock; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get |
89
|
|
|
|
|
|
|
{ |
90
|
7
|
|
|
7
|
0
|
67
|
my ($sock, $json_msg) = @_; |
91
|
7
|
|
|
|
|
138
|
$sock->send ($json_msg . chr (0)); |
92
|
7
|
|
|
|
|
473
|
my $got = ''; |
93
|
7
|
|
|
|
|
26
|
my ($ok) = eval { |
94
|
7
|
|
|
|
|
12
|
my $data; |
95
|
7
|
|
|
|
|
22
|
my $max = 1000; |
96
|
7
|
|
66
|
|
|
355
|
while (! defined $data || length ($data) == $max) { |
97
|
7
|
|
|
|
|
49
|
$data = ''; |
98
|
7
|
|
|
|
|
107
|
$sock->recv ($data, $max); |
99
|
7
|
|
|
|
|
6559
|
$got .= $data; |
100
|
7
|
100
|
|
|
|
92
|
if ($got =~ s/\x00//) { |
101
|
6
|
|
|
|
|
17
|
last; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
7
|
|
|
|
|
24
|
1; |
105
|
|
|
|
|
|
|
}; |
106
|
7
|
|
|
|
|
37
|
return ($got, $ok); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |