[xSocket-develop] Trying to implement a Dual Port Server with XSocket
Status: Inactive
Brought to you by:
grro
|
From: Phyo A. <phy...@gm...> - 2009-10-31 01:49:20
|
Hello XSocket Developers I am new to XSocket and trying to implement a Dual Port Server , One Port accept incoming connection , read data and when other side connect , reply to them here is my example RemoteWebServer<---Agent ---------------------------->XSocketServer:8090 <----------------> XSocketServer:9080 <<<<--------->Browser I am wondering how to communicate data Internally between XSocketServer:8090 <---------> XSocketServer:9080; I am thinking to use shared synchronized object : Only Xsocket is used on server side relay is with socat ( dest-unreachable.com) Below is my code as far as it goes it dont work :D. will appreciate a lot for anyyy help : //------------ Begins import java.io.IOException; import java.nio.BufferUnderflowException; import org.xsocket.MaxReadSizeExceededException; import org.xsocket.connection.IDataHandler; import org.xsocket.connection.IConnectHandler; import org.xsocket.connection.INonBlockingConnection; class Shared { static INonBlockingConnection camNBC = null; static INonBlockingConnection brwNBC = null; boolean value_cam = false; boolean value_brw = false; synchronized INonBlockingConnection get_cam() { if (value_cam == false) try { wait(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Gives Cam's: " + camNBC); value_cam = false; notify(); return camNBC; } synchronized void put_cam(INonBlockingConnection nbc) { if (value_cam == true) try { wait(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } this.camNBC = nbc; System.out.println("Set Camera: " + nbc); value_cam = false; notify(); } synchronized INonBlockingConnection get_brw() { if (value_brw == false) try { wait(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } System.out.println("Gives brw: " + brwNBC); value_brw = false; notify(); return brwNBC; } synchronized void put_brw(INonBlockingConnection nbc) { if (value_brw == true) try { wait(); } catch (InterruptedException e) { System.out.println("InterruptedException caught"); } this.brwNBC = nbc; System.out.println("Set brw: " + nbc); value_brw = false; notify(); } } class BrowserHandler implements IDataHandler,IConnectHandler { Shared s; BrowserHandler(Shared s) { this.s = s; } @Override public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException { // TODO Auto-generated method stub s.put_brw(connection); return false; } public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException { String data = nbc.readStringByDelimiter("\n"); this.s.get_cam().write(data + " \r\n\r\n"); return true; } } class CameraHandler implements IDataHandler,IConnectHandler { Shared s; public CameraHandler(Shared s) { this.s = s; } @Override public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException { // TODO Auto-generated method stub s.put_cam(connection); return false; } public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException { String data = nbc.readStringByDelimiter("\n"); this.s.get_brw().write(data + " \r\n"); return true; } } import java.io.IOException; import org.xsocket.connection.IServer; import org.xsocket.connection.Server; public class main { public static void main(String args[]) { try { Shared s= new Shared(); IServer browse_side = new Server(8090, new BrowserHandler(s)); IServer cam_side = new Server(9080, new CameraHandler(s)); // run it within the current thread. //srv.run(); // the call will not return // ... or start it by using a dedicated thread browse_side.start(); cam_side.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // returns after the server has been started } } |