| PushletPingApplication.java |
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 * 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 ©
24 **/
25public class PushletPingApplication extends Thread implements PushletClientListener, Protocol {
26 private PushletClient pushletClient;
27 private String host;
28 private int port;
29 private static final String SUBJECT = "/test/ping";
30 private static final long PUBLISH_INTERVAL_MILLIS = 3000;
31
32 public PushletPingApplication(String 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 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 eventData = new HashMap(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 e) {
73 p("Postlet exception: " + e);
74 System.exit(-1);
75 }
76 }
77 }
78
79 /** Error occurred. */
80 public void onError(String 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);
00 }
01
02 /** Generic print. */
03 public void p(String s) {
04 System.out.println("[PushletPing] " + s);
05 }
06
07 /** Main program. */
08 public static void main(String args[]) {
09 for (int i = 0; i < 1; i++) {
10 if (args.length == 0) {
11 new PushletPingApplication("localhost", 8080).start();
12 } else {
13 // Supply a host and port
14 new PushletPingApplication(args[0], Integer.parseInt(args[1])).start();
15 }
16 }
17
18 }
19}
20
21
22/*
23 * $Log: PushletPingApplication.java,v $
24 * Revision 1.15 2005/02/21 16:59:17 justb
25 * SessionManager and session lease introduced
26 *
27 * Revision 1.14 2005/02/21 11:50:48 justb
28 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
29 *
30 * Revision 1.13 2005/02/20 13:05:33 justb
31 * removed the Postlet (integrated in Pushlet protocol)
32 *
33 * Revision 1.12 2005/02/18 09:54:15 justb
34 * refactor: rename Publisher Dispatcher and single Subscriber class
35 *
36 * Revision 1.11 2005/02/15 15:46:37 justb
37 * client API improves
38 *
39 * Revision 1.10 2005/02/15 13:28:08 justb
40 * use new protocol lib and publish with PushletClient
41 *
42 * Revision 1.9 2004/10/24 13:52:52 justb
43 * small fixes in client lib
44 *
45 * Revision 1.8 2004/10/24 12:58:19 justb
46 * revised client and test classes for new protocol
47 *
48 * Revision 1.7 2004/09/03 22:35:37 justb
49 * Almost complete rewrite, just checking in now
50 *
51 * Revision 1.6 2004/08/12 13:18:54 justb
52 * cosmetic changes
53 *
54 * Revision 1.5 2004/03/10 14:53:10 justb
55 * new
56 *
57 * Revision 1.4 2003/08/17 20:30:20 justb
58 * cosmetic changes
59 *
60 * Revision 1.3 2003/08/15 08:37:41 justb
61 * fix/add Copyright+LGPL file headers and footers
62 *
63 * Revision 1.2 2003/05/18 16:15:08 justb
64 * support for XML encoded Events
65 *
66 * Revision 1.1.1.1 2002/09/24 21:02:33 justb
67 * import to sourceforge
68 *
69 * Revision 1.1.1.1 2002/09/20 22:48:19 justb
70 * import to SF
71 *
72 * Revision 1.1.1.1 2002/09/20 14:19:02 justb
73 * first import into SF
74 *
75 * Revision 1.3 2000/08/31 12:49:50 just
76 * added CVS comment tags for log and copyright
77 *
78 *
79 */
80| PushletPingApplication.java |