diff options
author | jestin | 2011-07-22 16:49:57 +0000 |
---|---|---|
committer | jestin | 2011-07-22 16:49:57 +0000 |
commit | 750f33265d208df8218f85359e3f027900c58363 (patch) | |
tree | 105db356fc9b87fc04f1c09a4c2a567e93b37eed /src/ProxyClient.java | |
parent | 90ac7a3566995cc244f9fdaff41e6c5122c7ca2e (diff) | |
download | ivy-java-750f33265d208df8218f85359e3f027900c58363.zip ivy-java-750f33265d208df8218f85359e3f027900c58363.tar.gz ivy-java-750f33265d208df8218f85359e3f027900c58363.tar.bz2 ivy-java-750f33265d208df8218f85359e3f027900c58363.tar.xz |
Passage en 1.2.14
Diffstat (limited to 'src/ProxyClient.java')
-rw-r--r-- | src/ProxyClient.java | 96 |
1 files changed, 53 insertions, 43 deletions
diff --git a/src/ProxyClient.java b/src/ProxyClient.java index 91af0ba..431cfc5 100644 --- a/src/ProxyClient.java +++ b/src/ProxyClient.java @@ -7,6 +7,13 @@ * (c) ENAC * * changelog: + * 1.2.14 + * - remove the Thread.start() from the constructor, to avoid mulithread issues + * see * http://findbugs.sourceforge.net/bugDescriptions.html#SC_START_IN_CTOR + * now ,we have to call IvyClient.start() after it has been created + * - 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 * @@ -16,22 +23,21 @@ import java.io.*; import java.net.*; import java.util.* ; import gnu.getopt.Getopt; -import org.apache.regexp.*; +import java.util.regex.*; public class ProxyClient extends Ivy { private Socket clientSocket; private PrintWriter out; private BufferedReader in; - private boolean isRunning=false; private static boolean debug = (System.getProperty("IVY_DEBUG")!=null) ; private volatile Thread clientThread; // volatile to ensure the quick communication - private Hashtable id=new Hashtable(); - private Hashtable ghosts = new Hashtable(); - private Hashtable puppets =new Hashtable(); // key=id value=Puppet + private Hashtable<String,String> id=new Hashtable<String,String>(); + private Hashtable<String,Ghost>ghosts = new Hashtable<String,Ghost>(); + private Hashtable<String,Puppet> puppets =new Hashtable<String,Puppet>(); // key=id value=Puppet String domain=null; - public static int DEFAULT_SERVICE_PORT = 3456 ; + public static final int DEFAULT_SERVICE_PORT = 3456 ; public static final String DEFAULTNAME = "ProxyClient"; public static final String helpmsg = "usage: java fr.dgac.ivy.ProxyClient [options] hostname\n\t-b BUS\tspecifies the Ivy bus domain\n\t-p\tport number, default "+DEFAULT_SERVICE_PORT+"\n\t-n ivyname (default "+DEFAULTNAME+")\n\t-q\tquiet, no tty output\n\t-d\tdebug\n\t-h\thelp\ncontacts the ProxyMaster on hostname\n"; @@ -63,25 +69,25 @@ public class ProxyClient extends Ivy { 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 ; } String hostname="localhost"; try { - ProxyClient pc = new ProxyClient(hostname,servicePort,domain); + new ProxyClient(hostname,servicePort,domain).start(); } catch (IvyException ie) { System.out.println("error, can't connect to Ivy"); ie.printStackTrace(); - System.exit(-1); + throw new RuntimeException(); } catch (IOException ioe) { System.out.println("error, can't connect to the proxy master"); ioe.printStackTrace(); - System.exit(-1); + throw new RuntimeException(); } } @@ -92,52 +98,54 @@ public class ProxyClient extends Ivy { in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream())); clientThread=new Thread(new Servicer()); // - clientThread.start(); this.domain=domain; + } + + protected void start() throws IvyException { + clientThread.start(); start(domain); send("Hello bus="+domain); } - static RE getId,fwdGhost,fwdPuppet,puppetRe; + static Pattern getId,fwdGhost,fwdPuppet,puppetRe; static { try { - getId=new RE("^ID id=(.*) value=(.*)"); - fwdGhost=new RE("^ForwardGhost id=(.*) buffer=(.*)"); - fwdPuppet=new RE("^ForwardPuppet id=(.*) buffer=(.*)"); - puppetRe=new RE("^CreatePuppet id=(.*)"); - } catch ( RESyntaxException res ) { + getId=Pattern.compile("^ID id=(.*) value=(.*)"); + fwdGhost=Pattern.compile("^ForwardGhost id=(.*) buffer=(.*)"); + fwdPuppet=Pattern.compile("^ForwardPuppet id=(.*) buffer=(.*)"); + puppetRe=Pattern.compile("^CreatePuppet id=(.*)"); + } catch ( PatternSyntaxException res ) { res.printStackTrace(); System.out.println("Regular Expression bug in Ivy source code ... bailing out"); - System.exit(0); + throw new RuntimeException(); } } void parseMsg(String msg) { // System.out.println("PC parsing "+msg); - if (getId.match(msg)) { - id.put(getId.getParen(1),getId.getParen(2)); - } else if (puppetRe.match(msg)) { // I must create a puppet - String puppetId = puppetRe.getParen(1); + Matcher m; + if ((m=getId.matcher(msg)).matches()) { + id.put(m.group(1),m.group(2)); + } else if ((m=puppetRe.matcher(msg)).matches()) { // I must create a puppet + String puppetId = m.group(1); puppets.put(puppetId,new Puppet(this,puppetId,domain)); - } else if (fwdGhost.match(msg)) { // I must forward to the ghost - Ghost g = (Ghost)ghosts.get(fwdGhost.getParen(1)); - try { g.sendBuffer(fwdGhost.getParen(2)); } catch( IvyException ie) { ie.printStackTrace(); } - } else if (fwdPuppet.match(msg)) { // I must forward to the puppet - Puppet p = (Puppet)puppets.get(fwdPuppet.getParen(1)); - try { p.parse(fwdPuppet.getParen(2)); } catch( IvyException ie) { ie.printStackTrace(); } + } else if ((m=fwdGhost.matcher(msg)).matches()) { // I must forward to the ghost + Ghost g = ghosts.get(m.group(1)); + try { g.sendBuffer(m.group(2)); } catch( IvyException ie) { ie.printStackTrace(); } + } else if ((m=fwdPuppet.matcher(msg)).matches()) { // I must forward to the puppet + Puppet p = puppets.get(m.group(1)); + try { p.parse(m.group(2)); } catch( IvyException ie) { ie.printStackTrace(); } } else { System.out.println("unknown message "+msg); } } - void removePuppet(Puppet p){puppets.remove(p);} - class Servicer implements Runnable { public void run() { - Thread thisThread = Thread.currentThread(); + //Thread thisThread = Thread.currentThread(); traceDebug("Thread started"); - String msg = null; + String msg; try { while (true) { msg=in.readLine(); @@ -147,11 +155,11 @@ public class ProxyClient extends Ivy { } catch (IOException ioe) { traceDebug("Subreader exception ..."); ioe.printStackTrace(); - System.exit(0); + throw new RuntimeException(); } traceDebug("Subreader Thread stopped"); System.out.println("connexion to ProxyMaster lost"); - for (Enumeration e=puppets.elements();e.hasMoreElements();) ((Puppet)e.nextElement()).stop(); + for (Puppet p:puppets.values()) p.stop(); stop(); // leave the bus TODO: make a disconnexion/reconnexion possible ? } } @@ -167,27 +175,29 @@ public class ProxyClient extends Ivy { * TODO: remember everything in case a new proxy client comes ? */ protected IvyClient createIvyClient(Socket s,int port, boolean domachin) throws IOException { - // TODO si c'est un puppet, je ne dois pas créer de Ghost - // voir même me déconnecter du biniou ? - for (Enumeration e=puppets.elements();e.hasMoreElements();) { - if (( ((Puppet)e.nextElement()).bus.getAP() == port ) && !domachin ) { + IvyClient i; + // TODO si c'est un puppet, je ne dois pas creer de Ghost + // voir meme me deconnecter du biniou ? + for (Puppet p:puppets.values()) { + if (( p.bus.getAP() == port ) && !domachin ) { // this new Ivy agent is in fact one of my puppets ... System.out.println("not Ghosting this (probable) Puppet Ivy agent"); - return new IvyClient(this,s,port,domachin); + i= new IvyClient(this,s,port,domachin); + i.start(); + return i; } } String key = getWBUId(); String ghostId; send("GetID id="+key); // asks a centralized ID from ProxyMaster try { // waits for the answer - while ((ghostId=(String)id.get(key))==null) { Thread.sleep(200); } + while ((ghostId=id.get(key))==null) { Thread.sleep(200); } Ghost g = new Ghost(this,s,port,domachin,ghostId,this); ghosts.put(ghostId,g); return g; } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("error waiting"); - System.exit(0); - return null; + throw new RuntimeException(); } /* |