package whackamole; import java.util.*; import java.io.*; import java.lang.reflect.*; /** Represent a call on an object with arguments o.methCall(args) * This object can be serialized, sent across a network, and when * deserialized, it can be invoked remotely. * The object at the other end is located via a {@link Map} interface * based on the object's id on the sender's side. */ public class RMI implements Serializable { final static boolean debug = true; final private int id; final private String type; final private String methodCall; final private List args; /** Don't save the {@link Object} because it will be different * on the receiver's side. Instead, save o's id (gotten via * System.identityHashCode(o), and o's type (gotten via * o.getClass().getName(). */ public RMI(Object o, String methodCall, List args) { // FILL IN } /** Constructor taking an array of arguments instead of a {@link List} */ public RMI(Object o, String methodCall, Object[] args) { this(o, methodCall, arrayToList(args)); } public String toString() { return "call " + id + "<"+type+">." + methodCall + args; } /** Use the {@link RMI#invoke(Map, Creator)} method, but pass it * an inner class that implements {@link Creator} by just instantiating * the object reflectively. */ public Object invoke(Map m) { return null; // FIX } /** Find the object in the supplied {@link Map} and invoke the * method on it, returning the answer. */ public Object invoke(Map m, Creator c) { return null; // FIX } /** Helper method to find the method for obj and the * method name and parameters supplied when this RMI * was instantiated. */ protected Method getMethod(Object obj) throws Exception { return null; // FIX } private static List arrayToList(Object[] args) { List ans = new LinkedList(); for (int i=0; i < args.length; ++i) ans.add(args[i]); return ans; } public static void main(String[] a) { HashMap map = new HashMap(); MutInt num = new MutInt(); RMI r1 = new RMI(num, "add", new Object[] { new MutInt(202) }); System.out.println(r1.invoke(map)); List args = new ArrayList(); args.add(new MutInt(101)); RMI env = new RMI(num, "add", args); System.out.println(env.invoke(map)); } }