diff options
author | jestin | 2006-08-01 15:52:16 +0000 |
---|---|---|
committer | jestin | 2006-08-01 15:52:16 +0000 |
commit | 21cec28549587db7e34d0ac455244fe4d73a0ed4 (patch) | |
tree | 336320cb1ad63fe7633fb57d91959dd53cec69b3 /src/ProxyMaster.java | |
parent | d0afba319f95f0059d201c6b7cbfbc3322f2c0f6 (diff) | |
download | ivy-java-21cec28549587db7e34d0ac455244fe4d73a0ed4.zip ivy-java-21cec28549587db7e34d0ac455244fe4d73a0ed4.tar.gz ivy-java-21cec28549587db7e34d0ac455244fe4d73a0ed4.tar.bz2 ivy-java-21cec28549587db7e34d0ac455244fe4d73a0ed4.tar.xz |
Changes to allow the creation of a tunnel between two (or more) ivy
bussssseeeeezzzz
Diffstat (limited to 'src/ProxyMaster.java')
-rw-r--r-- | src/ProxyMaster.java | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/ProxyMaster.java b/src/ProxyMaster.java new file mode 100644 index 0000000..7ed9460 --- /dev/null +++ b/src/ProxyMaster.java @@ -0,0 +1,140 @@ +/** + * ProxyMaster: Ivy relay, first attempt + * + * @author Yannick Jestin + * @author <a href="http://www.tls.cena.fr/products/ivy/">http://www.tls.cena.fr/products/ivy/</a> + * + * (c) ENAC + * + * changelog: + * 1.2.12 + */ +package fr.dgac.ivy ; +import java.io.*; +import java.net.*; +import java.util.* ; +import gnu.getopt.Getopt; +import org.apache.regexp.*; + +public class ProxyMaster { + + private ServerSocket serviceSocket; + private boolean isRunning=false; + private static boolean debug=false; + private Vector proxyClients = new Vector(); + private static int serial=0; + + public static int DEFAULT_SERVICE_PORT = 3456 ; + public static final String DEFAULTNAME = "ProxyMaster"; + public static final String helpmsg = "usage: java fr.dgac.ivy.ProxyMaster [options]\n\t-p\tport number, default "+DEFAULT_SERVICE_PORT+"\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-h\thelp\nListens on the TCP port for ProxyClients to join.\n"; + + private static String name = DEFAULTNAME; + public static void main(String[] args) { + Ivy bus; + Getopt opt = new Getopt("ProxyMaster",args,"dqp:h"); + int c; + int servicePort = DEFAULT_SERVICE_PORT; + boolean quiet = false; + while ((c = opt.getopt()) != -1) switch (c) { + case 'q': + quiet=true; + break; + case 'd': + Properties sysProp = System.getProperties(); + sysProp.put("IVY_DEBUG","yes"); + break; + case 'p': + String s=""; + try { + servicePort = Integer.parseInt(s=opt.getOptarg()); + } catch (NumberFormatException nfe) { + System.out.println("Invalid port number: " + s ); + System.exit(0); + } + break; + case 'h': + default: + System.out.println(helpmsg); + System.exit(0); + } + try { + if (!quiet) System.out.println("listening on "+servicePort); + ProxyMaster pm = new ProxyMaster(servicePort); + } catch (IOException ioe) { + System.out.println("error, can't set up the proxy master"); + ioe.printStackTrace(); + System.exit(-1); + } + } + + public ProxyMaster(int servicePort) throws IOException { + serviceSocket = new ServerSocket(servicePort) ; + while ( true ) { + try { + new SubReader(serviceSocket.accept()); + } catch( IOException e ) { + traceDebug("TCP socket reader caught an exception " + e.getMessage()); + } + } + } + + class SubReader extends Thread { + BufferedReader in; + PrintWriter out; + SubReader(Socket socket) throws IOException { + proxyClients.addElement(this); + System.out.println("ProxyClient connected"); + in=new BufferedReader(new InputStreamReader(socket.getInputStream())); + out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); + start(); + } + public void send(String s) { + out.println(s); + out.flush(); + } + public void run() { + traceDebug("Subreader Thread started"); + String msg = null; + try { + while (true) { + msg=in.readLine(); + if (msg==null) break; + parseMsg(msg); + } + } catch (IOException ioe) { + traceDebug("Subreader exception ..."); + ioe.printStackTrace(); + System.exit(0); + } + traceDebug("Subreader Thread stopped"); + System.out.println("ProxyClient disconnected"); + proxyClients.removeElement(this); + } + + RE getId=new RE("^GetID id=(.*)"); + RE fwd=new RE("^Forward id=(.*) buffer=(.*)"); + void parseMsg(String msg) { + System.out.println("parsing "+msg); + if (getId.match(msg)) { + // a new Ghost has appeared + System.out.println("a new Ghost has appeared"); + int newGhostId = serial++; + out.println("ID id="+getId.getParen(1)+" value="+newGhostId); + out.flush(); + // TODO create Puppets in each other ProxyClient + for (Enumeration e=proxyClients.elements();e.hasMoreElements();) + ((SubReader)e.nextElement()).send("CreatePuppet id="+newGhostId); + } else if (fwd.match(msg)) { + System.out.println("forwarding "+msg); + // TODO forward the message to all relevant puppets + } else { + System.out.println("error unknown message "+msg); + } + } + } // class SubReader + + private static void traceDebug(String s){ + if (debug) System.out.println("-->ProxyMaster "+name+"<-- "+s); + } + +} |