[libvirt] [PATCH] Initial work on the JNA port, still a work in progress
Bryan Kearney
bkearney at redhat.com
Mon Jul 27 23:05:05 UTC 2009
Daniel Veillard wrote:
> On Sat, Jul 25, 2009 at 08:02:08AM -0400, Bryan Kearney wrote:
>> ---
>> src/org/libvirt/Connect.java | 336 +++++++++++++++++++---------------
>> src/org/libvirt/Domain.java | 183 +++++++++++--------
>> src/org/libvirt/Error.java | 21 ++
>> src/org/libvirt/ErrorHandler.java | 22 +++
>> src/org/libvirt/Network.java | 74 +++++---
>> src/org/libvirt/NodeInfo.java | 16 ++
>> src/org/libvirt/jna/Libvirt.java | 66 +++++++
>> src/org/libvirt/jna/virError.java | 19 ++
>> src/org/libvirt/jna/virNodeInfo.java | 19 ++
>> src/test.java | 237 ++++++++++++------------
>> 10 files changed, 630 insertions(+), 363 deletions(-)
>> create mode 100644 src/org/libvirt/ErrorHandler.java
>> create mode 100644 src/org/libvirt/jna/Libvirt.java
>> create mode 100644 src/org/libvirt/jna/virError.java
>> create mode 100644 src/org/libvirt/jna/virNodeInfo.java
>>
>> diff --git a/src/org/libvirt/Connect.java b/src/org/libvirt/Connect.java
> [...]
>> +import org.libvirt.jna.Libvirt;
>> +import org.libvirt.jna.virError;
>> +import org.libvirt.jna.virNodeInfo;
>> +
>> +import com.sun.jna.Native;
>> +import com.sun.jna.Pointer;
>> +import com.sun.jna.ptr.ByReference;
>> +import com.sun.jna.ptr.LongByReference;
>
> As said before that new dependancy should be fine
>
>> /**
>> * the native virConnectPtr.
>> */
>> - long VCP;
>> + protected Pointer VCP;
>
> that
>
>> - private static native int _virInitialize();
>
> and the removal of all the "private native" look a real improvement !
>
>> diff --git a/src/org/libvirt/Error.java b/src/org/libvirt/Error.java
>> index ef05702..ada7be8 100644
>> --- a/src/org/libvirt/Error.java
>> +++ b/src/org/libvirt/Error.java
>> @@ -2,6 +2,8 @@ package org.libvirt;
>>
>> import java.io.Serializable;
>>
>> +import org.libvirt.jna.virError;
>> +
>> public class Error implements Serializable {
>>
>> public static enum ErrorDomain {
>> @@ -308,6 +310,25 @@ public class Error implements Serializable {
>> int int1;
>> int int2;
>> long VNP; /* Deprecated */
>> +
>> + public Error() {
>> +
>> + }
>> +
>> + public Error(virError vError) {
>> + code = ErrorNumber.values()[vError.code] ;
>> + domain = ErrorDomain.values()[vError.domain] ;
>> + level = ErrorLevel.values()[vError.level] ;
>> + message = vError.message ;
>> + str1 = vError.str1 ;
>> + str2 = vError.str2 ;
>> + str3 = vError.str3 ;
>> + int1 = vError.int1 ;
>> + int2 = vError.int2 ;
>> + VCP = vError.conn ;
>> + VDP = vError.dom ;
>> + VNP = vError.net ;
>> + }
>>
>> /**
>
> I think there are some cleanups needed w.r.t. spaces at end of lines
All whitespace errors were removed.
>
>> +++ b/src/org/libvirt/ErrorHandler.java
>> @@ -0,0 +1,22 @@
>> +package org.libvirt;
>> +
>> +import org.libvirt.jna.Libvirt;
>> +import org.libvirt.jna.virError;
>> +
>> +import com.sun.jna.Pointer;
>> +
>> +public class ErrorHandler implements Libvirt.virErrorFunc
>> +{
>> + public static ErrorHandler INSTANCE = new ErrorHandler() ;
>> +
>> + @Override
>> + public void handleError(Pointer userData, virError vError) throws LibvirtException
>> + {
>> + System.out.println("Hello") ;
>> + if (vError != null) {
>> + Error error = new Error(vError) ;
>> + throw new LibvirtException(error) ;
>> + }
>> +
>> + }
>> +}
>
> That look rather simple
This was a work in progress. The later version does not have this.
>
>> diff --git a/src/org/libvirt/Network.java b/src/org/libvirt/Network.java
>> index 6fda985..a194ca7 100644
>> --- a/src/org/libvirt/Network.java
>> +++ b/src/org/libvirt/Network.java
>> @@ -1,15 +1,25 @@
>> package org.libvirt;
>>
>> +import org.libvirt.jna.Libvirt;
>> +
>> +import com.sun.jna.Pointer;
>> +
>> public class Network {
>>
>> /**
>> * The native virNetworkPtr
>> */
>> - private long VNP;
>> + protected Pointer VNP;
>
> For my own education, that means that subclasses implementations may
> still use iit instead of keeping it fully private, right ?
Correct. Makes this much more friendly for subclassing.
>
>> @@ -18,9 +28,10 @@ public class Network {
>> * @param virConnect
>> * @param VNP
>> */
>> - Network(Connect virConnect, long VNP){
>> + Network(Connect virConnect, Pointer VNP){
>> this.virConnect = virConnect;
>> this.VNP = VNP;
>> + this.libvirt = virConnect.libvirt ;
>> }
>
> I think we are slightly breaking the API here but in a way that should
> be compatible with existing code, since VNP was returned from the
> library, right ?
We are. The core libvirt classes now return Pointers. So, the only issue
is if the caller was storing the pointer in their own code. Hopefully,
this was not being done.
>
> [...]
>> diff --git a/src/org/libvirt/NodeInfo.java b/src/org/libvirt/NodeInfo.java
>> index b7e2840..16855ca 100644
>> --- a/src/org/libvirt/NodeInfo.java
>> +++ b/src/org/libvirt/NodeInfo.java
>> @@ -1,5 +1,7 @@
>> package org.libvirt;
>>
>> +import org.libvirt.jna.virNodeInfo;
>> +
>> public class NodeInfo {
>> /**
>> * string indicating the CPU model
>> @@ -34,6 +36,20 @@ public class NodeInfo {
>> */
>> public int threads;
>>
>> +
>> + public NodeInfo() {
>> + }
>> +
>> + public NodeInfo(virNodeInfo vInfo) {
>> +// this.model = new String(vInfo.model) ;
>
> err, why ?
>
This is now a call to the JNA Native.toString() call. It abstracts out
some of the nuttiness of crossing the UTF-8 boundary.
>> + this.memory = vInfo.memory.longValue() ;
>> + this.cpus = vInfo.cpus ;
>> + this.mhz = vInfo.mhz ;
>> + this.nodes = vInfo.nodes ;
>> + this.sockets = vInfo.sockets ;
>> + this.cores = vInfo.cores ;
>> + this.threads = vInfo.threads ;
>> + }
>> /**
>> * @return the total number of CPUs supported but not neccessarily active in the host.
>> */
>> diff --git a/src/org/libvirt/jna/Libvirt.java b/src/org/libvirt/jna/Libvirt.java
>> new file mode 100644
>> index 0000000..1874d3e
>> --- /dev/null
>> +++ b/src/org/libvirt/jna/Libvirt.java
>> @@ -0,0 +1,66 @@
>> +package org.libvirt.jna;
>> +
>> +
>> +import com.sun.jna.Callback;
>> +import com.sun.jna.Library ;
>> +import com.sun.jna.Native;
>> +import com.sun.jna.Pointer;
>> +import com.sun.jna.Structure.ByReference;
>> +import com.sun.jna.ptr.LongByReference;
>> +import com.sun.jna.ptr.PointerByReference;
>> +
>> +public interface Libvirt extends Library
>> +{
>> + Libvirt INSTANCE = (Libvirt) Native.loadLibrary("libvirt", Libvirt.class) ;
>> +
>> + //Callbacks
>> + interface virErrorFunc extends Callback {
>> + void handleError(Pointer userData, virError error) throws Exception ;
>> + }
>> +
>> + // Global functions
>> + public virError virGetLastError() ;
>> + public int virGetVersion(LongByReference libVer, String type, LongByReference typeVer) ;
>> + public int virInitialize() ;
>> + public void virSetErrorFunc(long userData, virErrorFunc handler) ;
>> +
>> + //Connection Functions
>> + public int virConnCopyLastError(Pointer virConnectPtr) ;
>> + public int virConnectClose(Pointer virConnectPtr) ;
>> + public String virConnectGetCapabilities(Pointer virConnectPtr) ;
>> + public String virConnectGetHostname(Pointer virConnectPtr) ;
>> + public virError virConnGetLastError(Pointer virConnectPtr) ;
>> + public int virConnectGetMaxVcpus(Pointer virConnectPtr, String type) ;
>> + public String virConnectGetType(Pointer virConnectPtr) ;
>> + public String virConnectGetURI(Pointer virConnectPtr) ;
>> + public int virConnectGetVersion(Pointer virConnectPtr, LongByReference hvVer) ;
>> + public int virConnectListDomains(Pointer virConnectPtr, int[] ids, int maxnames) ;
>> + public int virConnectListNetworks(Pointer virConnectPtr, String[] name, int maxnames) ;
>> + public int virConnectListDefinedDomains(Pointer virConnectPtr, String[] name, int maxnames) ;
>> + public int virConnectListDefinedNetworks(Pointer virConnectPtr, String[] name, int maxnames) ;
>> + public int virConnectNumOfDomains(Pointer virConnectPtr) ;
>> + public int virConnectNumOfDefinedDomains(Pointer virConnectPtr) ;
>> + public int virConnectNumOfDefinedNetworks(Pointer virConnectPtr) ;
>> + public int virConnectNumOfNetworks(Pointer virConnectPtr) ;
>> + public Pointer virConnectOpen(String name) ;
>> + public Pointer virConnectOpenReadOnly(String name) ;
>> + public void virConnSetErrorFunc(Pointer virConnectPtr, long userData, virErrorFunc handler) ;
>> +
>> + // Node functions
>> + public int virNodeGetInfo(Pointer virConnectPtr, virNodeInfo virNodeInfo) ;
>> +
>> + // Network functions
>> + public Pointer virNetworkCreateXML(Pointer virConnectPtr, String xmlDesc) ;
>> + public Pointer virNetworkDefineXML(Pointer virConnectPtr, String xmlDesc) ;
>> + public String virNetworkGetName(Pointer virNetorkPtr) ;
>> + public Pointer virNetworkLookupByName(Pointer virConnectPtr, String name) ;
>> + public Pointer virNetworkLookupByUUIDString(Pointer virConnectPtr, String uuidstr) ;
>> + public Pointer virNetworkLookupByUUID(Pointer virConnectPtr, String uuidstr) ;
>> +
>> + // Domain functions
>> + public Pointer virDomainCreateLinux(Pointer virConnectPtr, String xmlDesc, int flags) ;
>> + public Pointer virDomainCreateXML(Pointer virConnectPtr, String xmlDesc, int flags) ;
>> + public Pointer virDomainDefineXML(Pointer virConnectPtr, String xmlDesc) ;
>> + public Pointer virDomainLookupByUUIDString(Pointer virConnectPtr, String uuidstr) ;
>> +
>> +}
>
> That's refreshingly simple !!!
>
>> diff --git a/src/org/libvirt/jna/virError.java b/src/org/libvirt/jna/virError.java
>> new file mode 100644
>> index 0000000..db462fa
>> --- /dev/null
>> +++ b/src/org/libvirt/jna/virError.java
>> @@ -0,0 +1,19 @@
>> +package org.libvirt.jna;
>> +
>> +import com.sun.jna.Structure ;
>> +
>> +public class virError extends Structure
>> +{
>> + public int code ;
>> + public int domain ;
>> + public String message ;
>> + public int level ;
>> + public long conn ;
>> + public long dom ;
>> + public String str1 ;
>> + public String str2 ;
>> + public String str3 ;
>> + public int int1 ;
>> + public int int2 ;
>> + public long net ;
>> +}
>
> same here
>
>> diff --git a/src/org/libvirt/jna/virNodeInfo.java b/src/org/libvirt/jna/virNodeInfo.java
>> new file mode 100644
>> index 0000000..5a6449e
>> --- /dev/null
>> +++ b/src/org/libvirt/jna/virNodeInfo.java
>> @@ -0,0 +1,19 @@
>> +package org.libvirt.jna;
>> +
>> +import com.sun.jna.NativeLong;
>> +import com.sun.jna.Structure;
>> +
>> +public class virNodeInfo extends Structure
>> +{
>> + public class ByValue extends virNodeInfo implements Structure.ByValue {};
>> + public class ByReference extends virNodeInfo implements Structure.ByReference {};
>> +
>> + public byte model[] = new byte[32];
>> + public NativeLong memory ;
>> + public int cpus ;
>> + public int mhz ;
>> + public int nodes ;
>> + public int sockets ;
>> + public int cores ;
>> + public int threads ;
>> +}
>
> and here
>
>> \ No newline at end of file
>
> Should be fixed, only one though !
> It would be great to trick your editor into showing extra spaces at the
> end of lines.
I ran it though the auto formatter.
>
>> diff --git a/src/test.java b/src/test.java
>> index 73f4eb7..2ca6a92 100644
>> --- a/src/test.java
>> +++ b/src/test.java
>> @@ -18,20 +18,21 @@ public class test {
>> Integer.decode("0xf0"), Integer.decode("0x3c"), Integer.decode("0x87"), Integer.decode("0xd2"), Integer.decode("0x1e"), Integer.decode("0x69")} ;
>>
>
> I just hope that at the end of the patch series the test file is back
> to its original state. Maybe not worth fixing in all intermediary
> steps ...
>
> Daniel
>
Yes, the last patch set has the entire test class un-commented.
-- bk
More information about the libvir-list
mailing list