From 750f33265d208df8218f85359e3f027900c58363 Mon Sep 17 00:00:00 2001 From: jestin Date: Fri, 22 Jul 2011 16:49:57 +0000 Subject: Passage en 1.2.14 --- src/ProxyMaster.java | 73 ++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 33 deletions(-) (limited to 'src/ProxyMaster.java') diff --git a/src/ProxyMaster.java b/src/ProxyMaster.java index f8585fd..65d8c7f 100644 --- a/src/ProxyMaster.java +++ b/src/ProxyMaster.java @@ -7,6 +7,10 @@ * (c) ENAC * * changelog: + * 1.2.14 + * - throws RuntimeException instead of System.exit(), allows code reuse + * - switch from gnu regexp (deprecated) to the built in java regexp + * - add generic types to declarations * 1.2.13 * - adds support for RESyntaxException * 1.2.12 @@ -17,23 +21,37 @@ import java.io.*; import java.net.*; import java.util.* ; import gnu.getopt.Getopt; -import org.apache.regexp.*; +import java.util.regex.*; public class ProxyMaster { private ServerSocket serviceSocket; - private boolean isRunning=false; private static boolean debug=false; private boolean doRun=true; // stops running when set to false - private Vector proxyClients = new Vector(); - private Hashtable ghostFathers = new Hashtable(); // key: ghostId value: SubReader + private Vector proxyClients = new Vector(); + private Hashtable ghostFathers = new Hashtable(); // key: ghostId value: SubReader private static int serial=0; - public static int DEFAULT_SERVICE_PORT = 3456 ; + public static final 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"; + static Pattern helloRE, getId,fwdPuppet,fwdGhost; private static String name = DEFAULTNAME; + + static { + try { + helloRE=Pattern.compile("^Hello bus=(.*)"); + getId=Pattern.compile("^GetID id=(.*)"); + fwdPuppet=Pattern.compile("^ForwardPuppet id=(.*) buffer=(.*)"); + fwdGhost=Pattern.compile("^ForwardGhost id=(.*) buffer=(.*)"); + } catch ( PatternSyntaxException res ) { + res.printStackTrace(); + System.out.println("Regular Expression bug in Ivy source code ... bailing out"); + throw new RuntimeException(); + } + } + public static void main(String[] args) { Ivy bus; Getopt opt = new Getopt("ProxyMaster",args,"dqp:h"); @@ -54,21 +72,21 @@ public class ProxyMaster { servicePort = Integer.parseInt(s=opt.getOptarg()); } catch (NumberFormatException nfe) { System.out.println("Invalid port number: " + s ); - System.exit(0); + throw new RuntimeException(); } break; case 'h': default: System.out.println(helpmsg); - System.exit(0); + return; } try { if (!quiet) System.out.println("listening on "+servicePort); - ProxyMaster pm = new ProxyMaster(servicePort); + new ProxyMaster(servicePort); } catch (IOException ioe) { System.out.println("error, can't set up the proxy master"); ioe.printStackTrace(); - System.exit(-1); + return; } } @@ -88,19 +106,8 @@ public class ProxyMaster { PrintWriter out; String hostname=null; // I will know from the socket String busDomain=null; // I will know it from the Hello message - RE helloRE, getId,fwdPuppet,fwdGhost; SubReader(Socket socket) throws IOException { - try { - helloRE=new RE("^Hello bus=(.*)"); - getId=new RE("^GetID id=(.*)"); - fwdPuppet=new RE("^ForwardPuppet id=(.*) buffer=(.*)"); - fwdGhost=new RE("^ForwardGhost id=(.*) buffer=(.*)"); - } catch ( RESyntaxException res ) { - res.printStackTrace(); - System.out.println("Regular Expression bug in Ivy source code ... bailing out"); - System.exit(0); - } proxyClients.addElement(this); hostname = socket.getInetAddress().getHostName(); in=new BufferedReader(new InputStreamReader(socket.getInputStream())); @@ -125,7 +132,7 @@ public class ProxyMaster { } catch (IOException ioe) { traceDebug("Subreader exception ..."); ioe.printStackTrace(); - System.exit(0); + throw new RuntimeException(); } traceDebug("Subreader Thread stopped"); System.out.println("ProxyClient on "+hostname+", bus "+busDomain+" disconnected"); @@ -136,19 +143,19 @@ public class ProxyMaster { void parseMsg(String msg) { // System.out.println("PM parsing "+msg); - if (helloRE.match(msg)) { - busDomain = helloRE.getParen(1); + Matcher m; + if ((m=helloRE.matcher(msg)).matches()) { + busDomain = m.group(1); System.out.println("PC connected from "+hostname+", on the bus "+busDomain); - } else if (getId.match(msg)) { + } else if ((m=getId.matcher(msg)).matches()) { // a new Ghost has appeared and requests an Id System.out.println("PM registering a new Ghost"); - String newGhostId = new Integer(serial++).toString(); + String newGhostId = Integer.valueOf(serial++).toString(); // I give it its ID - send("ID id="+getId.getParen(1)+" value="+newGhostId); + send("ID id="+m.group(1)+" value="+newGhostId); ghostFathers.put(newGhostId,this); // remember the SubReader holding this Ghost // I ask all other Clients to prepare a puppet - for (Enumeration e=proxyClients.elements();e.hasMoreElements();) { - SubReader sr = (SubReader)e.nextElement(); + for (SubReader sr : proxyClients) { if (sr!=SubReader.this) { // System.out.println("propagate CreatePuppet to "+sr.busDomain); sr.send("CreatePuppet id="+newGhostId); @@ -156,14 +163,14 @@ public class ProxyMaster { // System.out.println("won't propagate CreatePuppet to "+sr.busDomain); } } - } else if (fwdGhost.match(msg)) { + } else if ((m=fwdGhost.matcher(msg)).matches()) { System.out.println("PM forwarding ["+msg+"] to its Ghost"); - SubReader sr = (SubReader) ghostFathers.get(fwdGhost.getParen(1)); + SubReader sr = ghostFathers.get(m.group(1)); sr.send(msg); - } else if (fwdPuppet.match(msg)) { + } else if ((m=fwdPuppet.matcher(msg)).matches()) { System.out.println("PM forwarding ["+msg+"] to all other PCs"); - for (Enumeration e=proxyClients.elements();e.hasMoreElements();) { - SubReader sr = (SubReader)e.nextElement(); + for (Enumeratione=proxyClients.elements();e.hasMoreElements();) { + SubReader sr = e.nextElement(); if (sr!=SubReader.this) sr.send(msg); } } else { -- cgit v1.1