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

Re: [libvirt] [PATCH 3/3] Domain events - test harness



This test app prints domain events as they are emitted.
I was not sure where it belonged in the tree - so this is not in the context of the rest of the libvirt tree

Signed-off-by: Ben Guthro <bguthro virtualiron com>

 Makefile     |    2
 event-test.c |  134 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+)
--- /dev/null	2008-09-18 12:09:06 -04:00
+++ ./event-test.c	2008-10-03 14:31:49 -04:00
@@ -0,0 +1,134 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/poll.h>
+
+#include <libvirt/libvirt.h>
+
+#define DEBUG0(fmt) printf("%s:%d :: " fmt "\n", __FUNCTION__, __LINE__)
+#define DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__)
+
+int g_fd = 0;
+int g_event = 0;
+virEventHandleCallback g_cb = NULL;
+void *g_opaque = NULL;
+#define TIMEOUT_MS 1000
+
+int myDomainEventCallback (virConnectPtr conn,
+                            virDomainPtr dom,
+                            int event,
+                            void *opaque)
+{
+    printf("EVENT: Domain %s(%d) ", virDomainGetName(dom), virDomainGetID(dom));
+    switch(event) {
+        case VIR_DOMAIN_EVENT_ADDED:
+            printf("Added");
+            break;
+        case VIR_DOMAIN_EVENT_REMOVED:
+            printf("Removed");
+            break;
+        case VIR_DOMAIN_EVENT_STARTED:
+            printf("Started");
+            break;
+        case VIR_DOMAIN_EVENT_SUSPENDED:
+            printf("Suspended");
+            break;
+        case VIR_DOMAIN_EVENT_RESUMED:
+            printf("Resumed");
+            break;
+        case VIR_DOMAIN_EVENT_STOPPED:
+            printf("Stopped");
+            break;
+        case VIR_DOMAIN_EVENT_SAVED:
+            printf("Saved");
+            break;
+        case VIR_DOMAIN_EVENT_RESTORED:
+            printf("Restored");
+            break;
+        default:
+            printf("Unknown Event");
+    }
+    printf("\n");
+    return 0;
+}
+
+int  myEventAddHandleFunc(int fd, int event, virEventHandleCallback cb, void *opaque)
+{
+    DEBUG("Add handle %d %d %p %p", fd, event, cb, opaque);
+    g_fd = fd;
+    g_event = event;
+    g_cb = cb;
+    g_opaque = opaque;
+    return 0;
+}
+void myEventUpdateHandleFunc(int fd, int event)
+{
+    DEBUG("Updated Handle %d %d", fd, event);
+    return;
+}
+int  myEventRemoveHandleFunc(int fd)
+{
+    DEBUG("Removed Handle %d", fd);
+    return 0;
+}
+
+void virEventRegisterHandleImpl(virEventAddHandleFunc addHandle,
+                                virEventUpdateHandleFunc updateHandle,
+                                virEventRemoveHandleFunc removeHandle);
+
+void usage(const char *pname)
+{
+    printf("%s uri\n", pname);
+}
+int main(int argc, char **argv)
+{
+    int run=1;
+    int sts;
+
+    if(argv[1] && strcmp(argv[1],"--help")==0) { 
+        usage(argv[0]);
+        return -1;
+    }
+    virEventRegisterHandleImpl( myEventAddHandleFunc, 
+                                myEventUpdateHandleFunc,
+                                myEventRemoveHandleFunc);
+    virConnectPtr dconn = NULL;
+    dconn = virConnectOpen (argv[1] ? argv[1] : "qemu:///system");
+    if (!dconn) {
+        printf("error opening\n");
+        return -1;
+    }
+
+    DEBUG0("Registering domain event cb");
+    virConnectDomainEventRegister(dconn, myDomainEventCallback, NULL);
+
+    while(run) {
+        struct pollfd pfd = { .fd = g_fd, 
+                          .events = g_event,
+                          .revents = 0};
+
+        sts = poll(&pfd, 1, TIMEOUT_MS);
+        if (sts == 0) {
+            /* DEBUG0("Poll timeout"); */
+            continue;
+        }
+        if (sts < 0 ) {
+            DEBUG0("Poll failed");
+            continue;
+        }
+        if ( pfd.revents & POLLHUP ) {
+            DEBUG0("Reset by peer");
+            return -1;
+        }
+
+        DEBUG("sts = %d", sts);
+        DEBUG("Calling CB: %p (%d,%d,%p)", g_cb, g_fd, g_event, g_opaque);
+        g_cb(g_fd, g_event, g_opaque);
+        
+    }
+
+    if( dconn && virConnectClose(dconn)<0 ) {
+        printf("error closing\n");
+    }
+    printf("done\n");
+}
+
--- /dev/null	2008-09-18 12:09:06 -04:00
+++ ./Makefile	2008-09-30 12:56:44 -04:00
@@ -0,0 +1,2 @@
+LDFLAGS += -lvirt
+event-test: event-test.c

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