package wrapper; import java.util.*; import java.io.*; import java.rmi.*; import java.rmi.server.*; /**General class for interfacing to a megamodule. Subclasses implement the various protocols for CPAM. This class offers a test-routine to test wrapped megamodules locally without using a distribution system to access them. Methods for accepting messages over CPAM and returning messages must be added by subclasses. */ public class CPAMInterface { // extends UnicastRemoteObject { /**Reference to the megamodule instance belonging to this interface instance. An instance of CPAMMEgamodule is automatically created when CPAMInterface is instanciated. The reference m is needed for accessing its methods when handling CPAM messages.*/ protected CPAMMegamodule m; /**Indicates which kind of parameters have to be expected; valid values are so far "String" and "Gentype" */ protected String paramType; /**Never use this constructor.*/ public CPAMInterface() throws RemoteException{ System.err.println("Fatal error: do not use this constructor"); System.exit(0); } /**Instantiates a megamodule inclusive its interface to a distribution protocol. Megamodule is the name of the wrapped computational code and is a subclass of CPAMMegamodule. Paramtype is so far either String or Gentype and indicates the type of parameters used by the interface class (and of course also by the wrapped computational code).

This constructor starts up a megamodule instance. It can be expanded by a subclass of CPAMInterface to also register the megamodule in a registry for remote access.*/ public CPAMInterface(String paramType, String megamodule) throws RemoteException{ try { Class[] constructClassArray = {Class.forName("java.lang.String")}; Object[] constructObjectArray = {paramType}; m = (CPAMMegamodule) Class.forName(megamodule).getConstructor (constructClassArray).newInstance(constructObjectArray); } catch (ClassNotFoundException e) {System.out.println("Could not find the class file for " + megamodule); System.exit(0);} catch (ClassCastException e) {System.out.println("Class " + megamodule + " is not a subclass of CPAMMegamodule."); System.exit(0);} catch (Exception e) {System.out.println(e.getMessage()); e.printStackTrace(); System.exit(0);} this.paramType = paramType; } /**Interactive test method. Allows to test a megamodule instance interactively even without any CPAM interface. In case of wrong inputs or other errors, exceptions will be thrown and printed out, yet interaction can continue. For a description of the commands and the possible parameters, see the description of CPAM and the appropriate CHAIMS repository.*/ public void test() { //works only with String parameters if (!paramType.equals("String")) {System.out.println ("Direct interactive testing of megamodules works only for String parameters."); return; } String c = ".", status = "", s=""; Integer id; int i; Invocation inv; Connection con; String name, value, methodname; Estimates e; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); System.out.println("The following is the class of the Megamodule: " + m.getClass().getName()); System.out.println("Possible commands are: setup, estimate, getparam, setparam," + " invoke, extract, examine, terminate, terminateall"); do { try { System.out.print("Enter command: "); c=input.readLine(); if (c.equals("setup")) System.out.println("Your connection number is: " + m.createConnection().toString()); else if (c.equals("estimate")) { System.out.print("Enter number of connection: "); id= new Integer(input.readLine()); System.out.print("Enter name of method: "); name=input.readLine(); e = new Estimates(); System.out.print("Which estimates? 1 for time, 2 for fee, 4 for datavolume, add together for more than one: "); char ch=input.readLine().charAt(0); if (ch=='1' | ch=='3' | ch=='5' | ch=='7') e.timeRequired = true; if (ch=='2' | ch=='3' | ch=='6' | ch=='7') e.feeRequired = true; if (ch=='4' | ch=='5' | ch=='6' | ch=='7') e.datavolumeRequired = true; if (! m.getConnection(id).getEstimates(name, e)) System.out.println("Sorry, this megamodule does not offer pre-invocation estimates"); else { if (e.timeRequired) { if (e.timeProvided) System.out.println(" Time for executing method: "+e.timeValue + " with an accuracy of " + e.timeAccuracy); else System.out.println(" Time: no estimate provided"); } if (e.feeRequired) { if (e.feeProvided) System.out.println(" Fee for using method: "+e.feeValue + " with an accuracy of " + e.feeAccuracy); else System.out.println(" Fee: no estimate provided"); } if (e.datavolumeRequired) { if (e.datavolumeProvided) System.out.println(" Volume of data: "+e.datavolumeValue + " with an accuracy of " + e.datavolumeAccuracy); else System.out.println(" Datavolume: no estimate provided"); } } } else if (c.equals("setparam")) { System.out.print("Enter number of connection: "); id= new Integer(input.readLine()); con = m.getConnection(id); do { System.out.print("Enter name of parameter or '.' if no more entries: "); name=input.readLine(); if (! name.equals(".")) { System.out.print("Enter value of parameter: "); value=input.readLine(); con.updateParam (name, value); System.out.println("If no exception has been thrown, the setting the parameter was successful."); } } while (!name.equals(".")); } else if (c.equals("getparam")) { System.out.print("Enter number of connection: "); id= new Integer(input.readLine()); con = m.getConnection(id); do { System.out.print("Enter name of parameter or '.' if no more entries: "); name=input.readLine(); if (!name.equals(".")) { value = ""; value = (String) con.getParam (name); System.out.println("Value is: " + value); } } while (!name.equals(".")); } else if (c.equals("invoke")) { System.out.print("Enter number of connection: "); id= new Integer(input.readLine()); System.out.print("Enter name of method: "); methodname = input.readLine(); inv = m.getConnection(id).createInvocation(methodname); do { System.out.print("Enter name of parameter or '.' if no more entries: "); name=input.readLine(); if (! name.equals(".")) { System.out.print("Enter value of parameter: "); value=input.readLine(); inv.initParam (name, value); } } while (!name.equals(".")); inv.startExecution(); System.out.println("Invocation has started, its id is: " + inv.getInvocationId().toString()); } else if (c.equals("extract")) { System.out.print("Sorry, this primitive has not yet been implemented"); } else if (c.equals("examine")) { System.out.print("Enter number of invocation: "); id= new Integer(input.readLine()); inv = m.getInvocation(id); System.out.print("Enter '.' (if you want status of invocation) or enter name of parameter (if you want status of a result): "); name= input.readLine(); if (name.equals(".") ) { InvocationStatus invs = inv.status(); System.out.println("Status is: " + invs.status + ", progress is: " + invs.progress); } else { ResultStatus ress = inv.getParamStatus (name); System.out.println("Status is: " + ress.status + ", progress is: " + ress.accuracy); } } else if (c.equals("terminate")) { System.out.print("Enter number of invocation to terminate: "); id= new Integer(input.readLine()); m.getInvocation(id).terminate(); System.out.println("Invocation " + id.toString() + " terminated"); } else if (c.equals("terminateall")) { System.out.print("Sorry, you cannot terminate a connection as this primitive has not yet been implemented. Abort the megamodule to terminate all connections to the megamodule."); } else System.out.println ("Unknown primitive."); } catch (Exception ex) { System.out.println("Exception caught in test interface: " + ex.getMessage()); ex.printStackTrace(); } System.out.println("--"); } while (!c.equals(".")); } }