aboutsummaryrefslogtreecommitdiff
path: root/src/ProxyClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/ProxyClient.java')
-rw-r--r--src/ProxyClient.java155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/ProxyClient.java b/src/ProxyClient.java
new file mode 100644
index 0000000..3f616c5
--- /dev/null
+++ b/src/ProxyClient.java
@@ -0,0 +1,155 @@
+/**
+ * ProxyClient: 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 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 Vector ghosts = new Vector();
+
+ public static 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";
+
+ private static String name = DEFAULTNAME;
+ public static void main(String[] args) {
+ Ivy bus;
+ Getopt opt = new Getopt("ProxyClient",args,"n:b:dqp:h");
+ int c;
+ int servicePort = DEFAULT_SERVICE_PORT;
+ boolean quiet = false;
+ String domain=Ivy.getDomain(null);
+ while ((c = opt.getopt()) != -1) switch (c) {
+ case 'n':
+ name=opt.getOptarg();
+ break;
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ 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);
+ }
+ String hostname="localhost";
+ try {
+ ProxyClient pc = new ProxyClient(hostname,servicePort);
+ if (!quiet) System.out.println("broadcasting on "+pc.domains(domain));
+ if (!quiet) System.out.println("contacting tcp:"+hostname+":"+servicePort);
+ pc.start(domain);
+ System.out.println("client running...");
+ } catch (IvyException ie) {
+ System.out.println("error, can't connect to Ivy");
+ ie.printStackTrace();
+ System.exit(-1);
+ } catch (IOException ioe) {
+ System.out.println("error, can't connect to the proxy master");
+ ioe.printStackTrace();
+ System.exit(-1);
+ }
+ }
+
+ public ProxyClient(String hostname,int servicePort) throws IOException {
+ super(name,name+" ready",null);
+ clientSocket = new Socket(hostname,servicePort) ;
+ in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
+ out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
+ clientThread=new Thread(new Servicer());
+ clientThread.start();
+ }
+
+ RE getId=new RE("^ID id=(.*) value=(.*)");
+ RE fwd=new RE("^Forward id=(.*) buffer=(.*)");
+ void parseMsg(String msg) {
+ System.out.println("parsing "+msg);
+ if (getId.match(msg)) {
+ id.put(getId.getParen(1),getId.getParen(2));
+ } else if (fwd.match(msg)) {
+ // received a forward request
+ // TODO give it to the puppet
+ } else {
+ System.out.println("unknown message "+msg);
+ }
+ }
+
+ class Servicer implements Runnable {
+ public void run() {
+ Thread thisThread = Thread.currentThread();
+ traceDebug("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");
+ }
+ }
+
+ protected IvyClient createIvyClient(Socket s,int port, boolean domachin) throws IOException {
+ String key = getWBUId();
+ System.out.println("asking for a key");
+ out.println("GetID id="+key); // asks a centralized ID from ProxyMaster
+ out.flush();
+ 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);
+ }
+
+ private void forward(String id,String buffer) {
+ out.println("Forward id="+id+" buffer="+buffer);
+ out.flush();
+ }
+
+ private static void traceDebug(String s){
+ if (debug) System.out.println("-->ProxyClient "+name+"<-- "+s);
+ }
+
+}