line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::Client; |
2
|
10
|
|
|
10
|
|
5038
|
use warnings; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
334
|
|
3
|
10
|
|
|
10
|
|
52
|
use strict; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
190
|
|
4
|
10
|
|
|
10
|
|
54
|
use utf8; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
62
|
|
5
|
10
|
|
|
10
|
|
196
|
use Carp; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
570
|
|
6
|
10
|
|
|
10
|
|
54
|
use JSON::Parse '0.60_01', 'valid_json'; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
1080
|
|
7
|
10
|
|
|
10
|
|
78
|
use JSON::Create '0.30_04'; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
854
|
|
8
|
10
|
|
|
10
|
|
70
|
use JSON::Server; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
336
|
|
9
|
10
|
|
|
10
|
|
62
|
use IO::Socket; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
96
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new |
13
|
|
|
|
|
|
|
{ |
14
|
4
|
|
|
4
|
1
|
3008048
|
my ($class, %options) = @_; |
15
|
4
|
|
|
|
|
80
|
my $port = $options{port}; |
16
|
4
|
50
|
|
|
|
116
|
if (! $port) { |
17
|
0
|
|
|
|
|
0
|
carp "No port specified"; |
18
|
0
|
|
|
|
|
0
|
return undef; |
19
|
|
|
|
|
|
|
} |
20
|
4
|
|
|
|
|
52
|
delete $options{port}; |
21
|
4
|
|
|
|
|
33
|
my $verbose = $options{verbose}; |
22
|
4
|
50
|
|
|
|
71
|
if ($verbose) { |
23
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "->new - messages on.\n"; |
24
|
|
|
|
|
|
|
} |
25
|
4
|
|
|
|
|
34
|
delete $options{verbose}; |
26
|
4
|
|
|
|
|
94
|
for my $k (keys %options) { |
27
|
0
|
|
|
|
|
0
|
carp "Unknown option '$k'"; |
28
|
0
|
|
|
|
|
0
|
delete $options{$k}; |
29
|
|
|
|
|
|
|
} |
30
|
4
|
|
|
|
|
170
|
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
|
|
|
|
|
500
|
$client->{jp}->upgrade_utf8 (1); |
37
|
4
|
|
|
|
|
26
|
return $client; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub JSON::Client::send |
41
|
|
|
|
|
|
|
{ |
42
|
7
|
|
|
7
|
1
|
2002995
|
my ($jcl, $input) = @_; |
43
|
7
|
50
|
|
|
|
89
|
if (! $input) { |
44
|
0
|
|
|
|
|
0
|
carp "Nothing to send"; |
45
|
0
|
|
|
|
|
0
|
return {error => 'empty input'}; |
46
|
|
|
|
|
|
|
} |
47
|
7
|
|
|
|
|
120
|
my $json_msg = $jcl->{jc}->run ($input); |
48
|
7
|
|
|
|
|
285
|
my $sock = make_sock ($jcl->{port}); |
49
|
7
|
100
|
|
|
|
67
|
if (! $sock) { |
50
|
3
|
|
|
|
|
47
|
return {error => 'make_sock failed'}; |
51
|
|
|
|
|
|
|
} |
52
|
4
|
50
|
|
|
|
67
|
if ($jcl->{verbose}) { |
53
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - sending $json_msg\n"; |
54
|
|
|
|
|
|
|
} |
55
|
4
|
|
|
|
|
28
|
my ($got, $ok) = get ($sock, $json_msg); |
56
|
4
|
50
|
|
|
|
21
|
if (! $ok) { |
57
|
0
|
|
|
|
|
0
|
carp "Error reading from server: $@"; |
58
|
0
|
|
|
|
|
0
|
return {error => "Error reading from server: $@"}; |
59
|
|
|
|
|
|
|
} |
60
|
4
|
50
|
|
|
|
24
|
if ($jcl->{verbose}) { |
61
|
0
|
|
|
|
|
0
|
print __PACKAGE__ . "::send - got reply '$got'\n"; |
62
|
|
|
|
|
|
|
} |
63
|
4
|
50
|
|
|
|
65
|
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
|
|
|
|
|
150
|
return $jcl->{jp}->parse ($got); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub make_sock |
73
|
|
|
|
|
|
|
{ |
74
|
8
|
|
|
8
|
0
|
1001327
|
my ($port) = @_; |
75
|
8
|
|
|
|
|
158
|
my %so = JSON::Server::so (); |
76
|
8
|
|
|
|
|
136
|
%so = ( |
77
|
|
|
|
|
|
|
%so, |
78
|
|
|
|
|
|
|
PeerPort => $port, |
79
|
|
|
|
|
|
|
PeerHost => 'localhost', |
80
|
|
|
|
|
|
|
); |
81
|
8
|
|
|
|
|
313
|
my $sock = IO::Socket->new (%so); |
82
|
8
|
100
|
|
|
|
11934
|
if (! $sock) { |
83
|
3
|
|
|
|
|
281
|
warn "IO::Socket->new failed: $!"; |
84
|
|
|
|
|
|
|
} |
85
|
8
|
|
|
|
|
68
|
return $sock; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub get |
89
|
|
|
|
|
|
|
{ |
90
|
6
|
|
|
6
|
0
|
73
|
my ($sock, $json_msg) = @_; |
91
|
6
|
|
|
|
|
190
|
$sock->send ($json_msg . chr (0)); |
92
|
6
|
|
|
|
|
569
|
my $got = ''; |
93
|
6
|
|
|
|
|
43
|
my ($ok) = eval { |
94
|
6
|
|
|
|
|
17
|
my $data; |
95
|
6
|
|
|
|
|
13
|
my $max = 1000; |
96
|
6
|
|
33
|
|
|
49
|
while (! defined $data || length ($data) == $max) { |
97
|
6
|
|
|
|
|
77
|
$data = ''; |
98
|
6
|
|
|
|
|
117
|
$sock->recv ($data, $max); |
99
|
6
|
|
|
|
|
9714
|
$got .= $data; |
100
|
6
|
50
|
|
|
|
181
|
if ($got =~ s/\x00//) { |
101
|
6
|
|
|
|
|
32
|
last; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
6
|
|
|
|
|
39
|
1; |
105
|
|
|
|
|
|
|
}; |
106
|
6
|
|
|
|
|
42
|
return ($got, $ok); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |