>From 5454587749040dd028f7255259b28dce22253531 Mon Sep 17 00:00:00 2001
From: Shao He Feng <shaohef@linux.vnet.ibm.com>
Date: Tue, 13 Dec 2011 04:33:18 +0800
Subject: for testing the new qemu specific regiater API


Signed-off-by: Shao He Feng <shaohef@linux.vnet.ibm.com>
---
 tools/virsh.c |  107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 107 insertions(+), 0 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index d58b827..4c9a69d 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -36,6 +36,7 @@
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <libxml/uri.h>
 #include <libxml/xpath.h>
 #include <libxml/xmlsave.h>
 
@@ -53,6 +54,8 @@
 #include "memory.h"
 #include "xml.h"
 #include "libvirt/libvirt-qemu.h"
+#include "libvirt/libvirt.h"
+#include "datatypes.h"
 #include "virfile.h"
 #include "event_poll.h"
 #include "configmake.h"
@@ -11790,6 +11793,108 @@ cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
     return ret;
 }
 
+static const vshCmdInfo info_deregevent[] = {
+    {"help", N_("deregister an qemu event")},
+    {"desc", N_("you please input you eventID, that the register-event returns.")},
+    {NULL, NULL}
+};
+static const vshCmdOptDef opts_deregevent[] = {
+    {"eventID", VSH_OT_INT, VSH_OFLAG_REQ, N_("deregister qemu spici eventID ")},
+    {NULL, 0, 0, NULL}
+};
+
+static bool
+cmdDeregisterEvent(vshControl *ctl, const vshCmd *cmd)
+{
+    virDomainPtr dom;
+    bool ret = false;
+    int eventID = -1;
+    int retEventID = -1;
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (vshCommandOptInt(cmd, "eventID", &eventID) < 0) {
+        vshError(ctl, "%s", _("Please specify valid eventID"));
+        return false;
+    }
+    if (eventID < 0) {
+        fprintf(stdout, "please input a positive Int\n");
+        return false;
+    }
+
+    if ((retEventID = virConnectDomainQemuEventDeregister(ctl->conn, eventID)) < 0) {
+       fprintf(stdout, "eventID: %d Deregister error.\n", eventID);
+       return false;
+    }
+    fprintf(stdout, "event Deregister successi, the remote callbackID is %d.\n", retEventID);
+    ret = true;
+ cleanup:
+    return ret;
+}
+
+static const vshCmdInfo info_regevent[] = {
+    {"help", N_("register an qemu event")},
+    {"desc", N_("you please input you event name.")},
+    {NULL, NULL}
+};
+
+static const vshCmdOptDef opts_regevent[] = {
+    {"event", VSH_OT_DATA, VSH_OFLAG_REQ, N_("register qemu spici event ")},
+    {NULL, 0, 0, NULL}
+};
+
+void printRegistEvent(virConnectPtr conn,
+                      virDomainPtr dom,
+                      const char *eventName, /* The JSON event name */
+                      const char *eventArgs, /* The JSON string of args */
+                      void *opaque){
+    char *uriName = virConnectGetURI (conn);
+    fputc('\n', stdout);
+    if (uriName != NULL)
+       fprintf(stdout, "connect URI: %s, ",uriName);
+        //xmlPrintURI(stdout, conn->uri);
+
+    if (dom != NULL)
+       fprintf(stdout, "dom: %s(%d) receive an event:\n", dom->name, dom->id);
+
+    if (eventName != NULL)
+        fprintf(stdout, "{ event: %s", eventName);
+    if (eventArgs != NULL)
+        fprintf(stdout, ", data: %s }\n", eventArgs);
+    else
+        fprintf(stdout, " }\n");
+    fprintf(stdout, "----------------------------------------------");
+    fputc('\n', stdout);
+}
+
+static bool
+cmdRegisterEvent(vshControl *ctl, const vshCmd *cmd)
+{
+    const char *name = NULL;
+    virDomainPtr dom;
+    bool ret = false;
+    int eventID = -1;
+
+    if (!vshConnectionUsability(ctl, ctl->conn))
+        return false;
+
+    if (vshCommandOptString(cmd, "event", &name) < 0) {
+        vshError(ctl, "%s", _("Please specify valid event name"));
+        return false;
+    }
+
+    dom = vshCommandOptDomain(ctl, cmd, NULL);
+    eventID = virConnectDomainQemuEventRegister(ctl->conn, dom, name, printRegistEvent, NULL, NULL);
+    if (eventID < 0) {
+       fprintf(stdout, "%s event register error.\n", name);
+       return false;
+    }
+    fprintf(stdout, "%s event call back ID is %d, you can use it to deregister the event.\n", name, eventID);
+    ret = true;
+ cleanup:
+    return ret;
+}
+
 /*
  * "ttyconsole" command
  */
@@ -15172,6 +15277,8 @@ static const vshCmdDef domManagementCmds[] = {
     {"vcpupin", cmdVcpuPin, opts_vcpupin, info_vcpupin, 0},
     {"version", cmdVersion, opts_version, info_version, 0},
     {"vncdisplay", cmdVNCDisplay, opts_vncdisplay, info_vncdisplay, 0},
+    {"register-event", cmdRegisterEvent, opts_regevent, info_regevent, 0},
+    {"deregister-event", cmdDeregisterEvent, opts_deregevent, info_deregevent, 0},
     {NULL, NULL, NULL, NULL, 0}
 };
 
-- 
1.7.5.4

