diff mbox

[10/22] qapi: add core QMP server support

Message ID 1299460984-15849-11-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori March 7, 2011, 1:22 a.m. UTC
This is the infrastructure to register commands.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Comments

Stefan Hajnoczi March 7, 2011, 1:09 p.m. UTC | #1
On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori <aliguori@us.ibm.com> wrote:
> +char *qobject_as_string(QObject *obj)
> +{
> +    char buffer[1024];
> +
> +    switch (qobject_type(obj)) {
> +    case QTYPE_QINT:
> +        snprintf(buffer, sizeof(buffer), "%" PRId64,
> +                 qint_get_int(qobject_to_qint(obj)));
> +        return qemu_strdup(buffer);
> +    case QTYPE_QSTRING:
> +        return qemu_strdup(qstring_get_str(qobject_to_qstring(obj)));
> +    case QTYPE_QFLOAT:
> +        snprintf(buffer, sizeof(buffer), "%.17g",
> +                 qfloat_get_double(qobject_to_qfloat(obj)));
> +        return qemu_strdup(buffer);

qemu_asprintf() would be a nice helper function to have ;).

Stefan
Anthony Liguori March 7, 2011, 1:39 p.m. UTC | #2
On 03/07/2011 07:09 AM, Stefan Hajnoczi wrote:
> On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>  wrote:
>    
>> +char *qobject_as_string(QObject *obj)
>> +{
>> +    char buffer[1024];
>> +
>> +    switch (qobject_type(obj)) {
>> +    case QTYPE_QINT:
>> +        snprintf(buffer, sizeof(buffer), "%" PRId64,
>> +                 qint_get_int(qobject_to_qint(obj)));
>> +        return qemu_strdup(buffer);
>> +    case QTYPE_QSTRING:
>> +        return qemu_strdup(qstring_get_str(qobject_to_qstring(obj)));
>> +    case QTYPE_QFLOAT:
>> +        snprintf(buffer, sizeof(buffer), "%.17g",
>> +                 qfloat_get_double(qobject_to_qfloat(obj)));
>> +        return qemu_strdup(buffer);
>>      
> qemu_asprintf() would be a nice helper function to have ;).
>    

Indeed :-)

Regards,

Anthony Liguori

> Stefan
>
>
Daniel P. Berrangé March 7, 2011, 1:46 p.m. UTC | #3
On Mon, Mar 07, 2011 at 07:39:41AM -0600, Anthony Liguori wrote:
> On 03/07/2011 07:09 AM, Stefan Hajnoczi wrote:
> >On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>  wrote:
> >>+char *qobject_as_string(QObject *obj)
> >>+{
> >>+    char buffer[1024];
> >>+
> >>+    switch (qobject_type(obj)) {
> >>+    case QTYPE_QINT:
> >>+        snprintf(buffer, sizeof(buffer), "%" PRId64,
> >>+                 qint_get_int(qobject_to_qint(obj)));
> >>+        return qemu_strdup(buffer);
> >>+    case QTYPE_QSTRING:
> >>+        return qemu_strdup(qstring_get_str(qobject_to_qstring(obj)));
> >>+    case QTYPE_QFLOAT:
> >>+        snprintf(buffer, sizeof(buffer), "%.17g",
> >>+                 qfloat_get_double(qobject_to_qfloat(obj)));
> >>+        return qemu_strdup(buffer);
> >qemu_asprintf() would be a nice helper function to have ;).
> 
> Indeed :-)

Since you've introduced glib, you get that function for free:

    g_strdup_printf()/g_strdup_vprintf()

similarly qemu_strdup & malloc related friends could be replaced with
the equivalent glib functions.

Regards,
Daniel
Anthony Liguori March 7, 2011, 1:54 p.m. UTC | #4
On 03/07/2011 07:46 AM, Daniel P. Berrange wrote:
> On Mon, Mar 07, 2011 at 07:39:41AM -0600, Anthony Liguori wrote:
>    
>> On 03/07/2011 07:09 AM, Stefan Hajnoczi wrote:
>>      
>>> On Mon, Mar 7, 2011 at 1:22 AM, Anthony Liguori<aliguori@us.ibm.com>   wrote:
>>>        
>>>> +char *qobject_as_string(QObject *obj)
>>>> +{
>>>> +    char buffer[1024];
>>>> +
>>>> +    switch (qobject_type(obj)) {
>>>> +    case QTYPE_QINT:
>>>> +        snprintf(buffer, sizeof(buffer), "%" PRId64,
>>>> +                 qint_get_int(qobject_to_qint(obj)));
>>>> +        return qemu_strdup(buffer);
>>>> +    case QTYPE_QSTRING:
>>>> +        return qemu_strdup(qstring_get_str(qobject_to_qstring(obj)));
>>>> +    case QTYPE_QFLOAT:
>>>> +        snprintf(buffer, sizeof(buffer), "%.17g",
>>>> +                 qfloat_get_double(qobject_to_qfloat(obj)));
>>>> +        return qemu_strdup(buffer);
>>>>          
>>> qemu_asprintf() would be a nice helper function to have ;).
>>>        
>> Indeed :-)
>>      
> Since you've introduced glib, you get that function for free:
>
>      g_strdup_printf()/g_strdup_vprintf()
>
> similarly qemu_strdup&  malloc related friends could be replaced with
> the equivalent glib functions.
>    

Good point.

Regards,

Anthony Liguori

> Regards,
> Daniel
>
diff mbox

Patch

diff --git a/Makefile.objs b/Makefile.objs
index f51eab3..dbdce3c 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -103,6 +103,7 @@  common-obj-y += block-migration.o
 common-obj-y += pflib.o
 common-obj-y += bitmap.o bitops.o
 common-obj-y += qmp-marshal-types.o qmp-marshal-types-core.o
+common-obj-y += qmp-core.o
 
 common-obj-$(CONFIG_BRLAPI) += baum.o
 common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
diff --git a/qmp-core.c b/qmp-core.c
new file mode 100644
index 0000000..78aef3a
--- /dev/null
+++ b/qmp-core.c
@@ -0,0 +1,77 @@ 
+/*
+ * QAPI
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.  See
+ * the COPYING.LIB file in the top-level directory.
+ */
+#include "qmp.h"
+#include "qmp-core.h"
+#include "json-lexer.h"
+#include "json-parser.h"
+#include "json-streamer.h"
+#include "qemu_socket.h"
+#include <glib.h>
+#include "qemu-queue.h"
+#include "sysemu.h"
+
+typedef struct QmpCommand
+{
+    const char *name;
+    bool stateful;
+    QmpCommandFunc *fn;
+    QmpStatefulCommandFunc *sfn;
+    QTAILQ_ENTRY(QmpCommand) node;
+} QmpCommand;
+
+static QTAILQ_HEAD(, QmpCommand) qmp_commands =
+    QTAILQ_HEAD_INITIALIZER(qmp_commands);
+
+void qmp_register_command(const char *name, QmpCommandFunc *fn)
+{
+    QmpCommand *cmd = qemu_mallocz(sizeof(*cmd));
+
+    cmd->name = name;
+    cmd->stateful = false;
+    cmd->fn = fn;
+    QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
+}
+
+void qmp_register_stateful_command(const char *name, QmpStatefulCommandFunc *fn)
+{
+    QmpCommand *cmd = qemu_mallocz(sizeof(*cmd));
+
+    cmd->name = name;
+    cmd->stateful = true;
+    cmd->sfn = fn;
+    QTAILQ_INSERT_TAIL(&qmp_commands, cmd, node);
+}
+
+char *qobject_as_string(QObject *obj)
+{
+    char buffer[1024];
+
+    switch (qobject_type(obj)) {
+    case QTYPE_QINT:
+        snprintf(buffer, sizeof(buffer), "%" PRId64,
+                 qint_get_int(qobject_to_qint(obj)));
+        return qemu_strdup(buffer);
+    case QTYPE_QSTRING:
+        return qemu_strdup(qstring_get_str(qobject_to_qstring(obj)));
+    case QTYPE_QFLOAT:
+        snprintf(buffer, sizeof(buffer), "%.17g",
+                 qfloat_get_double(qobject_to_qfloat(obj)));
+        return qemu_strdup(buffer);
+    case QTYPE_QBOOL:
+        if (qbool_get_int(qobject_to_qbool(obj))) {
+            return qemu_strdup("on");
+        }
+        return qemu_strdup("off");
+    default:
+        return NULL;
+    }
+}
diff --git a/qmp-core.h b/qmp-core.h
new file mode 100644
index 0000000..e3235ec
--- /dev/null
+++ b/qmp-core.h
@@ -0,0 +1,30 @@ 
+/*
+ * QAPI
+ *
+ * Copyright IBM, Corp. 2011
+ *
+ * Authors:
+ *  Anthony Liguori   <aliguori@us.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.  See
+ * the COPYING.LIB file in the top-level directory.
+ */
+#ifndef QMP_CORE_H
+#define QMP_CORE_H
+
+#include "monitor.h"
+#include "qmp-marshal-types.h"
+#include "error_int.h"
+
+typedef struct QmpState QmpState;
+
+typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
+typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict *, QObject **, Error **);
+
+void qmp_register_command(const char *name, QmpCommandFunc *fn);
+void qmp_register_stateful_command(const char *name, QmpStatefulCommandFunc *fn);
+void qmp_init_chardev(CharDriverState *chr);
+
+char *qobject_as_string(QObject *obj);
+
+#endif