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.util.HashMap ; 13 import java.util.Map ; 14 15 25 public 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 try { 41 pushletClient = new PushletClient(host, port); 42 pushletClient.setDebug(true); 43 pushletClient.join(); 44 pushletClient.listen(this, Protocol.MODE_STREAM); 45 46 String subscriptionId = pushletClient.subscribe(SUBJECT); 48 pushletClient.unsubscribe(subscriptionId); 49 50 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 Map eventData = new HashMap (2); 60 int seqNr = 1; 61 while (true) { 62 try { 63 eventData.put("seqNr", "" + seqNr++); 65 eventData.put("time", "" + System.currentTimeMillis()); 66 67 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 80 public void onError(String message) { 81 p(message); 82 } 83 84 85 public void onAbort(Event theEvent) { 86 p("onAbort received: " + theEvent); 87 } 88 89 90 public void onData(Event theEvent) { 91 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 98 public void onHeartbeat(Event theEvent) { 99 p("onHeartbeat received: " + theEvent); 100 } 101 102 103 public void p(String s) { 104 System.out.println("[PushletPing] " + s); 105 } 106 107 108 public static void main(String args[]) { 109 for (int i = 0; i < 1; i++) { 110 if (args.length == 0) { 111 new PushletPingApplication("localhost", 8080).start(); 112 } else { 113 new PushletPingApplication(args[0], Integer.parseInt(args[1])).start(); 115 } 116 } 117 118 } 119 } 120 121 122 180 | Popular Tags |