import java.io.*; import java.util.*; import java.rmi.*; import java.rmi.registry.*; /** Creates a Portal and provides a (very simple) command-line interface to Services offered by it. */ public class Main { static final String myService = System.getProperty ("myservice"); public static void main(String args[]) throws Exception { // You would need this if you wanted to download code... // if (System.getSecurityManager() == null) { // System.setSecurityManager(new RMISecurityManager()); // } final int port = Integer.getInteger("port",5678).intValue(); final Portal p = new Portal(port, System.getProperty("wkp","rmi://ramen.cs.umd.edu:1099/Portal")); p.addService ("Duplicator", new NullService () { public String transformString (String input) { return input + input; } }); p.addService ("OneA", new NullService () { public String transformString (String input) { return "A"; } }); try { if (myService != null) p.addService (myService, (Service) Class.forName(myService).newInstance()); } catch (ClassNotFoundException e) { System.out.println ("No service loaded.\n"); } final Thread t = new Thread() { public void run() { for(int i = 0; i < 500; i++) { try { Thread.sleep(5000); Message m = new BroadcastTextMessage(p.portalAddress, " At port " + port + " on " + java.net.InetAddress.getLocalHost() + " it is " + new Date().toString()); p.forward(m); } catch (InterruptedException e) { } catch (java.net.UnknownHostException e) { } } }}; t.start(); BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); String data = "initial data string (to be transformed by services)"; while (true) { System.out.print (data + ">"); String command = input.readLine(); if (command == null || command.equals("quit")) break; if (command.equals("")) continue; Service svc = p.getService (command); if (svc == null) { System.out.println ("Command not found. Requesting..."); p.forward (new FindServiceMessage (p.getAddress(), command)); } else { try { data = svc.transformString (data); } catch (RemoteException e) { System.out.println (e); } } } System.out.println("Shutting down"); p.shutdown(); System.exit(0); } }