CS 696 Emerging Technologies: Distributed Objects |
---|
package whitney.rmi.examples.basic; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.io.IOException; import java.rmi.server.*; import java.net.InetAddress; import sdsu.util.ProgramProperties; import sdsu.rmi.server.*; public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException { } public String sayHello() { return "Hello World from " + getHostName(); } protected static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (java.net.UnknownHostException who) { return "Unknown"; } }
private static String getHelloHostAddress( String args[] ) throws IOException { ProgramProperties flags = new ProgramProperties( args ); String port = flags.getString( "port", "1099" ); String host = flags.getString( "host", getHostName()); 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(); } } }
package whitney.rmi.examples.basic; public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
package whitney.rmi.examples.basic; import java.rmi.*; import java.net.MalformedURLException; import java.io.IOException; import java.rmi.server.*; import sdsu.util.ProgramProperties; import sdsu.rmi.server.*; 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( "h" ); String port = flags.getString( "p", "1099" ); return "rmi://" + host + ":" + port + "/HelloServer"; } }
package whitney.rmi.examples.basic; import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.UnicastRemoteObject; import java.io.IOException; import java.rmi.server.*; import java.net.InetAddress; import sdsu.util.ProgramProperties; public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException { } public String sayHello() { return "Hello World from " + getHostName(); } protected static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); } catch (java.net.UnknownHostException who) { return "Unknown"; } }
public static void main(String args[]) { // Create and install a security manager System.setSecurityManager(new RMISecurityManager()); try { ProgramProperties flags = new ProgramProperties( args ); int port = flags.getInt( "p", 1099 ); String serverLabel = "HelloServer"; HelloServer serverObject = new HelloServer(); Registry localRegistry = LocateRegistry.createRegistry(port); localRegistry.rebind( serverLabel, serverObject); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloServer err: "); e.printStackTrace(); } } }
class Roger implements Serializable { private int lowBid; private float averageBid; private int highBid; public Roger(int lowBid, int highBid ) { this.lowBid = lowBid; this.highBid = highBid; averageBid = (lowBid + highBid)/2; } private void readObject( ObjectInputStream in) throws IOException { lowBid = in.readInt(); averageBid = in.readFloat(); highBid = in.readInt(); } private void writeObject( ObjectOutputStream out) throws IOException { out.writeInt( lowBid ); out.writeFloat( averageBid ); out.writeInt( highBid ); } public String toString() { return " " + lowBid + " " + averageBid; } }
class Roger implements Externalizable { private int lowBid; private float averageBid; private int highBid; public Roger() {}; //Required public Roger(int lowBid, int highBid ) { this.lowBid = lowBid; this.highBid = highBid; averageBid = (lowBid + highBid)/2; } public void readExternal( ObjectInput in) throws IOException { lowBid = in.readInt(); averageBid = in.readFloat(); highBid = in.readInt(); } public void writeExternal( ObjectOutput out) throws IOException { out.writeInt( lowBid ); out.writeFloat( averageBid ); out.writeInt( highBid ); } public String toString() { return " " + lowBid + " " + averageBid; } }
import java.io.*; public class Test { public static void main( String args[] ) throws Exception { serialize(); deserialize(); System.out.println( "Done" ); } public static void serialize() throws Exception { OutputStream outputFile = new FileOutputStream( "Serialized" ); ObjectOutputStream cout = new ObjectOutputStream( outputFile ); cout.writeObject( new Roger( 1, 5)); cout.close(); } public static void deserialize() throws Exception { InputStream inputFile = new FileInputStream( "Serialized" ); ObjectInputStream cin = new ObjectInputStream( inputFile ); System.out.println( cin.readObject() ); } }