gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r3141 - in freeway: . native po src/org/gnu/freeway src/org


From: mdonoughe
Subject: [GNUnet-SVN] r3141 - in freeway: . native po src/org/gnu/freeway src/org/gnu/freeway/cwrappers/util src/org/gnu/freeway/protocol/stats src/org/gnu/freeway/server src/org/gnu/freeway/services/c src/org/gnu/freeway/services/java src/org/gnu/freeway/util src/org/gnu/freeway/util/net
Date: Thu, 27 Jul 2006 01:03:33 -0700 (PDT)

Author: mdonoughe
Date: 2006-07-27 01:02:54 -0700 (Thu, 27 Jul 2006)
New Revision: 3141

Added:
   freeway/native/connection.c
   freeway/native/connection.h
   freeway/native/core.c
   freeway/native/core.h
   freeway/native/handler.c
   freeway/native/handler.h
   freeway/native/java.c
   freeway/native/tcpserver.c
   freeway/native/tcpserver.h
   freeway/native/util.c
   freeway/native/util.h
Removed:
   freeway/native/org_gnu_freeway_server_CPluginLoader.c
   freeway/po/remove-potcdate.sed
Modified:
   freeway/
   freeway/native/Makefile.am
   freeway/native/org_gnu_freeway_server_CPluginLoader.h
   freeway/src/org/gnu/freeway/AbstractApplication.java
   freeway/src/org/gnu/freeway/GNUNetDaemon.java
   freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
   freeway/src/org/gnu/freeway/protocol/stats/NativeStatsProtocol.java
   freeway/src/org/gnu/freeway/protocol/stats/StatsProtocol.java
   freeway/src/org/gnu/freeway/server/CPluginLoader.java
   freeway/src/org/gnu/freeway/server/CSGetClientCount.java
   freeway/src/org/gnu/freeway/server/ClientServer.java
   freeway/src/org/gnu/freeway/server/ConnectionService.java
   freeway/src/org/gnu/freeway/server/CoreAPI.java
   freeway/src/org/gnu/freeway/server/CoreService.java
   freeway/src/org/gnu/freeway/services/c/StatsService.java
   freeway/src/org/gnu/freeway/services/java/StatsService.java
   freeway/src/org/gnu/freeway/util/ServiceManager.java
   freeway/src/org/gnu/freeway/util/net/CSMessage.java
   freeway/src/org/gnu/freeway/util/net/NativeCSHandler.java
   freeway/src/org/gnu/freeway/util/net/PersistentDecoder.java
Log:
 the invocation interface will be used if it is available and a new native 
thread is trying to access the Java code
 unregistering a thread no longer frees memory from a neighborring entry
 only one CoreAPI should ever be created
 native protocols now register themselves properly
 blasted the C side of CPluginLoader into smaller parts
 all JNI calls now pass a CoreAPI into C
 module unloading



Property changes on: freeway
___________________________________________________________________
Name: svk:merge
   - ca0d4bff-9018-0410-8ce9-c5d843b21c37:/local/freeway:66
   + ca0d4bff-9018-0410-8ce9-c5d843b21c37:/local/freeway:69

Modified: freeway/native/Makefile.am
===================================================================
--- freeway/native/Makefile.am  2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/Makefile.am  2006-07-27 08:02:54 UTC (rev 3141)
@@ -1,15 +1,15 @@
 if HAVE_GNUNET
 AM_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\"
 
-lib_LTLIBRARIES = \
-  liborg_gnu_freeway_server_CPluginLoader.la
+lib_LTLIBRARIES = libcpluginloader.la
 
-liborg_gnu_freeway_server_CPluginLoader_la_LDFLAGS = \
+libcpluginloader_la_LDFLAGS = \
   -export-dynamic -version-info 0:0:0 
-liborg_gnu_freeway_server_CPluginLoader_la_LIBADD = \
+libcpluginloader_la_LIBADD = \
   -lgnunetutil $(LIBINTL)
 
-liborg_gnu_freeway_server_CPluginLoader_la_SOURCES = \
-  org_gnu_freeway_server_CPluginLoader.c jni.h \
-  org_gnu_freeway_server_CPluginLoader.h gettext.h
+libcpluginloader_la_SOURCES = core.c connection.c \
+  handler.c tcpserver.c util.c jni.h java.c \
+  org_gnu_freeway_server_CPluginLoader.h gettext.h \
+  connection.h handler.h tcpserver.h util.h core.h
 endif

Added: freeway/native/connection.c
===================================================================
--- freeway/native/connection.c 2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/connection.c 2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,69 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/connection.c
+ * @brief the freeway equivalent of connection.c
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "util.h"
+
+int forAllConnectedNodes(PerNodeCallback method, void * arg) {
+fprintf(stderr, "in forAllConnectedNodes\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jmethod;
+  jobject jarg;
+  jmethodID newHandle;
+  jmethodID methodi;
+  jobject jret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  methodi = (*env)->GetMethodID(env, capiClazz, "forAllConnectedNodes", 
"(Lorg/gnu/freeway/server/CPluginLoader$Handle;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(methodi != NULL);
+  jmethod = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
method);
+  GNUNET_ASSERT(jmethod != NULL);
+  jarg = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) arg);
+  GNUNET_ASSERT(jarg != NULL);
+  jret = (*env)->CallObjectMethod(env, t->jcapi, methodi, jmethod, jarg);
+  GNUNET_ASSERT(jret != NULL);
+fprintf(stderr, "leaving forAllConnectedNodes\n");
+  return convCIntToInt(jret, env);
+}
+

Added: freeway/native/connection.h
===================================================================
--- freeway/native/connection.h 2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/connection.h 2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,27 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/connection.h
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+int forAllConnectedNodes(PerNodeCallback method, void * arg);

