package wrapper; import java.rmi.*; import java.rmi.server.*; import java.util.*; import cpam_rmi.*; import convert.*; public class RMI_CPAM_V2_0 extends CPAMInterface implements ChaimsCompliantObject { HashMap clientIds = new HashMap(); //This HashMap is needed because // this wrapper provides itself the connection Ids, yet this CPAM protocol // also gives a client Id, so we have to map these Ids here. // Keys: Strings from client, Values: Integers from Megamodule private int paramtype; // 1 for String, 2 for Gentype public RMI_CPAM_V2_0(String paramType, String megamodulename) throws RemoteException { super(paramType, megamodulename); if (paramType.equals("String")) paramtype=1; else if (paramType.equals("Gentype")) paramtype=2; else throw new RuntimeException("Cannot handle parameters of type "+paramType); } //********************** start up server ********************** public static void main(String[] args) { // do I really need one? System.setSecurityManager(new RMISecurityManager()); String registryname; try { if (args.length < 2) { System.out.println("Choose the parameter type for CPAM (String or Gentype) " + "and provide class name of megamodule (must be a subclass of CPAMMegamodule). "+ "Optionally choose a registry name for the megamodule as third argument."); System.exit(0); } RMI_CPAM_V2_0 server = new RMI_CPAM_V2_0(args[0],args[1]); System.out.println ("Megamodule instance for " + args[1] + " created"); if (args.length >=3) registryname=args[2]; else registryname=args[1]; Naming.rebind(registryname, server); System.out.println ("Megamodule instance registered under the name " + registryname); server.test(); } catch (Exception e) {System.out.println("I1: "+e.getMessage()); e.printStackTrace(); System.exit(0);} } //********************** testRMI********************** public String testRMI() { System.err.println("testRMI is not implemented"); return "This is a string!"; } //********************** SETUP ********************** public void SETUP(String clientID) throws RemoteException { // IN clientID: ID of the calling megaprogram try{ Integer conid = m.createConnection(); clientIds.put(clientID, conid); System.out.println("New connection with number "+conid.toString()+" created"); } catch (Exception e) {System.err.println("I2: "+e.getMessage());e.printStackTrace();} } //********************** INVOKE ********************* public long INVOKE(String clientID, String methodName, ChaimsAttrValContainer attrValContainer) throws RemoteException{ // IN clientID : ID of the calling megaprogram // IN methodName: name of the method to be invoked. Valid method names are given by // the repository. // IN cont: list of name-value pairs that are the invocation-specific parameters for // this invocation. Valid names are given by the repository. // OUT long: callID by which this invocation can be referenced later on until it // gets terminated try{ ChaimsAttrValObject attrval; Object param; Invocation inv = m.getConnection((Integer)clientIds.get(clientID)).createInvocation(methodName); int num = attrValContainer.getNumAttrValObject(); for (int i=0; i>seq<< and the values are // the corresponding computed ones or being // -1 for fee, -1 for time, and -1 for volume, if no estimate is possible. ChaimsAttrValContainer returnresults = new ChaimsAttrValContainer(); try{ Integer id= (Integer)clientIds.get(clientID); Estimates est = new Estimates(); //automatically sets defaultvalues est.timeRequired = seq.isIncluded("time"); est.feeRequired = seq.isIncluded("fee"); est.datavolumeRequired = seq.isIncluded("datavolume"); m.getConnection(id).getEstimates(methodName, est); if (paramtype==1) { if (est.timeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "time", Integer.toString(est.timeValue).getBytes())); if (est.feeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "fee", Integer.toString(est.feeValue).getBytes())); if (est.datavolumeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "datavolume", Integer.toString(est.datavolumeValue).getBytes())); } else if (paramtype == 2) { //we have to convert to gentype-blobs if (est.timeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "time", SimpleConvert.intToBlob("Time for "+methodName, est.timeValue))); if (est.feeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "fee", SimpleConvert.intToBlob("Fee for "+methodName, est.feeValue))); if (est.datavolumeRequired) returnresults.addAttrValObject(new ChaimsAttrValObject( "datavolume", SimpleConvert.intToBlob("Datavolume for "+methodName, est.datavolumeValue))); } } catch (Exception e) {System.err.println("I5: "+e.getMessage());e.printStackTrace();} return returnresults; } //********************** EXTRACT ******************** public ChaimsAttrValContainer EXTRACT(long callID, ChaimsAttrSequence seq ) throws RemoteException{ // IN callID: reference of the invocation from which we want the results // IN seq: list of names. The names tell the megamodule which results are requested. // OUT ChaimsAttrValContainer: list of (name, value) pairs, where the names correspond to those in >>seq<< and the values are // the corresponding result values // If a value is not yet available, an empty value is returned ChaimsAttrValContainer cont = new ChaimsAttrValContainer(); Object param; byte[] blob; try{ Invocation inv = m.getInvocation(new Integer((int)callID)); int num = seq.getNumAttr(); for (int i=0; i>seq<< and the values contain // the corresponding current // settings for the calling megaprogram. If no client-specific settings // have been made yet, default values (for the megamodule itself) are returned. ChaimsAttrValContainer cont = new ChaimsAttrValContainer(); Object param; byte[] blob; try{ Connection con = m.getConnection((Integer)clientIds.get(clientID)); int num = seq.getNumAttr(); for (int i=0; i