CS 696 Emerging Technologies: Distributed Objects |
---|
public class HelloServer extends UnicastRemoteObject implements Hello { }
java.lang.Object | +----java.rmi.server.RemoteObject | +----java.rmi.server.RemoteServer | +----java.rmi.server.UnicastRemoteObject
package whitney.rmi.examples.server; import java.rmi.server.ServerNotActiveException; public interface ServerMethods extends java.rmi.Remote { public ServerMethods tryMe( int callNumber ) throws java.rmi.RemoteException, ServerNotActiveException; }
package whitney.rmi.examples.server; import java.rmi.*; import java.rmi.registry.Registry; import java.net.MalformedURLException; import java.io.IOException; import sdsu.util.ProgramProperties; public class ServerMethodsClient { public static void main(String args[]) { try { String serverLabel = ServerMethodsServer.RMI_NAME; String server = getServerURL( args, serverLabel ); ServerMethods first = (ServerMethods) Naming.lookup( server ); ServerMethods second = (ServerMethods) Naming.lookup( server ); ServerMethods third = first.tryMe( 1 ); first.tryMe( 2 ); second.tryMe( 3 ); if ( first == second ) System.out.println( "1 = 2" ); else System.out.println( "1 != 2" ); if ( first == third ) System.out.println( "1 = 3" ); else System.out.println( "1 != 3" ); System.out.println( "1 " + first.hashCode() ); System.out.println( "2 " + second.hashCode() ); System.out.println( "3 " + third.hashCode() ); } catch ( Exception error) { error.printStackTrace(); } }
private static String getServerURL( String args[], String serverLabel ) throws IOException { String hostKey = "h"; String portKey = "p"; ProgramProperties flags = new ProgramProperties( args ); String host = null; if ( flags.containsKey( hostKey ) ) host = flags.getString( hostKey ); else { System.out.println( "Missing flag " + hostKey ); System.exit( 0 ); } int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( portKey, defaultPort ); return "rmi://" + host + ":" + port + "/" + serverLabel; } }
package whitney.rmi.examples.server; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.rmi.server.ServerNotActiveException; import java.rmi.registry.Registry; import sdsu.rmi.registry.Registrar; import sdsu.util.ProgramProperties; import java.io.*; public class ServerMethodsServer extends UnicastRemoteObject implements ServerMethods { public ServerMethodsServer() throws RemoteException, IOException { FileOutputStream log = new FileOutputStream( "MyLog"); setLog ( new BufferedOutputStream( log ) ); } public ServerMethods tryMe( int callNumber ) throws RemoteException, ServerNotActiveException { System.out.println( "Call " + callNumber + " from: " + getClientHost() ); PrintStream logStream = getLog(); if (logStream == null ) System.out.println( "Null logger" ); else logStream.println( "Call " + callNumber + " from: " + getClientHost() ); setLog( null ); // turns logging off logStream = getLog(); if (logStream == null ) { System.out.println( "Null logger" ); setLog( System.out ); } else System.out.println( "You can still log"); return this; }
public static String RMI_NAME = "cs696/ServerMethods"; public static void main(String args[]) throws RemoteException, IOException { int port = getPort( args ); ServerMethodsServer serverObject = new ServerMethodsServer(); Naming.rebind("//:" + port + "/" + RMI_NAME, serverObject ); System.out.println("ServerMethodsServer bound in registry"); } private static int getPort( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "p", defaultPort ); return port; } }
java.io.ObjectOutput out = call.getResultStream(true); out.writeObject($result);
java.io.ObjectOutputStream log = new java.io.ObjectOutputStream( new java.io.FileOutputStream("return.log", true)); log.writeObject($result);
java.io.ObjectOutput out = call.getOutputStream(); out.writeObject($_arrayOf__Object_1);
java.io.ObjectOutputStream log = new java.io.ObjectOutputStream(new java.io.FileOutputStream("call.log", true)); log.writeObject($_arrayOf__Object_1);
import sdsu.logging.*; public class Tester { public static void main( String[] args) throws LoggerCreationException { FileLogger myLog = FileLogger.register( "LogFile"); myLog.debugOff(); myLog.warningOff(); // Some where else in the program Logger.debug( "I am lost, This does not work"); Logger.log( "Routine log stuff" ); } }
package whitney.rmi.examples.server; import java.rmi.*; public interface Echo extends Remote { public void print( String message ) throws RemoteException; }
package whitney.rmi.examples.server; import java.rmi.RemoteException; public class EchoImpl implements Echo { private static int NEXT_ID = 1; private int id = NEXT_ID++; private String name; public EchoImpl( String name) { this.name = name; } public void print( String message ) throws RemoteException { System.out.println( "From name: " + name + " " + id + " : " + message); } //These are needed to replace UnicastRemoteObject parent public int hashCode() { return id; } public boolean equals( Object anEcho ) { if ( !(anEcho instanceof EchoImpl) ) return false; if ( id == ((EchoImpl) anEcho).id ) return true; else return false; } public String toString() { return "Echo(" + name + "," + id + ")"; } }
package whitney.rmi.examples.server; import java.rmi.RemoteException; public interface EchoExample extends java.rmi.Remote { public Echo getNewEcho( ) throws RemoteException; public void setRemoteEcho( Echo fromClient ) throws RemoteException; public Echo getRemoteEcho() throws RemoteException; }
package whitney.rmi.examples.server; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.rmi.registry.Registry; import sdsu.util.ProgramProperties; import java.io.*; public class EchoServer extends UnicastRemoteObject implements EchoExample { Echo remoteEcho; public EchoServer() throws RemoteException { } public Echo getNewEcho( ) throws RemoteException { Echo registerFirst = new EchoImpl( "ServerSam"); registerFirst.print( "In Server.getNewEcho"); UnicastRemoteObject.exportObject( registerFirst); return registerFirst; } public void setRemoteEcho( Echo fromClient ) throws RemoteException { remoteEcho = fromClient; fromClient.print( "In Server.setRemoteEcho"); } public Echo getRemoteEcho() throws RemoteException { remoteEcho.print( "In Server.getRemoteEcho"); return remoteEcho; } public static String RMI_NAME = "cs696/EchoServer"; public static void main(String args[]) throws RemoteException, IOException { int port = getPort( args ); EchoServer serverObject = new EchoServer(); Naming.rebind("//:" + port + "/" + RMI_NAME, serverObject ); System.out.println("EchoServer bound in registry"); } private static int getPort( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "p", defaultPort ); return port; } }
package whitney.rmi.examples.server; import java.rmi.*; import java.rmi.registry.Registry; import java.rmi.server.UnicastRemoteObject; import java.net.MalformedURLException; import java.io.IOException; import sdsu.util.ProgramProperties; import sdsu.logging.Debug; public class EchoClient { public static void main(String args[]) { try { String serverLabel = EchoServer.RMI_NAME; String server = getServerURL( args, serverLabel ); EchoExample echoServer = (EchoExample) Naming.lookup( server ); Debug.println("First getNewEcho"); Echo fromServer = echoServer.getNewEcho(); fromServer.print( "In Client"); Echo clientEcho = new EchoImpl( "ClientClem"); clientEcho.print( "In Client"); UnicastRemoteObject.exportObject( clientEcho); Debug.println( "First setRemoteEcho"); echoServer.setRemoteEcho( clientEcho ); Debug.println( "First getRemoteEcho"); fromServer = echoServer.getRemoteEcho(); fromServer.print( "In Client"); Debug.println( "Done"); System.exit( 0 ); } catch ( Exception error) { error.printStackTrace(); } }
private static String getServerURL( String args[], String serverLabel ) throws IOException { String hostKey = "h"; String portKey = "p"; ProgramProperties flags = new ProgramProperties( args ); String host = null; if ( flags.containsKey( hostKey ) ) host = flags.getString( hostKey ); else { System.out.println( "Missing flag " + hostKey ); System.exit( 0 ); } int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( portKey, defaultPort ); return "rmi://" + host + ":" + port + "/" + serverLabel; } }
From name: ServerSam 1 : In Server.getNewEcho From name: ServerSam 1 : In Client
Debug server.EchoClient.main(Compiled Code): First getNewEcho From name: ClientClem 1 : In Client Debug server.EchoClient.main(Compiled Code): First setRemoteEcho From name: ClientClem 1 : In Server.setRemoteEcho Debug server.EchoClient.main(Compiled Code): First getRemoteEcho From name: ClientClem 1 : In Server.getRemoteEcho From name: ClientClem 1 : In Client Debug server.EchoClient.main(Compiled Code): Done
java.rmi.server.Unreferenced interface