CS 696 Emerging Technologies: Distributed Objects |
---|
public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
// Required for Remote Implementation import java.rmi.*; import java.rmi.server.UnicastRemoteObject; // Used in method getUnixHostName import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException { } // The actual remote sayHello public String sayHello() throws RemoteException { return "Hello World from " + getUnixHostName(); }
protected String getUnixHostName() { try { Process hostName; BufferedReader answer; hostName = Runtime.getRuntime().exec( "hostname" ); answer = new BufferedReader( new InputStreamReader( hostName.getInputStream()) ); hostName.waitFor(); return answer.readLine().trim(); } catch (Exception noName) { return "Nameless"; } }
public static void main(String args[]) { // Create and install a security manager System.setSecurityManager(new RMISecurityManager()); try { HelloServer serverObject = new HelloServer (); Naming.rebind("//roswell.sdsu.edu/HelloServer", serverObject ); System.out.println("HelloServer bound in registry"); } catch (Exception error) { System.out.println("HelloServer err: "); error.printStackTrace(); } } }
import java.rmi.*; import java.net.MalformedURLException; public class HelloClient { public static void main(String args[]) { try { Hello remote = (Hello) Naming.lookup( "//roswell.sdsu.edu/HelloServer"); String message = remote.sayHello(); System.out.println( message ); } catch ( NotBoundException error) { error.printStackTrace(); } catch ( MalformedURLException error) { error.printStackTrace(); } catch ( UnknownHostException error) { error.printStackTrace(); } catch ( RemoteException error) { error.printStackTrace(); } } }Note the multiple catches are to illustrate which exceptions are thrown
Hello remote = (Hello) Naming.lookup( "//roswell.sdsu.edu/HelloServer");
Naming.rebind("//roswell.sdsu.edu/HelloServer", serverObject);
java -Dkey1=value1 -Dkey2=value2 className
String value = System.getProperty( key); String defaultValue = "Hi Mom"; value = System.getProperty( key, defaultValue);
public class Tester { public static void main( String[] args) throws IOException { String shortConfigFileName = "config.properties"; ProgramProperties options; options = new ProgramProperties( args, shortConfigFileName ); String defaultHost = "roswell.sdsu.edu"; String host = options.getString( "host", defaultHost ); int port = options.getInt( "port", 1099 ); System.out.println( "host: " + host + " port: " + port ); } }
public class SampleRepository { public static void main( String[] args ) throws IOException { // Open an existing Repository Repository files = new LocalRepository( "SimpleTest"); // Add an object to the repository // test will be saved in the file: letter.Properties Properties test = new Properties(); test.put( "hi", "mom" ); test.put( "bye", "dad" ); files.put( "letter", test ); // read file grades.Table and converts contents // to a table Table grades = (Table) files.get( "grades" ); } }
import java.rmi.*; import java.rmi.registry.Registry; import java.net.MalformedURLException; import java.io.IOException; import sdsu.util.ProgramProperties; public class HelloClient { public static void main(String args[]) { try { String server = getHelloHostAddress( args); Hello remote = (Hello) Naming.lookup( server ); String message = remote.sayHello(); System.out.println( message ); } catch ( Exception error) { error.printStackTrace(); } } private static String getHelloHostAddress( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); String host = flags.getString( "host" ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "port", defaultPort ); return "rmi://" + host + ":" + port + "/HelloServer"; } }
import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.rmi.registry.Registry; // for port number import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import sdsu.util.ProgramProperties; public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException { } public String sayHello() throws RemoteException { return "Hello World from " + getUnixHostName(); }
protected String getUnixHostName() { try { Process hostName; BufferedReader answer; hostName = Runtime.getRuntime().exec( "hostname" ); answer = new BufferedReader( new InputStreamReader( hostName.getInputStream()) ); hostName.waitFor(); return answer.readLine().trim(); } catch (Exception noName) { return "Nameless"; } }
private static String getHelloHostAddress( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); int defaultPort = Registry.REGISTRY_PORT; int port = flags.getInt( "port", defaultPort ); // can only bind to local host, protocol defaults // to local host, do not add a host here return "rmi://" + ":" + port + "/HelloServer"; } public static void main(String args[]) { // Create and install a security manager System.setSecurityManager(new RMISecurityManager()); try { String serverAddress = getHelloHostAddress( args ); HelloServer serverObject = new HelloServer(); Naming.rebind( serverAddress, serverObject); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloServer err: "); e.printStackTrace(); } } }
public HelloServer() throws RemoteException { }
/usr/bin/ps -o pid,stime,comm -uwhitney
alias pps '/usr/bin/ps -o pid,stime,comm -uwhitney'
public class HelloClient { public static void main(String args[]) { try { String server = getHelloHostAddress( args); Hello remote = (Hello) Naming.lookup( server );
// Stub class generated by rmic, do not edit. // Contents subject to change without notice. public final class HelloServer_Stub extends java.rmi.server.RemoteStub implements Hello, java.rmi.Remote { private static java.rmi.server.Operation[] operations = { new java.rmi.server.Operation("java.lang.String sayHello()") }; private static final long interfaceHash = 6486744599627128933L; // Constructors public HelloServer_Stub() { super(); } public HelloServer_Stub(java.rmi.server.RemoteRef rep) { super(rep); }
// Methods from remote interfaces // Implementation of sayHello public java.lang.String sayHello() throws java.rmi.RemoteException { int opnum = 0; java.rmi.server.RemoteRef sub = ref; java.rmi.server.RemoteCall call = sub.newCall( (java.rmi.server.RemoteObject)this, operations, opnum, interfaceHash); try { sub.invoke(call); } catch (java.rmi.RemoteException ex) { throw ex; } catch (java.lang.Exception ex) { throw new java.rmi.UnexpectedException( "Unexpected exception", ex); }; java.lang.String $result; try { java.io.ObjectInput in = call.getInputStream(); $result = (java.lang.String)in.readObject(); } catch (java.io.IOException ex) { throw new java.rmi.UnmarshalException("Error unmarshaling return", ex); } catch (java.lang.ClassNotFoundException ex) { throw new java.rmi.UnmarshalException("Return value class not found", ex); } catch (Exception ex) { throw new java.rmi.UnexpectedException("Unexpected exception", ex); } finally { sub.done(call); } return $result; } }
rmiregistry portNumber & javac HelloServer.java rmic HelloServer java HelloServer & javac BankServer.java rmic BankServer java BankServer &
Class.forName( nameHere ).newInstance();
~whitney/languages/java/whitney/rmi/examples/basic
~whitney/rmi/examples/basicI make sure that my classpath points to the directory containing the whitney directory
package whitney.rmi.examples.basic; //stuff removed public class HelloServer extends UnicastRemoteObject implements Hello { //more stuff removed
javac HelloServer.java
rmic whitney.rmi.examples.basic.HelloServerTo run HelloServer main to register object:
java whitney.rmi.examples.basic.HelloServer
rmic whitney.rmi.examples.basic.$1Make the file executable and place the file in my bin directory which is in my path
java whitney.rmi.examples.basic.$1
rmiregistry 7654 &2. Run UniVMRegistry's main
java sdsu.rmi.registry.UniVMRegistry -p=7654 &3. Use Registrar to register objects with UniVMRegistry
int port = 7654; String serverClass = "whitney.rmi.examples.basic.SingleHelloServer"; String nameList = Registrar.verboseRebind( port, "HelloServer", serverClass); System.out.println( nameList );
Registrar aRegistrar = new Registrar( "rohan", port ); aRegistrar.rebind( "HelloServer", serverClass );