[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r3080 - in freeway/src/org/gnu/freeway: . cwrappers/util se
From: |
mdonoughe |
Subject: |
[GNUnet-SVN] r3080 - in freeway/src/org/gnu/freeway: . cwrappers/util server services services/c util |
Date: |
Fri, 30 Jun 2006 20:01:56 -0700 (PDT) |
Author: mdonoughe
Date: 2006-06-30 20:01:46 -0700 (Fri, 30 Jun 2006)
New Revision: 3080
Added:
freeway/src/org/gnu/freeway/services/c/
freeway/src/org/gnu/freeway/services/c/StatsService.java
Removed:
freeway/src/org/gnu/freeway/services/c/StatsService.java
freeway/src/org/gnu/freeway/services/impl/
Modified:
freeway/src/org/gnu/freeway/GNUNetDaemon.java
freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
freeway/src/org/gnu/freeway/server/CPluginLoader.java
freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java
freeway/src/org/gnu/freeway/server/CoreAPI.java
freeway/src/org/gnu/freeway/services/StatsService.java
freeway/src/org/gnu/freeway/util/NativeService.java
freeway/src/org/gnu/freeway/util/ServiceManager.java
Log:
Moved services.impl to services.c to make room for services.java
Made the use of org.gnu.freeway.services more transparent. Request the
service by interface name and the ServiceManager figures it out. All
service loading should use this.
Added new preferences. Create a section called [FREEWAY-SERVICES] and
add lines like 'STATS = "java, c"' to control the order ServiceManager
uses to find an implementation for an interface. "java, c" is the
default.
Modified: freeway/src/org/gnu/freeway/GNUNetDaemon.java
===================================================================
--- freeway/src/org/gnu/freeway/GNUNetDaemon.java 2006-06-30 17:32:12 UTC
(rev 3079)
+++ freeway/src/org/gnu/freeway/GNUNetDaemon.java 2006-07-01 03:01:46 UTC
(rev 3080)
@@ -8,7 +8,7 @@
import org.gnu.freeway.util.*;
import org.gnu.freeway.util.io.*;
import org.gnu.freeway.util.net.*;
-import org.gnu.freeway.services.impl.StatsService;
+import org.gnu.freeway.services.StatsService;
import java.util.logging.*;
Modified: freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
===================================================================
--- freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
2006-06-30 17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/cwrappers/util/SwitchTableGenerator.java
2006-07-01 03:01:46 UTC (rev 3080)
@@ -46,7 +46,7 @@
/**
* @file SwitchTableGenerator.java
* @brief This class should generate the switch-table.c, switch-table.h and
- * service.impl.* classes by reflecting over the various service
interfaces
+ * service.c.* classes by reflecting over the various service interfaces
* and generating the appropriate code
* @author Christian Grothoff
* @author mdonoughe
@@ -125,9 +125,9 @@
File sourcesDir = new File(srcPrefix + servicesSubpath);
File nativeCDir = new File(natPrefix.toString());
- File implDir = new File(sourcesDir, "impl");
+ File implDir = new File(sourcesDir, "c");
- //create impl folder
+ //create services.c folder
if(!implDir.exists())
if(!implDir.mkdirs()) {
try {
@@ -584,22 +584,22 @@
methodList.put(methods[i].getName(), buffer.toString());
}
- //put these here since they are only used once
- String nativeServiceString =
cleanClassName("org.gnu.freeway.util.NativeService", imports);
- String serviceString =
cleanClassName("org.gnu.freeway.util.Service", imports);
-
writer.write("// This class was autogenerated by
SwitchTableGenerator\n");
- writer.write("package org.gnu.freeway.services.impl;\n");
+ writer.write("package org.gnu.freeway.services.c;\n");
writer.write("\n");
Collection importSet = imports.values();
for(Iterator i = importSet.iterator(); i.hasNext(); )
writer.write("import " + (String) i.next() + ";\n");
writer.write("\n");
- writer.write("public class " + stripPackage(c.getName()) + "
extends " + nativeServiceString + " implements " + serviceString + ", " +
c.getName() + " {\n");
+ writer.write("public class " + stripPackage(c.getName()) + "
extends " +cleanClassName("org.gnu.freeway.util.AbstractNativeService",
imports) + " implements " +
cleanClassName("org.gnu.freeway.util.NativeService", imports) + ", " +
c.getName() + " {\n");
writer.write("\n");
int ic = 0;
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(" }\n");
+ writer.write("\n");
writer.write(" public " + stripPackage(c.getName()) + "(" +
cleanClassName("org.gnu.freeway.server.CPluginLoader", imports) + " loader)
{\n");
writer.write(" super(loader);\n");
writer.write(" }\n");
Modified: freeway/src/org/gnu/freeway/server/CPluginLoader.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CPluginLoader.java 2006-06-30
17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/server/CPluginLoader.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -59,38 +59,6 @@
public CPluginLoader() {
super(true);
}
-
- /**
- * This is the actual public CPluginLoaderAPI. The basic
- * idea is that a CPluginLoader is given the name of an
- * interface (say "StatsService") and it will create an
- * instance of that service (which is implemented by
- * loading the respective C plugin).
- *
- * @param serviceName
- * @return null on error
- */
- public NativeService createService(String serviceName) {
- if(!serviceName.endsWith("Service"))
- serviceName = serviceName.substring(0, 1).toUpperCase()
+ serviceName.substring(1) + "Service";
- try {
- NativeService ret = (NativeService)
Class.forName("org.gnu.freeway.services.impl." +
serviceName).getConstructor(new Class[] { CPluginLoader.class}).newInstance(new
Object[] { this });
- ret.init();
- return ret;
- } catch (ClassNotFoundException cnfe) {
- return null;
- } catch (IllegalAccessException iae) {
- return null;
- } catch (NoSuchMethodException nsme) {
- return null;
- } catch (InvocationTargetException ite) {
- return null;
- } catch (InstantiationException ie) {
- return null;
- } catch (Error e) {
- return null;
- }
- }
/**
* This method is used by the generated "services.impl"
Modified: freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java 2006-06-30
17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/server/CPluginLoaderTest.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -2,7 +2,7 @@
import junit.framework.TestCase;
import junit.textui.TestRunner;
-import org.gnu.freeway.services.StatsService;
+import org.gnu.freeway.services.c.StatsService;
import org.gnu.freeway.cwrappers.ConstCString;
import org.gnu.freeway.cwrappers.CInt;
import org.gnu.freeway.cwrappers.ConstCLong;
@@ -20,7 +20,8 @@
public void testStatService() {
CPluginLoader cpl = new CPluginLoader();
- StatsService statsService = (StatsService)
cpl.createService("StatsService");
+ StatsService statsService = new StatsService(cpl);
+ statsService.init();
CInt statHandle = statsService.create(new ConstCString("# of
test stats"));
statsService.set(statHandle, new ConstCLong(0));
statsService.change(statHandle, new ConstCInt(1));
Modified: freeway/src/org/gnu/freeway/server/CoreAPI.java
===================================================================
--- freeway/src/org/gnu/freeway/server/CoreAPI.java 2006-06-30 17:32:12 UTC
(rev 3079)
+++ freeway/src/org/gnu/freeway/server/CoreAPI.java 2006-07-01 03:01:46 UTC
(rev 3080)
@@ -24,6 +24,7 @@
import java.util.Iterator;
import org.gnu.freeway.Server;
import org.gnu.freeway.util.NativeService;
+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.CSSession;
@@ -80,107 +81,22 @@
//TODO: write this and put it where it belongs
}
- public class ServiceDesc {
- String dsoName;
- CPluginLoader.Handle library;
- int applicationInitialized;
- int serviceCount;
- CPluginLoader.Handle servicePtr;
-
- public ServiceDesc(String dsoName, CPluginLoader.Handle
library, int applicationInitialized, int serviceCount, CPluginLoader.Handle
servicePtr) {
- this.dsoName = dsoName;
- this.library = library;
- this.applicationInitialized = applicationInitialized;
- this.serviceCount = serviceCount;
- this.servicePtr = servicePtr;
+ public CPluginLoader.Handle requestService(CString iname) {
+ Class c = null;
+ try {
+ 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);
}
-
- public String getDsoName() {
- return dsoName;
+ if(!NativeService.class.isAssignableFrom(c)) {
+ System.err.println(c.getCanonicalName() + " is not a
NativeService");
+ return new CPluginLoader.Handle(0);
}
-
- public void setServiceCount(int count) {
- serviceCount = count;
- }
-
- public int getServiceCount() {
- return serviceCount;
- }
-
- public CPluginLoader.Handle getServicePtr() {
- return servicePtr;
- }
-
- public int incrementServiceCount() {
- return serviceCount++;
- }
-
- public void setServicePtr(CPluginLoader.Handle ptr) {
- servicePtr = ptr;
- }
-
- public CPluginLoader.Handle getLibrary() {
- return library;
- }
+ NativeService s = (NativeService)
ServiceManager.manager.service(c);
+ return s.getHandle();
}
- public CPluginLoader.Handle requestService(CString iname) {
- //CPluginLoader loader = new CPluginLoader();
- //String rpos = iname.getValue();
- //String pos = getConfigurationString("MODULES", rpos);
- //if(pos == null)
- // pos = rpos;
-
- //String name = "module_" + pos;
-
- //handling modules in C space because we do all loading there
anyway
- /*ServiceDesc nxt = null;
- for(Iterator i = shutdownList.iterator(); i.hasNext(); nxt =
(ServiceDesc) i.next()) {
- if(name.equals(nxt.getDsoName())) {
- if(nxt.getServiceCount() > 0) {
- if(nxt.getServicePtr() != null)
- nxt.incrementServiceCount();
- return nxt.getServicePtr();
- } else {
- /*CPluginLoader.Handle mptr =
CPluginLoader.bindDynamicMethod(nxt.getLibrary(), "provide_", name);
- if(mptr == null) {
- return null;
- }
- nxt.setServicePtr(mptr);
//mptr(&applicationCore)*/
-
/*nxt.setServicePtr(loader.loadService(name));
- if(nxt.getServicePtr() != null)
- nxt.incrementServiceCount();
- return nxt.getServicePtr();
- }
- }
- }*/
-
- //CPluginLoader.Handle library =
CPluginLoader.loadDynamicLibrary(DSO_PREFIX, name);
-
- //if(library == null)
- // return null;
-
- /*CPluginLoader.Handle mptr =
CPluginLoader.bindDynamicMethod(library, "provide_", name);
- if(mptr == null) {
- CPluginLoader.unloadDynamicLibrary(library);
- return null;
- }*/
- //nxt = new ServiceDesc(name, library, NO.getValue(), 1, null);
- //shutdownList.add(nxt);
- //CPluginLoader.Handle api = mptr; //mptr(&applicationCore)
- //CPluginLoader.Handle api = loader.loadService(name);
- /*if(api != null) {
- nxt.setServicePtr(api);
- } else {
- nxt.setServiceCount(0);
- }*/
- //return api;
- NativeService ret = new
CPluginLoader().createService(iname.getValue());
- if(ret == null)
- return new CPluginLoader.Handle(0);
- return ret.handle;
- }
-
public CInt releaseService(CPluginLoader.Handle service) {
return SYSERR;
}
Modified: freeway/src/org/gnu/freeway/services/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/StatsService.java 2006-06-30
17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/services/StatsService.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -18,11 +18,7 @@
*/
package org.gnu.freeway.services;
-import org.gnu.freeway.cwrappers.ConstCString;
-import org.gnu.freeway.cwrappers.ConstCInt;
-import org.gnu.freeway.cwrappers.ConstCLong;
-import org.gnu.freeway.cwrappers.CLong;
-import org.gnu.freeway.cwrappers.CInt;
+import org.gnu.freeway.util.NativeService;
import org.gnu.freeway.cwrappers.*;
/**
@@ -30,7 +26,7 @@
* @brief
* @author mdonoughe
*/
-public interface StatsService {
+public interface StatsService extends NativeService {
public CInt create(ConstCString name);
public void set(ConstCInt handle, ConstCLong value);
public CLong get(ConstCInt handle); //cannot return a constant type
because those can't be deserialized
Copied: freeway/src/org/gnu/freeway/services/c (from rev 3019,
freeway/src/org/gnu/freeway/services/impl)
Deleted: freeway/src/org/gnu/freeway/services/c/StatsService.java
===================================================================
--- freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-17
23:58:02 UTC (rev 3019)
+++ freeway/src/org/gnu/freeway/services/c/StatsService.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -1,34 +0,0 @@
-// This class was autogenerated by SwitchTableGenerator
-package org.gnu.freeway.services.impl;
-
-import org.gnu.freeway.util.Service;
-import org.gnu.freeway.util.NativeService;
-import org.gnu.freeway.cwrappers.CInt;
-import org.gnu.freeway.cwrappers.ConstCLong;
-import org.gnu.freeway.cwrappers.CLong;
-import org.gnu.freeway.cwrappers.ConstCInt;
-import org.gnu.freeway.cwrappers.ConstCString;
-import org.gnu.freeway.server.CPluginLoader;
-
-public class StatsService extends NativeService implements Service,
org.gnu.freeway.services.StatsService {
-
- public CInt create(ConstCString arg0) {
- return (CInt) loader.callC(handle, 0, 13, new Object[] {arg0});
- }
-
- public void set(ConstCInt arg0, ConstCLong arg1) {
- loader.callC(handle, 1, 60, new Object[] {arg0, arg1});
- }
-
- public CLong get(ConstCInt arg0) {
- return (CLong) loader.callC(handle, 2, 10, new Object[] {arg0});
- }
-
- public void change(ConstCInt arg0, ConstCInt arg1) {
- loader.callC(handle, 3, 42, new Object[] {arg0, arg1});
- }
-
- public StatsService() {
- super();
- }
-}
Copied: freeway/src/org/gnu/freeway/services/c/StatsService.java (from rev
3079, freeway/src/org/gnu/freeway/services/impl/StatsService.java)
===================================================================
--- freeway/src/org/gnu/freeway/services/impl/StatsService.java 2006-06-30
17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/services/c/StatsService.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -0,0 +1,57 @@
+// This class was autogenerated by SwitchTableGenerator
+package org.gnu.freeway.services.c;
+
+import org.gnu.freeway.util.AbstractNativeService;
+import org.gnu.freeway.util.NativeService;
+import org.gnu.freeway.util.ServiceException;
+import org.gnu.freeway.cwrappers.CInt;
+import org.gnu.freeway.cwrappers.ConstCLong;
+import org.gnu.freeway.cwrappers.CLong;
+import org.gnu.freeway.cwrappers.ConstCInt;
+import org.gnu.freeway.cwrappers.CString;
+import org.gnu.freeway.cwrappers.ConstCString;
+import org.gnu.freeway.server.CPluginLoader;
+
+public class StatsService extends AbstractNativeService implements
NativeService, org.gnu.freeway.services.StatsService {
+
+ public CInt create(ConstCString arg0) {
+ return (CInt) loader.callC(handle, 0, 13, new Object[] {arg0});
+ }
+
+ public void set(ConstCInt arg0, ConstCLong arg1) {
+ loader.callC(handle, 1, 60, new Object[] {arg0, arg1});
+ }
+
+ public CLong get(ConstCInt arg0) {
+ return (CLong) loader.callC(handle, 2, 10, new Object[] {arg0});
+ }
+
+ public void change(ConstCInt arg0, ConstCInt arg1) {
+ loader.callC(handle, 3, 42, new Object[] {arg0, arg1});
+ }
+
+ public void init() throws ServiceException {
+ super.init();
+ System.err.println("Loaded stats service.");
+ CInt c = create(new CString("test"));
+ System.err.println("created stat \"test\":" + c.getValue());
+ set(c, new CLong(12345));
+ c = create(new CString("test2"));
+ System.err.println("created stat \"test2\":" + c.getValue());
+ set(c, new CLong(54321));
+ c = create(new CString("-1"));
+ System.err.println("created stat \"-1\":" + c.getValue());
+ set(c, new CLong(-1));
+ c = create(new CString("-2"));
+ System.err.println("created stat \"-2\":" + c.getValue());
+ set(c, new CLong(-2));
+ }
+
+ public StatsService() {
+ this(new CPluginLoader());
+ }
+
+ public StatsService(CPluginLoader loader) {
+ super(loader);
+ }
+}
Modified: freeway/src/org/gnu/freeway/util/NativeService.java
===================================================================
--- freeway/src/org/gnu/freeway/util/NativeService.java 2006-06-30 17:32:12 UTC
(rev 3079)
+++ freeway/src/org/gnu/freeway/util/NativeService.java 2006-07-01 03:01:46 UTC
(rev 3080)
@@ -26,75 +26,6 @@
* @brief an abstract class to make org.gnu.freeway.services types fit
* @author mdonoughe
*/
-public abstract class NativeService extends AbstractService implements Service
{
-
- protected CPluginLoader loader;
- public CPluginLoader.Handle handle;
-
- protected NativeService(CPluginLoader loader) {
- super();
- setDebug(true);
- this.loader = loader;
- }
-
- protected void finalize() throws Throwable {
- super.finalize();
- done();
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#getName()
- */
- public String toString() {
- String ret = this.getClass().getSimpleName();
- if(ret.endsWith("Service")) {
- ret = ret.substring(0, ret.length() - 7);
- }
- return ret;
- }
-
- /* (non-Javadoc)
- * @see
org.gnu.freeway.util.Service#attach(org.gnu.freeway.util.ServiceManager)
- */
- public void attach(ServiceManager s) {
- super.attach(s);
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#detach()
- */
- public void detach() {
- super.detach();
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#init()
- */
- public void init() throws ServiceException {
- super.init();
- handle =
loader.loadService(CPluginLoader.classToLibraryName(getName()));
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#start()
- */
- public void start() throws ServiceException {
- super.start();
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#stop()
- */
- public void stop() throws ServiceException {
- super.stop();
- }
-
- /* (non-Javadoc)
- * @see org.gnu.freeway.util.Service#done()
- */
- public void done() throws ServiceException {
- super.done();
- loader.unloadService(handle);
- }
-
+public interface NativeService extends Service {
+ public CPluginLoader.Handle getHandle();
}
Modified: freeway/src/org/gnu/freeway/util/ServiceManager.java
===================================================================
--- freeway/src/org/gnu/freeway/util/ServiceManager.java 2006-06-30
17:32:12 UTC (rev 3079)
+++ freeway/src/org/gnu/freeway/util/ServiceManager.java 2006-07-01
03:01:46 UTC (rev 3080)
@@ -17,7 +17,7 @@
{
public static final String EOL =
System.getProperty("line.separator");
- private static ServiceManager manager;
+ public static ServiceManager manager;
static {
manager=null;
@@ -56,15 +56,53 @@
public Service service( Class c )
{
Service s;
+
+ s = (Service) services.get(c);
+ if(s!=null)
+ return s;
+
+ // find an implementation for a org.gnu.freeway.services
interface
+ if(c.isInterface() &&
c.getCanonicalName().startsWith("org.gnu.freeway.services")) {
+ Class t = null;
+ String[] prefixes;
+ String type;
+ String className;
+
+ type = c.getSimpleName();
+ type = type.substring(0, type.length() -
7).toUpperCase(); // -"Service"
+ prefixes =
app.getPreferences().getString("FREEWAY-SERVICES", type, "java, c").split(",");
+
+ for(int i = 0; i < prefixes.length; i++) {
+ type = prefixes[i].trim();
+ className = c.getPackage().getName() + "." +
type.toLowerCase() + "." + c.getSimpleName();
+
+ System.err.println((i == 0 ? "Loading " :
"Falling back on ") + type + " service: " + className);
+ try {
+ t = Class.forName(className);
+ } catch(ClassNotFoundException e) {
+ System.err.println(className + " was
not found");
+ continue;
+ }
+ if(!c.isAssignableFrom(t)) {
+ System.err.println(className + " does
not implement its interface");
+ continue;
+ }
+ s = service(t);
+ if(s == null) {
+ continue;
+ }
+ services.put(c, s);
+ System.err.println("Loaded the " + type + "
implementation for " + c.getSimpleName());
+ return s;
+ }
+ System.err.println("No suitable implementations for " +
c.getSimpleName() + " could be found");
+ return null;
+ }
//todo: pister les dependances + les ordres d'instanciation
- s=(Service) services.get(c);
- if (s!=null) {
- return s;
- }
-
if (temporary.contains(c)) {
+ System.err.println("Circular loop (requested:
"+c.getName()+") !");
trace("Circular loop (requested: "+c.getName()+") !");
return null;
}
@@ -73,6 +111,8 @@
s=(Service) c.newInstance();
}
catch( Throwable x ) {
+ x.printStackTrace();
+ System.err.println("Unable to instantiate service of
class "+c.getName()+".");
err("Unable to instantiate service of class
"+c.getName()+".",x);
return null;
}
@@ -95,6 +135,7 @@
}
}
catch( ServiceException x ) {
+ System.err.println("Failed to init/start service
'"+s.getName()+"' !");
err("Failed to init/start service '"+s.getName()+"'
!",x);
}
return s;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r3080 - in freeway/src/org/gnu/freeway: . cwrappers/util server services services/c util,
mdonoughe <=