[libvirt-java] [PATCH 44/65] events: handle registration for Reboot events

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

 



Add RebootListener interface in package org.libvirt.event and
implement addRebootListener methods in classes Domain and Connect.

Add removeRebootListener to Connect class.

Signed-off-by: Claudio Bley <cbley@xxxxxxxxxx>
---
 src/main/java/org/libvirt/Connect.java             |   41 ++++++++++++++++++++
 src/main/java/org/libvirt/Domain.java              |   17 ++++++++
 .../java/org/libvirt/event/RebootListener.java     |   15 +++++++
 src/main/java/org/libvirt/jna/Libvirt.java         |    4 ++
 4 files changed, 77 insertions(+)
 create mode 100644 src/main/java/org/libvirt/event/RebootListener.java

diff --git a/src/main/java/org/libvirt/Connect.java b/src/main/java/org/libvirt/Connect.java
index 5637337..bbbd000 100644
--- a/src/main/java/org/libvirt/Connect.java
+++ b/src/main/java/org/libvirt/Connect.java
@@ -18,6 +18,7 @@ import org.libvirt.jna.StorageVolPointer;
 import org.libvirt.jna.StreamPointer;
 import org.libvirt.jna.virConnectAuth;
 import org.libvirt.jna.virNodeInfo;
+import org.libvirt.event.*;
 
 import static org.libvirt.Library.libvirt;
 import static org.libvirt.Library.getConstant;
@@ -455,6 +456,46 @@ public class Connect {
         domainEventRegister(null, l);
     }
 
+    void domainEventRegister(Domain domain, final RebootListener cb) throws LibvirtException {
+        if (cb == null)
+            throw new IllegalArgumentException("RebootCallback cannot be null");
+
+        Libvirt.VirConnectDomainEventGenericCallback virCB = new Libvirt.VirConnectDomainEventGenericCallback() {
+                @Override
+                public void eventCallback(ConnectionPointer virConnectPtr,
+                                          DomainPointer virDomainPointer,
+                                          Pointer opaque) {
+                    assert VCP.equals(virConnectPtr);
+
+                    try {
+                        Domain d = Domain.constructIncRef(Connect.this, virDomainPointer);
+                        cb.onReboot(d);
+                    } catch (LibvirtException e) {
+                        throw new RuntimeException("libvirt error in reboot callback", e);
+                    }
+                }
+            };
+
+        domainEventRegister(domain, DomainEventID.REBOOT, virCB, cb);
+    }
+
+    /**
+     * Adds the specified reboot listener to receive reboot events for
+     * domains of this connection.
+     *
+     * @param l   the reboot listener
+     * @throws    LibvirtException on failure
+     *
+     * @see Domain#addRebootListener
+     * @see <a
+     *       href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectDomainEventRegisterAny";
+     *      >virConnectDomainEventRegisterAny</a>
+     * @since 1.5.2
+     */
+    public void addRebootListener(final RebootListener l) throws LibvirtException {
+        domainEventRegister(null, l);
+    }
+
     /**
      * Removes the specified I/O error listener so that it no longer
      * receives I/O error events.
diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index f37f299..010fee0 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -13,6 +13,7 @@ import org.libvirt.jna.virDomainJobInfo;
 import org.libvirt.jna.virDomainMemoryStats;
 import org.libvirt.jna.virSchedParameter;
 import org.libvirt.jna.virVcpuInfo;
+import org.libvirt.event.RebootListener;
 import static org.libvirt.Library.libvirt;
 import static org.libvirt.ErrorHandler.processError;
 import static org.libvirt.ErrorHandler.processErrorIfZero;
@@ -1047,6 +1048,22 @@ public class Domain {
     }
 
     /**
+     * Adds the specified listener to receive reboot events for this domain.
+     *
+     * @param  l   the reboot listener
+     * @throws     LibvirtException on failure
+     *
+     * @see Connect#addRebootListener
+     * @see <a
+     *       href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectDomainEventRegisterAny";
+     *      >virConnectDomainEventRegisterAny</a>
+     * @since 1.5.2
+     */
+    public void addRebootListener(final RebootListener l) throws LibvirtException {
+        virConnect.domainEventRegister(this, l);
+    }
+
+    /**
      * Revert the domain to a given snapshot.
      *
      * @see <a href=
diff --git a/src/main/java/org/libvirt/event/RebootListener.java b/src/main/java/org/libvirt/event/RebootListener.java
new file mode 100644
index 0000000..1b440c0
--- /dev/null
+++ b/src/main/java/org/libvirt/event/RebootListener.java
@@ -0,0 +1,15 @@
+package org.libvirt.event;
+
+import org.libvirt.Domain;
+
+/**
+ * Interface for receiving domain reboot events.
+ */
+public interface RebootListener extends EventListener {
+    /**
+     * This method gets called upon a domain reboot event.
+     *
+     * @param domain   the domain which was rebooted
+     */
+    void onReboot(Domain domain);
+}
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 0e36251..913fe08 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -85,6 +85,10 @@ public interface Libvirt extends Library {
                            Pointer opaque);
     }
 
+    interface VirConnectDomainEventGenericCallback extends VirDomainEventCallback {
+        void eventCallback(ConnectionPointer virConnectPtr, DomainPointer virDomainPointer, Pointer opaque);
+    }
+
     /**
      * Error callback
      */
-- 
1.7.9.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]