[PATCH 06/16] qemuMonitorJSONAttachCharDevGetProps: Modernize construction of JSON objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use 'virJSONValueObjectAdd' instead of the step-by-step manual JSON
object building.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/qemu/qemu_monitor_json.c | 195 ++++++++++++++++++-----------------
 1 file changed, 99 insertions(+), 96 deletions(-)

diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 1ced942161..a9e87de4d3 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -6598,139 +6598,139 @@ int qemuMonitorJSONGetTPMTypes(qemuMonitor *mon,
     return qemuMonitorJSONGetStringArray(mon, "query-tpm-types", tpmtypes);
 }

-static int
-qemuMonitorJSONBuildChrChardevReconnect(virJSONValue *object,
-                                        const virDomainChrSourceReconnectDef *def)
-{
-    if (def->enabled != VIR_TRISTATE_BOOL_YES)
-        return 0;
-
-    return virJSONValueObjectAppendNumberUint(object, "reconnect", def->timeout);
-}

 static virJSONValue *
 qemuMonitorJSONAttachCharDevGetProps(const char *chrID,
                                      const virDomainChrSourceDef *chr)
 {
     g_autoptr(virJSONValue) props = NULL;
-    g_autoptr(virJSONValue) backend = virJSONValueNewObject();
-    g_autoptr(virJSONValue) data = virJSONValueNewObject();
-    g_autoptr(virJSONValue) addr = NULL;
-    const char *backend_type = NULL;
-    const char *host;
-    const char *port;
-    g_autofree char *tlsalias = NULL;
-    bool telnet;
+    g_autoptr(virJSONValue) backend = NULL;
+    g_autoptr(virJSONValue) backendData = virJSONValueNewObject();
+    const char *backendType = NULL;

     switch ((virDomainChrType)chr->type) {
     case VIR_DOMAIN_CHR_TYPE_NULL:
-        backend_type = "null";
-        break;
-
     case VIR_DOMAIN_CHR_TYPE_VC:
-        backend_type = "vc";
-        break;
-
     case VIR_DOMAIN_CHR_TYPE_PTY:
-        backend_type = "pty";
+        backendType = virDomainChrTypeToString(chr->type);
         break;

     case VIR_DOMAIN_CHR_TYPE_FILE:
-        backend_type = "file";
-        if (virJSONValueObjectAppendString(data, "out", chr->data.file.path) < 0)
-            return NULL;
-        if (virJSONValueObjectAdd(&data,
+        backendType = "file";
+
+        if (virJSONValueObjectAdd(&backendData,
+                                  "s:out", chr->data.file.path,
                                   "T:append", chr->data.file.append,
                                   NULL) < 0)
             return NULL;
+
         break;

     case VIR_DOMAIN_CHR_TYPE_DEV:
-        backend_type = STRPREFIX(chrID, "parallel") ? "parallel" : "serial";
-        if (virJSONValueObjectAppendString(data, "device",
-                                           chr->data.file.path) < 0)
-            return NULL;
-        break;
+        if (STRPREFIX(chrID, "parallel"))
+            backendType = "parallel";
+        else
+            backendType = "serial";

-    case VIR_DOMAIN_CHR_TYPE_TCP:
-        backend_type = "socket";
-        addr = qemuMonitorJSONBuildInetSocketAddress(chr->data.tcp.host,
-                                                     chr->data.tcp.service);
-        if (!addr ||
-            virJSONValueObjectAppend(data, "addr", &addr) < 0)
+        if (virJSONValueObjectAdd(&backendData,
+                                  "s:device", chr->data.file.path,
+                                  NULL) < 0)
             return NULL;

-        telnet = chr->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
+        break;

-        if (chr->data.tcp.listen &&
-            virJSONValueObjectAppendBoolean(data, "wait", false) < 0)
-            return NULL;
+    case VIR_DOMAIN_CHR_TYPE_UNIX:
+    case VIR_DOMAIN_CHR_TYPE_TCP: {
+        g_autofree char *tlsalias = NULL;
+        g_autoptr(virJSONValue) addr = NULL;
+        virTristateBool waitval = VIR_TRISTATE_BOOL_ABSENT;
+        virTristateBool telnet = VIR_TRISTATE_BOOL_ABSENT;
+        bool server = false;
+        int reconnect = -1;
+
+        backendType = "socket";
+
+        if (chr->type == VIR_DOMAIN_CHR_TYPE_TCP) {
+            telnet = virTristateBoolFromBool(chr->data.tcp.protocol == VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET);
+
+            if (chr->data.tcp.listen) {
+                server = true;
+                waitval = VIR_TRISTATE_BOOL_NO;
+            }

-        if (virJSONValueObjectAppendBoolean(data, "telnet", telnet) < 0 ||
-            virJSONValueObjectAppendBoolean(data, "server", chr->data.tcp.listen) < 0)
-            return NULL;
-        if (chr->data.tcp.tlscreds) {
-            if (!(tlsalias = qemuAliasTLSObjFromSrcAlias(chrID)))
+            if (chr->data.tcp.tlscreds &&
+                !(tlsalias = qemuAliasTLSObjFromSrcAlias(chrID)))
                 return NULL;

-            if (virJSONValueObjectAppendString(data, "tls-creds", tlsalias) < 0)
+            if (!(addr = qemuMonitorJSONBuildInetSocketAddress(chr->data.tcp.host,
+                                                               chr->data.tcp.service)))
                 return NULL;
-        }
-
-        if (qemuMonitorJSONBuildChrChardevReconnect(data, &chr->data.tcp.reconnect) < 0)
-            return NULL;
-        break;

-    case VIR_DOMAIN_CHR_TYPE_UDP:
-        backend_type = "udp";
-        host = chr->data.udp.connectHost;
-        if (!host)
-            host = "";
-        addr = qemuMonitorJSONBuildInetSocketAddress(host,
-                                                     chr->data.udp.connectService);
-        if (!addr ||
-            virJSONValueObjectAppend(data, "remote", &addr) < 0)
-            return NULL;
+            if (chr->data.tcp.reconnect.enabled == VIR_TRISTATE_BOOL_YES)
+                reconnect = chr->data.tcp.reconnect.timeout;
+            else if (chr->data.tcp.reconnect.enabled == VIR_TRISTATE_BOOL_NO)
+                reconnect = 0;
+        } else {
+            if (chr->data.nix.listen) {
+                server = true;
+                waitval = VIR_TRISTATE_BOOL_NO;
+            }

-        host = chr->data.udp.bindHost;
-        port = chr->data.udp.bindService;
-        if (host || port) {
-            if (!host)
-                host = "";
-            if (!port)
-                port = "";
-            addr = qemuMonitorJSONBuildInetSocketAddress(host, port);
-            if (!addr ||
-                virJSONValueObjectAppend(data, "local", &addr) < 0)
+            if (!(addr = qemuMonitorJSONBuildUnixSocketAddress(chr->data.nix.path)))
                 return NULL;
+
+            if (chr->data.nix.reconnect.enabled == VIR_TRISTATE_BOOL_YES)
+                reconnect = chr->data.tcp.reconnect.timeout;
+            else if (chr->data.nix.reconnect.enabled == VIR_TRISTATE_BOOL_NO)
+                reconnect = 0;
         }
+
+        if (virJSONValueObjectAdd(&backendData,
+                                  "a:addr", &addr,
+                                  "T:wait", waitval,
+                                  "T:telnet", telnet,
+                                  "b:server", server,
+                                  "S:tls-creds", tlsalias,
+                                  "k:reconnect", reconnect,
+                                  NULL) < 0)
+            return NULL;
+    }
         break;

-    case VIR_DOMAIN_CHR_TYPE_UNIX:
-        backend_type = "socket";
-        addr = qemuMonitorJSONBuildUnixSocketAddress(chr->data.nix.path);

-        if (!addr ||
-            virJSONValueObjectAppend(data, "addr", &addr) < 0)
-            return NULL;
+    case VIR_DOMAIN_CHR_TYPE_UDP: {
+        g_autoptr(virJSONValue) local = NULL;
+        g_autoptr(virJSONValue) remote = NULL;

-        if (chr->data.nix.listen &&
-            virJSONValueObjectAppendBoolean(data, "wait", false) < 0)
-            return NULL;
+        backendType = "udp";

-        if (virJSONValueObjectAppendBoolean(data, "server", chr->data.nix.listen) < 0)
+        if (!(remote = qemuMonitorJSONBuildInetSocketAddress(NULLSTR_EMPTY(chr->data.udp.connectHost),
+                                                             chr->data.udp.connectService)))
             return NULL;

-        if (qemuMonitorJSONBuildChrChardevReconnect(data, &chr->data.nix.reconnect) < 0)
+        if (chr->data.udp.bindHost || chr->data.udp.bindService) {
+            if (!(local = qemuMonitorJSONBuildInetSocketAddress(NULLSTR_EMPTY(chr->data.udp.bindHost),
+                                                                NULLSTR_EMPTY(chr->data.udp.bindService))))
+                return NULL;
+        }
+
+        if (virJSONValueObjectAdd(&backendData,
+                                  "a:remote", &remote,
+                                  "A:local", &local,
+                                  NULL) < 0)
             return NULL;
+    }
         break;

+
     case VIR_DOMAIN_CHR_TYPE_SPICEVMC:
-        backend_type = "spicevmc";
+        backendType = "spicevmc";

-        if (virJSONValueObjectAppendString(data, "type",
-                                           virDomainChrSpicevmcTypeToString(chr->data.spicevmc)) < 0)
+        if (virJSONValueObjectAdd(&backendData,
+                                  "s:type", virDomainChrSpicevmcTypeToString(chr->data.spicevmc),
+                                  NULL) < 0)
             return NULL;
+
         break;

     case VIR_DOMAIN_CHR_TYPE_SPICEPORT:
@@ -6748,15 +6748,18 @@ qemuMonitorJSONAttachCharDevGetProps(const char *chrID,
         return NULL;
     }

-    if (chr->logfile &&
-        virJSONValueObjectAdd(&data,
-                              "s:logfile", chr->logfile,
-                              "T:logappend", chr->logappend,
-                              NULL) < 0)
-        return NULL;
+    if (chr->logfile) {
+        if (virJSONValueObjectAdd(&backendData,
+                                  "S:logfile", chr->logfile,
+                                  "T:logappend", chr->logappend,
+                                  NULL) < 0)
+            return NULL;
+    }

-    if (virJSONValueObjectAppendString(backend, "type", backend_type) < 0 ||
-        virJSONValueObjectAppend(backend, "data", &data) < 0)
+    if (virJSONValueObjectAdd(&backend,
+                              "s:type", backendType,
+                              "A:data", &backendData,
+                              NULL) < 0)
         return NULL;

     if (virJSONValueObjectAdd(&props,
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux