aboutsummaryrefslogtreecommitdiff
path: root/src/ProxyMaster.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ProxyMaster.java')
-rw-r--r--src/ProxyMaster.java63
1 files changed, 45 insertions, 18 deletions
diff --git a/src/ProxyMaster.java b/src/ProxyMaster.java
index 09216ed..f80fa11 100644
--- a/src/ProxyMaster.java
+++ b/src/ProxyMaster.java
@@ -9,7 +9,8 @@
* changelog:
* 1.2.12
*/
-package fr.dgac.ivy ; // TODO go into sub tools, and build a shell/.BAT script
+package fr.dgac.ivy.tools ; // TODO go into sub tools, and build a shell/.BAT script
+import fr.dgac.ivy.* ;
import java.io.*;
import java.net.*;
import java.util.* ;
@@ -21,7 +22,9 @@ 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 static int serial=0;
public static int DEFAULT_SERVICE_PORT = 3456 ;
@@ -81,22 +84,27 @@ public class ProxyMaster {
class SubReader extends Thread {
BufferedReader in;
PrintWriter out;
+ String hostname=null; // I will know from the socket
+ String busDomain=null; // I will know it from the Hello message
+
SubReader(Socket socket) throws IOException {
proxyClients.addElement(this);
- System.out.println("ProxyClient connected");
+ hostname = socket.getInetAddress().getHostName();
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
start();
}
- public void send(String s) {
+
+ public void send(String s) { // sends a message to the SubReader peer ( a ProxyClient )
out.println(s);
out.flush();
}
+
public void run() {
traceDebug("Subreader Thread started");
String msg = null;
try {
- while (true) {
+ while (doRun) {
msg=in.readLine();
if (msg==null) break;
parseMsg(msg);
@@ -107,28 +115,47 @@ public class ProxyMaster {
System.exit(0);
}
traceDebug("Subreader Thread stopped");
- System.out.println("ProxyClient disconnected");
+ System.out.println("ProxyClient on "+hostname+", bus "+busDomain+" disconnected");
proxyClients.removeElement(this);
}
+ RE helloRE=new RE("^Hello bus=(.*)");
RE getId=new RE("^GetID id=(.*)");
- RE fwd=new RE("^Forward id=(.*) buffer=(.*)");
+ RE fwdPuppet=new RE("^ForwardPuppet id=(.*) buffer=(.*)");
+ RE fwdGhost=new RE("^ForwardGhost 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
+ // System.out.println("PM parsing "+msg);
+ if (helloRE.match(msg)) {
+ busDomain = helloRE.getParen(1);
+ System.out.println("PC connected from "+hostname+", on the bus "+busDomain);
+ } else if (getId.match(msg)) {
+ // a new Ghost has appeared and requests an Id
+ System.out.println("PM registering a new Ghost");
+ String newGhostId = new Integer(serial++).toString();
+ // I give it its ID
+ send("ID id="+getId.getParen(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();
+ if (sr!=SubReader.this) {
+ // System.out.println("propagate CreatePuppet to "+sr.busDomain);
+ sr.send("CreatePuppet id="+newGhostId);
+ } else {
+ // System.out.println("won't propagate CreatePuppet to "+sr.busDomain);
+ }
+ }
+ } else if (fwdGhost.match(msg)) {
+ System.out.println("PM forwarding ["+msg+"] to its Ghost");
+ SubReader sr = (SubReader) ghostFathers.get(fwdGhost.getParen(1));
+ sr.send(msg);
+ } else if (fwdPuppet.match(msg)) {
+ System.out.println("PM forwarding ["+msg+"] to all other PCs");
for (Enumeration e=proxyClients.elements();e.hasMoreElements();) {
SubReader sr = (SubReader)e.nextElement();
- if (sr!=SubReader.this) sr.send("CreatePuppet id="+newGhostId);
+ if (sr!=SubReader.this) sr.send(msg);
}
- } 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);
}