aboutsummaryrefslogtreecommitdiff
path: root/src/IvyHttpGatewayServlet.java
blob: ca886b59e5d76e5fa1fcf4a846fe5975bb9439bf (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
 * IvyHttpGatewayServlet.java
 */

package fr.dgac.ivy.tools;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import fr.dgac.ivy.*;


/**
 * Simple gateway between HTTP and Ivy software bus protocols.
 * <br><br>
 * The gateway enables remote clients to manage Ivy clients via
 * HTTP protocol. It aims at solving Ivy limitations to local networks.
 * Features are limited to Ivy bus start(), stop() and sendMsg() methods.
 * The Ivy HTTP gateway considers two Ivy instances as identical if their
 * buses names, buses domains and clients IP names equals pair-by-pair.
 * <br><br>
 * Ivy main web page:
 * <a target="_blank" href="http://www.tls.cena.fr/products/ivy/">
 * http://www.tls.cena.fr/products/ivy/</a>
 * <br>
 * Author home page:
 * <a target="_blank" href="http://www-clips.imag.fr/multicom/User/francis.jambon/index-en.html">
 * http://www-clips.imag.fr/multicom/User/francis.jambon/index-en.html</a>
 * <br><br>
 *
 * <strong>Requests examples:</strong>
 * <pre>
 * - start bus: http://host/IvyHttpGatewayServlet?cmd=start&name=test&domain=228.1.2.4:5678
 * - start bus (with message): http://host/IvyHttpGatewayServlet?cmd=start&name=test&domain=228.1.2.4:5678&msg=ready
 * - stop bus: http://host/IvyHttpGatewayServlet?cmd=stop&name=test&domain=228.1.2.4:5678
 * - send message: http://host/IvyHttpGatewayServlet?cmd=sendmsg&name=test&domain=228.1.2.4:5678&msg=hello
 * - interactive webpage: http://host/IvyHttpGatewayServlet
 * - status information: http://host/IvyHttpGatewayServlet?cmd=status
 * - kill all buses managed by the servlet: http://host/IvyHttpGatewayServlet?cmd=killall
 * </pre>
 *
 * <strong>Versions history:</strong>
 * <pre>
 * - 1.1: new package structure, default address and documentation
 *        non-regression tests with new Ivy 1.2.8-rc2 library
 * - 1.0.5: correction of the form encoding attribute incompatibility with IE
 * - 1.0.4: correction of the button element incompatibility with IE
 * - 1.0.3: correction of the remote host identification bug
 * - 1.0.2: start command can be invoked without message
 * - 1.0.1: licence and some name modifications
 * - 1.0: first working release
 * </pre>
 *
 * <strong>Bugs:</strong>
 * <pre>
 * - the POST method does not work in the interactive webpage
 *   (the parameters are not transmitted)
 * </pre>
 *
 * <strong>License:</strong>
 * <br>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 * GNU Lesser General Public License, see <a target="_blank" href="http://www.gnu.org/copyleft/lesser.html">http://www.gnu.org/copyleft/lesser.html</a>
 *
 * @see fr.dgac.ivy
 * @author Francis JAMBON - CLIPS-IMAG/MultiCom
 * @version 1.1
 */
public class IvyHttpGatewayServlet extends HttpServlet {
    
    // Ivy bus(es) list
    private Hashtable buses;
    
    // Valid name, domain and msg regexps
    private static final String NAME_REGEXP="[^(\\r)]+";
    private static final String DOMAIN_REGEXP="(\\d){1,3}+(\\.)(\\d){1,3}+(\\.)(\\d){1,3}+(\\.)(\\d){1,3}+(\\:)(\\d){1,5}+";
    private static final String MSG_REGEXP="[^(\\r)]*";
    
    // Default values for the interactive webpage
    private static final String DEFAULT_NAME="test";
    private static final String DEFAULT_DOMAIN="228.1.2.4:5678";
    private static final String DEFAULT_MSG="hello";
    
    
    /**
     * Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.buses = new Hashtable();
    }
    
    
    /**
     * Destroys the servlet.
     */
    public void destroy() {
        Enumeration eb = this.buses.elements();
        while (eb.hasMoreElements()) {
            ((Ivy)eb.nextElement()).stop();
        }
        // wait 100ms to prevent ugly Ivy bus(es) disconnection
        try { Thread.sleep(100); } catch (InterruptedException ie) {}
        this.buses.clear();
        this.buses=null;
    }
    
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected synchronized void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if (request.getQueryString()!=null) {
            if (request.getParameter("cmd").equals("start"))
                processRequestStart(request,response);
            else if (request.getParameter("cmd").equals("stop"))
                processRequestStop(request,response);
            else if (request.getParameter("cmd").equals("sendmsg"))
                processRequestSendMsg(request,response);
            else if (request.getParameter("cmd").equals("status"))
                processRequestStatus(request,response);
            else if (request.getParameter("cmd").equals("killall"))
                processRequestKillAll(request,response);
            else
                processRequestError(request, response);
        } else
            processRequestWebpage(request,response);
    }
    
    
    /**
     * Starts an Ivy bus and adds it to the list.
     */
    private void processRequestStart(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");

        String from = request.getRemoteHost();
        String name = request.getParameter("name");
        String domain = request.getParameter("domain");
        String msg = request.getParameter("msg");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+from+"]");
        out.println();
        out.println("START");
        out.println("- name="+name);
        out.println("- domain="+domain);
        if (msg!=null) out.println("- msg="+msg);
        out.println();
        
        if ( domain!=null && domain.matches(DOMAIN_REGEXP) &&
                name!=null && name.matches(NAME_REGEXP) &&
                ( msg==null || (msg!=null && msg.matches(MSG_REGEXP)) ) )  {
            
            IvyHashKey ihk = new IvyHashKey(from, name, domain);
            if (!this.buses.containsKey(ihk)) {
                try {
                    Ivy bus = new Ivy(name, msg, null);
                    bus.start(domain);
                    this.buses.put(ihk,bus);
                    out.println("Ivy bus started");
                } catch (IvyException ie) {
                    out.println("ERROR while starting Ivy bus: "+ie.getMessage());
                }
            } else {
                out.println("ERROR: Similar Ivy bus already started");
            }
        } else {
            out.println("ERROR: Parameter(s) not valid");
        }
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Stops an Ivy bus and removes it from the list.
     */
    private void processRequestStop(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");
        
        String from = request.getRemoteHost();
        String name = request.getParameter("name");
        String domain = request.getParameter("domain");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+from+"]");
        out.println();
        out.println("STOP");
        out.println("- name="+name);
        out.println("- domain="+domain);
        out.println();
        
        if ( domain!=null && domain.matches(DOMAIN_REGEXP) &&
                name!=null && name.matches(NAME_REGEXP) ) {
            
            IvyHashKey ihk = new IvyHashKey(from, name, domain);
            if (this.buses.containsKey(ihk)) {
                Ivy bus = (Ivy)(this.buses.get(ihk));
                bus.stop();
                // wait 100ms to prevent ugly Ivy bus disconnection
                try { Thread.sleep(100); } catch (InterruptedException ie) {}
                this.buses.remove(ihk);
                bus=null;
                out.println("Ivy bus stopped");
            } else {
                out.println("ERROR: Ivy bus does not exist");
            }
        } else {
            out.println("ERROR: Parameter(s) not valid");
        }
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Sends a message to an Ivy bus.
     */
    private void processRequestSendMsg(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");
        
        String from = request.getRemoteHost();
        String name = request.getParameter("name");
        String domain = request.getParameter("domain");
        String msg = request.getParameter("msg");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+from+"]");
        out.println();
        out.println("SEND MESSAGE");
        out.println("- name="+name);
        out.println("- domain="+domain);
        out.println("- msg="+msg);
        out.println();
        
        if ( domain!=null && domain.matches(DOMAIN_REGEXP) &&
                name!=null && name.matches(NAME_REGEXP) &&
                msg !=null && msg.matches(MSG_REGEXP) ) {
            
            IvyHashKey ihk = new IvyHashKey(from, name, domain);
            if (this.buses.containsKey(ihk)) {
                try {
                    Ivy bus = (Ivy)(this.buses.get(ihk));
                    int count = bus.sendMsg(msg);
                    out.println("Message sent to "+count+" client(s)");
                } catch (IvyException ie) {
                    out.println("ERROR sending message: "+ie.getMessage());
                }
            } else {
                out.println("ERROR: Ivy bus does not exist");
            }
        } else {
            out.println("ERROR: Parameter(s) not valid");
        }
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Status page.
     */
    private void processRequestStatus(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+request.getRemoteHost()+"]");
        out.println();
        out.println("STATUS");
        out.println();
        out.println(this.buses.size()+" Ivy bus(es) alive");
        
        Enumeration ek = this.buses.keys();
        while (ek.hasMoreElements()) {
            IvyHashKey ihk = (IvyHashKey)(ek.nextElement());
            out.println("- Ivy bus ["+ihk.getName()+
                    "] on domain ["+ihk.getDomain()+
                    "] managed by ["+ihk.getFrom()+"]");
        }
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Stops all Ivy buses.
     */
    private void processRequestKillAll(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+request.getRemoteHost()+"]");
        out.println();
        out.println("KILL ALL");
        out.println();
        
        if (this.buses.size()!=0) {
            
            Enumeration eb = this.buses.elements();
            while (eb.hasMoreElements()) {
                ((Ivy)eb.nextElement()).stop();
            }
            Enumeration ek = this.buses.keys();
            while (ek.hasMoreElements()) {
                IvyHashKey ihk = (IvyHashKey)(ek.nextElement());
                out.println("Ivy bus ["+ihk.getName()+
                        "] on domain ["+ihk.getDomain()+
                        "] managed by ["+ihk.getFrom()+
                        "] stopped");
            }
            // wait 100ms to prevent ugly Ivy bus(es) disconnection
            try { Thread.sleep(100); } catch (InterruptedException ie) {}
            this.buses.clear();
        } else {
            out.println("NOTHING TO DO: no alive Ivy bus");
        }
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Error (request not understood).
     */
    private void processRequestError(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ivy HTTP Gateway</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<hr>");
        out.println("<pre>");
        
        out.println("Ivy HTTP Gateway");
        out.println("Request from ["+request.getRemoteHost()+"]");
        out.println();
        out.println("ERROR: request not understood");
        out.println();
        out.println("Parameters string readed: "+request.getQueryString());
        
        out.println("</pre>");
        out.println("<hr>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Interactive webpage (request without parameter).
     */
    private void processRequestWebpage(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("    <head>");
        out.println("        <title>Ivy HTTP Gateway</title>");
        out.println("    </head>");
        out.println("    <body>");
        out.println("    <h2>Ivy HTTP Gateway Servlet Interactive Webpage</h2>");
        out.println("    <hr>");
        out.println("    <h3>Global Commands</h3>");
        out.println("    <form method=\"get\"");
        out.println("        action=\""+request.getRequestURL()+"\"");
        out.println("        name=\"Global Commands\">");
        out.println("        <table style=\"text-align: left;\" border=\"0\" cellpadding=\"0\" cellspacing=\"6\">");
        out.println("            <tbody>");
        out.println("                <tr>");
        out.println("                    <td>Ivy bus(es) status: </td>");
        out.println("                    <td><input type=\"submit\" value=\"status\" name=\"cmd\"></td>");
        out.println("                <tr>");
        out.println("                </tr>");
        out.println("                    <td>Kill all (warning!): </td>");
        out.println("                    <td><input type=\"submit\" value=\"killall\" name=\"cmd\"></td>");
        out.println("                </tr>");
        out.println("            </tbody>");
        out.println("        </table>");
        out.println("    </form>");
        out.println("    <hr>");
        out.println("    <h3>Bus Commands</h3>");
        out.println("    <form method=\"get\"");
        out.println("        action=\""+request.getRequestURL()+"\"");
        out.println("        name=\"Bus Commands\">");
        out.println("        <table style=\"text-align: left;\" border=\"0\" cellpadding=\"0\" cellspacing=\"6\">");
        out.println("            <tbody>");
        out.println("                <tr>");
        out.println("                    <td><label>Name:</label> </td>");
        out.println("                    <td><input name=\"name\" value=\""+DEFAULT_NAME+"\"> </td>");
        out.println("                    <td>(name of the Ivy agent - <strong>required</strong>)</td>");
        out.println("                </tr>");
        out.println("                <tr>");
        out.println("                    <td><label>Domain:</label> </td>");
        out.println("                    <td><input value=\""+DEFAULT_DOMAIN+"\" name=\"domain\"> </td>");
        out.println("                    <td>(IP address and port - <strong>required</strong>) </td>");
        out.println("                </tr>");
        out.println("                <tr>");
        out.println("                    <td><label>Message:</label> </td>");
        out.println("                    <td><input name=\"msg\" value=\""+DEFAULT_MSG+"\"> </td>");
        out.println("                    <td>(message to be sent)</td>");
        out.println("                </tr>");
        out.println("            </tbody>");
        out.println("        </table>");
        out.println("        <table style=\"text-align: left;\" border=\"0\" cellpadding=\"0\" cellspacing=\"6\">");
        out.println("            <tbody>");
        out.println("                <tr>");
        out.println("                    <td>Commands: </td>");
        out.println("                    <td><input type=\"submit\" value=\"start\" name=\"cmd\"> </td>");
        out.println("                    <td><input type=\"submit\" value=\"stop\" name=\"cmd\"> </td>");
        out.println("                    <td><input type=\"submit\" value=\"sendmsg\" name=\"cmd\"> </td>");
        out.println("                </tr>");
        out.println("            </tbody>");
        out.println("        </table>");
        out.println("    </form>");
        out.println("    <hr>");
        out.println("    <code>Request from ["+request.getRemoteHost()+"]</code>");
        out.println("    <br>");
        out.println("    <code>CLIPS-IMAG/MultiCom</code>");
        out.println("    </body>");
        out.println("</html>");
        out.close();
    }
    
    
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    
    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    
    /**
     * Returns a short description of the servlet.
     * @return the short description
     */
    public String getServletInfo() {
        return "Ivy HTTP Gateway";
    }
    
    
    /**
     * Internal class for Ivy bus hashkey definition.
     */
    private class IvyHashKey {
        
        private String from;
        private String name;
        private String domain;
        
        public IvyHashKey(String from, String name, String domain) {
            this.from=from;
            this.name=name;
            this.domain=domain;
        }
        
        public String getFrom() { return this.from; }
        
        public String getName() { return this.name; }
        
        public String getDomain() { return this.domain; }
        
        public boolean equals(Object o) {
            if (o!=null && o instanceof IvyHashKey) {
                IvyHashKey ihk = (IvyHashKey)o;
                return (this.from.equals(ihk.from) &&
                        this.name.equals(ihk.name) &&
                        this.domain.equals(ihk.domain) );
            } else return false;
        }
        
        public int hashCode() {
            return this.name.hashCode();
        }
        
    }
    
    
}