Added: freeway/native/core.c
===================================================================
--- freeway/native/core.c       2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/core.c       2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,192 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/core.c
+ * @brief the Freeway equivalent of GNUnet's core.c
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "util.h"
+#include "connection.h"
+#include "handler.h"
+#include "tcpserver.h"
+#include "core.h"
+
+CoreAPIForApplication jcapi;
+
+static int loadApplicationModule(const char * name) {
+fprintf(stderr, "in loadApplicationModule\n");
+  ActiveThreadList * t;
+  jstring mname;
+  jmethodID method;
+  jclass capiClazz;
+  JNIEnv * env;
+  jobject ret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  mname = (*env)->NewStringUTF(env, name);
+  GNUNET_ASSERT(mname != 0);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != 0);
+  method = (*env)->GetMethodID(env, capiClazz, "loadApplicationModule", 
"(Lorg/gnu/freeway/cwrappers/CString;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != 0);
+  ret = (*env)->CallObjectMethod(env, t->jcapi, method, 
convJStringToCString(mname, env));
+fprintf(stderr, "leaving loadApplicationModule\n");
+  return convCIntToInt(ret, env);
+}
+
+static int unloadApplicationModule(const char * name) {
+fprintf(stderr, "in unloadApplicationModule\n");
+  ActiveThreadList * t;
+  jstring mname;
+  jmethodID method;
+  jclass capiClazz;
+  JNIEnv * env;
+  jobject ret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  mname = (*env)->NewStringUTF(env, name);
+  GNUNET_ASSERT(mname != 0);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != 0);
+  method = (*env)->GetMethodID(env, capiClazz, "unloadApplicationModule", 
"(Lorg/gnu/freeway/cwrappers/CString;)V");
+  GNUNET_ASSERT(method != 0);
+  (*env)->CallVoidMethod(env, t->jcapi, method, convJStringToCString(mname, 
env));
+fprintf(stderr, "leaving loadApplicationModule\n");
+  return OK;
+}
+
+static void * requestService(const char * name) {
+fprintf(stderr, "in requestService %s\n", name);
+  ActiveThreadList * t;
+  jstring mname;
+  jmethodID method;
+  jclass capiClazz;
+  JNIEnv * env;
+  jobject retHandle;
+  jlong ret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  mname = (*env)->NewStringUTF(env, name);
+  GNUNET_ASSERT(mname != 0);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != 0);
+  method = (*env)->GetMethodID(env, capiClazz, "requestService", 
"(Lorg/gnu/freeway/cwrappers/CString;)Lorg/gnu/freeway/server/CPluginLoader$Handle;");
+  GNUNET_ASSERT(method != 0);
+  retHandle = (*env)->CallObjectMethod(env, t->jcapi, method, 
convJStringToCString(mname, env));
+  jfieldID handle_ = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, 
retHandle), "_", "J");
+  if(handle_ == NULL)
+    return 0;
+  ret = (*env)->GetLongField(env, retHandle, handle_);
+fprintf(stderr, "leaving requestService: %x\n", ret);
+  return (void *) (long) ret;
+}
+
+static int releaseService(void * ptr) {
+fprintf(stderr, "in releaseService\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jptr;
+  jmethodID newHandle;
+  jmethodID method;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "releaseService", 
"(Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jptr = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) ptr);
+  GNUNET_ASSERT(jptr != NULL);
+  (*env)->CallVoidMethod(env, t->jcapi, method, jptr);
+fprintf(stderr, "leaving releaseService\n");
+  return OK;
+}
+
+void initCore() {
+  // core.c
+  jcapi.version = 0;
+  jcapi.myIdentity = NULL;
+  jcapi.loadApplicationModule = &loadApplicationModule;
+  jcapi.unloadApplicationModule = &unloadApplicationModule;
+  jcapi.requestService = &requestService;
+  jcapi.releaseService = &releaseService;
+
+  // connection.c
+  jcapi.sendPlaintext = NULL;
+  jcapi.unicast = NULL;
+  jcapi.unicastCallback = NULL;
+  jcapi.forAllConnectedNodes = &forAllConnectedNodes;
+  jcapi.registerSendCallback = NULL;
+  jcapi.unregisterSendCallback = NULL;
+  jcapi.registerSendNotify = NULL;
+  jcapi.unregisterSendNotify = NULL;
+  jcapi.offerTSessionFor = NULL;
+  jcapi.assignSessionKey = NULL;
+  jcapi.getCurrentSessionKey = NULL;
+  jcapi.confirmSessionUp = NULL;
+  jcapi.preferTrafficFrom = NULL;
+  jcapi.queryBPMfromPeer = NULL;
+  jcapi.disconnectFromPeer = NULL;
+  jcapi.computeIndex = NULL;
+  jcapi.getConnectionModuleLock = NULL;
+  jcapi.getSlotCount = NULL;
+  jcapi.isSlotUsed = NULL;
+  jcapi.getLastActivityOf = NULL;
+
+  // handler.c
+  jcapi.registerHandler = &registerHandler;
+  jcapi.unregisterHandler = &unregisterHandler;
+  jcapi.isHandlerRegistered = &isHandlerRegistered;
+  jcapi.registerPlaintextHandler = NULL;
+  jcapi.unregisterPlaintextHandler = NULL;
+  jcapi.injectMessage = NULL;
+
+  // tcpserver.c
+  jcapi.sendValueToClient = &sendValueToClient;
+  jcapi.sendToClient = &sendToClient;
+  jcapi.registerClientHandler = &registerClientHandler;
+  jcapi.unregisterClientHandler = &unregisterClientHandler;
+  jcapi.registerClientExitHandler = NULL;
+  jcapi.unregisterClientExitHandler = NULL;
+  jcapi.terminateClientConnection = NULL;
+}

Added: freeway/native/core.h
===================================================================
--- freeway/native/core.h       2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/core.h       2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,29 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/core.h
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <GNUnet/gnunet_util.h>
+
+CoreAPIForApplication jcapi;

Added: freeway/native/handler.c
===================================================================
--- freeway/native/handler.c    2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/handler.c    2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,121 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/connection.c
+ * @brief the freeway equivalent of connection.c
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "util.h"
+
+int registerHandler(unsigned short type, MessagePartHandler callback) {
+fprintf(stderr, "in registerHandler\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jtype;
+  jobject jcallback;
+  jmethodID newHandle;
+  jmethodID method;
+  jobject jret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "registerHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jtype = convIntToCInt((int) type, env);
+  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
+  GNUNET_ASSERT(jcallback != NULL);
+  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
+  GNUNET_ASSERT(jret != NULL);
+fprintf(stderr, "leaving jregisterHandler\n");
+  return convCIntToInt(jret, env);
+}
+
+int unregisterHandler(unsigned short type, MessagePartHandler callback) {
+fprintf(stderr, "in unregisterHandler\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jtype;
+  jobject jcallback;
+  jmethodID newHandle;
+  jmethodID method;
+  jobject jret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "unregisterHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jtype = convIntToCInt((int) type, env);
+  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
+  GNUNET_ASSERT(jcallback != NULL);
+  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
+  GNUNET_ASSERT(jret != NULL);
+fprintf(stderr, "leaving junregisterHandler\n");
+  return convCIntToInt(jret, env);
+}
+
+int isHandlerRegistered(unsigned short type, unsigned short htype) {
+fprintf(stderr, "in isHandlerRegistered\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jmethodID method;
+  jobject ok;
+  
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  GNUNET_ASSERT(t->jcapi != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "isHandlerRegistered", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/cwrappers/CInt;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  ok = (*env)->CallObjectMethod(env, t->jcapi, method, convIntToCInt(type, 
env), convIntToCInt(htype, env));
+  GNUNET_ASSERT(ok != NULL);
+fprintf(stderr, "leaving isHandlerRegistered\n");
+  return convCIntToInt(ok, env);
+}

Added: freeway/native/handler.h
===================================================================
--- freeway/native/handler.h    2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/handler.h    2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,33 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/handler.h
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <GNUnet/gnunet_util.h>
+
+int registerHandler(unsigned short type, CSHandler callback) ;
+
+int unregisterHandler(unsigned short type, CSHandler callback);
+
+int isHandlerRegistered(unsigned short type, unsigned short htype);

Added: freeway/native/java.c
===================================================================
--- freeway/native/java.c       2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/java.c       2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,310 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/java.c
+ * @brief contains all the JNI calls
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "org_gnu_freeway_server_CPluginLoader.h"
+#include "util.h"
+#include "switch-table.h"
+#include "core.h"
+
+JNIEXPORT jlong JNICALL Java_org_gnu_freeway_server_CPluginLoader_cLoadService
+  (JNIEnv *env, jobject cls, jstring serviceName, jobject capi) {
+       fprintf(stderr, "in cLoadService\n");
+       registerThread(env, capi);
+       const char *strServiceName;
+       ModuleList * m;
+       ServiceInitMethod mptr;
+       jclass UnsatisfiedLinkError;
+
+       strServiceName = (*env)->GetStringUTFChars(env, serviceName, NULL);
+       GNUNET_ASSERT(strServiceName != NULL);
+       m = getModule(DSO_PREFIX, strServiceName);
+       if (m == NULL) 
+               goto ULE;
+       if(m->moduleFptrStruct == NULL) {
+               mptr = bindDynamicMethod(m->modulePtr,
+                                       "provide_",
+                                       strServiceName);
+               if (mptr == NULL) {
+                       releaseModule(m);
+                       goto ULE;
+               }
+               m->moduleFptrStruct = mptr(&jcapi);
+               if (m->moduleFptrStruct == NULL) {
+                 releaseModule(m);
+                 goto ULE;
+               }
+               (*env)->ReleaseStringUTFChars(env, serviceName, strServiceName);
+       }
+       unregisterThread();
+       fprintf(stderr, "leaving cLoadService\n");
+       return (jlong) (long) m;
+ULE:        
+        UnsatisfiedLinkError = (*env)->FindClass(env, 
"java/lang/UnsatisfiedLinkError");
+        if(UnsatisfiedLinkError)
+               (*env)->ThrowNew(env, UnsatisfiedLinkError, strServiceName);
+       (*env)->ReleaseStringUTFChars(env, serviceName, strServiceName);
+        unregisterThread();
+       return 0;
+}
+
+JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cLoadApplicationModule
+  (JNIEnv *env, jobject cls, jstring protocolName, jobject capi) {
+       fprintf(stderr, "in cLoadApplicationModule\n");
+       registerThread(env, capi);
+       const char *strProtocolName;
+       int ok;
+        ModuleList * m;
+        ApplicationInitMethod mptr;
+       jclass UnsatisfiedLinkError;
+       strProtocolName = (*env)->GetStringUTFChars(env, protocolName, NULL);
+       if (strProtocolName == NULL)
+               return 0;
+       m = getModule(DSO_PREFIX, strProtocolName);
+       if (m == NULL)
+               goto ULE;
+       MUTEX_LOCK(&m->mutex);
+       if(m->protocolInit) {
+               MUTEX_UNLOCK(&m->mutex);
+               releaseModule(m);
+               return SYSERR;
+       }
+       mptr = bindDynamicMethod(m->modulePtr,
+                                        "initialize_",
+                                strProtocolName);
+       if (mptr == NULL) {
+               MUTEX_UNLOCK(&m->mutex);
+               releaseModule(m);
+               goto ULE;
+       }
+       ok = mptr(&jcapi);
+       if (ok == SYSERR) {
+               MUTEX_UNLOCK(&m->mutex);
+               releaseModule(m);
+       } else {
+               m->protocolInit = 1;
+               MUTEX_UNLOCK(&m->mutex);
+       }
+       (*env)->ReleaseStringUTFChars(env, protocolName, strProtocolName);
+       unregisterThread();
+       fprintf(stderr, "leaving cLoadApplicationModule\n");
+       return (jlong) (long) m;
+ULE:
+        UnsatisfiedLinkError = (*env)->FindClass(env, 
"java/lang/UnsatisfiedLinkError");
+        if(UnsatisfiedLinkError)
+               (*env)->ThrowNew(env, UnsatisfiedLinkError, strProtocolName);
+       (*env)->ReleaseStringUTFChars(env, protocolName, strProtocolName);
+        unregisterThread();
+       return 0;
+}
+
+JNIEXPORT jint JNICALL Java_org_gnu_freeway_server_CPluginLoader_cInitUtil
+(JNIEnv * env, jclass cls, jobjectArray args, jobject capi) {
+  fprintf(stderr, "in cInitUtil\n");
+  initCore();
+  MUTEX_CREATE(&threadsLock);
+  MUTEX_CREATE(&modulesLock);
+  registerThread(env, capi);
+  char * * jargs = NULL;
+  int jargLength;
+  GNUNET_ASSERT(env != NULL);
+  GNUNET_ASSERT(args != NULL);
+  jargLength = (*env)->GetArrayLength(env, args);
+  int ok;
+  if(jargLength > 0) {
+    jargs = MALLOC(sizeof(char *) * jargLength);
+    int jargsI;
+    for(jargsI = 0; jargsI < jargLength; jargsI++) {
+      jobject objString = (*env)->GetObjectArrayElement(env, args, jargsI);
+      GNUNET_ASSERT(objString != NULL);
+      jargs[jargsI] = (*env)->GetStringUTFChars(env, objString, NULL);
+      fprintf(stderr, "%s\n", jargs[jargsI]);
+    }
+  }
+  ok = initUtil(jargLength, jargs, &parseGnunetdCommandLine);
+  if(jargs != NULL) {
+    int jargsI;
+    for(jargsI = 0; jargsI < jargLength; jargsI++) {
+      jobject objString = (*env)->GetObjectArrayElement(env, args, jargsI);
+      (*env)->ReleaseStringUTFChars(env, objString, jargs[jargsI]);
+    }
+    FREE(jargs);
+  }
+  startCron();
+  unregisterThread();
+  fprintf(stderr, "leaving cInitUtil\n");
+  return (jint) ok;
+}
+
+JNIEXPORT jobject JNICALL Java_org_gnu_freeway_server_CPluginLoader_cCallC
+(JNIEnv *env, jclass cls, jlong modulePtr, jobject capi, jint functionOffset, 
jint functionType, jobjectArray arguments) {
+  ActiveThreadList * t = registerThread(env, capi);
+  ModuleList * m = (ModuleList*) (long) modulePtr;
+  jobject oret = 0;
+
+  GNUNET_ASSERT(m != NULL);
+
+#include "switch-table.c"
+#if 0
+  /* here is the idea of what the generated code should look like: */
+  switch (functionType) { 
+  case 0: {
+    int arg1 = (*env)->...(arguments,0);
+    char * arg2 = (*env)->...(arguments,1);
+    float arg3 = (*env)->...(arguments,2);
+    /* FunctionType0 is defined in switch-table.h as
+       "typedef int (FunctionType0*)(int a1, char* a2, float a3);" */ 
+    int ret = ((FunctionType0) ((void**)m->modulePtr)[functionOffset])(arg1, 
arg2, arg3);
+    oret = (*env)->...(ret);
+  }
+  case 1: {
+    /* ... */
+  }
+  default:
+    // error: unsupported function type
+  }
+#endif
+
+  unregisterThread();
+  return oret;
+}
+
+JNIEXPORT void JNICALL Java_org_gnu_freeway_server_CPluginLoader_cUnloadService
+  (JNIEnv *env, jobject cls, jlong ml, jobject capi) {
+  fprintf(stderr, "in cUnloadService\n");
+  registerThread(env, capi);
+  ModuleList * m = (ModuleList *) ml;
+  ServiceDoneMethod mptr = bindDynamicMethod(m->modulePtr, "release_", 
m->name);
+  if (mptr != NULL) {
+    fprintf(stderr, "Releasing the %s service\n", m->name);
+    mptr();
+  }
+
+  releaseModule(m);
+
+  unregisterThread();
+  fprintf(stderr, "leaving cUnloadService\n");
+}
+
+JNIEXPORT void JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cUnloadApplicationModule
+  (JNIEnv *env, jobject cls, jlong ml, jobject capi) {
+  fprintf(stderr, "in cUnloadApplicationModule\n");
+  registerThread(env, capi);
+  ModuleList * m = (ModuleList *) ml;
+  ApplicationDoneMethod mptr = bindDynamicMethod(m->modulePtr, "done_", 
m->name);
+  if (mptr != NULL) {
+    fprintf(stderr, "Finalizing the %s protocol\n", m->name);
+    mptr();
+  }
+
+  releaseModule(m);
+
+  unregisterThread();
+  fprintf(stderr, "leaving cUnloadApplicationModule\n");
+}
+
+/*JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cBindDynamicMethod
+  (JNIEnv *env, jobject cls, jlong libHandle, jstring methodPrefix, jstring 
dsoName) {
+  registerThread(env, 0);
+  GNUNET_ASSERT(methodPrefix != NULL);
+  GNUNET_ASSERT(dsoName != NULL);
+  const char * strMethodPrefix = (*env)->GetStringUTFChars(env, methodPrefix, 
NULL);
+  GNUNET_ASSERT(strMethodPrefix != NULL);
+  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
+  GNUNET_ASSERT(strDsoName != NULL);
+  long returnValue = (long) bindDynamicMethod((void *)(long)libHandle, 
strMethodPrefix, strDsoName);
+  (*env)->ReleaseStringUTFChars(env, methodPrefix, strMethodPrefix);
+  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
+  unregisterThread();
+  return (jlong) returnValue;
+}
+
+JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cLoadDynamicLibrary
+  (JNIEnv *env, jobject cls, jstring libPrefix, jstring dsoName) {
+  registerThread(env, 0);
+  GNUNET_ASSERT(libPrefix != NULL);
+  GNUNET_ASSERT(dsoName != NULL);
+  const char * strLibPrefix = (*env)->GetStringUTFChars(env, libPrefix, NULL);
+  GNUNET_ASSERT(strLibPrefix != NULL);
+  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
+  GNUNET_ASSERT(strDsoName != NULL);
+  long returnValue = (long) getModule(strLibPrefix, strDsoName);
+  //long returnValue = (long) loadDynamicLibrary(strLibPrefix, strDsoName);
+  (*env)->ReleaseStringUTFChars(env, libPrefix, strLibPrefix);
+  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
+  unregisterThread();
+  return (jlong) returnValue;
+}*/
+
+JNIEXPORT jboolean JNICALL Java_org_gnu_freeway_server_CPluginLoader_cHasModule
+  (JNIEnv *env, jobject cls, jstring libPrefix, jstring dsoName, jobject capi) 
{
+  fprintf(stderr, "in cHasModule\n");
+  registerThread(env, capi);
+  GNUNET_ASSERT(libPrefix != NULL);
+  GNUNET_ASSERT(dsoName != NULL);
+  const char * strLibPrefix = (*env)->GetStringUTFChars(env, libPrefix, NULL);
+  GNUNET_ASSERT(strLibPrefix != NULL);
+  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
+  GNUNET_ASSERT(strDsoName != NULL);
+  int returnValue = hasModule(strLibPrefix, strDsoName);
+  (*env)->ReleaseStringUTFChars(env, libPrefix, strLibPrefix);
+  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
+  unregisterThread();
+  fprintf(stderr, "leaving cHasModule\n");
+  return (jboolean) (returnValue == YES);
+}
+
+/*JNIEXPORT void JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cUnloadDynamicLibrary
+  (JNIEnv *env, jobject cls, jlong libHandle) {
+  registerThread(env, 0);
+  //unloadDynamicLibrary((void *)(long)libHandle);
+  releaseModule((void *)(long)libHandle);
+  unregisterThread();
+}*/
+
+JNIEXPORT jint JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cCallCallCSHandle
+(JNIEnv * env, jclass cls, jobject capi, jlong fptr, jobject session, 
jbyteArray data) {
+  registerThread(env, capi);
+  int ok;
+  GNUNET_ASSERT(fptr != 0 && data != NULL);
+  char * adata = (*env)->GetByteArrayElements(env, data, NULL);
+  GNUNET_ASSERT(adata != NULL);
+  
+//fprintf(stderr, "calling %x to handle a packet\n", fptr);
+//fprintf(stderr, "cCallCallCSHandle %02x %02x %02x %02x\n", adata[0] & 0xff, 
adata[1] & 0xff, adata[2] & 0xff, adata[3] & 0xff);
+//fprintf(stderr, "packet is %d bytes long\n", htons(((CS_MESSAGE_HEADER *) 
adata)->size));
+  ok = ((CSHandler) (void*) (long)fptr)((ClientHandle) (long) session, 
(CS_MESSAGE_HEADER *) adata);
+  (*env)->ReleaseByteArrayElements(env, data, adata, 0);
+  unregisterThread();
+  return (jint) ok;
+}

Deleted: freeway/native/org_gnu_freeway_server_CPluginLoader.c
===================================================================
--- freeway/native/org_gnu_freeway_server_CPluginLoader.c       2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/native/org_gnu_freeway_server_CPluginLoader.c       2006-07-27 
08:02:54 UTC (rev 3141)
@@ -1,1056 +0,0 @@
-/*
-     This file is part of Freeway.
-     (C) 2006 Free Software Foundation
-
-     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.
-
-     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.
-*/
-
-/**
- * @file native/org_gnu_freeway_server_CPluginLoader.c
- * @brief Loads C plugins into Freeway
- * @author mdonoughe
- * @author Christian Grothoff
- */
-
-#include <config.h>
-#include <stdio.h>
-#include <jni.h>
-#include <GNUnet/gnunet_util.h>
-#include <GNUnet/gnunet_core.h>
-#include "org_gnu_freeway_server_CPluginLoader.h"
-#include "switch-table.h"
-#include <locale.h>
-#include "gettext.h"
-
-/* FIXME: #include "platform.h", plibc? config.h? */
-#define _(a) a
-
-#define DSO_PREFIX "libgnunet" // from GNUnet's core.c
-
-typedef struct ModuleList {
-  void * modulePtr;
-  void * moduleFptrStruct;
-  char * prefix;
-  char * name;
-  unsigned int refs;
-  char protocolInit;
-  Mutex mutex; //stop initialization races
-  struct ModuleList * next;
-} ModuleList;
-
-/**
- * Used to identify which env / jcapi to use when called back.
- */
-typedef struct ActiveThreadList {
-  JNIEnv * env;
-  jobject jcapi;
-  unsigned int regs;
-  PTHREAD_T thread;
-  struct ActiveThreadList * next;
-} ActiveThreadList;
-
-typedef struct PointerObject {
-  void * pointer;
-  jobject object;
-} PointerObject;
-
-/**
- * FIXME: add proper locking for concurrent access to modules.
- */
-static ModuleList * modules;
-static ActiveThreadList * threads;
-Mutex modulesLock;
-Mutex threadsLock;
-
-/**
- * checks if the specified module is already loaded
- * @param the prefix
- * @param the name of the library
- * @return YES or NO
- */
-int hasModule(char * prefix, char * name) {
-  ModuleList * pos;
-  void * temp;
-  MUTEX_LOCK(&modulesLock);
-  pos = modules;
-  while(pos != NULL) {
-    if(pos->prefix == NULL || pos->name == NULL)
-      continue;
-    if(strcmp(prefix, pos->prefix) != 0)
-      continue;
-    if(strcmp(name, pos->name) == 0)
-      break;
-    pos = pos->next;
-  }
-  MUTEX_UNLOCK(&modulesLock);
-  if(pos != NULL)
-    return YES;
-  else
-    return NO;
-}
-
-/**
- * get a ModuleList structure. will attempt to load the library if needed
- * @param the prefix
- * @param the name of the library
- * @return the ModuleList or NULL if the library cannot be loaded
- */
-ModuleList * getModule(char * prefix, char * name) {
-  //Look for an already loaded module
-  ModuleList * pos;
-  void * temp;
-  MUTEX_LOCK(&modulesLock);
-  pos = modules;
-  while(pos != NULL) {
-    if(pos->prefix == NULL || pos->name == NULL)
-      continue;
-    if(strcmp(prefix, pos->prefix) != 0)
-      continue;
-    if(strcmp(name, pos->name) == 0)
-      break;
-    pos = pos->next;
-  }
-  if(pos != NULL) {
-    pos->refs++;
-    MUTEX_UNLOCK(&modulesLock);
-    return pos;
-  }
-  //Load a new module
-fprintf(stderr, "Loading new dynamic library %s %s\n", prefix, name);
-  temp = loadDynamicLibrary(prefix, name);
-  if(temp == NULL) {
-    MUTEX_UNLOCK(&modulesLock);
-    return NULL;
-  }
-  pos = MALLOC(sizeof(ModuleList));
-  pos->modulePtr = temp;
-  pos->moduleFptrStruct = NULL;
-  pos->prefix = STRDUP(prefix);
-  pos->name = STRDUP(name);
-  pos->refs = 1;
-  pos->protocolInit = 0;
-  MUTEX_CREATE(&pos->mutex); //does not need locked here
-  pos->next = modules;
-  modules = pos;
-  MUTEX_UNLOCK(&modulesLock);
-  return pos;
-}
-
-/**
- * decrement the references count on a ModuleList. module will be destroyed if 
the count reaches 0
- * @param target ModuleList
- */
-void releaseModule(ModuleList * module) {
-  MUTEX_LOCK(&modulesLock);
-  module->refs--;
-  if(module->refs != 0) {
-    MUTEX_UNLOCK(&modulesLock);
-    //module still in use
-    return;
-  }
-  //Look for this module in the list
-  ModuleList * pos;
-  pos = modules;
-  if(pos != module) {
-    while(pos->next != NULL && pos->next != module) {
-      pos = pos->next;
-    }
-    if(pos->next != NULL)
-      pos->next = pos->next->next;
-  } else {
-    modules = modules->next;
-  }
-  MUTEX_UNLOCK(&modulesLock);
-  unloadDynamicLibrary(module->modulePtr);
-  FREE(module->prefix);
-  FREE(module->name);
-  MUTEX_DESTROY(&module->mutex);
-  FREE(module);
-}
-
-/**
- * find our thread in ActiveThreadList. Does not check for null or unlock the 
mutex.
- * Consider using findThread instead.
- * @return our thread entry or null.
- */
-ActiveThreadList * _findThread() {
-       ActiveThreadList * pos;
-       MUTEX_LOCK(&threadsLock);
-       pos = threads;
-       while(pos != NULL) {
-               if(PTHREAD_SELF_TEST(&pos->thread))
-                       break;
-               pos = pos->next;
-       }
-       return pos;
-}
-
-/**
- * add an entry for this thread in ActiveThreadList
- * @param our JNIEnv
- * @param our jcapi
- * @return our thread entry
- */
-ActiveThreadList * registerThread(JNIEnv *env, jobject jcapi) {
-       ActiveThreadList * thread;
-       thread = _findThread();
-       if(thread == NULL) {
-               thread = MALLOC(sizeof(ActiveThreadList));
-               PTHREAD_GET_SELF(&thread->thread);
-               thread->env = env;
-               thread->jcapi = jcapi;
-               thread->regs = 1;
-               thread->next = threads;
-               threads = thread;
-       } else {
-               thread->regs++;
-       }
-       MUTEX_UNLOCK(&threadsLock);
-       return thread;
-}
-
-/**
- * find our thread in ActiveThreadList
- * WARNING: aborts program if not found!
- * @return our thread entry
- */
-ActiveThreadList * findThread() {
-       ActiveThreadList * t = _findThread();
-       MUTEX_UNLOCK(&threadsLock);
-       if (t == NULL)
-               errexit(_("Thread creation in C modules not supported by 
Freeway.\n"));
-       return t;
-}
-
-/**
- * find our thread in ActiveThreadList and return env
- * don't use this if you also need the api
- * WARNING: aborts program if not found!
- * @return env
- */
-JNIEnv * getEnv() {
-       return findThread()->env;
-}
-
-/**
- * remove our thread from the list and free its entry
- */
-void unregisterThread() {
-       ActiveThreadList * pos;
-       ActiveThreadList * this;
-       MUTEX_LOCK(&threadsLock);
-       pos = threads;
-       if(PTHREAD_SELF_TEST(&pos->thread)) {
-               pos->regs--;
-               if(pos->regs < 1) {
-                       threads = pos->next;
-                       MUTEX_UNLOCK(&threadsLock);
-                       PTHREAD_REL_SELF(&pos->thread);
-                       pos->env = NULL;
-                       pos->jcapi = 0;
-                       FREE(pos);
-                       return;
-               }
-               MUTEX_UNLOCK(&threadsLock);
-               return;
-       }
-       while(pos->next != NULL) {
-               if(PTHREAD_SELF_TEST(&pos->next->thread))
-                       break;
-               pos = pos->next;
-       }
-       if(pos->next != NULL) {
-               pos->regs--;
-               if(pos->regs < 1) {
-                       this = pos->next;
-                       pos->next = pos->next->next;
-                       MUTEX_UNLOCK(&threadsLock);
-                       PTHREAD_REL_SELF(&pos->thread);
-                       pos->env = NULL;
-                       pos->jcapi = 0;
-                       FREE(this);
-                       return;
-               }
-       }
-       MUTEX_UNLOCK(&threadsLock);
-}
-
-static int convCIntToInt(jobject input, JNIEnv * env) {
-fprintf(stderr, "in convCIntToInt\n");
-  jclass classCInt;
-  jmethodID method;
-
-  if(input == NULL)
-    return 0;
-  classCInt = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CInt");
-  if(classCInt == NULL)
-    return 0;
-  method = (*env)->GetMethodID(env, classCInt, "getValue", "()I");
-  if(method == NULL)
-    return 0;
-fprintf(stderr, "leaving convCIntToInt\n");
-  return (*env)->CallIntMethod(env, input, method);
-}
-
-static jobject convIntToCInt(int input, JNIEnv * env) {
-fprintf(stderr, "in convIntToCInt\n");
-  jclass classCInt;
-  jmethodID method;
-
-  classCInt = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CInt");
-  if(classCInt == NULL)
-    return 0;
-  method = (*env)->GetMethodID(env, classCInt, "<init>", "(I)V");
-  if(method == NULL)
-    return 0;
-fprintf(stderr, "leaving convIntToCInt\n");
-  return (*env)->NewObject(env, classCInt, method, input);
-}
-
-static long convCLongToLong(jobject input, JNIEnv * env) {
-fprintf(stderr, "in convCLongToLong\n");
-  jclass classCLong;
-  jmethodID method;
-
-  if(input == NULL)
-    return 0;
-  classCLong = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CLong");
-  if(classCLong == NULL)
-    return 0;
-  method = (*env)->GetMethodID(env, classCLong, "getValue", "()J");
-  if(method == NULL)
-    return 0;
-fprintf(stderr, "leaving convCLongToLong\n");
-  return (*env)->CallLongMethod(env, input, method);
-}
-
-static jobject convLongToCLong(long input, JNIEnv * env) {
-fprintf(stderr, "in convLongToCLong\n");
-  jclass classCLong;
-  jmethodID method;
-
-  classCLong = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CLong");
-  if(classCLong == NULL)
-    return 0;
-  method = (*env)->GetMethodID(env, classCLong, "<init>", "(J)V");
-  if(method == NULL)
-    return 0;
-fprintf(stderr, "leaving convLongToCLong\n");
-  return (*env)->NewObject(env, classCLong, method, input);
-}
-
-static PointerObject * convObjectToPtr(jobject input, JNIEnv * env) {
-fprintf(stderr, "in convObjectToPtr\n");
-  jclass objectClass;
-  jmethodID method;
-  PointerObject * po;
-
-  po = MALLOC(sizeof(PointerObject));
-  
-  if(input == NULL)
-    return NULL;
-  objectClass = (*env)->GetObjectClass(env, input);
-  if(objectClass == NULL)
-    return NULL;
-  method = (*env)->GetMethodID(env, objectClass, "serializeToByteArray", 
"()[B");
-  if(method == NULL)
-    return NULL;
-  po->object = (*env)->CallObjectMethod(env, input, method);
-  po->pointer = (*env)->GetByteArrayElements(env, po->object, NULL);
-fprintf(stderr, "leaving convObjectToPtr\n");
-  return po;
-}
-
-static jobject convPtrToNewObject(void * input, jclass returnType, JNIEnv * 
env) {
-fprintf(stderr, "in convPtrToNewObject\n");
-  jmethodID method;
-  jmethodID methodSize;
-  jint size;
-  jobject object;
-  jbyteArray array;
-  int i;
-  
-  if(input == NULL || returnType == NULL)
-    return NULL;
-  method = (*env)->GetStaticMethodID(env, returnType, "newFromPointer", 
"(J)Ljava/lang/Object;");
-  if(method != NULL)
-    return (*env)->CallStaticObjectMethod(env, returnType, method, input);
-  (*env)->ExceptionClear(env);
-  object = (*env)->AllocObject(env, returnType);
-  if(object == NULL)
-    return;
-  methodSize = (*env)->GetMethodID(env, object, "getSerializedSize", "()I");
-  if(methodSize == NULL)
-    return NULL;
-  method = (*env)->GetMethodID(env, returnType, "<init>", "([B)V");
-  if(method == NULL)
-    return NULL;
-  size = (*env)->CallIntMethod(env, object, methodSize);
-  array = (*env)->NewByteArray(env, size);
-  if(array == NULL)
-    return NULL;
-  (*env)->SetByteArrayRegion(env, array, 0, size, input);
-fprintf(stderr, "leaving convPtrToNewObject\n");
-  return (*env)->NewObject(env, returnType, method, array);
-}
-
-static void updateObjectFromPtr(jobject target, PointerObject * input, JNIEnv 
* env) {
-fprintf(stderr, "in updateObjectFromPtr\n");
-  jclass objectClass;
-  jmethodID method;
-  
-  if(input == NULL)
-       return;
-  if(input->pointer == NULL || input->object == NULL)
-    return;
-  objectClass = (*env)->GetObjectClass(env, target);
-  if(objectClass == NULL)
-    return;
-  method = (*env)->GetMethodID(env, objectClass, "deserializeFromPointer", 
"(J)V");
-  if(method != NULL) {
-    (*env)->CallVoidMethod(env, target, method, input->pointer);
-    (*env)->ReleaseByteArrayElements(env, input->object, input->pointer, 0);
-    FREE(input);
-fprintf(stderr, "leaving updateObjectFromPtr\n");
-    return;
-  }
-  (*env)->ExceptionClear(env);
-  (*env)->ReleaseByteArrayElements(env, input->object, input->pointer, 0);
-  method = (*env)->GetMethodID(env, objectClass, "deserializeFromByteArray", 
"([B)V");
-  if(method != NULL) {
-    (*env)->CallVoidMethod(env, target, method, input->object);
-    FREE(input);
-fprintf(stderr, "leaving updateObjectFromPtr\n");
-    return;
-  }
-  (*env)->ExceptionClear(env);
-  //Constant CWrappers cannot be updated
-}
-
-static jobject convJStringToCString(jstring input, JNIEnv * env) {
-fprintf(stderr, "in convJStringToCString\n");
-  jclass classCString;
-  jmethodID method;
-
-  if(input == NULL)
-    return NULL;
-  classCString = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CString");
-  if(classCString == NULL)
-    return NULL;
-  method = (*env)->GetMethodID(env, classCString, "<init>", 
"(Ljava/lang/String;)V");
-  if(method == NULL)
-    return NULL;
-fprintf(stderr, "leaving convJStringToCString\n", env, classCString, method, 
input);
-  return (*env)->NewObject(env, classCString, method, input);
-}
-
-static void * jrequestService(const char * name) {
-fprintf(stderr, "in jrequestService %s\n", name);
-  ActiveThreadList * t;
-  jstring mname;
-  jmethodID method;
-  jclass capiClazz;
-  JNIEnv * env;
-  jobject retHandle;
-  jlong ret;
-
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  mname = (*env)->NewStringUTF(env, name);
-  GNUNET_ASSERT(mname != 0);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != 0);
-  method = (*env)->GetMethodID(env, capiClazz, "requestService", 
"(Lorg/gnu/freeway/cwrappers/CString;)Lorg/gnu/freeway/server/CPluginLoader$Handle;");
-  GNUNET_ASSERT(method != 0);
-  retHandle = (*env)->CallObjectMethod(env, t->jcapi, method, 
convJStringToCString(mname, env));
-  jfieldID handle_ = (*env)->GetFieldID(env, (*env)->GetObjectClass(env, 
retHandle), "_", "J");
-  if(handle_ == NULL)
-    return 0;
-  ret = (*env)->GetLongField(env, retHandle, handle_);
-fprintf(stderr, "leaving jrequestService: %x\n", ret);
-  return (void *) (long) ret;
-}
-
-static int jloadApplicationModule(const char * name) {
-fprintf(stderr, "in jloadApplicationModule\n");
-  ActiveThreadList * t;
-  jstring mname;
-  jmethodID method;
-  jclass capiClazz;
-  JNIEnv * env;
-  jobject ret;
-
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  mname = (*env)->NewStringUTF(env, name);
-  GNUNET_ASSERT(mname != 0);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != 0);
-  method = (*env)->GetMethodID(env, capiClazz, "loadApplicationModule", 
"(Lorg/gnu/freeway/cwrappers/CString;)Lorg/gnu/freeway/cwrappers/CInt;");
-  GNUNET_ASSERT(method != 0);
-  ret = (*env)->CallObjectMethod(env, t->jcapi, method, 
convJStringToCString(mname, env));
-fprintf(stderr, "leaving jloadApplicationModule\n");
-  return convCIntToInt(ret, env);
-}
-
-static int jregisterClientHandler(unsigned short type, CSHandler callback) {
-fprintf(stderr, "in jregisterClientHandler\n");
-  ActiveThreadList * t;
-  JNIEnv * env;
-  jclass capiClazz;
-  jclass handleClazz;
-  jobject jtype;
-  jobject jcallback;
-  jmethodID newHandle;
-  jmethodID method;
-  jobject jret;
-
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != NULL);
-  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
-  GNUNET_ASSERT(handleClazz != NULL);
-  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
-  GNUNET_ASSERT(newHandle != NULL);
-  method = (*env)->GetMethodID(env, capiClazz, "registerClientHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
-  GNUNET_ASSERT(method != NULL);
-  jtype = convIntToCInt((int) type, env);
-  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
-  GNUNET_ASSERT(jcallback != NULL);
-  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
-  GNUNET_ASSERT(jret != NULL);
-fprintf(stderr, "leaving jregisterClientHandler\n");
-  return convCIntToInt(jret, env);
-}
-
-static int jregisterHandler(unsigned short type, CSHandler callback) {
-fprintf(stderr, "in jregisterHandler\n");
-  ActiveThreadList * t;
-  JNIEnv * env;
-  jclass capiClazz;
-  jclass handleClazz;
-  jobject jtype;
-  jobject jcallback;
-  jmethodID newHandle;
-  jmethodID method;
-  jobject jret;
-
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != NULL);
-  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
-  GNUNET_ASSERT(handleClazz != NULL);
-  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
-  GNUNET_ASSERT(newHandle != NULL);
-  method = (*env)->GetMethodID(env, capiClazz, "registerHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
-  GNUNET_ASSERT(method != NULL);
-  jtype = convIntToCInt((int) type, env);
-  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
-  GNUNET_ASSERT(jcallback != NULL);
-  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
-  GNUNET_ASSERT(jret != NULL);
-fprintf(stderr, "leaving jregisterHandler\n");
-  return convCIntToInt(jret, env);
-}
-
-int getVersion() {
-//TODO: not implemented. ask CoreAPI?
-return 0;
-}
-
-static void printDot(void * unused) {
-  LOG(LOG_DEBUG, ".");
-}
-
-static void printhelp() {
-  static Help help[] = {
-    HELP_CONFIG,
-    { 'd', "debug", NULL,
-      gettext_noop("run in debug mode; gnunetd will "
-                  "not daemonize and error messages will "
-                  "be written to stderr instead of a logfile") },
-    HELP_HELP,
-    HELP_LOGLEVEL,
-#if 0  /* not supported */
-    { 'u', "user", "LOGIN",
-      gettext_noop("run as user LOGIN") },
-#endif
-    HELP_VERSION,
-    HELP_END,
-  };
-  formatHelp("gnunetd [OPTIONS]",
-            _("Starts the gnunetd daemon."),
-            help);
-}
-
-int parseGnunetdCommandLine(int argc,
-                           char * argv[]) {
-  int cont = OK;
-  int c;
-
-  /* set the 'magic' code that indicates that
-     this process is 'gnunetd' (and not any of
-     the tools).  This can be used by code
-     that runs in both the tools and in gnunetd
-     to distinguish between the two cases. */
-printf("parseGnunetdCommandLine0 %d %x\n", argc, argv);
-  FREENONNULL(setConfigurationString("GNUNETD",
-                                    "_MAGIC_",
-                                    "YES"));
-  while (0) { //TODO: why does this crash?
-    int option_index = 0;
-    static struct GNoption long_options[] = {
-      { "loglevel",1, 0, 'L' },
-      { "config",  1, 0, 'c' },
-      { "version", 0, 0, 'v' },
-      { "help",    0, 0, 'h' },
-      { "user",    1, 0, 'u' },
-      { "debug",   0, 0, 'd' },
-      { "livedot", 0, 0, 'l' },
-      { "padding", 1, 0, 'p' },
-      { "win-service", 0, 0, '@' },
-      { 0,0,0,0 }
-    };
-printf("parseGnunetdCommandLine1 %d %x\n", argc, argv);
-    c = GNgetopt_long(argc,
-                     argv,
-                     "vhdc:u:L:lp:@",
-                     long_options,
-                     &option_index);
-printf("parseGnunetdCommandLine2 %d %x\n", argc, argv);
-    if (c == -1)
-      break;  /* No more flags to process */
-
-    switch(c) {
-    case 'p':
-      FREENONNULL(setConfigurationString("GNUNETD-EXPERIMENTAL",
-                                        "PADDING",
-                                        GNoptarg));
-      break;
-    case 'l':
-      addCronJob(&printDot,
-                1 * cronSECONDS,
-                1 * cronSECONDS,
-                NULL);
-      break;
-    case 'c':
-      FREENONNULL(setConfigurationString("FILES",
-                                        "gnunet.conf",
-                                        GNoptarg));
-      break;
-    case 'v':
-      printf("GNUnet v%s\n",
-            getVersion());
-      cont = SYSERR;
-      break;
-    case 'h':
-      printhelp();
-      cont = SYSERR;
-      break;
-    case 'L':
-      FREENONNULL(setConfigurationString("GNUNETD",
-                                        "LOGLEVEL",
-                                        GNoptarg));
-      break;
-    case 'd':
-      //TODO: debug_flag_ = YES;
-      FREENONNULL(setConfigurationString("GNUNETD",
-                                        "LOGFILE",
-                                        NULL));
-      break;
-#if 0  /* not supported */
-    case 'u':
-      changeUser(GNoptarg);
-      break;
-#endif
-#if 0
-    case '@':
-      win_service_ = YES;
-      break;
-#endif
-    default:
-      LOG(LOG_FAILURE,
-         _("Use --help to get a list of options.\n"));
-      cont = SYSERR;
-    } /* end of parsing commandline */
-  }printf("parseGnunetdCommandLine3 %d %x\n", argc, argv);
-  if (GNoptind < argc) {
-    LOG(LOG_WARNING,
-       _("Invalid command-line arguments:\n"));
-    while (GNoptind < argc) {
-      LOG(LOG_WARNING,
-         _("Argument %d: `%s'\n"),
-         GNoptind+1,
-         argv[GNoptind]);
-      GNoptind++;
-    }
-    LOG(LOG_FATAL,
-       _("Invalid command-line arguments.\n"));
-    return SYSERR;
-  }
-  return cont;
-}
-
-int jforAllConnectedNodes(PerNodeCallback method, void * arg) {
-fprintf(stderr, "in jforAllConnectedNodes\n");
-  ActiveThreadList * t;
-  JNIEnv * env;
-  jclass capiClazz;
-  jclass handleClazz;
-  jobject jmethod;
-  jobject jarg;
-  jmethodID newHandle;
-  jmethodID methodi;
-  jobject jret;
-
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != NULL);
-  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
-  GNUNET_ASSERT(handleClazz != NULL);
-  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
-  GNUNET_ASSERT(newHandle != NULL);
-  methodi = (*env)->GetMethodID(env, capiClazz, "forAllConnectedNodes", 
"(Lorg/gnu/freeway/server/CPluginLoader$Handle;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
-  GNUNET_ASSERT(methodi != NULL);
-  jmethod = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
method);
-  GNUNET_ASSERT(jmethod != NULL);
-  jarg = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) arg);
-  GNUNET_ASSERT(jarg != NULL);
-  jret = (*env)->CallObjectMethod(env, t->jcapi, methodi, jmethod, jarg);
-  GNUNET_ASSERT(jret != NULL);
-fprintf(stderr, "leaving jforAllConnectedNodes\n");
-  return convCIntToInt(jret, env);
-}
-
-int jsendToClient(ClientHandle handle, const CS_MESSAGE_HEADER * message) {
-fprintf(stderr, "in jsendToClient\n");
-  ActiveThreadList * t;
-  JNIEnv * env;
-  jclass capiClazz;
-  jmethodID method;
-  jbyteArray jarray;
-  jobject ok;
-  
-  GNUNET_ASSERT(handle != NULL);
-  GNUNET_ASSERT(message != NULL);
-  GNUNET_ASSERT(htons(message->size) >= 4);
-//fprintf(stderr, "jsendToClient %02x %02x %02x %02x\n", ((char *) message)[0] 
& 0xff, ((char *) message)[1] & 0xff, ((char *) message)[2] & 0xff, ((char *) 
message)[3] & 0xff);
-//fprintf(stderr, "packet is %d bytes long\n", htons(message->size));
-  t = findThread();
-  env = t->env;
-  GNUNET_ASSERT(env != NULL);
-  GNUNET_ASSERT(t->jcapi != NULL);
-  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
-  GNUNET_ASSERT(capiClazz != NULL);
-  method = (*env)->GetMethodID(env, capiClazz, "sendToClient", 
"(Lorg/gnu/freeway/util/net/CSSession;[B)Lorg/gnu/freeway/cwrappers/CInt;");
-  GNUNET_ASSERT(method != NULL);
-  jarray = (*env)->NewByteArray(env, htons(message->size));
-  GNUNET_ASSERT(jarray != NULL);
-  (*env)->SetByteArrayRegion(env, jarray, 0, htons(message->size), (char*) 
message);
-  ok = (*env)->CallObjectMethod(env, t->jcapi, method, handle, jarray);
-(*env)->ExceptionDescribe(env);
-  GNUNET_ASSERT(ok != NULL);
-fprintf(stderr, "leaving jsendToClient\n");
-  return convCIntToInt(ok, env);
-}
-
-static CoreAPIForApplication jcapi = {
-  0, // version
-  NULL, /* FIXME... */ // myIdentity
-  &jloadApplicationModule, // loadApplicationModule
-  NULL, /* FIXME... */ // unloadApplicationModule
-  &jrequestService, // requestService
-  NULL, /* FIXME... */ // releaseService
-  NULL, /* FIXME... */ // sendPlaintext
-  NULL, /* FIXME... */ // unicast
-  NULL, /* FIXME... */ // unicastCallback
-  &jforAllConnectedNodes, // forAllConnectedNodes
-  NULL, /* FIXME... */ // registerSendCallback
-  NULL, /* FIXME... */ // unregisterSendCallback
-  NULL, /* FIXME... */ // registerSendNotify
-  NULL, /* FIXME... */ // unregisterSendNotify
-  &jregisterHandler, // registerHandler
-  NULL, /* FIXME... */ // unregisterHandler
-  NULL, /* FIXME... */ // isHandlerRegistered
-  NULL, /* FIXME... */ // registerPlaintextHandler
-  NULL, /* FIXME... */ // unregisterPlaintextHandler
-  NULL, /* FIXME... */ // offerTSessionFor
-  NULL, /* FIXME... */ // assignSessionKey
-  NULL, /* FIXME... */ // getCurrentSessionKey
-  NULL, /* FIXME... */ // confirmSessionUp
-  NULL, /* FIXME... */ // preferTrafficFrom
-  NULL, /* FIXME... */ // queryBPMfromPeer
-  NULL, /* FIXME... */ // disconnectFromPeer
-  NULL, /* FIXME... */ // sendValueToClient
-  jsendToClient, // sendToClient
-  &jregisterClientHandler, // registerClientHandler
-  NULL, /* FIXME... */ // unregisterClientHandler
-  NULL, /* FIXME... */ // registerClientExitHandler
-  NULL, /* FIXME... */ // unregisterClientExitHandler
-  NULL, /* FIXME... */ // terminateClientConnection
-  NULL, /* FIXME... */ // injectMessage
-  NULL, /* FIXME... */ // computeIndex
-  NULL, /* FIXME... */ // getConnectionModuleLock
-  NULL, /* FIXME... */ // getSlotCount
-  NULL, /* FIXME... */ // isSlotUsed
-  NULL  /* FIXME... */ // getLastActivityOf
-};
-
-JNIEXPORT jlong JNICALL Java_org_gnu_freeway_server_CPluginLoader_cLoadService
-  (JNIEnv *env, jobject cls, jstring serviceName, jobject capi) {
-       registerThread(env, capi);
-       const char *strServiceName;
-       ModuleList * m;
-       ServiceInitMethod mptr;
-       jclass UnsatisfiedLinkError;
-
-       strServiceName = (*env)->GetStringUTFChars(env, serviceName, NULL);
-       GNUNET_ASSERT(strServiceName != NULL);
-       m = getModule(DSO_PREFIX, strServiceName);
-       if (m == NULL) 
-               goto ULE;
-       if(m->moduleFptrStruct == NULL) {
-               mptr = bindDynamicMethod(m->modulePtr,
-                                       "provide_",
-                                       strServiceName);
-               if (mptr == NULL) {
-                       releaseModule(m);
-                       goto ULE;
-               }
-               m->moduleFptrStruct = mptr(&jcapi);
-               if (m->moduleFptrStruct == NULL) {
-                 releaseModule(m);
-                 goto ULE;
-               }
-               (*env)->ReleaseStringUTFChars(env, serviceName, strServiceName);
-       }
-       unregisterThread();
-       return (jlong) (long) m;
-ULE:        
-        UnsatisfiedLinkError = (*env)->FindClass(env, 
"java/lang/UnsatisfiedLinkError");
-        if(UnsatisfiedLinkError)
-               (*env)->ThrowNew(env, UnsatisfiedLinkError, strServiceName);
-       (*env)->ReleaseStringUTFChars(env, serviceName, strServiceName);
-        unregisterThread();
-       return 0;
-}
-
-JNIEXPORT jint JNICALL Java_org_gnu_freeway_server_CPluginLoader_cLoadProtocol
-  (JNIEnv *env, jobject cls, jstring protocolName, jobject capi) {
-       registerThread(env, capi);
-       const char *strProtocolName;
-       int ok;
-        ModuleList * m;
-        ApplicationInitMethod mptr;
-       jclass UnsatisfiedLinkError;
-       strProtocolName = (*env)->GetStringUTFChars(env, protocolName, NULL);
-       if (strProtocolName == NULL)
-               return SYSERR;
-       m = getModule(DSO_PREFIX, strProtocolName);
-       if (m == NULL)
-               goto ULE;
-       MUTEX_LOCK(&m->mutex);
-       if(m->protocolInit) {
-               MUTEX_UNLOCK(&m->mutex);
-               releaseModule(m);
-               return SYSERR;
-       }
-       mptr = bindDynamicMethod(m->modulePtr,
-                                        "initialize_",
-                                strProtocolName);
-       if (mptr == NULL) {
-               MUTEX_UNLOCK(&m->mutex);
-               releaseModule(m);
-               goto ULE;
-       }
-       ok = mptr(&jcapi);
-       if (ok == SYSERR) {
-               MUTEX_UNLOCK(&m->mutex);
-               releaseModule(m);
-       } else {
-               m->protocolInit = 1;
-               MUTEX_UNLOCK(&m->mutex);
-       }
-       (*env)->ReleaseStringUTFChars(env, protocolName, strProtocolName);
-       unregisterThread();
-       return (jint) ok;
-ULE:
-        UnsatisfiedLinkError = (*env)->FindClass(env, 
"java/lang/UnsatisfiedLinkError");
-        if(UnsatisfiedLinkError)
-               (*env)->ThrowNew(env, UnsatisfiedLinkError, strProtocolName);
-       (*env)->ReleaseStringUTFChars(env, protocolName, strProtocolName);
-        unregisterThread();
-       return SYSERR;
-}
-
-JNIEXPORT jint JNICALL Java_org_gnu_freeway_server_CPluginLoader_cInitUtil
-(JNIEnv * env, jclass cls, jobjectArray args) {
-  MUTEX_CREATE(&threadsLock);
-  MUTEX_CREATE(&modulesLock);
-  registerThread(env, 0);
-  char * * jargs = NULL;
-  int jargLength;
-  GNUNET_ASSERT(env != NULL);
-  GNUNET_ASSERT(args != NULL);
-  jargLength = (*env)->GetArrayLength(env, args);
-  int ok;
-  if(jargLength > 0) {
-    jargs = MALLOC(sizeof(char *) * jargLength);
-    int jargsI;
-    for(jargsI = 0; jargsI < jargLength; jargsI++) {
-      jobject objString = (*env)->GetObjectArrayElement(env, args, jargsI);
-      GNUNET_ASSERT(objString != NULL);
-      jargs[jargsI] = (*env)->GetStringUTFChars(env, objString, NULL);
-      fprintf(stderr, "%s\n", jargs[jargsI]);
-    }
-  }
-  ok = initUtil(jargLength, jargs, &parseGnunetdCommandLine);
-  if(jargs != NULL) {
-    int jargsI;
-    for(jargsI = 0; jargsI < jargLength; jargsI++) {
-      jobject objString = (*env)->GetObjectArrayElement(env, args, jargsI);
-      (*env)->ReleaseStringUTFChars(env, objString, jargs[jargsI]);
-    }
-    FREE(jargs);
-  }
-  startCron();
-  unregisterThread();
-  return (jint) ok;
-}
-
-JNIEXPORT jobject JNICALL Java_org_gnu_freeway_server_CPluginLoader_cCallC
-(JNIEnv *env, jclass cls, jlong modulePtr, jobject capi, jint functionOffset, 
jint functionType, jobjectArray arguments) {
-  ActiveThreadList * t = registerThread(env, capi);
-  ModuleList * m = (ModuleList*) (long) modulePtr;
-  jobject oret = 0;
-
-  GNUNET_ASSERT(m != NULL);
-
-#include "switch-table.c"
-#if 0
-  /* here is the idea of what the generated code should look like: */
-  switch (functionType) { 
-  case 0: {
-    int arg1 = (*env)->...(arguments,0);
-    char * arg2 = (*env)->...(arguments,1);
-    float arg3 = (*env)->...(arguments,2);
-    /* FunctionType0 is defined in switch-table.h as
-       "typedef int (FunctionType0*)(int a1, char* a2, float a3);" */ 
-    int ret = ((FunctionType0) ((void**)m->modulePtr)[functionOffset])(arg1, 
arg2, arg3);
-    oret = (*env)->...(ret);
-  }
-  case 1: {
-    /* ... */
-  }
-  default:
-    // error: unsupported function type
-  }
-#endif
-
-  unregisterThread();
-  return oret;
-}
-
-JNIEXPORT void JNICALL Java_org_gnu_freeway_server_CPluginLoader_cUnloadService
-  (JNIEnv *env, jobject cls, jlong modulePtr) {
-  registerThread(env, 0);
-
-  //   unloadDynamicLibrary(modulePtr);
-
-  unregisterThread();
-}
-
-JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cBindDynamicMethod
-  (JNIEnv *env, jobject cls, jlong libHandle, jstring methodPrefix, jstring 
dsoName) {
-  registerThread(env, 0);
-  GNUNET_ASSERT(methodPrefix != NULL);
-  GNUNET_ASSERT(dsoName != NULL);
-  const char * strMethodPrefix = (*env)->GetStringUTFChars(env, methodPrefix, 
NULL);
-  GNUNET_ASSERT(strMethodPrefix != NULL);
-  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
-  GNUNET_ASSERT(strDsoName != NULL);
-  long returnValue = (long) bindDynamicMethod((void *)(long)libHandle, 
strMethodPrefix, strDsoName);
-  (*env)->ReleaseStringUTFChars(env, methodPrefix, strMethodPrefix);
-  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
-  unregisterThread();
-  return (jlong) returnValue;
-}
-
-JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cLoadDynamicLibrary
-  (JNIEnv *env, jobject cls, jstring libPrefix, jstring dsoName) {
-  registerThread(env, 0);
-  GNUNET_ASSERT(libPrefix != NULL);
-  GNUNET_ASSERT(dsoName != NULL);
-  const char * strLibPrefix = (*env)->GetStringUTFChars(env, libPrefix, NULL);
-  GNUNET_ASSERT(strLibPrefix != NULL);
-  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
-  GNUNET_ASSERT(strDsoName != NULL);
-  long returnValue = (long) getModule(strLibPrefix, strDsoName);
-  //long returnValue = (long) loadDynamicLibrary(strLibPrefix, strDsoName);
-  (*env)->ReleaseStringUTFChars(env, libPrefix, strLibPrefix);
-  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
-  unregisterThread();
-  return (jlong) returnValue;
-}
-
-JNIEXPORT jboolean JNICALL Java_org_gnu_freeway_server_CPluginLoader_cHasModule
-  (JNIEnv *env, jobject cls, jstring libPrefix, jstring dsoName) {
-  registerThread(env, 0);
-  GNUNET_ASSERT(libPrefix != NULL);
-  GNUNET_ASSERT(dsoName != NULL);
-  const char * strLibPrefix = (*env)->GetStringUTFChars(env, libPrefix, NULL);
-  GNUNET_ASSERT(strLibPrefix != NULL);
-  const char * strDsoName = (*env)->GetStringUTFChars(env, dsoName, NULL);
-  GNUNET_ASSERT(strDsoName != NULL);
-  int returnValue = hasModule(strLibPrefix, strDsoName);
-  (*env)->ReleaseStringUTFChars(env, libPrefix, strLibPrefix);
-  (*env)->ReleaseStringUTFChars(env, dsoName, strDsoName);
-  unregisterThread();
-  return (jboolean) (returnValue == YES);
-}
-
-JNIEXPORT void JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cUnloadDynamicLibrary
-  (JNIEnv *env, jobject cls, jlong libHandle) {
-  registerThread(env, 0);
-  //unloadDynamicLibrary((void *)(long)libHandle);
-  releaseModule((void *)(long)libHandle);
-  unregisterThread();
-}
-
-JNIEXPORT jint JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cCallCallCSHandle
-(JNIEnv * env, jclass cls, jobject capi, jlong fptr, jobject session, 
jbyteArray data) {
-  registerThread(env, capi);
-  int ok;
-  GNUNET_ASSERT(fptr != 0 && data != NULL);
-  char * adata = (*env)->GetByteArrayElements(env, data, NULL);
-  GNUNET_ASSERT(adata != NULL);
-  
-//fprintf(stderr, "calling %x to handle a packet\n", fptr);
-//fprintf(stderr, "cCallCallCSHandle %02x %02x %02x %02x\n", adata[0] & 0xff, 
adata[1] & 0xff, adata[2] & 0xff, adata[3] & 0xff);
-//fprintf(stderr, "packet is %d bytes long\n", htons(((CS_MESSAGE_HEADER *) 
adata)->size));
-  ok = ((CSHandler) (void*) (long)fptr)((ClientHandle) (long) session, 
(CS_MESSAGE_HEADER *) adata);
-  (*env)->ReleaseByteArrayElements(env, data, adata, 0);
-  unregisterThread();
-  return (jint) ok;
-}

