qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Adam Litke <agl@us.ibm.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Luiz Capitulino <lcapitulino@redhat.com>,
	Avi Kivity <avi@redhat.com>,
	Markus Armbruster <armbru@redhat.com>
Subject: [Qemu-devel] [PATCH 04/15] qapi: add signal support to core QMP server
Date: Fri, 11 Mar 2011 17:05:34 -0600	[thread overview]
Message-ID: <1299884745-521-5-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1299884745-521-1-git-send-email-aliguori@us.ibm.com>

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

diff --git a/qmp-core.c b/qmp-core.c
index d906557..7f60942 100644
--- a/qmp-core.c
+++ b/qmp-core.c
@@ -42,6 +42,13 @@ struct QmpCommandState
     QObject *tag;
 };
 
+struct QmpState
+{
+    int (*add_connection)(QmpState *s, QmpConnection *conn);
+    void (*del_connection)(QmpState *s, int global_handle, Error **errp);
+    void (*event)(QmpState *s, QObject *data);
+};
+
 static QTAILQ_HEAD(, QmpCommand) qmp_commands =
     QTAILQ_HEAD_INITIALIZER(qmp_commands);
 
@@ -111,3 +118,90 @@ char *qobject_as_string(QObject *obj)
         return NULL;
     }
 }
+
+void qmp_state_add_connection(QmpState *sess, const char *event_name, QmpSignal *obj, int handle, QmpConnection *conn)
+{
+    conn->state = sess;
+    conn->event_name = event_name;
+    conn->signal = obj;
+    conn->handle = handle;
+    conn->global_handle = sess->add_connection(sess, conn);
+}
+
+void qmp_put_event(QmpState *sess, int global_handle, Error **errp)
+{
+    sess->del_connection(sess, global_handle, errp);
+}
+
+void qmp_state_event(QmpConnection *conn, QObject *data)
+{
+    QDict *event = qdict_new();
+    qemu_timeval tv;
+    QObject *ts;
+
+    qemu_gettimeofday(&tv);
+
+    ts = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
+                            "'microseconds': %" PRId64 " }",
+                            (int64_t)tv.tv_sec, (int64_t)tv.tv_usec);
+    qdict_put_obj(event, "timestamp", ts);
+
+    qdict_put(event, "event", qstring_from_str(conn->event_name));
+    if (data) {
+        qobject_incref(data);
+        qdict_put_obj(event, "data", data);
+    }
+
+    qdict_put(event, "tag", qint_from_int(conn->global_handle));
+
+    conn->state->event(conn->state, QOBJECT(event));
+    QDECREF(event);
+}
+
+QmpSignal *qmp_signal_init(void)
+{
+    QmpSignal *obj = qemu_mallocz(sizeof(*obj));
+    obj->max_handle = 0;
+    obj->ref = 1;
+    QTAILQ_INIT(&obj->slots);
+    return obj;
+}
+
+void qmp_signal_ref(QmpSignal *obj)
+{
+    obj->ref++;
+}
+
+void qmp_signal_unref(QmpSignal *obj)
+{
+    if (--obj->ref) {
+        qemu_free(obj);
+    }
+}
+
+int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque)
+{
+    int handle = ++obj->max_handle;
+    QmpSlot *slot = qemu_mallocz(sizeof(*slot));
+
+    slot->handle = handle;
+    slot->func = func;
+    slot->opaque = opaque;
+
+    QTAILQ_INSERT_TAIL(&obj->slots, slot, node);
+
+    return handle;
+}
+
+void qmp_signal_disconnect(QmpSignal *obj, int handle)
+{
+    QmpSlot *slot;
+
+    QTAILQ_FOREACH(slot, &obj->slots, node) {
+        if (slot->handle == handle) {
+            QTAILQ_REMOVE(&obj->slots, slot, node);
+            qemu_free(slot);
+            break;
+        }
+    }
+}
diff --git a/qmp-core.h b/qmp-core.h
index 4765b69..c9c8b63 100644
--- a/qmp-core.h
+++ b/qmp-core.h
@@ -20,6 +20,31 @@ typedef void (QmpCommandFunc)(const QDict *, QObject **, Error **);
 typedef void (QmpStatefulCommandFunc)(QmpState *qmp__sess, const QDict *, QObject **, Error **);
 typedef void (QmpAsyncCommandFunc)(const QDict *, Error **, QmpCommandState *);
 
+typedef struct QmpSlot
+{
+    int handle;
+    void *func;
+    void *opaque;
+    QTAILQ_ENTRY(QmpSlot) node;
+} QmpSlot;
+
+struct QmpSignal
+{
+    int max_handle;
+    int ref;
+    QTAILQ_HEAD(, QmpSlot) slots;
+};
+
+typedef struct QmpConnection
+{
+    QmpState *state;
+    const char *event_name;
+    QmpSignal *signal;
+    int handle;
+    int global_handle;
+    QTAILQ_ENTRY(QmpConnection) node;
+} QmpConnection;
+
 void qmp_register_command(const char *name, QmpCommandFunc *fn);
 void qmp_register_stateful_command(const char *name, QmpStatefulCommandFunc *fn);
 void qmp_register_async_command(const char *name, QmpAsyncCommandFunc *fn);
