| 1 | package App::ArduinoBuilder::JsonTool; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Package that implement bi-directionnal communication with a tool talking | ||||||||||||||||||||||||||||
| 4 | # JSON (like the Arduino pluggable discovery and monitor tools). | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use App::ArduinoBuilder::System 'split_cmd'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use IPC::Run qw(start pump finish timeout); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Log::Any::Simple ':default'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # TODO: look into what should be done with UTF-8 encoding when communicating | ||||||||||||||||||||||||||||
| 16 | # with the tool. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | my $timeout_exn; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 3 | my ($class, $cmd) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | my $this = bless { | |||||||||||||||||||||||||||
| 24 | out => '', # Out from the JsonTool class point of view, this will be the tool STDIN | ||||||||||||||||||||||||||||
| 25 | in => '', # This will be the tool STDOUT. | ||||||||||||||||||||||||||||
| 26 | cmd => $cmd, | ||||||||||||||||||||||||||||
| 27 | }, $class; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | $this->{tool} = start [split_cmd($cmd)], \$this->{out}, \$this->{in}, ($this->{timer} = timeout('inf')); | |||||||||||||||||||||||||||
| 30 | 3 | $this->{timer}->exception(\$timeout_exn); | |||||||||||||||||||||||||||
| 31 | 3 | trace "Creating tool for command: ${cmd}"; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 3 | return $this; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 37 | 3 | my ($this) = @_; | |||||||||||||||||||||||||||
| 38 | 3 | local($., $@, $!, $^E, $?); | |||||||||||||||||||||||||||
| 39 | 3 | ▶ | return unless $this->{tool}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 3 | $this->{timer}->start(5); | |||||||||||||||||||||||||||
| 41 | 3 | trace "Waiting for tool to stop"; | |||||||||||||||||||||||||||
| 42 | 3 | eval { | |||||||||||||||||||||||||||
| 43 | 3 | $this->{tool}->finish(); | |||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $this->{tool}->kill_kill(grace => 5); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 3 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Read our input pipe for one full json object. | ||||||||||||||||||||||||||||
| 52 | # $json can be omitted or it can be a single already read character. | ||||||||||||||||||||||||||||
| 53 | 8 | ▶ | sub _read_and_parse_json { | ||||||||||||||||||||||||||
|
Subroutine
_read_and_parse_json: called CC=7 SCAR=21.0
| |||||||||||||||||||||||||||||
| 54 | 8 | my ($this) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 8 | my $json = ''; | |||||||||||||||||||||||||||
| 57 | 8 | my $braces = 0; | |||||||||||||||||||||||||||
| 58 | 8 | my $primed = 0; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 8 | $this->{timer}->start(5); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | while (1) { | |||||||||||||||||||||||||||
| 63 | 11 | eval { pump $this->{tool} }; | |||||||||||||||||||||||||||
| 64 | 11 | ▶ | if (my $err = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | $this->{tool}->kill_kill(grace => 5); | |||||||||||||||||||||||||||
| 66 | 0 | debug 'Received: %s%s', $json, $this->{in}; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($err == \$timeout_exn) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | fatal "External tool is not responding: %s", $this->{cmd}; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | chomp($err); | |||||||||||||||||||||||||||
| 71 | 0 | fatal "Error while reading output of external tool: %s (%s)", $err, $this->{cmd}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 11 | $json .= $this->{in}; | |||||||||||||||||||||||||||
| 75 | 11 | ▶ | 1 while $this->{in} =~ m/\{ (?{$braces++; $primed++}) | \} (?{$braces--})/gx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 11 | $this->{in} = ''; | |||||||||||||||||||||||||||
| 77 | 11 | ▶ | if ($primed && $braces == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$primed and $braces == 0
MC/DC50%1/2
$primed and $braces == 0
$primed
$braces == 0
Truth table66%2/3
A and B
A= $primedB= $braces == 0
| |||||||||||||||||||||||||||||
| 78 | 8 | my $data = eval { decode_json ${json} }; | |||||||||||||||||||||||||||
| 79 | 8 | trace "Received following JSON:\n%s", $json; | |||||||||||||||||||||||||||
| 80 | 8 | ▶ | fatal "Could not parse JSON from tool output: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 8 | $this->{timer}->reset(); | |||||||||||||||||||||||||||
| 82 | 8 | return $data; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 8 | ▶ | sub send { | ||||||||||||||||||||||||||
|
Subroutine
send: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 8 | my ($this, $msg) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 8 | trace "Sending message to tool: ${msg}"; | |||||||||||||||||||||||||||
| 91 | 8 | $this->{out} .= $msg; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 8 | return $this->_read_and_parse_json(); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | 1; | |||||||||||||||||||||||||||