CS 696 Emerging Technologies: Distributed Objects |
---|
public class XorInputStream extends FilterInputStream { byte codeMask = 0; public XorInputStream(InputStream in, byte mask) { super(in); codeMask = mask; } public int read() throws IOException { int input = in.read(); if ( input < 0 ) return input; else return (byte) input ^ codeMask; } public int read(byte inputBuffer[], int offset, int length) throws IOException { byte[] codedInput = new byte[length]; int bytesRead = in.read( codedInput ); for (int k = 0; k < bytesRead;k++) inputBuffer[offset + k ] = (byte) (codedInput[k] ^ codeMask); return bytesRead; } }
public class XorOutputStream extends FilterOutputStream { byte codeMask = 0; public XorOutputStream(OutputStream out, byte mask) { super(out); codeMask = mask; } public void write(int output) throws IOException { if ( output < 0 ) //not a byte, { out.write(output); } else out.write( ((byte) output) ^ codeMask ); } public void write(byte output[], int offset, int length) throws IOException { byte[] coded = new byte[length]; for (int i = 0 ; i < length ; i++) { coded[i] = (byte) ( output[offset + i] ^ (byte) codeMask ); } out.write( coded); } }
public class XorSocket extends java.net.Socket { /* Streams used by socket */ private InputStream in; private OutputStream out; private byte xorMask = 0; public XorSocket( byte mask) // This is needed!! throws IOException { super(); xorMask = mask; } public XorSocket(String host, int port, byte mask) throws IOException { super(host, port); xorMask = mask; }
public InputStream getInputStream() throws IOException { if (in == null) { BufferedInputStream buffered = new BufferedInputStream( super.getInputStream() ); in = new XorInputStream( buffered , xorMask); } return in; } public OutputStream getOutputStream() throws IOException { if (out == null) { BufferedOutputStream buffered = new BufferedOutputStream( super.getOutputStream() ); out = new XorOutputStream(buffered, xorMask); } return out; } public synchronized void close() throws IOException { OutputStream out = getOutputStream(); out.flush(); super.close(); } }
public class XorServerSocket extends ServerSocket { private byte xorMask = 0; public XorServerSocket(int port, byte mask) throws IOException { super(port); xorMask = mask; } public XorServerSocket(int port, int backlog, byte mask) throws IOException { super(port, backlog); xorMask = mask; } public XorServerSocket(int port, int backlog, InetAddress bindAddress, byte mask) throws IOException { super(port, backlog, bindAddress); xorMask = mask; } public Socket accept() throws IOException { Socket socket = new XorSocket( xorMask ); implAccept(socket); return socket; } }
public class XorRMISocketFactory extends RMISocketFactory { private byte xorMask = 0; public XorRMISocketFactory( byte mask ) { xorMask = mask; } public Socket createSocket(String host, int port) throws IOException { XorSocket socket = new XorSocket(host, port, xorMask); return socket; } public ServerSocket createServerSocket(int port) throws IOException { XorServerSocket server = new XorServerSocket(port, xorMask); return server; } }
public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
public class HelloClient { public static void main(String args[]) { try { String server = getHelloHostAddress( args); Hello remote = (Hello) Naming.lookup( server ); RMISocketFactory.setSocketFactory( new XorRMISocketFactory( (byte) 0x1A ) ); 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" ); String port = flags.getString( "port", "1099" ); return "rmi://" + host + ":" + port + "/HelloServer"; } }
public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException {} public String sayHello() throws RemoteException { return "Hello World from someWhere" ;} 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); RMISocketFactory.setSocketFactory( new XorRMISocketFactory( (byte) 0x1A ) ); System.out.println("HelloServer bound in registry"); } catch (Exception e) { System.out.println("HelloServer err: " + e); } } }
package whitney.rmi.examples.basic; import java.io.*; import java.net.*; import java.rmi.*; import java.rmi.server.*; import sdsu.net.*; public class MultiRMISocketFactory extends RMISocketFactory { /* Get the default RMISocketFactory */ private RMISocketFactory defaultFactory = RMISocketFactory.getDefaultSocketFactory(); /* * Override createSocket to call the default * RMISocketFactory's createSocket method. This * way, you'll get a TCP connection if you don't * specify another SocketType */ public Socket createSocket(String host, int port) throws IOException { return defaultFactory.createSocket(host, port); }
/* * Override createServerSocket to call the default * RMISocketFactory's createServerSocket method. */ public ServerSocket createServerSocket(int port) throws IOException { return defaultFactory.createServerSocket(port); } /* * Override createSocket to create the type of socket * specified by the SocketType parameter. */ public Socket createSocket(String host, int port, SocketType type) throws IOException { Socket socket; String protocol = type.getProtocol(); if(protocol.equals("xor")) { byte[] xorMask = type.getRefData(); socket = new XorSocket(host, port, xorMask[0]); } else throw new IOException("protocol " + protocol + " not supported"); return socket; }
/* * Override createServerSocket to create the type of socket * specified by the SocketType parameter. */ public ServerSocket createServerSocket(int port, SocketType type) throws IOException { ServerSocket server; String protocol = type.getProtocol(); if(protocol.equals("xor")) { Byte xorMask = (Byte) type.getServerData(); server = new XorServerSocket(port, xorMask.byteValue()); } else throw new IOException("protocol " + protocol + " not supported"); return server; } }
package whitney.rmi.examples.basic; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.rmi.server.*; import sdsu.util.ProgramProperties; import sdsu.rmi.server.*; public class HelloServer extends UnicastRemoteObject implements Hello { public HelloServer() throws RemoteException { super(0, getSocketType() ); } private static SocketType getSocketType() { String protocol = "xor"; byte[] forClientSide = { (byte)0xAA }; Byte forServerSide = new Byte( (byte)0xAA ); return new SocketType( protocol, forClientSide, forServerSide); } public String sayHello() throws RemoteException { return "Hello World from " + getUnixHostName(); } protected static String getUnixHostName() { try { Process hostName = Runtime.getRuntime().exec( "hostname" ); BufferedReader 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 ); String port = flags.getString( "port", "1099" ); String host = flags.getString( "host", getUnixHostName()); 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 ); RMISocketFactory.setSocketFactory( new MultiRMISocketFactory( ) ); 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; 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 { // Set before contacting Registry!! RMISocketFactory.setSocketFactory( new MultiRMISocketFactory( ) ); 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" ); String port = flags.getString( "port", "1099" ); return "rmi://" + host + ":" + port + "/HelloServer"; } }