package wrapper; import java.util.*; final class Connection { private ParamValueTable clientParams; //the client specific settings of the parameters /**Status of the client parameters. Contains all the parameter names as the table clientParams, and maps them to integers. Possible values are:
* 1: default setting of megamodule
* 2: client-specific setting (after a SETPARAM)*/ private Map clientParamsStatus; /**List of all the invocations of this connection. It maps invocationids to invocation objects.*/ private Map invocations; /**Pointer to the parent megamodule.*/ protected CPAMMegamodule parent; /**Id of this connection. This id is unique and cannot be changed after the creation of the connection. The connection id is created by the wrapper, not by the client accessing the megamodule. If the client itself also creates connection or client ids, these have to be mapped into the connection id of the wrapper in the subclass of CPAMInterface. Yet our approach is cleaner, it allows one client to have several connections to the same megamodule (e.g. for web browsers), and it avoids coincidentally equal client ids from different clients.*/ protected final Integer connectionId; /**Random number generator for generating the invocation ids.*/ private Random random = new Random(); Connection(ParamValueTable defaultParams, CPAMMegamodule megamodule, Integer connectionId) { parent = megamodule; clientParams = new ParamValueTable(parent.paramType); clientParams.putAll(defaultParams); clientParamsStatus = Collections.synchronizedMap(new HashMap()); Set keys = clientParams.keySet(); Iterator i = keys.iterator(); while (i.hasNext()) clientParamsStatus.put((String)i.next(), new Integer(1)); invocations = Collections.synchronizedMap(new HashMap()); this.connectionId = connectionId; } ParamValueTableReader getClientParams() {return clientParams;} Invocation getInvocation(Integer invocationId) { if (! invocations.containsKey(invocationId)) throw new InvalidInvocationIdException("Invocation id " + invocationId.toString() + " unknown"); return (Invocation) invocations.get(invocationId); } void updateParam(String name, Object value){ clientParams.set (name, value); clientParamsStatus.put (name, new Integer(2)); } Object getParam(String name){ return clientParams.get (name); //tests also for existence of param } Invocation createInvocation(String methodname) { if (invocations.size() >= CPAMMegamodule.noInv) throw new OutOfResourcesException("Maximum number of invocation pro connection reached."); Integer newInvocationId; int newId = Math.abs(random.nextInt() % CPAMMegamodule.noInv); do { newId = (++newId) % CPAMMegamodule.noInv; newInvocationId=new Integer(newId*CPAMMegamodule.noCon+connectionId.intValue()); } while (invocations.containsKey(newInvocationId)); Invocation inv = new Invocation(clientParams, methodname, this, newInvocationId); invocations.put(newInvocationId, inv); return inv; } void invocationTerminated(Integer invocationId) { invocations.remove(invocationId); } boolean getEstimates(String methodname, Estimates estimates) { return parent.makeEstimate(estimates, methodname, clientParams); } void terminate() { Object[] invArray = invocations.values().toArray(); for (int i=0; i