]> git.proxmox.com Git - qemu.git/commitdiff
QMP: Asynchronous events infrastructure
authorLuiz Capitulino <lcapitulino@redhat.com>
Fri, 27 Nov 2009 00:59:03 +0000 (22:59 -0200)
committerAnthony Liguori <aliguori@us.ibm.com>
Thu, 3 Dec 2009 15:41:23 +0000 (09:41 -0600)
Asynchronous events are generated with a call to
monitor_protocol_event().

This function builds the right data-type and emit the event
right away. The emitted data is always a JSON object and its
format is as follows:

{ "event": json-string,
  "timestamp": { "seconds": json-number, "microseconds": json-number },
  "data": json-value }

This design is based on ideas by Amit Shah <amit.shah@redhat.com>.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
monitor.c
monitor.h
qemu-tool.c

index beaff6425e7ba4bac8db0e116f2aaa74d194803e..68b63cacc351405769e1c9cdfe89ff953d0c6123 100644 (file)
--- a/monitor.c
+++ b/monitor.c
@@ -307,6 +307,56 @@ static void monitor_protocol_emitter(Monitor *mon, QObject *data)
     QDECREF(qmp);
 }
 
+static void timestamp_put(QDict *qdict)
+{
+    int err;
+    QObject *obj;
+    struct timeval tv;
+
+    err = gettimeofday(&tv, NULL);
+    if (err < 0)
+        return;
+
+    obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
+                                "'microseconds': %" PRId64 " }",
+                                (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
+    assert(obj != NULL);
+
+    qdict_put_obj(qdict, "timestamp", obj);
+}
+
+/**
+ * monitor_protocol_event(): Generate a Monitor event
+ *
+ * Event-specific data can be emitted through the (optional) 'data' parameter.
+ */
+void monitor_protocol_event(MonitorEvent event, QObject *data)
+{
+    QDict *qmp;
+    const char *event_name;
+    Monitor *mon = cur_mon;
+
+    assert(event < EVENT_MAX);
+
+    if (!monitor_ctrl_mode(mon))
+        return;
+
+    switch (event) {
+        default:
+            abort();
+            break;
+    }
+
+    qmp = qdict_new();
+    timestamp_put(qmp);
+    qdict_put(qmp, "event", qstring_from_str(event_name));
+    if (data)
+        qdict_put_obj(qmp, "data", data);
+
+    monitor_json_emitter(mon, QOBJECT(qmp));
+    QDECREF(qmp);
+}
+
 static int compare_cmd(const char *name, const char *list)
 {
     const char *p, *pstart;
index 556507ce8948f924904ae0a4e1e2016cb1e8f24f..a1d8b7a5f0e1ee422bae41c435a1757a68e6066d 100644 (file)
--- a/monitor.h
+++ b/monitor.h
@@ -13,6 +13,12 @@ extern Monitor *cur_mon;
 #define MONITOR_USE_READLINE  0x02
 #define MONITOR_USE_CONTROL   0x04
 
+/* QMP events */
+typedef enum MonitorEvent {
+    EVENT_MAX,
+} MonitorEvent;
+
+void monitor_protocol_event(MonitorEvent event, QObject *data);
 const char *monitor_cmdline_parse(const char *cmdline, int *flags);
 void monitor_init(CharDriverState *chr, int flags);
 
index b35ea8e1c21c661f63f161b6737e0d55241bc8ba..18b48af319a02cd3e2791b9bd96c072b8069fea6 100644 (file)
@@ -56,6 +56,10 @@ int get_async_context_id(void)
     return 0;
 }
 
+void monitor_protocol_event(MonitorEvent event, QObject *data)
+{
+}
+
 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
 {
     QEMUBH *bh;