Skip to content

Commit

Permalink
CompressedRMISocketFactory + Co
Browse files Browse the repository at this point in the history
  • Loading branch information
ePaul committed Jun 3, 2011
1 parent b94bf05 commit e17a844
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 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);
}



}
22 changes: 22 additions & 0 deletions 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"));

}

}
9 changes: 9 additions & 0 deletions 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;

}
64 changes: 64 additions & 0 deletions 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);
}


}
225 changes: 225 additions & 0 deletions 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();
}


}


}

0 comments on commit e17a844

Please sign in to comment.