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

[libvirt] [PATCH 3/8] domain_event: Add virDomainEventState structure



This structure will be used to unify lots of duplicated event handling code
across the state drivers.

Signed-off-by: Cole Robinson <crobinso redhat com>
---
 src/conf/domain_event.c |   91 ++++++++++++++++++++++++++++++++++++----------
 src/conf/domain_event.h |   47 +++++++++++++++++-------
 2 files changed, 104 insertions(+), 34 deletions(-)

diff --git a/src/conf/domain_event.c b/src/conf/domain_event.c
index 5f086bd..4f0e4da 100644
--- a/src/conf/domain_event.c
+++ b/src/conf/domain_event.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include "domain_event.h"
+#include "event.h"
 #include "logging.h"
 #include "datatypes.h"
 #include "memory.h"
@@ -505,6 +506,25 @@ void virDomainEventFree(virDomainEventPtr event)
     VIR_FREE(event);
 }
 
+/**
+ * virDomainEventQueueFree:
+ * @queue: pointer to the queue
+ *
+ * Free the memory in the queue. We process this like a list here
+ */
+void
+virDomainEventQueueFree(virDomainEventQueuePtr queue)
+{
+    int i;
+    if (!queue)
+        return;
+
+    for (i = 0; i < queue->count ; i++) {
+        virDomainEventFree(queue->events[i]);
+    }
+    VIR_FREE(queue->events);
+    VIR_FREE(queue);
+}
 
 virDomainEventQueuePtr virDomainEventQueueNew(void)
 {
@@ -518,6 +538,57 @@ virDomainEventQueuePtr virDomainEventQueueNew(void)
     return ret;
 }
 
+/**
+ * virDomainEventStateFree:
+ * @list: virDomainEventStatePtr to free
+ *
+ * Free a virDomainEventStatePtr and its members, and unregister the timer.
+ */
+void
+virDomainEventStateFree(virDomainEventStatePtr state)
+{
+    virDomainEventCallbackListFree(state->callbacks);
+    virDomainEventQueueFree(state->queue);
+
+    if (state->timer != -1)
+        virEventRemoveTimeout(state->timer);
+}
+
+virDomainEventStatePtr
+virDomainEventStateNew(virEventTimeoutCallback timeout_cb,
+                       void *timeout_opaque,
+                       virFreeCallback timeout_free)
+{
+    virDomainEventStatePtr state = NULL;
+
+    if (VIR_ALLOC(state) < 0) {
+        virReportOOMError();
+        goto error;
+    }
+
+    if (VIR_ALLOC(state->callbacks) < 0) {
+        virReportOOMError();
+        goto error;
+    }
+
+    if (!(state->queue = virDomainEventQueueNew())) {
+        goto error;
+    }
+
+    if ((state->timer = virEventAddTimeout(-1,
+                                           timeout_cb,
+                                           timeout_opaque,
+                                           timeout_free)) < 0) {
+        goto error;
+    }
+
+    return state;
+
+error:
+    virDomainEventStateFree(state);
+    return NULL;
+}
+
 static virDomainEventPtr virDomainEventNewInternal(int eventID,
                                                    int id,
                                                    const char *name,
@@ -776,26 +847,6 @@ virDomainEventPtr virDomainEventGraphicsNewFromObj(virDomainObjPtr obj,
 
 
 /**
- * virDomainEventQueueFree:
- * @queue: pointer to the queue
- *
- * Free the memory in the queue. We process this like a list here
- */
-void
-virDomainEventQueueFree(virDomainEventQueuePtr queue)
-{
-    int i;
-    if (!queue)
-        return;
-
-    for (i = 0; i < queue->count ; i++) {
-        virDomainEventFree(queue->events[i]);
-    }
-    VIR_FREE(queue->events);
-    VIR_FREE(queue);
-}
-
-/**
  * virDomainEventQueuePop:
  * @evtQueue: the queue of events
  *
diff --git a/src/conf/domain_event.h b/src/conf/domain_event.h
index e28293d..98a2870 100644
--- a/src/conf/domain_event.h
+++ b/src/conf/domain_event.h
@@ -25,6 +25,7 @@
 #ifndef __DOMAIN_EVENT_H__
 # define __DOMAIN_EVENT_H__
 
+# include "event.h"
 # include "domain_conf.h"
 
 typedef struct _virDomainEventCallback virDomainEventCallback;
@@ -38,6 +39,33 @@ struct _virDomainEventCallbackList {
 typedef struct _virDomainEventCallbackList virDomainEventCallbackList;
 typedef virDomainEventCallbackList *virDomainEventCallbackListPtr;
 
+/**
+ * Dispatching domain events that come in while
+ * in a call / response rpc
+ */
+typedef struct _virDomainEvent virDomainEvent;
+typedef virDomainEvent *virDomainEventPtr;
+
+struct _virDomainEventQueue {
+    unsigned int count;
+    virDomainEventPtr *events;
+};
+typedef struct _virDomainEventQueue virDomainEventQueue;
+typedef virDomainEventQueue *virDomainEventQueuePtr;
+
+struct _virDomainEventState {
+    /* The list of domain event callbacks */
+    virDomainEventCallbackListPtr callbacks;
+    /* The queue of domain events */
+    virDomainEventQueuePtr queue;
+    /* Timer for flushing events queue */
+    int timer;
+    /* Flag if we're in process of dispatching */
+    int isDispatching;
+};
+typedef struct _virDomainEventState virDomainEventState;
+typedef virDomainEventState *virDomainEventStatePtr;
+
 void virDomainEventCallbackListFree(virDomainEventCallbackListPtr list);
 
 int virDomainEventCallbackListAdd(virConnectPtr conn,
@@ -91,20 +119,6 @@ int virDomainEventCallbackListEventID(virConnectPtr conn,
                                       int callbackID)
     ATTRIBUTE_NONNULL(1);
 
-/**
- * Dispatching domain events that come in while
- * in a call / response rpc
- */
-typedef struct _virDomainEvent virDomainEvent;
-typedef virDomainEvent *virDomainEventPtr;
-
-struct _virDomainEventQueue {
-    unsigned int count;
-    virDomainEventPtr *events;
-};
-typedef struct _virDomainEventQueue virDomainEventQueue;
-typedef virDomainEventQueue *virDomainEventQueuePtr;
-
 virDomainEventQueuePtr virDomainEventQueueNew(void);
 
 virDomainEventPtr virDomainEventNew(int id, const char *name, const unsigned char *uuid, int type, int detail);
@@ -163,6 +177,11 @@ virDomainEventQueuePop(virDomainEventQueuePtr evtQueue);
 
 void virDomainEventFree(virDomainEventPtr event);
 void virDomainEventQueueFree(virDomainEventQueuePtr queue);
+void virDomainEventStateFree(virDomainEventStatePtr state);
+virDomainEventStatePtr
+virDomainEventStateNew(virEventTimeoutCallback timeout_cb,
+                       void *timeout_opaque,
+                       virFreeCallback timeout_free);
 
 typedef void (*virDomainEventDispatchFunc)(virConnectPtr conn,
                                            virDomainEventPtr event,
-- 
1.7.3.3


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