aboutsummaryrefslogtreecommitdiff
path: root/src/ProxyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ProxyClient.java')
-rw-r--r--src/ProxyClient.java77
1 files changed, 47 insertions, 30 deletions
diff --git a/src/ProxyClient.java b/src/ProxyClient.java
index d6c6932..9eb1e41 100644
--- a/src/ProxyClient.java
+++ b/src/ProxyClient.java
@@ -23,9 +23,9 @@ public class ProxyClient extends Ivy {
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 volatile Thread clientThread; // volatile to ensure the quick communication
private Hashtable id=new Hashtable();
- private Vector ghosts = new Vector();
+ private Hashtable ghosts = new Hashtable();
private Hashtable puppets =new Hashtable(); // key=id value=Puppet
String domain=null;
@@ -72,9 +72,6 @@ public class ProxyClient extends Ivy {
String hostname="localhost";
try {
ProxyClient pc = new ProxyClient(hostname,servicePort,domain);
- if (!quiet) System.out.println("broadcasting on "+pc.domains(domain));
- if (!quiet) System.out.println("contacting tcp:"+hostname+":"+servicePort);
- System.out.println("client running...");
} catch (IvyException ie) {
System.out.println("error, can't connect to Ivy");
ie.printStackTrace();
@@ -87,31 +84,35 @@ public class ProxyClient extends Ivy {
}
public ProxyClient(String hostname,int servicePort,String domain) throws IOException, IvyException {
- super(name,name+" ready",null);
- clientSocket = new Socket(hostname,servicePort) ;
+ super(name,name+" ready",null); // I will join the bus
+ System.out.println("PC contacting tcp:"+hostname+":"+servicePort);
+ clientSocket = new Socket(hostname,servicePort) ; // contacting hostname:servicePort
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
- clientThread=new Thread(new Servicer());
+ clientThread=new Thread(new Servicer()); //
clientThread.start();
this.domain=domain;
start(domain);
+ send("Hello bus="+domain);
}
RE getId=new RE("^ID id=(.*) value=(.*)");
- RE fwd=new RE("^Forward id=(.*) buffer=(.*)");
+ RE fwdGhost=new RE("^ForwardGhost id=(.*) buffer=(.*)");
+ RE fwdPuppet=new RE("^ForwardPuppet id=(.*) buffer=(.*)");
RE puppetRe=new RE("^CreatePuppet id=(.*)");
void parseMsg(String msg) {
- System.out.println("parsing "+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
- puppets.put(puppetRe.getParen(1),new Puppet(this,domain));
- } else if (fwd.match(msg)) {
- // received a forward request
- // TODO give it to the puppet
- Puppet p = (Puppet)puppets.get(fwd.getParen(1));
- p.parse(fwd.getParen(2));
+ } else if (puppetRe.match(msg)) { // I must create a puppet
+ String puppetId = puppetRe.getParen(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 {
System.out.println("unknown message "+msg);
}
@@ -136,28 +137,44 @@ public class ProxyClient extends Ivy {
System.exit(0);
}
traceDebug("Subreader Thread stopped");
- System.out.println("ProxyClient disconnected");
+ System.out.println("connexion to ProxyMaster lost");
+ for (Enumeration e=puppets.elements();e.hasMoreElements();) ((Puppet)e.nextElement()).stop();
stop(); // leave the bus TODO: make a disconnexion/reconnexion possible ?
}
}
+ void send(String s) { // sends a message to the proxyMaster
+ out.println(s);
+ out.flush();
+ }
+
/*
* here, I create a ghost instead of a normal Ivy Client, to catch the
* protocol and forward everything to the proxies.
* 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 ) {
+ // 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);
+ }
+ }
String key = getWBUId();
- System.out.println("asking for a key");
- out.println("GetID id="+key); // asks a centralized ID from ProxyMaster
- out.flush();
+ String ghostId;
+ send("GetID id="+key); // asks a centralized ID from ProxyMaster
try { // waits for the answer
- while (id.get(key)==null) { Thread.sleep(200); }
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- System.out.println("key received");
- return new Ghost(this,s,port,domachin,(String)id.get(key),this);
+ while ((ghostId=(String)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;
}
/*
@@ -165,8 +182,8 @@ public class ProxyClient extends Ivy {
* ProxyMaster
*
*/
- protected void forward(String id,String buffer) {
- out.println("Forward id="+id+" buffer="+buffer);
+ protected void forwardPuppet(String id,String buffer) {
+ out.println("ForwardPuppet id="+id+" buffer="+buffer);
out.flush();
}