1 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 ; 13 import java.awt.*; 14 15 21 public 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 31 public void init() { 32 subject = getParameter(P_SUBJECT); 34 35 host = getDocumentBase().getHost(); 36 port = getDocumentBase().getPort(); 37 38 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 80 public void onAbort(Event theEvent) { 81 p(theEvent.toXML()); 82 bailout(); 83 } 84 85 86 public void onData(Event theEvent) { 87 p(theEvent.toXML()); 88 } 89 90 91 public void onHeartbeat(Event theEvent) { 92 p(theEvent.toXML()); 93 } 94 95 96 public void onError(String 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 115 private void p(String s) { 116 dbg("event: " + s); 117 synchronized (textArea) { 118 textArea.append(s + "\n"); 119 } 120 } 121 122 123 private void dbg(String s) { 124 System.out.println("[PushletApplet] " + s); 125 } 126 } 127 128 179 | Popular Tags |