Commit 931a8a5e authored by Mike Hibler's avatar Mike Hibler

Revamped the RPC syncandsend code yet again.

Now we keep reading after we send the command, til we get the next prompt.
That way we will catch any "Input error" messages and can try again.

The downside is that, for reboots, we wait through the whole 10 second
countdown before returning to the caller.  Previously we would return
immediately after issuing the command.
parent 6793a59f
...@@ -32,6 +32,9 @@ use POSIX qw(strftime); ...@@ -32,6 +32,9 @@ use POSIX qw(strftime);
# Turn off line buffering on output # Turn off line buffering on output
$| = 1; $| = 1;
# Number of times to try sending command in the face of "Input error"
my $ntries = 3;
# Set for more output. # Set for more output.
my $debug = 0; my $debug = 0;
...@@ -62,11 +65,13 @@ sub rpc27status { ...@@ -62,11 +65,13 @@ sub rpc27status {
} }
# #
# Send the command. Try again one time if there is a retryable error. # Send the command. Try again a few times if there is a retryable error.
# #
my $status = syncandsend($controller, $TIP, "status", $statusp); my $status;
if ($status == -1) { for my $try (1..$ntries) {
$status = syncandsend($controller, $TIP, "status", $statusp); $status = syncandsend($controller, $TIP, "status", $statusp);
last
if $status >= 0;
} }
close($TIP); close($TIP);
return $status ? 1 : 0; return $status ? 1 : 0;
...@@ -124,9 +129,11 @@ sub rpc27ctrl { ...@@ -124,9 +129,11 @@ sub rpc27ctrl {
foreach my $outlet (@outlet_strings) { foreach my $outlet (@outlet_strings) {
my $command = "$RPC27_CMD{$cmd} $outlet"; my $command = "$RPC27_CMD{$cmd} $outlet";
my $status = syncandsend($controller,$TIP,$command,undef); my $status;
if ($status == -1) { for my $try (1..$ntries) {
$status = syncandsend($controller,$TIP,$command,undef); $status = syncandsend($controller, $TIP, $command, undef);
last
if $status >= 0;
} }
if ($status) { if ($status) {
close($TIP); close($TIP);
...@@ -188,7 +195,7 @@ sub syncandsend($$$) { ...@@ -188,7 +195,7 @@ sub syncandsend($$$) {
} }
if (! $insync) { if (! $insync) {
print STDERR "*** Could not sync with power controller! ". print STDERR "*** Could not sync with power controller! ".
"($controller/$outlet)\n"; "($controller)\n";
return 1; return 1;
} }
...@@ -213,23 +220,31 @@ sub syncandsend($$$) { ...@@ -213,23 +220,31 @@ sub syncandsend($$$) {
return 1; return 1;
} }
# If status is desired, slurp and parse everything up to the next prompt #
if ($statusp) { # Read and parse all the output until the next prompt to ensure that
my %status = (); # there was no read error. We also collect status here if desired.
#
print "Getting status\n" my %status = ();
my $gotcmd = 0;
print "Reading output following command\n"
if ($debug);
while (my $line = rpc_readline($TIP)) {
print "Read: $line"
if ($debug); if ($debug);
while (my $line = rpc_readline($TIP)) { # skip echoed prompt+command
print "Read: $line" if ($line =~ /$cmd/) {
if ($debug); $gotcmd = 1;
# skip echoed prompt+command next;
if ($line =~ /status/) { }
next; # didn't recognize our command for some reason, return failure
} if ($line =~ /Input error/) {
# XXX cannot look for final prompt because there is no newline return -1;
if ($line =~ /$RPC27_HELPMSG/) { }
last; # got the following prompt, all done
} if ($gotcmd && $line =~ $RPC27_PROMPT) {
last;
}
if ($statusp) {
if ($line =~ /Temperature:\s+(\d+\.\d+) C/) { if ($line =~ /Temperature:\s+(\d+\.\d+) C/) {
$status{tempC} = $1; $status{tempC} = $1;
} elsif ($line =~ /Average Power:\s+(\d+) Watts/) { } elsif ($line =~ /Average Power:\s+(\d+) Watts/) {
...@@ -244,14 +259,11 @@ sub syncandsend($$$) { ...@@ -244,14 +259,11 @@ sub syncandsend($$$) {
$status{$outlet} = $ohash{$o}; $status{$outlet} = $ohash{$o};
} }
} }
# didn't recognize our command for some reason
elsif ($line =~ /Input error/) {
return -1;
}
} }
}
if ($statusp) {
%$statusp = %status; %$statusp = %status;
print "Returning status\n"
if ($debug);
} }
return 0; return 0;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment