[libvirt] [PATCH 01/12] Introduce event loop support

Claudio Bley cbley at av-test.de
Thu Jan 17 15:18:59 UTC 2013


Add initEventLoop() and processEvent() static methods to the Connect
class.

Signed-off-by: Claudio Bley <cbley at av-test.de>
---
 src/main/java/org/libvirt/Connect.java     |   36 ++++++++++++++++++++++++++++
 src/main/java/org/libvirt/jna/Libvirt.java |    4 ++++
 2 files changed, 40 insertions(+)

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index b3e29be..8da83f3 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -342,6 +342,42 @@ public class Connect {
     }
 
     /**
+     * Registers a default event implementation based on the poll()
+     * system call.
+     * <p>
+     * Once registered, the application has to invoke
+     * {@link #processEvent} in a loop to process events.
+     * <p>
+     * Note: You must call this function before connecting to the
+     *       hypervisor.
+     *
+     * @see #processEvent
+     */
+    public static void initEventLoop() throws LibvirtException {
+        if (libvirt.virEventRegisterDefaultImpl() == -1)
+            ErrorHandler.processError(libvirt);
+    }
+
+    /**
+     * Run one iteration of the event loop.
+     * <p>
+     * Applications will generally want to have a thread which invokes
+     * this method in an infinite loop:
+     * <pre>
+     * {@code while (true) connection.processEvent(); }
+     * </pre>
+     * <p>
+     * Failure to do so may result in connections being closed
+     * unexpectedly as a result of keepalive timeout.
+     *
+     * @see #initEventLoop()
+     */
+    public void processEvent() throws LibvirtException {
+        if (libvirt.virEventRunDefaultImpl() == -1)
+            ErrorHandler.processError(Libvirt.INSTANCE);
+   }
+
+    /**
      * Finds a domain based on the hypervisor ID number.
      *
      * @param id
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 87979cd..b5e7503 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -176,6 +176,10 @@ public interface Libvirt extends Library {
     void virResetLastError();
     void virSetErrorFunc(Pointer userData, VirErrorCallback callback);
 
+    // Event loop functions.
+    int virEventRegisterDefaultImpl();
+    int virEventRunDefaultImpl();
+
     // Domain functions
     int virDomainAbortJob(DomainPointer virDomainPtr);
     int virDomainAttachDevice(DomainPointer virDomainPtr, String deviceXML);
-- 
1.7.9.5




More information about the libvir-list mailing list