KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > test > SimpleListener


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;
10 import nl.justobjects.pushlet.util.PushletException;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
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.2 2005/02/28 21:21:32 justb Exp $
23  * @author Just van den Broecke - Just Objects &copy;
24  **/

25 public class SimpleListener implements PushletClientListener, Protocol {
26     private static final String JavaDoc SUBJECT = "/temperature";
27     private static final String JavaDoc MODE = MODE_STREAM;
28
29     public SimpleListener(String JavaDoc 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(true);
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 JavaDoc 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
p("onData: " + theEvent);
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 JavaDoc s) {
66         System.out.println("[SimpleListener] " + s);
67     }
68
69     /** Main program. */
70     public static void main(String JavaDoc args[]) {
71         if (args.length == 0) {
72             new SimpleListener("localhost", 8080);
73         } else {
74             // args[0] and [1] should be host and port
75
new SimpleListener(args[0], Integer.parseInt(args[1]));
76         }
77     }
78 }
79
80
81 /*
82  * $Log: SimpleListener.java,v $
83  * Revision 1.2 2005/02/28 21:21:32 justb
84  * no chg
85  *
86  * Revision 1.1 2005/02/28 15:58:05 justb
87  * added SimpleListener example
88  *
89  *
90  */

91
Popular Tags