KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Tester to demonstrate Pushlet use in Java applications.
17  *
18  * This program does two things:
19  * (1) it subscribes to the subject "test/ping"
20  * (2) it publishes an Event with subject "/test/ping" every few seconds.
21  *
22  * @version $Id: PushletPingApplication.java,v 1.15 2005/02/21 16:59:17 justb Exp $
23  * @author Just van den Broecke - Just Objects &copy;
24  **/

25 public class PushletPingApplication extends Thread JavaDoc implements PushletClientListener, Protocol {
26     private PushletClient pushletClient;
27     private String JavaDoc host;
28     private int port;
29     private static final String JavaDoc SUBJECT = "/test/ping";
30     private static final long PUBLISH_INTERVAL_MILLIS = 3000;
31
32     public PushletPingApplication(String JavaDoc aHost, int aPort) {
33         host = aHost;
34         port = aPort;
35     }
36
37     public void run() {
38         // Create and start a Pushlet client; we receive callbacks
39
// through onHeartbeat() and onData().
40
try {
41             pushletClient = new PushletClient(host, port);
42             pushletClient.setDebug(true);
43             pushletClient.join();
44             pushletClient.listen(this, Protocol.MODE_STREAM);
45
46             // Test subscribe/unsubscribe
47
String JavaDoc subscriptionId = pushletClient.subscribe(SUBJECT);
48             pushletClient.unsubscribe(subscriptionId);
49
50             // The real subscribe
51
pushletClient.subscribe(SUBJECT);
52             p("pushletClient started");
53         } catch (PushletException pe) {
54             p("Error in setting up pushlet session pe=" + pe);
55             return;
56         }
57
58         // Publish an event to the server every N seconds.
59
Map JavaDoc eventData = new HashMap JavaDoc(2);
60         int seqNr = 1;
61         while (true) {
62             try {
63                 // Create event data
64
eventData.put("seqNr", "" + seqNr++);
65                 eventData.put("time", "" + System.currentTimeMillis());
66
67                 // POST event to pushlet server
68
pushletClient.publish(SUBJECT, eventData);
69
70                 p("published ping # " + (seqNr - 1) + " - sleeping...");
71                 Thread.sleep(PUBLISH_INTERVAL_MILLIS);
72             } catch (Exception JavaDoc e) {
73                 p("Postlet exception: " + e);
74                 System.exit(-1);
75             }
76         }
77     }
78
79     /** Error occurred. */
80     public void onError(String JavaDoc message) {
81         p(message);
82     }
83
84     /** Abort event from server. */
85     public void onAbort(Event theEvent) {
86         p("onAbort received: " + theEvent);
87     }
88
89     /** Data event from server. */
90     public void onData(Event theEvent) {
91         // Calculate round trip delay
92
long then = Long.parseLong(theEvent.getField("time"));
93         long delay = System.currentTimeMillis() - then;
94         p("onData: ping #" + theEvent.getField("seqNr") + " in " + delay + " ms");
95     }
96
97     /** Heartbeat event from server. */
98     public void onHeartbeat(Event theEvent) {
99         p("onHeartbeat received: " + theEvent);
100     }
101
102     /** Generic print. */
103     public void p(String JavaDoc s) {
104         System.out.println("[PushletPing] " + s);
105     }
106
107     /** Main program. */
108     public static void main(String JavaDoc args[]) {
109         for (int i = 0; i < 1; i++) {
110             if (args.length == 0) {
111                 new PushletPingApplication("localhost", 8080).start();
112             } else {
113                 // Supply a host and port
114
new PushletPingApplication(args[0], Integer.parseInt(args[1])).start();
115             }
116         }
117
118     }
119 }
120
121
122 /*
123  * $Log: PushletPingApplication.java,v $
124  * Revision 1.15 2005/02/21 16:59:17 justb
125  * SessionManager and session lease introduced
126  *
127  * Revision 1.14 2005/02/21 11:50:48 justb
128  * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
129  *
130  * Revision 1.13 2005/02/20 13:05:33 justb
131  * removed the Postlet (integrated in Pushlet protocol)
132  *
133  * Revision 1.12 2005/02/18 09:54:15 justb
134  * refactor: rename Publisher Dispatcher and single Subscriber class
135  *
136  * Revision 1.11 2005/02/15 15:46:37 justb
137  * client API improves
138  *
139  * Revision 1.10 2005/02/15 13:28:08 justb
140  * use new protocol lib and publish with PushletClient
141  *
142  * Revision 1.9 2004/10/24 13:52:52 justb
143  * small fixes in client lib
144  *
145  * Revision 1.8 2004/10/24 12:58:19 justb
146  * revised client and test classes for new protocol
147  *
148  * Revision 1.7 2004/09/03 22:35:37 justb
149  * Almost complete rewrite, just checking in now
150  *
151  * Revision 1.6 2004/08/12 13:18:54 justb
152  * cosmetic changes
153  *
154  * Revision 1.5 2004/03/10 14:53:10 justb
155  * new
156  *
157  * Revision 1.4 2003/08/17 20:30:20 justb
158  * cosmetic changes
159  *
160  * Revision 1.3 2003/08/15 08:37:41 justb
161  * fix/add Copyright+LGPL file headers and footers
162  *
163  * Revision 1.2 2003/05/18 16:15:08 justb
164  * support for XML encoded Events
165  *
166  * Revision 1.1.1.1 2002/09/24 21:02:33 justb
167  * import to sourceforge
168  *
169  * Revision 1.1.1.1 2002/09/20 22:48:19 justb
170  * import to SF
171  *
172  * Revision 1.1.1.1 2002/09/20 14:19:02 justb
173  * first import into SF
174  *
175  * Revision 1.3 2000/08/31 12:49:50 just
176  * added CVS comment tags for log and copyright
177  *
178  *
179  */

180
Popular Tags