1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3 
4 package nl.justobjects.pushlet.test;
5 
6 import nl.justobjects.pushlet.client.PushletClient;
7 import nl.justobjects.pushlet.client.PushletClientListener;
8 import nl.justobjects.pushlet.core.Event;
9 import nl.justobjects.pushlet.core.Protocol;
10import nl.justobjects.pushlet.util.PushletException;
11
12import java.util.HashMap;
13import java.util.Map;
14
15/**
16 * Demonstrates join-listen service.
17 *
18 * The pushlet protocol supports the "join-listen" service
19 * which allows stateless (e.g. RESTful) clients to join/subscribe/listen
20 * using a single HTTP request.
21 *
22 * @version $Id: SimpleListener.java,v 1.3 2005/03/14 14:07:23 justb Exp $
23 * @author Just van den Broecke - Just Objects &copy;
24 **/
25public class SimpleListener implements PushletClientListener, Protocol {
26    private static String SUBJECT = "/temperature";
27    private static final String MODE = MODE_STREAM;
28
29    public SimpleListener(String aHost, int aPort) {
30        // Create and start a Pushlet client; we receive callbacks
31        // through onHeartbeat() and onData().
32        try {
33            PushletClient pushletClient = new PushletClient(aHost, aPort);
34            pushletClient.setDebug(false);
35            pushletClient.join();
36            pushletClient.listen(this, MODE, SUBJECT);
37            p("pushletClient started");
38        } catch (PushletException pe) {
39            p("Error in setting up pushlet session pe=" + pe);
40        }
41    }
42
43    /** Error occurred. */
44    public void onError(String message) {
45        p(message);
46    }
47
48    /** Abort event from server. */
49    public void onAbort(Event theEvent) {
50        p("onAbort received: " + theEvent);
51    }
52
53    /** Data event from server. */
54    public void onData(Event theEvent) {
55        // Calculate round trip delay
56        System.out.println(theEvent.toXML());
57    }
58
59    /** Heartbeat event from server. */
60    public void onHeartbeat(Event theEvent) {
61        p("onHeartbeat received: " + theEvent);
62    }
63
64    /** Generic print. */
65    public void p(String s) {
66        System.out.println("[SimpleListener] " + s);
67    }
68
69    /** Main program. */
70    public static void main(String args[]) {
71        if (args.length == 0) {
72            new SimpleListener("localhost", 8080);
73        }
74        else if (args.length == 1) {
75            SUBJECT = args[0];
76            new SimpleListener("localhost", 8080);
77        } else {
78            SUBJECT = args[0];
79            // args[1] and [2] should be host and port
80            new SimpleListener(args[1], Integer.parseInt(args[2]));
81        }
82    }
83}
84
85
86/*
87 * $Log: SimpleListener.java,v $
88 * Revision 1.3  2005/03/14 14:07:23  justb
89 * addded subject arg
90 *
91 * Revision 1.2  2005/02/28 21:21:32  justb
92 * no chg
93 *
94 * Revision 1.1  2005/02/28 15:58:05  justb
95 * added SimpleListener example
96 *
97 *
98 */
99