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.applet.Applet;
13import 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 **/
21public class PushletApplet extends Applet implements PushletClientListener, Protocol {
22    private TextArea textArea;
23    private String host = "localhost";
24    private int port = 8080;
25    private String subject;
26    private PushletClient pushletClient;
27    private String VERSION = "15.feb.05 #5";
28    private String 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 message) {
97        p(message);
98        bailout();
99    }
00
01    private void bailout() {
02        if (pushletClient != null) {
03            p("Stopping PushletClient");
04            try {
05                pushletClient.leave();
06            } catch (PushletException ignore) {
07                p("Error during leave pe=" + ignore);
08
09            }
10            pushletClient = null;
11        }
12    }
13
14    /** Generic print. */
15    private void p(String s) {
16        dbg("event: " + s);
17        synchronized (textArea) {
18            textArea.append(s + "\n");
19        }
20    }
21
22    /** Generic print. */
23    private void dbg(String s) {
24        System.out.println("[PushletApplet] " + s);
25    }
26}
27
28/*
29 * $Log: PushletApplet.java,v $
30 * Revision 1.16  2005/02/18 09:54:15  justb
31 * refactor: rename Publisher Dispatcher and single Subscriber class
32 *
33 * Revision 1.15  2005/02/15 15:46:36  justb
34 * client API improves
35 *
36 * Revision 1.14  2005/02/15 13:28:33  justb
37 * use new PushletClient lib
38 *
39 * Revision 1.13  2004/10/25 21:22:26  justb
40 * *** empty log message ***
41 *
42 * Revision 1.12  2004/10/24 13:52:52  justb
43 * small fixes in client lib
44 *
45 * Revision 1.11  2004/10/24 12:58:19  justb
46 * revised client and test classes for new protocol
47 *
48 * Revision 1.10  2004/09/03 22:35:37  justb
49 * Almost complete rewrite, just checking in now
50 *
51 * Revision 1.9  2004/08/12 13:18:54  justb
52 * cosmetic changes
53 *
54 * Revision 1.8  2003/08/17 21:06:37  justb
55 * version info change
56 *
57 * Revision 1.7  2003/08/15 08:37:41  justb
58 * fix/add Copyright+LGPL file headers and footers
59 *
60 * Revision 1.6  2003/08/14 21:43:10  justb
61 * improved Java client lifecycle; notably stopping listener thread
62 *
63 * Revision 1.5  2003/08/13 14:00:00  justb
64 * some testing for applets; no real change
65 *
66 * Revision 1.4  2003/05/19 22:53:33  justb
67 * more fixes for applets
68 *
69 * Revision 1.3  2003/05/19 21:56:29  justb
70 * various fixes for applet clients
71 *
72 * Revision 1.2  2003/05/18 16:15:08  justb
73 * support for XML encoded Events
74 *
75 * Revision 1.1.1.1  2002/09/24 21:02:33  justb
76 * import to sourceforge
77 *
78 */
79