// FooServer.java
// Demonstrates RMI
// The client invokes doit() which runs on the server
// The client can send us a pipe object, which we

import java.rmi.*;
import java.rmi.server.*;


public class FooServer extends UnicastRemoteObject
		implements FooRemote
{
  PipeRemote pipe;
	
    public FooServer() throws RemoteException {
    	super();
    	pipe = null;
    }

    public String[] doit() throws RemoteException {
		Log.print("FooServer: doit start");
		
		serverInternal();

		if (pipe != null) pipe.send("Server says hello");

		// We'll try this later -- send executable content
		// back to the client
		// if (pipe != null) pipe.sendRunnable(new MyRunnable());

		// Make a little array to return
		// (arrays and strings automatically serializable)
		String[] result = new String[2];
		result[0] = "hello";
		result[1] = " there";
		
		Log.print("FooServer: doit done");
		return(result);
    }
    
    private void serverInternal() {
    	Log.print("FooServer: serverInternal");
    }
    
    
  // Sent by the client to give us a pipe
  public void install(PipeRemote pipe) throws RemoteException {
    this.pipe = pipe;
  }
  
  
	public static void main(String[] args) {
		if (args.length != 1) {
			System.out.println("Need port number");
			System.exit(0);
		}
		
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}
		String name = "//localhost:" + args[0] + 
			"/" + FooRemote.SERVICE;
		try {
			FooRemote impl = new FooServer();
			Naming.rebind(name, impl);
			Log.print("FooServer: server bound");
		} catch (Exception e) {
			System.err.println("FooServer exception: " + e.getMessage());
			e.printStackTrace();
		}
	}
}


/*

SERVER SIDE

rmiregistry 32456 &      ## started earlier and left running


elaine21:~/java/rmi2> javac *.java
elaine21:~/java/rmi2> rmic FooServer PipeServer ## do this when *Remote has changed
elaine21:~/java/rmi2> alias rjava     ## see the alias for rjava
java -Djava.security.policy=rtable.policy
elaine21:~/java/rmi2> rjava FooServer 32456
2000-05-16 11:46:55.789 FooServer: server bound
2000-05-16 11:47:14.475 FooServer: doit start
2000-05-16 11:47:14.48 FooServer: serverInternal
2000-05-16 11:47:14.481 FooServer: doit done
2000-05-16 11:47:15.0 FooServer: doit start
2000-05-16 11:47:15.001 FooServer: serverInternal
2000-05-16 11:47:15.09 FooServer: doit done
^C

CLIENT SIDE

elaine20:~/java/rmi2> rjava FooClient elaine21:32456
2000-05-16 11:47:14.493 Client: received from server -- hello there
2000-05-16 11:47:15.012 PipeServer: got message Server says hello
2000-05-16 11:47:15.072 PipeServer: got runnable
This code sent from the server 2
2000-05-16 11:47:15.082 Client: done
^C
*/



