// Purpose. Proxy design pattern // 1. Create a "wrapper" for a remote, or expensive, or sensitive target // 2. Encapsulate the complexity/overhead of the target in the wrapper // 3. The client deals with the wrapper // 4. The wrapper delegates to the target // 5. To support plug-compatibility of wrapper and target, create an interface import java.io.*; import java.net.*; import java.lang.String; import java.lang.*; interface SocketInterface { // 5. To support plug-compatibility String readLine(); // between the wrapper and the void writeLine( String str ); // target, create an interface void dispose(); } public class ButtonServer { public static void main( String[] args ) { BufferedReader nPut = new BufferedReader(new InputStreamReader(System.in)); SocketInterface socket = new SocketProxy( "soc4.acpub.duke.edu",8189,true ); IntenseComputation ic = new IntenseComputation(); String str = null; String processedLine = null; // boolean skip = true; while (true) { str = socket.readLine(); System.out.println( "Receive - " + str ); if (str.equals("quit")) break; processedLine = ic.compute(str); socket.writeLine(processedLine); } socket.dispose(); } } class IntenseComputation { public IntenseComputation(){} public String compute ( String str) { //StringTokenizer tokens = new StringTokenizer(args[0], " ,"); String[] tokens = str.split(","); String rt = null; if (tokens[3].equals("left")) { rt = str; // rt = concat("label - ").concat(tokens[0]).concat(" x - ").concat(tokens[1]).concat(" y - ").concat(tokens[2]); } else if (tokens[3].equals("right")) { // right click, needs to compute info int label = returnint(tokens[0]); int x = returnint(tokens[1]); int y = returnint(tokens[2]); int z = label+x+y; Integer iz = new Integer(z); // rt = rt(iz.toString()).concat(","); rt = iz.toString(); // rt = rt.concat("0"); } return rt; // add x + y coordinates and return string concatenated with final value // String value = Integer.toString( (int)(Math.random()*tokens[0])); } public int returnint (String s) { Integer x = new Integer(444); x = x.valueOf(s); int m = x.intValue(); return m; } } /*public class ProxyDemo { * public static void main( String[] args ) { * BufferedReader nPut = new BufferedReader(new InputStreamReader(System.in)); * // 152.2.131.180 is rockfish * //SocketInterface socket = new SocketProxy( "152.2.131.180", 8189, * // 152.2.130.147 is laptop * SocketInterface socket = new SocketProxy( "152.2.130.147", 8189, * args[0].equals("first") ? true : false ); // 3. The client deals * String str = null; // with the wrapper * boolean skip = true; * while (true) { * if (args[0].equals("second") && skip) skip = ! skip; * else { * str = socket.readLine(); * System.out.println( "Receive - " + str ); // java ProxyDemo first * if (str.equals("quit")) break; // Receive - 123 456 * } // Send ---- 234 567 * System.out.print( "Send ---- " ); // Receive - 345 678 * try { * str = nPut.readLine(); * } catch (IOException e) { e.printStackTrace(); } * socket.writeLine( str ); // java ProxyDemo second * if (str.equals("quit")) break; // Send ---- 123 456 * } // Receive - 234 567 * socket.dispose(); // Send ---- 345 678 * } } */ class SocketProxy implements SocketInterface { // 1. Create a "wrapper" for a private Socket socket; // remote, or expensive, or private BufferedReader in; // sensitive target private PrintWriter out; public SocketProxy( String host, int port, boolean wait ) { try { if (wait) { ServerSocket server = new ServerSocket( port ); socket = server.accept(); // 2. Encapsulate the complexi- } else // ty/overhead of the target socket = new Socket( host, port ); // in the wrapper in = new BufferedReader( new InputStreamReader( socket.getInputStream())); out = new PrintWriter( socket.getOutputStream(), true ); } catch( IOException e ) { e.printStackTrace(); } } public String readLine() { String str = null; try { str = in.readLine(); } catch( IOException e ) { e.printStackTrace(); } return str; } public void writeLine( String str ) { out.println( str ); // 4. The wrapper delegates to the target } public void dispose() { try { socket.close(); } catch( IOException e ) { e.printStackTrace(); } } }