aboutsummaryrefslogtreecommitdiff
path: root/src/IvyHttpGatewayClient.java
blob: 5c602d3d007c3b234e274a0b119c342cc693d150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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.
 * <strong>Warning:</strong> the error management is rather poor!
 * <br><br>
 *
 * <strong>License:</strong><br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 * See IvyHttpGatewayServlet
 *
 * @see IvyHttpGatewayServlet
 * @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();
    }  
    
}