From c9dbee9c2fb98a3ca467ac82a97a9d6f3dba9737 Mon Sep 17 00:00:00 2001 From: jestin Date: Tue, 11 Jul 2006 13:57:07 +0000 Subject: voir chaque fichier pour les modifs. Je rajoute les servlets dans la base, j'essayerai de fabriquer un ant file pour les construire --- src/IvyHttpGatewayClient.java | 120 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/IvyHttpGatewayClient.java (limited to 'src/IvyHttpGatewayClient.java') diff --git a/src/IvyHttpGatewayClient.java b/src/IvyHttpGatewayClient.java new file mode 100644 index 0000000..585e342 --- /dev/null +++ b/src/IvyHttpGatewayClient.java @@ -0,0 +1,120 @@ +/* + * IvyHttpGatewayClient.java + */ + +package fr.dgac.ivy.tools; + +import java.lang.*; +import java.io.*; +import java.net.*; + + +/** + * Light remote client for the Ivy HTTP Gateway. + * This code is given as example only: it is not compulsery + * to use this client with the IvyHttpGatewayServlet. + * Warning: the error management is rather poor! + *

+ * + * License:
+ *           + * See IvyHttpGatewayServlet + * + * @see IvyHttpGatewayServlet + * @see fr.dgac.ivy + * @author Francis JAMBON - CLIPS-IMAG/MultiCom + * @version 1.0.5 + */ +public class IvyHttpGatewayClient { + + private String name; // bus name + private String domain; // bus domain + private String url; // servlet URL + private boolean is_started; // bus state (supposed!) + private static final boolean debug=true; // debug option + + + /** + * Creates a new instance of IvyHttpGatewayClient. + * @param name the bus name + * @param url the servlet URL (protocol, host, port and complete servlet path) + */ + public IvyHttpGatewayClient(String name, String url) { + this.name = name; + this.url = url; + this.domain = null; + this.is_started = false; + } + + + /** + * Starts the Ivy bus. + * @param domain the bus domain (address and port) + */ + public void start(String domain) { + this.domain = domain; + if (!is_started) { + String request = this.url+"?"+"cmd=start"+"&name="+this.name+"&domain="+this.domain; + doHttpRequest(request); + this.is_started=true; + } + } + + + /** + * Stops the Ivy bus. + */ + public void stop() { + if (is_started) { + String request = this.url+"?"+"cmd=stop"+"&name="+this.name+"&domain="+this.domain; + doHttpRequest(request); + this.is_started=false; + } + } + + + /** + * Sends a message to the Ivy bus. + * The bus must be started before sending a message. + * @param msg the message to be sent + */ + public void sendMsg(String msg) { + if (is_started) { + String request = this.url+"?"+"cmd=sendmsg"+"&name="+this.name+"&domain="+this.domain+"&msg="+msg; + doHttpRequest(request); + } + } + + + /** + * Generic request to the servlet. + * @param request the URL (protocol, host, port, servlet path and parameters) + */ + private static void doHttpRequest(String request) { + if (debug) System.out.println(); + if (debug) System.out.println("-------------------- start of request ------------------"); + if (debug) System.out.println("IvyHttpGatewayClient"); + if (debug) System.out.println("REQUEST: "+request); + try { + URL servlet = new URL(request); + URLConnection connection = servlet.openConnection(); + BufferedReader in = new BufferedReader( + new InputStreamReader( + servlet.openStream())); + String input_line; + if (debug) System.out.println("ANSWER:"); + while ((input_line=in.readLine()) != null) + if (debug) System.out.println(input_line); + in.close(); + } catch (MalformedURLException e) { + System.err.println("IvyHttpGatewayClient ERROR: URL Malformed"); + if (debug) e.printStackTrace(); + } catch (IOException e) { + System.err.println("IvyHttpGatewayClient ERROR: Open connection failed"); + if (debug) e.printStackTrace(); + } + if (debug) System.out.println("-------------------- end of request --------------------"); + if (debug) System.out.println(); + } + +} -- cgit v1.1