[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r2929 - in freeway: native src/org/gnu/freeway/cwrappers sr
From: |
grothoff |
Subject: |
[GNUnet-SVN] r2929 - in freeway: native src/org/gnu/freeway/cwrappers src/org/gnu/freeway/cwrappers/util src/org/gnu/freeway/server |
Date: |
Sun, 28 May 2006 13:33:35 -0700 (PDT) |
Author: grothoff
Date: 2006-05-28 13:33:32 -0700 (Sun, 28 May 2006)
New Revision: 2929
Modified:
freeway/native/org_gnu_freeway_server_CPluginLoader.c
freeway/native/org_gnu_freeway_server_CPluginLoader.h
freeway/src/org/gnu/freeway/cwrappers/CInt.java
freeway/src/org/gnu/freeway/cwrappers/CLong.java
freeway/src/org/gnu/freeway/cwrappers/CString.java
freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java
freeway/src/org/gnu/freeway/cwrappers/ConstCInt.java
freeway/src/org/gnu/freeway/cwrappers/ConstCLong.java
freeway/src/org/gnu/freeway/cwrappers/ConstCString.java
freeway/src/org/gnu/freeway/cwrappers/ConstCUnsignedInt.java
freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java
freeway/src/org/gnu/freeway/server/CPluginLoader.java
freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java
Log:
function type kind determination code
Modified: freeway/native/org_gnu_freeway_server_CPluginLoader.c
===================================================================
--- freeway/native/org_gnu_freeway_server_CPluginLoader.c 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/native/org_gnu_freeway_server_CPluginLoader.c 2006-05-28
20:33:32 UTC (rev 2929)
@@ -185,7 +185,7 @@
}
JNIEXPORT jobject JNICALL Java_org_gnu_freeway_server_CPluginLoader_cCallC
-(JNIEnv *env, jobject cls, jlong modulePtr, jobject capi, jint functionOffset,
jint functionType, jobjectArray arguments, jint callType) {
+(JNIEnv *env, jobject cls, jlong modulePtr, jobject capi, jint functionOffset,
jint functionType, jobjectArray arguments) {
ModuleList * m = (ModuleList*) (long) modulePtr;
jobject oret = 0;
Modified: freeway/native/org_gnu_freeway_server_CPluginLoader.h
===================================================================
--- freeway/native/org_gnu_freeway_server_CPluginLoader.h 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/native/org_gnu_freeway_server_CPluginLoader.h 2006-05-28
20:33:32 UTC (rev 2929)
@@ -21,7 +21,7 @@
* Signature:
(JLorg/gnu/freeway/server/CoreAPI;II[Ljava/lang/Object;I)Ljava/lang/Object;
*/
JNIEXPORT jobject JNICALL Java_org_gnu_freeway_server_CPluginLoader_cCallC
- (JNIEnv *, jclass, jlong, jobject, jint, jint, jobjectArray, jint);
+ (JNIEnv *, jclass, jlong, jobject, jint, jint, jobjectArray);
/*
* Class: org_gnu_freeway_server_CPluginLoader
Modified: freeway/src/org/gnu/freeway/cwrappers/CInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CInt.java 2006-05-28 20:22:54 UTC
(rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/CInt.java 2006-05-28 20:33:32 UTC
(rev 2929)
@@ -28,6 +28,9 @@
*/
public class CInt extends ConstCInt implements CWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.INT_KIND;
+
public CInt(int value) {
super(value);
}
Modified: freeway/src/org/gnu/freeway/cwrappers/CLong.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CLong.java 2006-05-28 20:22:54 UTC
(rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/CLong.java 2006-05-28 20:33:32 UTC
(rev 2929)
@@ -28,6 +28,9 @@
*/
public class CLong extends ConstCLong implements CWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.LINT_KIND;
+
public CLong(long value) {
super(value);
}
Modified: freeway/src/org/gnu/freeway/cwrappers/CString.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CString.java 2006-05-28 20:22:54 UTC
(rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/CString.java 2006-05-28 20:33:32 UTC
(rev 2929)
@@ -29,6 +29,9 @@
*/
public class CString extends ConstCString implements CWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.PTR_KIND;
+
public CString(String value) {
super(value);
}
Modified: freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/CUnsignedInt.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -28,6 +28,9 @@
*/
public class CUnsignedInt extends ConstCUnsignedInt implements CWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.INT_KIND;
+
public CUnsignedInt(long value) {
super(value);
}
Modified: freeway/src/org/gnu/freeway/cwrappers/ConstCInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/ConstCInt.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/ConstCInt.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -19,6 +19,7 @@
package org.gnu.freeway.cwrappers;
+import org.gnu.freeway.cwrappers.util.CWrapper;
import org.gnu.freeway.cwrappers.util.ConstCWrapper;
/**
@@ -28,6 +29,9 @@
*/
public class ConstCInt implements ConstCWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.INT_KIND;
+
protected int value;
public ConstCInt(int value) {
Modified: freeway/src/org/gnu/freeway/cwrappers/ConstCLong.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/ConstCLong.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/ConstCLong.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -19,6 +19,7 @@
package org.gnu.freeway.cwrappers;
+import org.gnu.freeway.cwrappers.util.CWrapper;
import org.gnu.freeway.cwrappers.util.ConstCWrapper;
/**
@@ -28,6 +29,9 @@
*/
public class ConstCLong implements ConstCWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.LINT_KIND;
+
protected long value;
public ConstCLong(long value) {
Modified: freeway/src/org/gnu/freeway/cwrappers/ConstCString.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/ConstCString.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/ConstCString.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -19,6 +19,7 @@
package org.gnu.freeway.cwrappers;
+import org.gnu.freeway.cwrappers.util.CWrapper;
import org.gnu.freeway.cwrappers.util.ConstCWrapper;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
@@ -30,6 +31,9 @@
*/
public class ConstCString implements ConstCWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.PTR_KIND;
+
protected String value;
public ConstCString(String value) {
Modified: freeway/src/org/gnu/freeway/cwrappers/ConstCUnsignedInt.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/ConstCUnsignedInt.java
2006-05-28 20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/ConstCUnsignedInt.java
2006-05-28 20:33:32 UTC (rev 2929)
@@ -19,6 +19,7 @@
package org.gnu.freeway.cwrappers;
+import org.gnu.freeway.cwrappers.util.CWrapper;
import org.gnu.freeway.cwrappers.util.ConstCWrapper;
/**
@@ -28,6 +29,9 @@
*/
public class ConstCUnsignedInt implements ConstCWrapper {
+ @SuppressWarnings("unused")
+ private static final int KIND = CWrapper.INT_KIND;
+
protected long value;
public ConstCUnsignedInt(long value) {
Modified: freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/cwrappers/util/CWrapper.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -28,5 +28,19 @@
* Freeway and plugins that may be written in C.
*/
public interface CWrapper extends ConstCWrapper {
+
+ /**
+ * Each CWrapper must have a private static final field "KIND"
+ * (with a value between 0 and MAX_KIND-1) that specifies the
+ * kind of C calling convention that applies.
+ */
+ public static final int MAX_KIND = 6;
+ public static final int INT_KIND = 0;
+ public static final int PTR_KIND = 1;
+ public static final int FLT_KIND = 2;
+ public static final int LINT_KIND = 3;
+ public static final int DBLE_KIND = 4;
+ public static final int VOID_KIND = MAX_KIND;
+
public void deserializeFromByteArray(byte[] serializedData);
}
Modified: freeway/src/org/gnu/freeway/server/CPluginLoader.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CPluginLoader.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/server/CPluginLoader.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -1,66 +1,144 @@
- /*
- This file is part of Freeway
+/*
+ This file is part of Freeway
- Freeway is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
- option) any later version.
+ Freeway is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 2, or (at your
+ option) any later version.
- Freeway is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ Freeway is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Freeway; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ You should have received a copy of the GNU General Public License
+ along with Freeway; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
*/
-
package org.gnu.freeway.server;
-import org.gnu.freeway.cwrappers.CLibraryHandle;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import org.gnu.freeway.cwrappers.util.SwitchTableGenerator;
+
/**
* @file CPluginLoader.java
- * @brief
+ * @brief
* @author mdonoughe
*/
public class CPluginLoader {
-
+
static {
System.loadLibrary("org_gnu_freeway_server_CPluginLoader");
}
-
+
private static native long cLoadService(String serviceName, CoreAPI
capi);
- private static native Object cCallC(long modulePtr,
- CoreAPI capi,
- int functionOffset,
- int functionType,
- Object[] arguments,
- int callType);
+
+ private static native Object cCallC(long modulePtr, CoreAPI capi,
+ int functionOffset, int functionType, Object[]
arguments);
+
private static native void cUnloadService(long modulePtr);
- public static CLibraryHandle loadService(String serviceName) {
+
+ public CPluginLoader() {
+ }
+
+ /**
+ * This is the actual public CPluginLoaderAPI. The basic
+ * idea is that a CPluginLoader is given the name of an
+ * interface (say "StatsService") and it will create an
+ * instance of that service (which is implemented by
+ * loading the respective C plugin).
+ *
+ * @param serviceName
+ * @return null on error
+ */
+ public Object createService(String serviceName) {
+ try {
+ Class api =
Class.forName("org.gnu.freeway.services.impl." + serviceName);
+ return api.getConstructor(new Class[] {
CPluginLoader.class}).newInstance(new Object[] { this });
+ } catch (ClassNotFoundException cnfe) {
+ return null;
+ } catch (IllegalAccessException iae) {
+ return null;
+ } catch (NoSuchMethodException nsme) {
+ return null;
+ } catch (InvocationTargetException ite) {
+ return null;
+ } catch (InstantiationException ie) {
+ return null;
+ } catch (Error e) {
+ return null;
+ }
+ }
+
+ /**
+ * This method is used by the generated "services.impl"
+ * constructors to load the C plugin.
+ *
+ * @param serviceName
+ * @return
+ */
+ public Handle loadService(String serviceName) {
assert (serviceName != null);
- return new CLibraryHandle(cLoadService(serviceName, CoreAPI._));
+ return new Handle(cLoadService(serviceName, CoreAPI._));
}
- public static Object callC(CLibraryHandle modulePtr,
- int functionOffset,
- int functionType,
- Object[] arguments,
- int callType) {
- if(modulePtr._ == 0)
- throw new NullPointerException();
- return cCallC(modulePtr._, CoreAPI._,
- functionOffset,
- functionType,
- arguments,
- callType);
+
+ /**
+ * This method is used by the generated "services.impl"
+ * methods to invoke the respective C code.
+ *
+ * Internally, callC will determine the method signature
+ * and method index and then use cCallC to do the actual call.
+ *
+ * @param modulePtr the handle obtained when loading the module
+ * @param method the name of the method to call
+ * @param serviceInstance the "services.impl" object that
+ * is providing the service
+ * @param arguments list of arguments to pass
+ * @return return value (null for void)
+ */
+ public Object callC(Handle modulePtr,
+ String methodName,
+ Object serviceInstance,
+ Object[] arguments) {
+ Class c = serviceInstance.getClass();
+ Method[] methods = c.getMethods();
+ int offset = -1;
+ for (int i=0;i<methods.length;i++)
+ if (methods[i].getName().equals(methodName))
+ offset = i;
+ if (offset == -1)
+ throw new IllegalArgumentException(c + " must have
method " + methodName);
+ int type =
SwitchTableGenerator.getFunctionType(methods[offset]);
+ return cCallC(modulePtr._, CoreAPI._, offset, type, arguments);
}
- public static void unloadService(CLibraryHandle modulePtr) {
- if(modulePtr._ == 0)
- throw new NullPointerException();
+
+ /**
+ * The finalizer of "services.impl" objects calls this
+ * method to unload the C plugin.
+ *
+ * @param modulePtr
+ */
+ public void unloadService(Handle modulePtr) {
+ assert modulePtr != null;
cUnloadService(modulePtr._);
}
+
+ /**
+ * @file CLibraryHandle.java
+ * @brief
+ * @author mdonoughe
+ */
+ public static final class Handle {
+ final long _;
+
+ Handle(long value) {
+ _ = value;
+ assert value != 0;
+ }
+ }
+
}
Modified: freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java 2006-05-28
20:22:54 UTC (rev 2928)
+++ freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java 2006-05-28
20:33:32 UTC (rev 2929)
@@ -2,14 +2,14 @@
import junit.framework.TestCase;
import junit.textui.TestRunner;
-import org.gnu.freeway.cwrappers.CLibraryHandle;
public class CPluginLoaderTest extends TestCase {
public void testService() {
- CLibraryHandle modulePtr =
CPluginLoader.loadService("module_chat");
+ CPluginLoader cpl = new CPluginLoader();
+ CPluginLoader.Handle modulePtr = cpl.loadService("module_chat");
assertFalse(modulePtr._ == 0);
- CPluginLoader.unloadService(modulePtr);
+ cpl.unloadService(modulePtr);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r2929 - in freeway: native src/org/gnu/freeway/cwrappers src/org/gnu/freeway/cwrappers/util src/org/gnu/freeway/server,
grothoff <=