From e17a84411b40af12692b714fac4b78be095e67e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=C5=ADlo=20Ebermann?= Date: Fri, 3 Jun 2011 21:05:59 +0200 Subject: [PATCH] CompressedRMISocketFactory + Co --- .../examples/CompressedRMISocketFactory.java | 54 +++++ .../paul/examples/EchoClient.java | 22 ++ .../paul/examples/EchoServer.java | 9 + .../paul/examples/TracingSocketFactory.java | 64 +++++ .../paul/examples/WrappingSocketFactory.java | 225 ++++++++++++++++++ 5 files changed, 374 insertions(+) create mode 100644 src/de/fencing_game/paul/examples/CompressedRMISocketFactory.java create mode 100644 src/de/fencing_game/paul/examples/EchoClient.java create mode 100644 src/de/fencing_game/paul/examples/EchoServer.java create mode 100644 src/de/fencing_game/paul/examples/TracingSocketFactory.java create mode 100644 src/de/fencing_game/paul/examples/WrappingSocketFactory.java diff --git a/src/de/fencing_game/paul/examples/CompressedRMISocketFactory.java b/src/de/fencing_game/paul/examples/CompressedRMISocketFactory.java new file mode 100644 index 0000000..a0e497f --- /dev/null +++ b/src/de/fencing_game/paul/examples/CompressedRMISocketFactory.java @@ -0,0 +1,54 @@ +package de.fencing_game.paul.examples; + +import java.rmi.*; +import java.rmi.registry.*; +import java.rmi.server.*; +import java.io.*; + +import de.fencing_game.tools.*; + + +public class CompressedRMISocketFactory extends WrappingSocketFactory { + + + protected StreamPair wrap(InputStream in, OutputStream out, + boolean server) { + /* + return new StreamPair(new DecompressingInputStream(in), + new CompressingOutputStream(out)); + */ + return new StreamPair(in, out); + } + + private static class EchoServerImpl + implements EchoServer { + + public String echo(String param) { + return param + " " + param; + } + } + + public static void main(String[] egal) + throws Exception + { + CompressedRMISocketFactory fac = new CompressedRMISocketFactory(); + + Remote server = + UnicastRemoteObject.exportObject(new EchoServerImpl(), + 0, fac, fac); + System.err.println("server: " + server); + + Registry registry = + LocateRegistry.createRegistry(Registry.REGISTRY_PORT); + + registry.bind("echo", server); + + // EchoServer es = (EchoServer)registry.lookup("echo"); + // System.out.println(es.echo("hallo")); + + Thread.sleep(3*60*1000); + } + + + +} \ No newline at end of file diff --git a/src/de/fencing_game/paul/examples/EchoClient.java b/src/de/fencing_game/paul/examples/EchoClient.java new file mode 100644 index 0000000..ff07854 --- /dev/null +++ b/src/de/fencing_game/paul/examples/EchoClient.java @@ -0,0 +1,22 @@ +package de.fencing_game.paul.examples; + +import java.rmi.registry.*; + +public class EchoClient { + + public static void main(String[] egal) + throws Exception + { + // TracingSocketFactory fac = new TracingSocketFactory(); + + Registry registry = + LocateRegistry.getRegistry("localhost", + Registry.REGISTRY_PORT); + + EchoServer es = (EchoServer)registry.lookup("echo"); + System.err.println("es: " + es); + System.out.println(es.echo("hallo")); + + } + +} \ No newline at end of file diff --git a/src/de/fencing_game/paul/examples/EchoServer.java b/src/de/fencing_game/paul/examples/EchoServer.java new file mode 100644 index 0000000..b72cba3 --- /dev/null +++ b/src/de/fencing_game/paul/examples/EchoServer.java @@ -0,0 +1,9 @@ +package de.fencing_game.paul.examples; + +import java.rmi.*; + +public interface EchoServer extends Remote { + + public String echo(String bla) throws RemoteException; + +} \ No newline at end of file diff --git a/src/de/fencing_game/paul/examples/TracingSocketFactory.java b/src/de/fencing_game/paul/examples/TracingSocketFactory.java new file mode 100644 index 0000000..6eb1c60 --- /dev/null +++ b/src/de/fencing_game/paul/examples/TracingSocketFactory.java @@ -0,0 +1,64 @@ +package de.fencing_game.paul.examples; + +import java.rmi.*; +import java.rmi.registry.*; +import java.rmi.server.*; +import java.io.*; + +public class TracingSocketFactory extends WrappingSocketFactory { + + protected StreamPair wrap(InputStream in, OutputStream out, boolean server) + { + InputStream wrappedIn = in; + OutputStream wrappedOut = new FilterOutputStream(out) { + public void write(int b) throws IOException { + System.err.println("write(.)"); + super.write(b); + } + public void write(byte[] b, int off, int len) + throws IOException { + System.err.println("write(" + len + ")"); + super.out.write(b, off, len); + } + public void flush() throws IOException { + System.err.println("flush()"); + super.flush(); + } + }; + return new StreamPair(wrappedIn, wrappedOut); + } + + + private static class EchoServerImpl + implements EchoServer { + + public String echo(String param) { + return param + " " + param; + } + } + + public static void main(String[] egal) + throws Exception + { + TracingSocketFactory fac = new TracingSocketFactory(); + + + + Remote server = + UnicastRemoteObject.exportObject(new EchoServerImpl(), + 0, fac, fac); + System.err.println("server: " + server); + + Registry registry = + LocateRegistry.createRegistry(Registry.REGISTRY_PORT); + + registry.bind("echo", server); + + // EchoServer es = (EchoServer)registry.lookup("echo"); + // System.out.println(es.echo("hallo")); + + Thread.sleep(3*60*1000); + } + + +} \ No newline at end of file diff --git a/src/de/fencing_game/paul/examples/WrappingSocketFactory.java b/src/de/fencing_game/paul/examples/WrappingSocketFactory.java new file mode 100644 index 0000000..2cfc700 --- /dev/null +++ b/src/de/fencing_game/paul/examples/WrappingSocketFactory.java @@ -0,0 +1,225 @@ +package de.fencing_game.paul.examples; + +import java.io.*; +import java.net.*; +import java.rmi.server.*; + + +public abstract class WrappingSocketFactory + implements RMIClientSocketFactory, RMIServerSocketFactory, Serializable +{ + + public WrappingSocketFactory(RMIClientSocketFactory cFac, + RMIServerSocketFactory sFac) { + this.baseCFactory = cFac; + this.baseSFactory = sFac; + } + + public WrappingSocketFactory(RMISocketFactory fac) { + this(fac, fac); + } + + public WrappingSocketFactory() { + this( RMISocketFactory.getSocketFactory()); + } + + public static class StreamPair { + public InputStream input; + public OutputStream output; + public StreamPair(InputStream in, OutputStream out) { + this.input = in; this.output = out; + } + } + + RMIClientSocketFactory baseCFactory; + RMIServerSocketFactory baseSFactory; + + protected abstract StreamPair wrap(InputStream input, + OutputStream output, + boolean server); + + private RMIClientSocketFactory getCSFac() { + if(baseCFactory == null) { + return RMISocketFactory.getDefaultSocketFactory(); + } + return baseCFactory; + } + + private RMIServerSocketFactory getSSFac() { + if(baseSFactory == null) { + return RMISocketFactory.getDefaultSocketFactory(); + } + return baseSFactory; + } + + + public Socket createSocket(String host, int port) + throws IOException + { + System.err.println("createSocket(" + host + ", " + port + ")"); + Socket baseSocket = getCSFac().createSocket(host, port); + StreamPair streams = this.wrap(baseSocket.getInputStream(), + baseSocket.getOutputStream(), + false); + SocketImpl wrappingImpl = new WrappingSocketImpl(streams, baseSocket); + return new Socket(wrappingImpl) { + public boolean isConnected() { return true; } + }; + } + + public ServerSocket createServerSocket(int port) + throws IOException + { + System.err.println("createServerSocket(" + port + ")"); + final ServerSocket baseSocket = getSSFac().createServerSocket(port); + port = baseSocket.getLocalPort(); + ServerSocket ss = new WrappingServerSocket(baseSocket, port); + System.err.println(" => " + ss); + return ss; + } + + private class WrappingServerSocket extends ServerSocket { + private ServerSocket base; + private int port; + + public WrappingServerSocket(ServerSocket b, int port) + throws IOException + { + this.base = b; + this.port = port; + } + + public int getLocalPort() { + return port; + } + + public Socket accept() throws IOException { + System.err.println(this+".accept()"); + final Socket baseSocket = base.accept(); + System.err.println("baseSocket: " + baseSocket); + StreamPair streams = + WrappingSocketFactory.this.wrap(baseSocket.getInputStream(), + baseSocket.getOutputStream(), + true); + SocketImpl wrappingImpl = + new WrappingSocketImpl(streams, baseSocket); + System.err.println("wrappingImpl: " + wrappingImpl); + Socket result = new Socket(wrappingImpl) { + public boolean isConnected() { return true; } + public boolean isBound() { return true; } + public int getLocalPort() { + return baseSocket.getLocalPort(); + } + public InetAddress getLocalAddress() { + return baseSocket.getLocalAddress(); + } + }; + System.err.println("result: " + result); + return result; + } + } + + + private static class WrappingSocketImpl extends SocketImpl { + private InputStream inStream; + private OutputStream outStream; + + private Socket base; + + WrappingSocketImpl(StreamPair pair, Socket base) { + System.err.println("new WrappingSocketImpl()"); + this.inStream = pair.input; + this.outStream = pair.output; + this.base = base; + } + + + protected InputStream getInputStream() { + return inStream; + } + + protected OutputStream getOutputStream() { + return outStream; + } + + protected void close() throws IOException { + base.close(); + } + + protected int available() throws IOException { + return inStream.available(); + } + + + protected void shutdownInput() throws IOException { + base.shutdownInput(); + // TODO: inStream.close() ? + } + + protected void shutdownOutput() throws IOException { + base.shutdownOutput(); + // TODO: outStream.close()? + } + + + // only logging + + protected void create(boolean stream) { + System.err.println("create(" + stream + ")"); + } + + + public Object getOption(int optID) { + System.err.println("getOption(" + optID + ")"); + return null; + } + + public void setOption(int optID, Object value) { + System.err.println("setOption(" + optID +"," + value + ")"); + // noop. + } + + // unsupported operations + + + protected void connect(String host, int port) { + System.err.println("connect(" + host + ", " + port + ")"); + throw new UnsupportedOperationException(); + } + + + protected void connect(InetAddress address, int port) { + System.err.println("connect(" + address + ", " + port + ")"); + throw new UnsupportedOperationException(); + } + + protected void connect(SocketAddress addr, int timeout) { + System.err.println("connect(" + addr + ", " + timeout + ")"); + throw new UnsupportedOperationException(); + } + + protected void bind(InetAddress host, int port) { + System.err.println("bind(" + host + ", " + port + ")"); + throw new UnsupportedOperationException(); + } + + protected void listen(int backlog) { + System.err.println("listen(" + backlog + ")"); + throw new UnsupportedOperationException(); + } + + protected void accept(SocketImpl otherSide) { + System.err.println("accept(" + otherSide + ")"); + throw new UnsupportedOperationException(); + } + + protected void sendUrgentData(int data) { + System.err.println("sendUrgentData()"); + throw new UnsupportedOperationException(); + } + + + } + + +} \ No newline at end of file