Modified: freeway/native/org_gnu_freeway_server_CPluginLoader.h
===================================================================
--- freeway/native/org_gnu_freeway_server_CPluginLoader.h       2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/native/org_gnu_freeway_server_CPluginLoader.h       2006-07-27 
08:02:54 UTC (rev 3141)
@@ -17,19 +17,19 @@
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
- * Method:    cLoadProtocol
- * Signature: (Ljava/lang/String;Lorg/gnu/freeway/server/CoreAPI;)I
+ * Method:    cLoadApplicationModule
+ * Signature: (Ljava/lang/String;Lorg/gnu/freeway/server/CoreAPI;)J
  */
-JNIEXPORT jint JNICALL Java_org_gnu_freeway_server_CPluginLoader_cLoadProtocol
+JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cLoadApplicationModule
   (JNIEnv *, jclass, jstring, jobject);
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
  * Method:    cInitUtil
- * Signature: ([Ljava/lang/String;)I
+ * Signature: ([Ljava/lang/String;Lorg/gnu/freeway/server/CoreAPI;)I
  */
 JNIEXPORT jint JNICALL Java_org_gnu_freeway_server_CPluginLoader_cInitUtil
-  (JNIEnv *, jclass, jobjectArray);
+  (JNIEnv *, jclass, jobjectArray, jobject);
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
@@ -42,45 +42,29 @@
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
  * Method:    cUnloadService
- * Signature: (J)V
+ * Signature: (JLorg/gnu/freeway/server/CoreAPI;)V
  */
 JNIEXPORT void JNICALL Java_org_gnu_freeway_server_CPluginLoader_cUnloadService
-  (JNIEnv *, jclass, jlong);
+  (JNIEnv *, jclass, jlong, jobject);
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
- * Method:    cBindDynamicMethod
- * Signature: (JLjava/lang/String;Ljava/lang/String;)J
+ * Method:    cUnloadApplicationModule
+ * Signature: (JLorg/gnu/freeway/server/CoreAPI;)V
  */
-JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cBindDynamicMethod
-  (JNIEnv *, jclass, jlong, jstring, jstring);
+JNIEXPORT void JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cUnloadApplicationModule
+  (JNIEnv *, jclass, jlong, jobject);
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
  * Method:    cHasModule
- * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
+ * Signature: 
(Ljava/lang/String;Ljava/lang/String;Lorg/gnu/freeway/server/CoreAPI;)Z
  */
 JNIEXPORT jboolean JNICALL Java_org_gnu_freeway_server_CPluginLoader_cHasModule
-  (JNIEnv *, jclass, jstring, jstring);
+  (JNIEnv *, jclass, jstring, jstring, jobject);
 
 /*
  * Class:     org_gnu_freeway_server_CPluginLoader
- * Method:    cLoadDynamicLibrary
- * Signature: (Ljava/lang/String;Ljava/lang/String;)J
- */
-JNIEXPORT jlong JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cLoadDynamicLibrary
-  (JNIEnv *, jclass, jstring, jstring);
-
-/*
- * Class:     org_gnu_freeway_server_CPluginLoader
- * Method:    cUnloadDynamicLibrary
- * Signature: (J)V
- */
-JNIEXPORT void JNICALL 
Java_org_gnu_freeway_server_CPluginLoader_cUnloadDynamicLibrary
-  (JNIEnv *, jclass, jlong);
-
-/*
- * Class:     org_gnu_freeway_server_CPluginLoader
  * Method:    cCallCallCSHandle
  * Signature: 
(Lorg/gnu/freeway/server/CoreAPI;JLorg/gnu/freeway/util/net/CSSession;[B)I
  */

Added: freeway/native/tcpserver.c
===================================================================
--- freeway/native/tcpserver.c  2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/tcpserver.c  2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,153 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/connection.c
+ * @brief the freeway equivalent of connection.c
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "util.h"
+
+int sendToClient(ClientHandle handle, const CS_MESSAGE_HEADER * message) {
+fprintf(stderr, "in sendToClient\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jmethodID method;
+  jbyteArray jarray;
+  jobject ok;
+  
+  GNUNET_ASSERT(handle != NULL);
+  GNUNET_ASSERT(message != NULL);
+  GNUNET_ASSERT(htons(message->size) >= 4);
+//fprintf(stderr, "jsendToClient %02x %02x %02x %02x\n", ((char *) message)[0] 
& 0xff, ((char *) message)[1] & 0xff, ((char *) message)[2] & 0xff, ((char *) 
message)[3] & 0xff);
+//fprintf(stderr, "packet is %d bytes long\n", htons(message->size));
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  GNUNET_ASSERT(t->jcapi != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "sendToClient", 
"(Lorg/gnu/freeway/util/net/CSSession;[B)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jarray = (*env)->NewByteArray(env, htons(message->size));
+  GNUNET_ASSERT(jarray != NULL);
+  (*env)->SetByteArrayRegion(env, jarray, 0, htons(message->size), (char*) 
message);
+  ok = (*env)->CallObjectMethod(env, t->jcapi, method, handle, jarray);
+  GNUNET_ASSERT(ok != NULL);
+fprintf(stderr, "leaving sendToClient\n");
+  return convCIntToInt(ok, env);
+}
+
+int sendValueToClient(ClientHandle handle, int value) {
+fprintf(stderr, "in sendValueToClient\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jmethodID method;
+  jobject ok;
+  
+  GNUNET_ASSERT(handle != NULL);
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  GNUNET_ASSERT(t->jcapi != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "sendValueToClient", 
"(Lorg/gnu/freeway/util/net/CSSession;Lorg/gnu/freeway/cwrappers/CInt;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  ok = (*env)->CallObjectMethod(env, t->jcapi, method, handle, 
convIntToCInt(value, env));
+  GNUNET_ASSERT(ok != NULL);
+fprintf(stderr, "leaving sendValueToClient\n");
+  return convCIntToInt(ok, env);
+}
+
+int registerClientHandler(unsigned short type, CSHandler callback) {
+fprintf(stderr, "in registerClientHandler\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jtype;
+  jobject jcallback;
+  jmethodID newHandle;
+  jmethodID method;
+  jobject jret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "registerClientHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jtype = convIntToCInt((int) type, env);
+  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
+  GNUNET_ASSERT(jcallback != NULL);
+  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
+  GNUNET_ASSERT(jret != NULL);
+fprintf(stderr, "leaving registerClientHandler\n");
+  return convCIntToInt(jret, env);
+}
+
+int unregisterClientHandler(unsigned short type, CSHandler callback) {
+fprintf(stderr, "in unregisterClientHandler\n");
+  ActiveThreadList * t;
+  JNIEnv * env;
+  jclass capiClazz;
+  jclass handleClazz;
+  jobject jtype;
+  jobject jcallback;
+  jmethodID newHandle;
+  jmethodID method;
+  jobject jret;
+
+  t = findThread();
+  env = t->env;
+  GNUNET_ASSERT(env != NULL);
+  capiClazz = (*env)->GetObjectClass(env, t->jcapi);
+  GNUNET_ASSERT(capiClazz != NULL);
+  handleClazz = (*env)->FindClass(env, 
"org/gnu/freeway/server/CPluginLoader$Handle");
+  GNUNET_ASSERT(handleClazz != NULL);
+  newHandle = (*env)->GetMethodID(env, handleClazz, "<init>", "(J)V");
+  GNUNET_ASSERT(newHandle != NULL);
+  method = (*env)->GetMethodID(env, capiClazz, "unregisterClientHandler", 
"(Lorg/gnu/freeway/cwrappers/CInt;Lorg/gnu/freeway/server/CPluginLoader$Handle;)Lorg/gnu/freeway/cwrappers/CInt;");
+  GNUNET_ASSERT(method != NULL);
+  jtype = convIntToCInt((int) type, env);
+  jcallback = (*env)->NewObject(env, handleClazz, newHandle, (jlong) (long) 
callback);
+  GNUNET_ASSERT(jcallback != NULL);
+  jret = (*env)->CallObjectMethod(env, t->jcapi, method, jtype, jcallback);
+  GNUNET_ASSERT(jret != NULL);
+fprintf(stderr, "leaving unregisterClientHandler\n");
+  return convCIntToInt(jret, env);
+}

Added: freeway/native/tcpserver.h
===================================================================
--- freeway/native/tcpserver.h  2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/tcpserver.h  2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,35 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/tcpserver.h
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <GNUnet/gnunet_util.h>
+
+int sendToClient(ClientHandle handle, const CS_MESSAGE_HEADER * message);
+
+int sendValueToClient(ClientHandle handle, int value);
+
+int registerClientHandler(unsigned short type, CSHandler callback);
+
+int unregisterClientHandler(unsigned short type, CSHandler callback);

Added: freeway/native/util.c
===================================================================
--- freeway/native/util.c       2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/util.c       2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,459 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/util.c
+ * @brief all the helper functions
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <GNUnet/gnunet_util.h>
+#include <GNUnet/gnunet_core.h>
+#include <locale.h>
+#include "gettext.h"
+#include "jni.h"
+#include "util.h"
+
+static ModuleList * modules;
+static ActiveThreadList * threads;
+Mutex modulesLock;
+Mutex threadsLock;
+static JavaVM * jvm;
+
+/**
+ * checks if the specified module is already loaded
+ * @param the prefix
+ * @param the name of the library
+ * @return YES or NO
+ */
+int hasModule(char * prefix, char * name) {
+  ModuleList * pos;
+  void * temp;
+  MUTEX_LOCK(&modulesLock);
+  pos = modules;
+  while(pos != NULL) {
+    if(pos->prefix == NULL || pos->name == NULL)
+      continue;
+    if(strcmp(prefix, pos->prefix) != 0)
+      continue;
+    if(strcmp(name, pos->name) == 0)
+      break;
+    pos = pos->next;
+  }
+  MUTEX_UNLOCK(&modulesLock);
+  if(pos != NULL)
+    return YES;
+  else
+    return NO;
+}
+
+/**
+ * get a ModuleList structure. will attempt to load the library if needed
+ * @param the prefix
+ * @param the name of the library
+ * @return the ModuleList or NULL if the library cannot be loaded
+ */
+ModuleList * getModule(char * prefix, char * name) {
+  //Look for an already loaded module
+  ModuleList * pos;
+  void * temp;
+  MUTEX_LOCK(&modulesLock);
+  pos = modules;
+  while(pos != NULL) {
+    if(pos->prefix == NULL || pos->name == NULL)
+      continue;
+    if(strcmp(prefix, pos->prefix) != 0)
+      continue;
+    if(strcmp(name, pos->name) == 0)
+      break;
+    pos = pos->next;
+  }
+  if(pos != NULL) {
+    pos->refs++;
+    MUTEX_UNLOCK(&modulesLock);
+    return pos;
+  }
+  //Load a new module
+fprintf(stderr, "Loading new dynamic library %s %s\n", prefix, name);
+  temp = loadDynamicLibrary(prefix, name);
+  if(temp == NULL) {
+    MUTEX_UNLOCK(&modulesLock);
+    return NULL;
+  }
+  pos = MALLOC(sizeof(ModuleList));
+  pos->modulePtr = temp;
+  pos->moduleFptrStruct = NULL;
+  pos->prefix = STRDUP(prefix);
+  pos->name = STRDUP(name);
+  pos->refs = 1;
+  pos->protocolInit = 0;
+  MUTEX_CREATE(&pos->mutex); //does not need locked here
+  pos->next = modules;
+  modules = pos;
+  MUTEX_UNLOCK(&modulesLock);
+  return pos;
+}
+
+/**
+ * decrement the references count on a ModuleList. module will be destroyed if 
the count reaches 0
+ * @param target ModuleList
+ */
+void releaseModule(ModuleList * module) {
+  MUTEX_LOCK(&modulesLock);
+  module->refs--;
+  if(module->refs != 0) {
+    MUTEX_UNLOCK(&modulesLock);
+    //module still in use
+    return;
+  }
+  fprintf(stderr, "unloading the %s module\n", module->name);
+  //Look for this module in the list
+  ModuleList * pos;
+  pos = modules;
+  if(pos != module) {
+    while(pos->next != NULL && pos->next != module) {
+      pos = pos->next;
+    }
+    if(pos->next != NULL)
+      pos->next = pos->next->next;
+  } else {
+    modules = modules->next;
+  }
+  MUTEX_UNLOCK(&modulesLock);
+  unloadDynamicLibrary(module->modulePtr);
+  FREE(module->prefix);
+  FREE(module->name);
+  MUTEX_DESTROY(&module->mutex);
+  FREE(module);
+}
+
+/**
+ * find our thread in ActiveThreadList. Does not check for null or unlock the 
mutex.
+ * Consider using findThread instead.
+ * @return our thread entry or null.
+ */
+static ActiveThreadList * _findThread() {
+       ActiveThreadList * pos;
+       MUTEX_LOCK(&threadsLock);
+       pos = threads;
+       while(pos != NULL) {
+               if(PTHREAD_SELF_TEST(&pos->thread))
+                       break;
+               pos = pos->next;
+       }
+       return pos;
+}
+
+/**
+ * add an entry for this thread in ActiveThreadList
+ * @param our JNIEnv
+ * @param our jcapi
+ * @return our thread entry
+ */
+ActiveThreadList * registerThread(JNIEnv *env, jobject jcapi) {
+       ActiveThreadList * thread;
+       thread = _findThread();
+       if(thread == NULL) {
+               thread = MALLOC(sizeof(ActiveThreadList));
+               PTHREAD_GET_SELF(&thread->thread);
+               thread->env = env;
+               thread->jcapi = jcapi;
+               thread->regs = 1;
+               thread->next = threads;
+               threads = thread;
+       } else {
+               thread->regs++;
+       }
+       MUTEX_UNLOCK(&threadsLock);
+       return thread;
+}
+
+/**
+ * find our thread in ActiveThreadList
+ * WARNING: aborts program if we are not found and the invocation interface is 
unavailable!
+ * @return our thread entry
+ */
+ActiveThreadList * findThread() {
+       ActiveThreadList * thread = _findThread();
+       if (thread != NULL) {
+               if(thread->regs > 0) {
+                       MUTEX_UNLOCK(&threadsLock);
+                       return thread;
+               }
+       }
+       JNIEnv * env;
+       jobject jcapi;
+       jclass jcapiclass;
+       jfieldID jcapifield;
+       if(jvm == NULL) goto crash;
+       (*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL);
+       if(env == NULL) goto crash;
+       jcapiclass = (*env)->FindClass(env, "org/gnu/freeway/server/CoreAPI");
+       if(env == NULL) goto crash;
+       jcapifield = (*env)->GetStaticFieldID(env, jcapiclass, "_", 
"Lorg/gnu/freeway/server/CoreAPI;");
+       if(jcapifield == NULL) goto crash;
+       jcapi = (*env)->GetStaticObjectField(env, jcapiclass, jcapifield);
+       if(jcapifield == NULL) goto crash;
+       if(thread == NULL) {
+               thread = MALLOC(sizeof(ActiveThreadList));
+               PTHREAD_GET_SELF(&thread->thread);
+       }
+       thread->env = env;
+       thread->jcapi = jcapi;
+       thread->regs = 0;
+       thread->next = threads;
+       threads = thread;
+       MUTEX_UNLOCK(&threadsLock);
+       return thread;
+crash:
+       MUTEX_UNLOCK(&threadsLock);
+       errexit(_("Cannot obtain an env for the new thread!\n"));
+       return NULL;
+}
+
+/**
+ * find our thread in ActiveThreadList and return env
+ * don't use this if you also need the api
+ * WARNING: aborts program if we are not found and the invocation interface is 
unavailable!
+ * @return env
+ */
+JNIEnv * getEnv() {
+       return findThread()->env;
+}
+
+/**
+ * remove our thread from the list and free its entry
+ */
+void unregisterThread() {
+       ActiveThreadList * pos = NULL;
+       ActiveThreadList * this = NULL;
+       MUTEX_LOCK(&threadsLock);
+       if(PTHREAD_SELF_TEST(&threads->thread)) {
+               this = threads;
+       } else {
+               pos = threads;
+               while(pos->next != NULL) {
+                       if(PTHREAD_SELF_TEST(&pos->next->thread)) {
+                               this = pos->next;
+                               break;
+                       }
+                       pos = pos->next;
+               }
+       }
+       if(this != NULL) {
+               if(this->regs <= 1) {
+                       if(pos == NULL)
+                               threads = this->next;
+                       else
+                               pos->next = this->next;
+                       MUTEX_UNLOCK(&threadsLock);
+                       PTHREAD_REL_SELF(&this->thread);
+                       this->env = NULL;
+                       this->jcapi = 0;
+                       FREE(this);
+                       return;
+               } else {
+                       this->regs--;
+               }
+       }
+       MUTEX_UNLOCK(&threadsLock);
+}
+
+int convCIntToInt(jobject input, JNIEnv * env) {
+fprintf(stderr, "in convCIntToInt\n");
+  jclass classCInt;
+  jmethodID method;
+
+  if(input == NULL)
+    return 0;
+  classCInt = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CInt");
+  if(classCInt == NULL)
+    return 0;
+  method = (*env)->GetMethodID(env, classCInt, "getValue", "()I");
+  if(method == NULL)
+    return 0;
+fprintf(stderr, "leaving convCIntToInt\n");
+  return (*env)->CallIntMethod(env, input, method);
+}
+
+jobject convIntToCInt(int input, JNIEnv * env) {
+fprintf(stderr, "in convIntToCInt\n");
+  jclass classCInt;
+  jmethodID method;
+
+  classCInt = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CInt");
+  if(classCInt == NULL)
+    return 0;
+  method = (*env)->GetMethodID(env, classCInt, "<init>", "(I)V");
+  if(method == NULL)
+    return 0;
+fprintf(stderr, "leaving convIntToCInt\n");
+  return (*env)->NewObject(env, classCInt, method, input);
+}
+
+long convCLongToLong(jobject input, JNIEnv * env) {
+fprintf(stderr, "in convCLongToLong\n");
+  jclass classCLong;
+  jmethodID method;
+
+  if(input == NULL)
+    return 0;
+  classCLong = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CLong");
+  if(classCLong == NULL)
+    return 0;
+  method = (*env)->GetMethodID(env, classCLong, "getValue", "()J");
+  if(method == NULL)
+    return 0;
+fprintf(stderr, "leaving convCLongToLong\n");
+  return (*env)->CallLongMethod(env, input, method);
+}
+
+jobject convLongToCLong(long input, JNIEnv * env) {
+fprintf(stderr, "in convLongToCLong\n");
+  jclass classCLong;
+  jmethodID method;
+
+  classCLong = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CLong");
+  if(classCLong == NULL)
+    return 0;
+  method = (*env)->GetMethodID(env, classCLong, "<init>", "(J)V");
+  if(method == NULL)
+    return 0;
+fprintf(stderr, "leaving convLongToCLong\n");
+  return (*env)->NewObject(env, classCLong, method, input);
+}
+
+PointerObject * convObjectToPtr(jobject input, JNIEnv * env) {
+fprintf(stderr, "in convObjectToPtr\n");
+  jclass objectClass;
+  jmethodID method;
+  PointerObject * po;
+
+  po = MALLOC(sizeof(PointerObject));
+  
+  if(input == NULL)
+    return NULL;
+  objectClass = (*env)->GetObjectClass(env, input);
+  if(objectClass == NULL)
+    return NULL;
+  method = (*env)->GetMethodID(env, objectClass, "serializeToByteArray", 
"()[B");
+  if(method == NULL)
+    return NULL;
+  po->object = (*env)->CallObjectMethod(env, input, method);
+  po->pointer = (*env)->GetByteArrayElements(env, po->object, NULL);
+fprintf(stderr, "leaving convObjectToPtr\n");
+  return po;
+}
+
+jobject convPtrToNewObject(void * input, jclass returnType, JNIEnv * env) {
+fprintf(stderr, "in convPtrToNewObject\n");
+  jmethodID method;
+  jmethodID methodSize;
+  jint size;
+  jobject object;
+  jbyteArray array;
+  int i;
+  
+  if(input == NULL || returnType == NULL)
+    return NULL;
+  method = (*env)->GetStaticMethodID(env, returnType, "newFromPointer", 
"(J)Ljava/lang/Object;");
+  if(method != NULL)
+    return (*env)->CallStaticObjectMethod(env, returnType, method, input);
+  (*env)->ExceptionClear(env);
+  object = (*env)->AllocObject(env, returnType);
+  if(object == NULL)
+    return;
+  methodSize = (*env)->GetMethodID(env, object, "getSerializedSize", "()I");
+  if(methodSize == NULL)
+    return NULL;
+  method = (*env)->GetMethodID(env, returnType, "<init>", "([B)V");
+  if(method == NULL)
+    return NULL;
+  size = (*env)->CallIntMethod(env, object, methodSize);
+  array = (*env)->NewByteArray(env, size);
+  if(array == NULL)
+    return NULL;
+  (*env)->SetByteArrayRegion(env, array, 0, size, input);
+fprintf(stderr, "leaving convPtrToNewObject\n");
+  return (*env)->NewObject(env, returnType, method, array);
+}
+
+void updateObjectFromPtr(jobject target, PointerObject * input, JNIEnv * env) {
+fprintf(stderr, "in updateObjectFromPtr\n");
+  jclass objectClass;
+  jmethodID method;
+  
+  if(input == NULL)
+       return;
+  if(input->pointer == NULL || input->object == NULL)
+    return;
+  objectClass = (*env)->GetObjectClass(env, target);
+  if(objectClass == NULL)
+    return;
+  method = (*env)->GetMethodID(env, objectClass, "deserializeFromPointer", 
"(J)V");
+  if(method != NULL) {
+    (*env)->CallVoidMethod(env, target, method, input->pointer);
+    (*env)->ReleaseByteArrayElements(env, input->object, input->pointer, 0);
+    FREE(input);
+fprintf(stderr, "leaving updateObjectFromPtr\n");
+    return;
+  }
+  (*env)->ExceptionClear(env);
+  (*env)->ReleaseByteArrayElements(env, input->object, input->pointer, 0);
+  method = (*env)->GetMethodID(env, objectClass, "deserializeFromByteArray", 
"([B)V");
+  if(method != NULL) {
+    (*env)->CallVoidMethod(env, target, method, input->object);
+    FREE(input);
+fprintf(stderr, "leaving updateObjectFromPtr\n");
+    return;
+  }
+  (*env)->ExceptionClear(env);
+  //Constant CWrappers cannot be updated
+}
+
+jobject convJStringToCString(jstring input, JNIEnv * env) {
+fprintf(stderr, "in convJStringToCString\n");
+  jclass classCString;
+  jmethodID method;
+
+  if(input == NULL)
+    return NULL;
+  classCString = (*env)->FindClass(env, "org/gnu/freeway/cwrappers/CString");
+  if(classCString == NULL)
+    return NULL;
+  method = (*env)->GetMethodID(env, classCString, "<init>", 
"(Ljava/lang/String;)V");
+  if(method == NULL)
+    return NULL;
+fprintf(stderr, "leaving convJStringToCString\n", env, classCString, method, 
input);
+  return (*env)->NewObject(env, classCString, method, input);
+}
+
+jint JNI_OnLoad(JavaVM * vm, void * reserved) {
+  jvm = vm;
+  return JNI_VERSION_1_1;
+}
+
+//TODO: fix this
+int parseGnunetdCommandLine(int argc, char * argv[]) {
+  return OK;
+}

Added: freeway/native/util.h
===================================================================
--- freeway/native/util.h       2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/native/util.h       2006-07-27 08:02:54 UTC (rev 3141)
@@ -0,0 +1,94 @@
+/*
+     This file is part of Freeway.
+     (C) 2006 Free Software Foundation
+
+     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.
+
+     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.
+*/
+
+/**
+ * @file native/util.h
+ * @author mdonoughe
+ * @author Christian Grothoff
+ */
+
+#include "jni.h"
+
+#define DSO_PREFIX "libgnunet"
+
+#define _(a) a
+
+/**
+ * Used to identify which env / jcapi to use when called back.
+ */
+//TODO: make a cron task that cleans out ActiveThreadLists that have 0 
regs(created from a C thread) and haven't been retrieved recently.
+typedef struct ActiveThreadList {
+  JNIEnv * env;
+  jobject jcapi;
+  unsigned int regs;
+  PTHREAD_T thread;
+  struct ActiveThreadList * next;
+} ActiveThreadList;
+
+typedef struct ModuleList {
+  void * modulePtr;
+  void * moduleFptrStruct;
+  char * prefix;
+  char * name;
+  unsigned int refs;
+  char protocolInit;
+  Mutex mutex; //stop initialization races
+  struct ModuleList * next;
+} ModuleList;
+
+typedef struct PointerObject {
+  void * pointer;
+  jobject object;
+} PointerObject;
+
+Mutex modulesLock;
+Mutex threadsLock;
+
+int hasModule(char * prefix, char * name);
+
+ModuleList * getModule(char * prefix, char * name);
+
+void releaseModule(ModuleList * module);
+
+ActiveThreadList * registerThread(JNIEnv *env, jobject jcapi);
+
+ActiveThreadList * findThread();
+
+JNIEnv * getEnv();
+
+void unregisterThread();
+
+int convCIntToInt(jobject input, JNIEnv * env);
+
+jobject convIntToCInt(int input, JNIEnv * env);
+
+long convCLongToLong(jobject input, JNIEnv * env);
+
+jobject convLongToCLong(long input, JNIEnv * env);
+
+PointerObject * convObjectToPtr(jobject input, JNIEnv * env);
+
+jobject convPtrToNewObject(void * input, jclass returnType, JNIEnv * env);
+
+void updateObjectFromPtr(jobject target, PointerObject * input, JNIEnv * env);
+
+jobject convJStringToCString(jstring input, JNIEnv * env);
+
+int parseGnunetdCommandLine(int argc, char * argv[]);

Deleted: freeway/po/remove-potcdate.sed
===================================================================
--- freeway/po/remove-potcdate.sed      2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/po/remove-potcdate.sed      2006-07-27 08:02:54 UTC (rev 3141)
@@ -1,11 +0,0 @@
-/^"POT-Creation-Date: .*"$/{
-x
-s/P/P/
-ta
-g
-d
-bb
-:a
-x
-:b
-}

Modified: freeway/src/org/gnu/freeway/AbstractApplication.java
===================================================================
--- freeway/src/org/gnu/freeway/AbstractApplication.java        2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/AbstractApplication.java        2006-07-27 
08:02:54 UTC (rev 3141)
@@ -19,6 +19,9 @@
 
 public abstract class AbstractApplication extends Command implements 
Application
 {
+       public static final Thread mainThread = Thread.currentThread();
+
+
        static {
                // setup mac os specific ui properties
                if 
(System.getProperty("os.name").toLowerCase().indexOf("mac")>=0) {

Modified: freeway/src/org/gnu/freeway/GNUNetDaemon.java
===================================================================
--- freeway/src/org/gnu/freeway/GNUNetDaemon.java       2006-07-26 09:13:36 UTC 
(rev 3140)
+++ freeway/src/org/gnu/freeway/GNUNetDaemon.java       2006-07-27 08:02:54 UTC 
(rev 3141)
@@ -253,7 +253,7 @@
        public static void main( String[] args )
        {
                //TODO: use args
-               CPluginLoader.initUtil(new String[] {});
+               CPluginLoader._.initUtil(new String[] {});
                launch(GNUNetDaemon.class,args);
        }
 

Modified: freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java        
2006-07-27 08:02:54 UTC (rev 3141)
@@ -597,7 +597,7 @@
                for(Iterator i = order.iterator(); i.hasNext(); )
                        writer.write(((String) 
methodList.get(i.next())).replace("__FUNCTIONINDEX__", Integer.toString(ic++)) 
+ "\n\n");
                writer.write("  public " + stripPackage(c.getName()) + "() 
{\n");
-               writer.write("          this(new " + 
cleanClassName("org.gnu.freeway.server.CPluginLoader", imports) + "());\n");
+               writer.write("          this(" + 
cleanClassName("org.gnu.freeway.server.CPluginLoader", imports) + "._);\n");
                writer.write("  }\n");
                writer.write("\n");
                writer.write("  public " + stripPackage(c.getName()) + "(" + 
cleanClassName("org.gnu.freeway.server.CPluginLoader", imports) + " loader) 
{\n");

Modified: freeway/src/org/gnu/freeway/protocol/stats/NativeStatsProtocol.java
===================================================================
--- freeway/src/org/gnu/freeway/protocol/stats/NativeStatsProtocol.java 
2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/protocol/stats/NativeStatsProtocol.java 
2006-07-27 08:02:54 UTC (rev 3141)
@@ -29,6 +29,7 @@
 import org.gnu.freeway.util.Prefs;
 import org.gnu.freeway.util.net.CSHandler;
 import org.gnu.freeway.util.net.CSMessage;
+import org.gnu.freeway.util.net.CSNativeMessage;
 import org.gnu.freeway.util.net.CSSession;
 import org.gnu.freeway.util.net.P2PMessage;
 import org.gnu.freeway.util.net.PersistentDecoder;
@@ -48,7 +49,7 @@
        public NativeStatsProtocol() {
                super();
                
-               loader = new CPluginLoader();
+               loader = CPluginLoader._;
        }
        
        /* (non-Javadoc)
@@ -57,7 +58,7 @@
        public boolean init(CoreForProtocol api) {
                boolean ok = super.init(api);
                CoreAPI._.setProtocolCore(api);
-               ok &= CoreAPI.OK.getValue() == 
loader.loadProtocol(CPluginLoader.classToLibraryName(getName()));
+               ok &= CoreAPI.OK.getValue() == 
loader.loadApplicationModule(CPluginLoader.classToLibraryName(getName()));
                return ok;
        }
        
@@ -65,7 +66,7 @@
         * @see org.gnu.freeway.protocol.Protocol#done()
         */
        public void done() {
-               // TODO Auto-generated method stub
+               
loader.unloadApplicationModule(CPluginLoader.classToLibraryName(getName()));
                super.done();
        }
        
@@ -77,13 +78,4 @@
                        ret = ret.substring(0, ret.length() - 8);
                return ret.toLowerCase();
        }
-
-       /* (non-Javadoc)
-        * @see org.gnu.freeway.protocol.Protocol#createCSDecoder()
-        */
-       public PersistentDecoder createCSDecoder() {
-               // TODO Auto-generated method stub
-               return super.createCSDecoder();
-       }
-
 }

Modified: freeway/src/org/gnu/freeway/protocol/stats/StatsProtocol.java
===================================================================
--- freeway/src/org/gnu/freeway/protocol/stats/StatsProtocol.java       
2006-07-26 09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/protocol/stats/StatsProtocol.java       
2006-07-27 08:02:54 UTC (rev 3141)
@@ -34,6 +34,8 @@
 import org.gnu.freeway.util.Prefs;
 import org.gnu.freeway.util.net.CSHandler;
 import org.gnu.freeway.util.net.CSMessage;
+import org.gnu.freeway.util.net.CSNativeMessage;
+import org.gnu.freeway.util.net.CSResult;
 import org.gnu.freeway.util.net.CSSession;
 import org.gnu.freeway.util.net.ErrorReporter;
 import org.gnu.freeway.util.net.NativeCSHandler;
@@ -75,22 +77,13 @@
                boolean ok = super.init(api);
                service = (StatsService) 
api.service(org.gnu.freeway.services.StatsService.class, "java");
                ok &= service != null;
-               ok &= ((Server) 
api.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_GET_STATISTICS,
 CSMessage.class, this);
-               ok &= ((Server) 
api.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_STATISTICS, 
CSMessage.class, this);
-               ok &= ((Server) 
api.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED,
 CSMessage.class, this);
-               ok &= ((Server) 
api.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,
 CSMessage.class, this);
+               ok &= ((Server) 
coreAPI.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_GET_STATISTICS,
 CSNativeMessage.class, this);
+               ok &= ((Server) 
coreAPI.getApplication()).registerCSHandler(CSMessage.CS_PROTO_traffic_COUNT, 
CSNativeMessage.class, this);
+               ok &= ((Server) 
coreAPI.getApplication()).registerCSHandler(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,
 CSNativeMessage.class, this);
                //TODO: add noise
                return ok;
        }
        
-       /* (non-Javadoc)
-        * @see org.gnu.freeway.protocol.Protocol#done()
-        */
-       public void done() {
-               // TODO Auto-generated method stub
-               super.done();
-       }
-       
        public String toString() {
                String ret = this.getClass().getSimpleName();
                if(ret.startsWith("Native"))
@@ -100,22 +93,11 @@
                return ret.toLowerCase();
        }
 
-       /* (non-Javadoc)
-        * @see org.gnu.freeway.protocol.Protocol#createCSDecoder()
-        */
-       public PersistentDecoder createCSDecoder() {
-               // TODO Auto-generated method stub
-               return super.createCSDecoder();
-       }
-
        public boolean handle(CSSession session, CSMessage message) {
                if(message.getType() == 
CSMessage.CS_PROTO_stats_GET_STATISTICS) {
                        return handleGetStatistics(session, message);
-               } else if(message.getType() == 
CSMessage.CS_PROTO_stats_STATISTICS) {
-                       return handleStatistics(session, message);
-               } else if(message.getType() == 
CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED) {
-                       // TODO iirc, all messages are "supported" for now
-                       return handleGetCSMessageSupported(session, message);
+               } else if(message.getType() == 
CSMessage.CS_PROTO_traffic_COUNT) {
+                       return handleCount(session, message);
                } else if(message.getType() == 
CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED) {
                        return handleGetP2PMessageSupported(session, message);
                }
@@ -123,37 +105,23 @@
        }
 
        private boolean handleGetP2PMessageSupported(CSSession session, 
CSMessage message) {
-               // TODO Auto-generated method stub
-               return false;
+               // TODO implement this
+               session.send(new CSResult(0));
+               return true;
        }
 
-       private boolean handleGetCSMessageSupported(CSSession session, 
CSMessage message) {
-               // TODO Auto-generated method stub
-               return false;
-       }
-
-       private boolean handleStatistics(CSSession session, CSMessage message) {
-               // TODO Auto-generated method stub
-               System.err.println("I have recieved some statistics");
-               return false;
-       }
-
-       private boolean handleGetStatistics(CSSession session, CSMessage 
message) {
-               //assume that the server is telling the truth and just send a 
reply
-               sendStatistics(session);
+       private boolean handleCount(CSSession session, CSMessage message) {
+               session.send(new CSResult(coreAPI.forAllConnectedNodes(null, 
null)));
                return true;
        }
 
-       private void sendStatistics(CSSession session) {
+       private boolean handleGetStatistics(CSSession session, CSMessage 
message) {
                List                            list;
                CSStatsMessage          msg;
                int                             total,i;
                List stats = service.stats;
-
                list=new ArrayList();
-
                total = stats.size();
-
                i=0;
                while (i < stats.size()) {
                        msg=new CSStatsMessage(service.startTime);
@@ -174,7 +142,22 @@
                for(Iterator j = list.iterator(); j.hasNext();) {
                        msg = (CSStatsMessage) j.next();
                        session.send(msg);
-                       System.err.println(msg.toString() + msg.getByteSize());
                }
+               return true;
        }
+       
+       public PersistentDecoder createCSDecoder() {
+               PersistentDecoder dec = new PersistentDecoder();
+               dec.add(CSMessage.CS_PROTO_stats_GET_STATISTICS, 
CSNativeMessage.class);
+               dec.add(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED, 
CSMessage.class); //TODO: use a working class
+               dec.add(CSMessage.CS_PROTO_traffic_COUNT, 
CSNativeMessage.class);
+               return dec;
+       }
+       
+       public void  done() {
+               ((Server) 
coreAPI.getApplication()).unregisterCSHandler(CSMessage.CS_PROTO_stats_GET_STATISTICS,
 this);
+               ((Server) 
coreAPI.getApplication()).unregisterCSHandler(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,
 this);
+               ((Server) 
coreAPI.getApplication()).unregisterCSHandler(CSMessage.CS_PROTO_traffic_COUNT, 
this);
+               super.done();
+       }
 }

Modified: freeway/src/org/gnu/freeway/server/CPluginLoader.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CPluginLoader.java       2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/server/CPluginLoader.java       2006-07-27 
08:02:54 UTC (rev 3141)
@@ -21,25 +21,52 @@
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
 
+import org.gnu.freeway.AbstractApplication;
 import org.gnu.freeway.cwrappers.util.SwitchTableGenerator;
 import org.gnu.freeway.util.LoggedObject;
 import org.gnu.freeway.util.NativeService;
+import org.gnu.freeway.util.net.CSMessage;
 import org.gnu.freeway.util.net.CSNativeMessage;
 import org.gnu.freeway.util.net.CSSession;
+import org.gnu.freeway.util.net.PersistentDecoder;
 
 /**
  * @file CPluginLoader.java
  * @brief
  * @author mdonoughe
  */
-public class CPluginLoader extends LoggedObject {
-       public static boolean loaded = false;
+public class CPluginLoader {
+       private CPluginLoader instance;
+       private boolean loaded = false;
+       private Map protocols;
+       private PersistentDecoder dec;
 
-       static {
+       public static final CPluginLoader _ = getLoader();
+       
+       static CPluginLoader getLoader() {
                try {
-                       
System.loadLibrary("org_gnu_freeway_server_CPluginLoader");
+                       return (CPluginLoader) 
AbstractApplication.mainThread.getContextClassLoader().loadClass(CPluginLoader.class.getCanonicalName()).newInstance();
+               } catch (InstantiationException e) {
+                       System.err.println("Could not instantialize the 
CPluginLoader. Will try again.");
+                       e.printStackTrace();
+               } catch (IllegalAccessException e) {
+                       e.printStackTrace();
+               } catch (ClassNotFoundException e) {
+                       System.err.println("CPluginLoader was not found in the 
default classpath. Will try again.");
+                       e.printStackTrace();
+               }
+               return new CPluginLoader();
+       }
+       
+       protected CPluginLoader() {
+               try {
+                       System.loadLibrary("cpluginloader");
                        loaded = true;
+                       protocols = new HashMap();
+                       dec=new PersistentDecoder();
                } catch(SecurityException e) {
                        System.err.println("Security manager forbids loading 
native code.");
                } catch(UnsatisfiedLinkError e) {
@@ -47,30 +74,42 @@
                        
System.err.println("org_gnu_freeway_server_CPluginLoader could not be loaded.");
                }
        }
+       
+       public PersistentDecoder getDecoder() {
+               return dec;
+       }
+       
+       public void registerDecoder(int type) {
+               dec.add(type,CSNativeMessage.class);
+       }
+       
+       public void unregisterDecoder(int type) {
+               dec.remove(type);
+       }
 
        private static native long cLoadService(String serviceName, CoreAPI 
capi);
        
-       private static native int cLoadProtocol(String protocolName, CoreAPI 
capi);
+       private static native long cLoadApplicationModule(String protocolName, 
CoreAPI capi);
        
-       private static native int cInitUtil(String[] args);
+       private static native int cInitUtil(String[] args, CoreAPI capi);
 
        private static native Object cCallC(long modulePtr, CoreAPI capi,
                        int functionOffset, int functionType, Object[] 
arguments);
 
-       private static native void cUnloadService(long modulePtr);
+       private static native void cUnloadService(long modulePtr, CoreAPI capi);
        
-       private static native long cBindDynamicMethod(long libhandle, String 
methodprefix, String dsoname);
+       private static native void cUnloadApplicationModule(long modulePtr, 
CoreAPI capi);
        
-       private static native boolean cHasModule(String dsoprefix, String name);
+       //private static native long cBindDynamicMethod(long libhandle, String 
methodprefix, String dsoname);
        
-       private static native long cLoadDynamicLibrary(String dsoprefix, String 
name);
+       private static native boolean cHasModule(String dsoprefix, String name, 
CoreAPI capi);
        
-       private static native void cUnloadDynamicLibrary(long libhandle);
+       /*private static native long cLoadDynamicLibrary(String dsoprefix, 
String name);
+       
+       private static native void cUnloadDynamicLibrary(long libhandle);*/
+       
+       private static native int cCallCallCSHandle(CoreAPI capi, long fptr, 
CSSession session, byte[] arr);
 
-       public CPluginLoader() {
-               super(true);
-       }
-
        /**
         * This method is used by the generated "services.impl"
         * constructors to load the C plugin.
@@ -85,11 +124,17 @@
                return new Handle(cLoadService(serviceName, CoreAPI._));
        }
        
-       public int loadProtocol(String protocolName) {
+       public int loadApplicationModule(String protocolName) {
                assert (protocolName != null);
                if(!loaded)
                        return CoreAPI.SYSERR.getValue();
-               return cLoadProtocol(protocolName, CoreAPI._);
+               long ptr = cLoadApplicationModule(protocolName, CoreAPI._);
+               if(ptr == 0) {
+                       return CoreAPI.SYSERR.getValue();
+               } else {
+                       protocols.put(protocolName, new Handle(ptr));
+                       return CoreAPI.OK.getValue();
+               }
        }
        
        /**
@@ -97,11 +142,11 @@
         * @param args commadline arguments
         */
        
-       public static int initUtil(String[] args) {
+       public int initUtil(String[] args) {
                assert(args != null);
                if(!loaded)
                        return CoreAPI.SYSERR.getValue();
-               return cInitUtil(args);
+               return cInitUtil(args, CoreAPI._);
        }
 
        /**
@@ -133,40 +178,49 @@
                assert modulePtr != null;
                if(!loaded)
                        return;
-               cUnloadService(modulePtr._);
+               cUnloadService(modulePtr._, CoreAPI._);
        }
        
-       public static Handle bindDynamicMethod(Handle libhandle, String 
methodprefix, String dsoname) {
+       public void unloadApplicationModule(String protocolName) {
+               assert protocolName != null;
+               if(!loaded)
+                       return;
+               Handle modulePtr = (Handle) protocols.get(protocolName);
+               assert modulePtr != null;
+               cUnloadApplicationModule(modulePtr._, CoreAPI._);
+       }
+       
+       /*public Handle bindDynamicMethod(Handle libhandle, String 
methodprefix, String dsoname) {
                assert libhandle != null;
                assert methodprefix != null;
                assert dsoname != null;
                if(!loaded)
                        return Handle.NULL;
                return new Handle(cBindDynamicMethod(libhandle._, methodprefix, 
dsoname));
-       }
+       }*/
        
-       public static Handle loadDynamicLibrary(String dsoprefix, String name) {
+       /*public static Handle loadDynamicLibrary(String dsoprefix, String 
name) {
                assert dsoprefix != null;
                assert name != null;
                if(!loaded)
                        return Handle.NULL;
                return new Handle(cLoadDynamicLibrary(dsoprefix, name));
-       }
+       }*/
        
-       public static boolean hasModule(String dsoprefix, String name) {
+       public boolean hasModule(String dsoprefix, String name) {
                assert dsoprefix != null;
                assert name != null;
                if(!loaded)
                        return false;
-               return cHasModule(dsoprefix, name);
+               return cHasModule(dsoprefix, name, CoreAPI._);
        }
        
-       public static void unloadDynamicLibrary(Handle libhandle) {
+       /*public static void unloadDynamicLibrary(Handle libhandle) {
                assert libhandle != null;
                if(!loaded)
                        return;
                cUnloadDynamicLibrary(libhandle._);
-       }
+       }*/
        
        /**
         * Converts a class name to a module name. StatsService -> module_stats
@@ -188,7 +242,7 @@
         * @param the Java class
         * @return a Handle to the facade
         */
-       public static Handle getFacade(Object c) {
+       public Handle getFacade(Object c) {
                //TODO: not implemented
                return Handle.NULL;
        }
@@ -203,9 +257,7 @@
        }
 
 
-       public static boolean callCSHandle(Handle handle, CSSession session, 
CSNativeMessage message) {
+       public boolean callCSHandle(Handle handle, CSSession session, 
CSNativeMessage message) {
                return CoreAPI.OK.getValue() == cCallCallCSHandle(CoreAPI._, 
handle._, session, message.arr);
        }
-
-       private static native int cCallCallCSHandle(CoreAPI capi, long fptr, 
CSSession session, byte[] arr);
 }

Modified: freeway/src/org/gnu/freeway/server/CSGetClientCount.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CSGetClientCount.java    2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/server/CSGetClientCount.java    2006-07-27 
08:02:54 UTC (rev 3141)
@@ -19,7 +19,7 @@
 
        public CSGetClientCount()
        {
-               super(IS_CLIENT_COUNT);
+               super(CS_PROTO_traffic_COUNT);
        }
 
        public String toString()
@@ -43,12 +43,12 @@
                err.reportIf(size!=SIZE,"bad size !");
 
                type=buf.getShort() & 0x0000ffff;
-               err.reportIf(type!=IS_CLIENT_COUNT,"bad type !");
+               err.reportIf(type!=CS_PROTO_traffic_COUNT,"bad type !");
        }
 
        public void writeBytes( ByteBuffer buf )
        {
                buf.putShort((short) SIZE);
-               buf.putShort((short) IS_CLIENT_COUNT);
+               buf.putShort((short) CS_PROTO_traffic_COUNT);
        }
 }

Modified: freeway/src/org/gnu/freeway/server/ClientServer.java
===================================================================
--- freeway/src/org/gnu/freeway/server/ClientServer.java        2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/server/ClientServer.java        2006-07-27 
08:02:54 UTC (rev 3141)
@@ -64,8 +64,8 @@
                        core=(CoreService) appp.service(CoreService.class);
 
                        decoder=new PersistentDecoder();
-                       decoder.add(36, CSNativeMessage.class);
                        decoder.merge(core.createCSDecoder());
+                       decoder.merge(CPluginLoader._.getDecoder());
                        }
        }
        /**
@@ -87,9 +87,6 @@
                prefs=app.getPreferences();
 
                // register default handlers
-               registerCSHandler(CSMessage.CS_PROTO_stats_GET_STATISTICS,this);
-               
registerCSHandler(CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED,this);
-               
registerCSHandler(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,this);
                registerCSHandler(CSMessage.IS_GET_OPTION_REQUEST,this);
 
                port=prefs.getInt("NETWORK","PORT",0);
@@ -112,10 +109,7 @@
                        }
 
                // unregister default handlers
-               /*unregisterCSHandler(CSMessage.IS_GET_OPTION_REQUEST,this);
-               
unregisterCSHandler(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,this);
-               
unregisterCSHandler(CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED,this);*/
-               
unregisterCSHandler(CSMessage.CS_PROTO_stats_GET_STATISTICS,this);
+               unregisterCSHandler(CSMessage.IS_GET_OPTION_REQUEST,this);
 
                synchronized(handlers) {
                        if (exitHandlers.size()>0) {
@@ -183,16 +177,14 @@
                err=false;
                for (msg=(CSMessage) s.receive(decoder); msg!=null && !err; 
msg=(CSMessage) s.receive(decoder)) {
                        type=msg.getType();
-                       synchronized(handlers) {
-                               hd=getCSHandler(type);
-                               if (hd==null) {
-                                       log(Level.INFO,"Client-server message 
not understood (type is "+type+") !");
-                                       err=true;
-                                       }
-                               else {
-                                       hd.handle(s,msg);
-                                       }
+                       hd=getCSHandler(type);
+                       if (hd==null) {
+                               log(Level.INFO,"Client-server message not 
understood (type is "+type+") !");
+                               err=true;
                                }
+                       else {
+                               hd.handle(s,msg);
+                               }
                        }
                return !err;
        }
@@ -223,7 +215,7 @@
         *
         * @param type the message type
         * @return true if there is a handler for the type,
-        *      NO if there isn't
+        *      false if there isn't
         */
 
        public boolean isCSHandlerRegistered( int type )
@@ -257,11 +249,10 @@
                                log(Level.WARNING,"Invalid type : "+type+" 
!!!");
                                return false;
                                }
-                       //TODO: handlers needs to be a map
-                       /*if (type<handlers.size() && handlers.get(type)!=null) 
{
+                       if (type<handlers.size() && handlers.get(type)!=null) {
                                log(Level.WARNING,"Failed to register handler, 
slot "+type+" used.");
                                return false;
-                               }*/
+                               }
 
                        while (handlers.size()<=type) {
                                handlers.add(null);
@@ -289,7 +280,7 @@
                                return false;
                                }
 
-                       if (handlers.get(type)!=hd) {
+                       if (!(hd instanceof NativeCSHandler && 
handlers.get(type) instanceof NativeCSHandler && ((NativeCSHandler) 
handlers.get(type)).ptr._ == ((NativeCSHandler) hd).ptr._)&& 
handlers.get(type)!=hd) {
                                log(Level.WARNING,"Another handler present at 
slot #"+type+".");
                                return false;
                                }
@@ -301,7 +292,7 @@
 
        public boolean registerClientExitHandler( ClientExitHandler hd )
        {
-               synchronized(handlers) {
+               synchronized(exitHandlers) {
                        exitHandlers.add(hd);
                        }
                return true;
@@ -309,7 +300,7 @@
 
        public boolean unregisterClientExitHandler( ClientExitHandler hd )
        {
-               synchronized(handlers) {
+               synchronized(exitHandlers) {
                        if (!exitHandlers.contains(hd)) {
                                log(Level.WARNING,"Client exit handler not 
found ("+hd+") !");
                                return false;
@@ -338,9 +329,6 @@
                        }
                return false;
        }
-       protected boolean handleStatistics( CSSession client, 
CSStatisticsRequest msg ) {
-               return false;
-       }
        /* protected boolean handleStatistics( CSSession client, 
CSStatisticsRequest msg )
        {
                CSStatistics[]  resp;

Modified: freeway/src/org/gnu/freeway/server/ConnectionService.java
===================================================================
--- freeway/src/org/gnu/freeway/server/ConnectionService.java   2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/server/ConnectionService.java   2006-07-27 
08:02:54 UTC (rev 3141)
@@ -236,7 +236,7 @@
                
dispatcher.registerP2PHandler(P2PMessage.IS_CAPABILITY,P2PCapability.class,this);
                
dispatcher.registerP2PHandler(P2PMessage.IS_SESSION_KEY,P2PSessionKey.class,this);
 
-               ((Server) 
getManager().app()).registerCSHandler(CSMessage.IS_CLIENT_COUNT,CSGetClientCount.class,this);
+               //((Server) 
getManager().app()).registerCSHandler(CSMessage.IS_CLIENT_COUNT,CSGetClientCount.class,this);
 
                countTask=new ScheduledTask("COUNT-CONNECTIONS",new 
EvalAction(this,"cronCountConnections"),Scheduler.SECS_30);
                livenessTask=new ScheduledTask("DECREASE-LIVENESS",new 
EvalAction(this,"cronDecreaseLiveness"),Scheduler.MINUTES_1 / 
connectionBuffer.length);
@@ -262,7 +262,7 @@
                scheduler.deleteJob(livenessTask);
                scheduler.deleteJob(countTask);
 
-               ((Server) 
getManager().app()).unregisterCSHandler(CSMessage.IS_CLIENT_COUNT,this);
+               //((Server) 
getManager().app()).unregisterCSHandler(CSMessage.IS_CLIENT_COUNT,this);
 
                dispatcher=((Server) getManager().app()).getDispatcher();
                dispatcher.unregisterP2PHandler(P2PMessage.IS_SESSION_KEY,this);

Modified: freeway/src/org/gnu/freeway/server/CoreAPI.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CoreAPI.java     2006-07-26 09:13:36 UTC 
(rev 3140)
+++ freeway/src/org/gnu/freeway/server/CoreAPI.java     2006-07-27 08:02:54 UTC 
(rev 3141)
@@ -27,6 +27,7 @@
 import org.gnu.freeway.util.ServiceManager;
 import org.gnu.freeway.util.net.CSMessage;
 import org.gnu.freeway.util.net.CSNativeMessage;
+import org.gnu.freeway.util.net.CSResult;
 import org.gnu.freeway.util.net.CSSession;
 import org.gnu.freeway.util.net.NativeCSHandler;
 
@@ -69,11 +70,12 @@
        private ArrayList shutdownList;
        
        public CInt loadApplicationModule(CString name) {
-               return SYSERR;
+               return new 
CInt(CPluginLoader._.loadApplicationModule(name.getValue()));
        }
        
        public CInt unloadApplicationModule(CString name) {
-               return SYSERR;
+               CPluginLoader._.unloadApplicationModule(name.getValue());
+               return OK;
        }
        
        private String getConfigurationString(String section, String option) {
@@ -87,14 +89,14 @@
                        c = Class.forName("org.gnu.freeway.services." + 
iname.getValue().substring(0, 1).toUpperCase() + iname.getValue().substring(1) 
+ "Service");
                } catch (ClassNotFoundException e) {
                        System.err.println("org.gnu.freeway.services." + 
iname.getValue().substring(0, 1).toUpperCase() + iname.getValue().substring(1) 
+ "Service was not found");
-                       return new CPluginLoader.Handle(0);
+                       return CPluginLoader.Handle.NULL;
                }
                if(!NativeService.class.isAssignableFrom(c)) {
                        System.err.println(c.getCanonicalName() + " is not a 
NativeService");
                        return new CPluginLoader.Handle(0);
                }
                NativeService s = null;
-               if(CPluginLoader.hasModule("libgnunet", "module_" + 
iname.getValue())) {
+               if(CPluginLoader._.hasModule("libgnunet", "module_" + 
iname.getValue())) {
                        s = (NativeService) ServiceManager.manager.service(c, 
"c");
                } else {
                        s = (NativeService) ServiceManager.manager.service(c);
@@ -103,7 +105,8 @@
        }
        
        public CInt releaseService(CPluginLoader.Handle service) {
-               return SYSERR;
+               // TODO Freeway doesn't do this?
+               return OK;
        }
        
        public CInt sendPlaintext(CTSession session, CString msg, CUnsignedInt 
size) {
@@ -119,8 +122,7 @@
        }
        
        public CInt forAllConnectedNodes(CPluginLoader.Handle method, 
CPluginLoader.Handle arg) {
-               //TODO: implement this
-               return new CInt(1);
+               return new CInt(protocolCore.forAllConnectedNodes(null, null));
        }
        
        public CInt registerSendCallback(ConstCUnsignedInt minimumPadding, 
CBufferFillCallback callback) {
@@ -144,11 +146,16 @@
                return ((Server) 
protocolCore.getApplication()).registerCSHandler(type.getValue(), 
CSMessage.class, new NativeCSHandler(callback)) ? CoreAPI.OK : CoreAPI.SYSERR;
        }
        
-       public CInt unregisterHandler(CUnsignedShort type, CMessagePartHandler 
callback) {
-               return SYSERR;
+       public CInt unregisterHandler(CInt type, CPluginLoader.Handle callback) 
{
+//             TODO: this should be P2P
+               return ((Server) 
protocolCore.getApplication()).unregisterCSHandler(type.getValue(), new 
NativeCSHandler(callback)) ? CoreAPI.OK : CoreAPI.SYSERR;
        }
        
-       public CInt isHandlerRegistered(CUnsignedShort type, CUnsignedShort 
handlerType) {
+       public CInt isHandlerRegistered(CInt type, CInt handlerType) {
+               if(handlerType.getValue() > 3)
+                       return SYSERR;
+               if(handlerType.getValue() == 3)
+                       return ((Server) 
protocolCore.getApplication()).isCSHandlerRegistered(type.getValue()) ? YES : 
NO;
                return SYSERR;
        }
        
@@ -188,8 +195,8 @@
                
        }
        
-       public CInt sendValueToClient(CClientHandle handle, CInt value) {
-               return SYSERR;
+       public CInt sendValueToClient(CSSession handle, CInt value) {
+               return handle.send(new CSResult(value.getValue())) ? OK : 
SYSERR;
        }
        
        public CInt sendToClient(CSSession handle, byte[] data) {
@@ -197,11 +204,13 @@
        }
        
        public CInt registerClientHandler(CInt type, CPluginLoader.Handle 
callback) {
+               CPluginLoader._.registerDecoder(type.getValue());
                return ((Server) 
protocolCore.getApplication()).registerCSHandler(type.getValue(), 
CSMessage.class, new NativeCSHandler(callback)) ? CoreAPI.OK : CoreAPI.SYSERR;
        }
        
-       public CInt unregisterClientHandler(CUnsignedShort type, CCSHandler 
callback) {
-               return SYSERR;
+       public CInt unregisterClientHandler(CInt type, CPluginLoader.Handle 
callback) {
+               CPluginLoader._.unregisterDecoder(type.getValue());
+               return ((Server) 
protocolCore.getApplication()).unregisterCSHandler(type.getValue(), new 
NativeCSHandler(callback)) ? CoreAPI.OK : CoreAPI.SYSERR;
        }
        
        public CInt registerClientExitHandler(CClientExitHandler callback) {

Modified: freeway/src/org/gnu/freeway/server/CoreService.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CoreService.java 2006-07-26 09:13:36 UTC 
(rev 3140)
+++ freeway/src/org/gnu/freeway/server/CoreService.java 2006-07-27 08:02:54 UTC 
(rev 3141)
@@ -100,6 +100,7 @@
        {
                super.init();
                prefs=getManager().app().getPreferences();
+               connection = (ConnectionService) 
getManager().service(ConnectionService.class);
        }
 
        public void start()
@@ -442,16 +443,11 @@
                PersistentDecoder       dec;
 
                dec=new PersistentDecoder();
-               
//dec.add(CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED,CSGetCSMessageSupported.class);
-               //dec.add(CSMessage.IS_RESULT,CSResult.class);
-               
//dec.add(CSMessage.CS_PROTO_stats_STATISTICS,CSStatistics.class);
-               /*dec.add(CSMessage.IS_TRAFFIC_INFO,CSTrafficInfo.class);
-               dec.add(CSMessage.IS_TRAFFIC_QUERY,CSTrafficRequest.class);*/
-               
//dec.add(CSMessage.CS_PROTO_stats_GET_CS_MESSAGE_SUPPORTED,CSStatisticsRequest.class);
-               
//dec.add(CSMessage.CS_PROTO_stats_GET_P2P_MESSAGE_SUPPORTED,CSGetP2PMessageSupported.class);
-               /*dec.add(CSMessage.IS_SHUTDOWN,CSShutdownRequest.class);
-               dec.add(CSMessage.IS_CLIENT_COUNT,CSGetClientCount.class);
-               dec.add(CSMessage.IS_GET_HOST_INFO,CSGetHostInfo.class);*/
+               dec.add(CSMessage.IS_RESULT,CSResult.class);
+               dec.add(CSMessage.IS_TRAFFIC_INFO,CSTrafficInfo.class);
+               dec.add(CSMessage.IS_TRAFFIC_QUERY,CSTrafficRequest.class);
+               dec.add(CSMessage.IS_SHUTDOWN,CSShutdownRequest.class);
+               dec.add(CSMessage.IS_GET_HOST_INFO,CSGetHostInfo.class);
 
                dec.merge(createCSDecoderForProtocols());
                return dec;

Modified: freeway/src/org/gnu/freeway/services/c/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/c/StatsService.java    2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/services/c/StatsService.java    2006-07-27 
08:02:54 UTC (rev 3141)
@@ -48,7 +48,7 @@
        }
 
        public StatsService() {
-               this(new CPluginLoader());
+               this(CPluginLoader._);
        }
 
        public StatsService(CPluginLoader loader) {

Modified: freeway/src/org/gnu/freeway/services/java/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/java/StatsService.java 2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/services/java/StatsService.java 2006-07-27 
08:02:54 UTC (rev 3141)
@@ -71,7 +71,7 @@
        }
        
        public CPluginLoader.Handle getHandle() {
-               return CPluginLoader.getFacade(this);
+               return CPluginLoader._.getFacade(this);
        }
        
        public static final class Entry {

Modified: freeway/src/org/gnu/freeway/util/ServiceManager.java
===================================================================
--- freeway/src/org/gnu/freeway/util/ServiceManager.java        2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/util/ServiceManager.java        2006-07-27 
08:02:54 UTC (rev 3141)
@@ -17,12 +17,8 @@
 {
        public static final String      EOL     =       
System.getProperty("line.separator");
 
-       public static ServiceManager    manager;
+       public static ServiceManager    manager=null;
 
-       static {
-               manager=null;
-               }
-
        private Map             services;
        private Set             temporary;
        private boolean started;

Modified: freeway/src/org/gnu/freeway/util/net/CSMessage.java
===================================================================
--- freeway/src/org/gnu/freeway/util/net/CSMessage.java 2006-07-26 09:13:36 UTC 
(rev 3140)
+++ freeway/src/org/gnu/freeway/util/net/CSMessage.java 2006-07-27 08:02:54 UTC 
(rev 3141)
@@ -19,7 +19,7 @@
        public static final int IS_RESULT                               =       
0;
 
        /** core: client to gnunetd: to how many nodes are we connected ? */
-       public static final int IS_CLIENT_COUNT                 =       32;
+       public static final int CS_PROTO_traffic_COUNT                  =       
32;
 
        /** core: client to gnunetd: how much traffic do we have at the moment? 
*/
        public static final int IS_TRAFFIC_QUERY                        =       
33;
@@ -216,7 +216,7 @@
                Arrays.fill(names,null);
 
                names[IS_RESULT]="IS_RESULT";
-               names[IS_CLIENT_COUNT]="IS_CLIENT_COUNT";
+               names[CS_PROTO_traffic_COUNT]="CS_PROTO_traffic_COUNT";
                names[IS_TRAFFIC_QUERY]="IS_TRAFFIC_QUERY";
                names[IS_TRAFFIC_INFO]="IS_TRAFFIC_INFO";
                
names[CS_PROTO_stats_GET_STATISTICS]="CS_PROTO_stats_GET_STATISTICS";

Modified: freeway/src/org/gnu/freeway/util/net/NativeCSHandler.java
===================================================================
--- freeway/src/org/gnu/freeway/util/net/NativeCSHandler.java   2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/util/net/NativeCSHandler.java   2006-07-27 
08:02:54 UTC (rev 3141)
@@ -28,10 +28,10 @@
  */
 public class NativeCSHandler implements CSHandler {
        
-       private CPluginLoader.Handle handle;
+       public CPluginLoader.Handle ptr;
        
        public NativeCSHandler(CPluginLoader.Handle handle) {
-               this.handle = handle;
+               ptr = handle;
        }
 
        /* (non-Javadoc)
@@ -39,7 +39,7 @@
         */
        public boolean handle(CSSession session, CSMessage message) {
                assert(message instanceof CSNativeMessage);
-               return CPluginLoader.callCSHandle(handle, session, 
(CSNativeMessage) message);
+               return CPluginLoader._.callCSHandle(ptr, session, 
(CSNativeMessage) message);
        }
 
 }

Modified: freeway/src/org/gnu/freeway/util/net/PersistentDecoder.java
===================================================================
--- freeway/src/org/gnu/freeway/util/net/PersistentDecoder.java 2006-07-26 
09:13:36 UTC (rev 3140)
+++ freeway/src/org/gnu/freeway/util/net/PersistentDecoder.java 2006-07-27 
08:02:54 UTC (rev 3141)
@@ -180,8 +180,8 @@
                id=origID=buf.getShort(pos+2) & 0x0000ffff;
 
                p=null;
-               try { // why do we create a class to store this number??
-                       //id=((id<classes.length && classes[id]!=null) ? id : 
defaultID);
+               try {
+                       id=((id<classes.length && classes[id]!=null) ? id : 
defaultID);
                        if (id>=0) {
                                p=decode(buf,pos,size,id);
                                if (p==null && corruptedID>=0) {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]