KickJava   Java API By Example, From Geeks To Geeks.

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


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.applet.Applet JavaDoc;
13 import java.awt.*;
14
15 /**
16  * Tester for applet clients; displays incoming events in text area.
17  *
18  * @version $Id: PushletApplet.java,v 1.16 2005/02/18 09:54:15 justb Exp $
19  * @author Just van den Broecke - Just Objects &copy;
20  **/

21 public class PushletApplet extends Applet JavaDoc implements PushletClientListener, Protocol {
22     private TextArea textArea;
23     private String JavaDoc host = "localhost";
24     private int port = 8080;
25     private String JavaDoc subject;
26     private PushletClient pushletClient;
27     private String JavaDoc VERSION = "15.feb.05 #5";
28     private String JavaDoc PUSH_MODE = Protocol.MODE_PULL;
29
30     /** One-time setup. */
31     public void init() {
32         // Subject to subscribe to
33
subject = getParameter(P_SUBJECT);
34
35         host = getDocumentBase().getHost();
36         port = getDocumentBase().getPort();
37
38         // Hmm sometimes this value is -1...(Mozilla with Java 1.3.0 on Win)
39
if (port == -1) {
40             port = 80;
41         }
42
43         setLayout(new GridLayout(1, 1));
44         textArea = new TextArea(15, 40);
45         textArea.setForeground(Color.yellow);
46         textArea.setBackground(Color.gray);
47         textArea.setEditable(false);
48         add(textArea);
49         p("PushletApplet - " + VERSION);
50     }
51
52     public void start() {
53         dbg("start()");
54         bailout();
55
56         try {
57             pushletClient = new PushletClient(host, port);
58             p("Created PushletClient");
59
60             pushletClient.join();
61             p("Joined server");
62
63             pushletClient.listen(this, PUSH_MODE);
64             p("Listening in mode=" + PUSH_MODE);
65
66             pushletClient.subscribe(subject);
67             p("Subscribed to=" + subject);
68         } catch (PushletException pe) {
69             p("Error exception=" + pe);
70             bailout();
71         }
72     }
73
74     public void stop() {
75         dbg("stop()");
76         bailout();
77     }
78
79     /** Abort event from server. */
80     public void onAbort(Event theEvent) {
81         p(theEvent.toXML());
82         bailout();
83     }
84
85     /** Data event from server. */
86     public void onData(Event theEvent) {
87         p(theEvent.toXML());
88     }
89
90     /** Heartbeat event from server. */
91     public void onHeartbeat(Event theEvent) {
92         p(theEvent.toXML());
93     }
94
95     /** Error occurred. */
96     public void onError(String JavaDoc message) {
97         p(message);
98         bailout();
99     }
100
101     private void bailout() {
102         if (pushletClient != null) {
103             p("Stopping PushletClient");
104             try {
105                 pushletClient.leave();
106             } catch (PushletException ignore) {
107                 p("Error during leave pe=" + ignore);
108
109             }
110             pushletClient = null;
111         }
112     }
113
114     /** Generic print. */
115     private void p(String JavaDoc s) {
116         dbg("event: " + s);
117         synchronized (textArea) {
118             textArea.append(s + "\n");
119         }
120     }
121
122     /** Generic print. */
123     private void dbg(String JavaDoc s) {
124         System.out.println("[PushletApplet] " + s);
125     }
126 }
127
128 /*
129  * $Log: PushletApplet.java,v $
130  * Revision 1.16 2005/02/18 09:54:15 justb
131  * refactor: rename Publisher Dispatcher and single Subscriber class
132  *
133  * Revision 1.15 2005/02/15 15:46:36 justb
134  * client API improves
135  *
136  * Revision 1.14 2005/02/15 13:28:33 justb
137  * use new PushletClient lib
138  *
139  * Revision 1.13 2004/10/25 21:22:26 justb
140  * *** empty log message ***
141  *
142  * Revision 1.12 2004/10/24 13:52:52 justb
143  * small fixes in client lib
144  *
145  * Revision 1.11 2004/10/24 12:58:19 justb
146  * revised client and test classes for new protocol
147  *
148  * Revision 1.10 2004/09/03 22:35:37 justb
149  * Almost complete rewrite, just checking in now
150  *
151  * Revision 1.9 2004/08/12 13:18:54 justb
152  * cosmetic changes
153  *
154  * Revision 1.8 2003/08/17 21:06:37 justb
155  * version info change
156  *
157  * Revision 1.7 2003/08/15 08:37:41 justb
158  * fix/add Copyright+LGPL file headers and footers
159  *
160  * Revision 1.6 2003/08/14 21:43:10 justb
161  * improved Java client lifecycle; notably stopping listener thread
162  *
163  * Revision 1.5 2003/08/13 14:00:00 justb
164  * some testing for applets; no real change
165  *
166  * Revision 1.4 2003/05/19 22:53:33 justb
167  * more fixes for applets
168  *
169  * Revision 1.3 2003/05/19 21:56:29 justb
170  * various fixes for applet clients
171  *
172  * Revision 1.2 2003/05/18 16:15:08 justb
173  * support for XML encoded Events
174  *
175  * Revision 1.1.1.1 2002/09/24 21:02:33 justb
176  * import to sourceforge
177  *
178  */

179
Popular Tags