From bcfbc40b398154bb7a1384ef8309a6eae7fad678 Mon Sep 17 00:00:00 2001 From: Alexandre Derumier Date: Sun, 17 Mar 2013 16:09:07 +0100 Subject: [PATCH] qmpclient-qga : mux_input : parse qga result result sample: first json is guest-sync result, second json is command result { "return": 123456}\n{"return": {}} Signed-off-by: Alexandre Derumier Signed-off-by: Dietmar Maurer --- PVE/QMPClient.pm | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/PVE/QMPClient.pm b/PVE/QMPClient.pm index 3401ce3e..51f7c185 100755 --- a/PVE/QMPClient.pm +++ b/PVE/QMPClient.pm @@ -292,9 +292,15 @@ sub mux_close { sub mux_input { my ($self, $mux, $fh, $input) = @_; - return if $$input !~ s/^(.*})\r\n(.*)$/$2/so; + my $raw; - my $raw = $1; + if($self->{qga}){ + return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so; + $raw = $1; + }else{ + return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so; + $raw = $1; + } my $vmid = $self->{fhs_lookup}->{$fh}; if (!$vmid) { @@ -305,6 +311,32 @@ sub mux_input { eval { my @jsons = split("\n", $raw); + if($self->{qga}){ + + die "response is not complete" if @jsons != 2 ; + + my $obj = from_json($jsons[0]); + my $cmdid = $obj->{return}; + die "received responsed without command id\n" if !$cmdid; + + my $curcmd = $self->{current}->{$vmid}; + die "unable to lookup current command for VM $vmid\n" if !$curcmd; + + delete $self->{current}->{$vmid}; + + if ($curcmd->{id} ne $cmdid) { + die "got wrong command id '$cmdid' (expected $curcmd->{id})\n"; + } + + $obj = from_json($jsons[1]); + + if (my $callback = $curcmd->{callback}) { + &$callback($vmid, $obj); + } + + return; + } + foreach my $json (@jsons) { my $obj = from_json($json); next if defined($obj->{QMP}); # skip monitor greeting