@@ -28,4 +53,34 @@ void qmp_async_complete_command(QmpCommandState *cmd, QObject *retval, Error *er
 
 char *qobject_as_string(QObject *obj);
 
+QmpSignal *qmp_signal_init(void);
+void qmp_signal_ref(QmpSignal *obj);
+void qmp_signal_unref(QmpSignal *obj);
+int qmp_signal_connect(QmpSignal *obj, void *func, void *opaque);
+void qmp_signal_disconnect(QmpSignal *obj, int handle);
+
+void qmp_state_add_connection(QmpState *sess, const char *name, QmpSignal *obj, int handle, QmpConnection *conn);
+void qmp_put_event(QmpState *sess, int global_handle, Error **errp);
+void qmp_state_event(QmpConnection *conn, QObject *data);
+
+#define signal_init(obj) do {          \
+    (obj)->signal = qmp_signal_init(); \
+} while (0)
+
+#define signal_unref(obj) qmp_signal_unref((obj)->signal)
+
+#define signal_connect(obj, fn, opaque) \
+    qmp_signal_connect((obj)->signal, (obj)->func = fn, opaque)
+
+#define signal_disconnect(obj, handle) \
+    qmp_signal_disconnect((obj)->signal, handle)
+
+#define signal_notify(obj, ...) do {                         \
+    QmpSlot *qmp__slot;                                      \
+    QTAILQ_FOREACH(qmp__slot, &(obj)->signal->slots, node) { \
+        (obj)->func = qmp__slot->func;                       \
+        (obj)->func(qmp__slot->opaque, ## __VA_ARGS__);      \
+    }                                                        \
+} while(0)
+
 #endif
-- 
1.7.0.4

  parent reply	other threads:[~2011-03-11 23:05 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-11 23:05 [Qemu-devel] [PATCH 00/15] QAPI Round 1 (core code generator) (v2) Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 01/15] qapi: add code generator for qmp-types (v2) Anthony Liguori
2011-03-11 23:12   ` [Qemu-devel] " Anthony Liguori
2011-03-12 11:29   ` [Qemu-devel] " Blue Swirl
2011-03-12 15:00     ` Anthony Liguori
2011-03-18 14:18       ` Luiz Capitulino
2011-03-18 14:14   ` [Qemu-devel] " Luiz Capitulino
2011-03-11 23:05 ` [Qemu-devel] [PATCH 02/15] qapi: add code generator for type marshallers Anthony Liguori
2011-03-18 15:13   ` [Qemu-devel] " Luiz Capitulino
2011-03-11 23:05 ` [Qemu-devel] [PATCH 03/15] qapi: add core QMP server support (v2) Anthony Liguori
2011-03-11 23:05 ` Anthony Liguori [this message]
2011-03-11 23:05 ` [Qemu-devel] [PATCH 05/15] qapi: add QAPI module type Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 06/15] qapi: add code generators for QMP command marshaling Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 07/15] qapi: add query-version QMP command Anthony Liguori
2011-03-12 11:19   ` Blue Swirl
2011-03-12 15:06     ` Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 08/15] qapi: add new QMP server that uses CharDriverState (v2) Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 09/15] vl: add a new -qmp2 option to expose experimental QMP server Anthony Liguori
2011-03-11 23:14   ` [Qemu-devel] " Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 10/15] qapi: add QMP quit command Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 11/15] qapi: add QMP qmp_capabilities command Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 12/15] qapi: add QMP put-event command Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 13/15] qapi: add code generator for libqmp (v2) Anthony Liguori
2011-03-12 11:10   ` Blue Swirl
2011-03-12 14:53     ` Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 14/15] qapi: add test-libqmp Anthony Liguori
2011-03-12 11:23   ` Blue Swirl
2011-03-12 14:59     ` Anthony Liguori
2011-03-11 23:05 ` [Qemu-devel] [PATCH 15/15] qapi: generate HTML report for test-libqmp Anthony Liguori
2011-03-16 14:34 ` [Qemu-devel] Re: [PATCH 00/15] QAPI Round 1 (core code generator) (v2) Luiz Capitulino
2011-03-16 14:49   ` Paolo Bonzini
2011-03-16 15:00     ` Luiz Capitulino
2011-03-16 16:06       ` Anthony Liguori
2011-03-16 16:03     ` Anthony Liguori
2011-03-16 16:31       ` Paolo Bonzini
2011-03-16 18:06         ` Anthony Liguori
2011-03-16 15:59   ` Anthony Liguori
2011-03-16 18:09     ` Luiz Capitulino
2011-03-16 18:32       ` Anthony Liguori
2011-03-16 19:27         ` Luiz Capitulino
2011-03-16 20:00           ` Anthony Liguori
2011-03-18 14:10             ` Luiz Capitulino
2011-03-18 14:22               ` Anthony Liguori
2011-03-17 12:21     ` Kevin Wolf
2011-03-17 12:46       ` Anthony Liguori
2011-03-17 13:15         ` Kevin Wolf
2011-03-17 13:28           ` Anthony Liguori
2011-03-17 14:04             ` Kevin Wolf
2011-03-17 15:49               ` Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1299884745-521-5-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.com \
    --cc=agl@us.ibm.com \
    --cc=armbru@redhat.com \
    --cc=avi@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).