1 4 package nl.justobjects.pushlet.test; 5 6 import nl.justobjects.pushlet.core.Event; 7 import nl.justobjects.pushlet.core.EventPullSource; 8 import nl.justobjects.pushlet.core.SessionManager; 9 10 import java.util.Random ; 11 12 18 public class TestEventPullSources { 19 20 21 static public class TemperatureEventPullSource extends EventPullSource { 22 String [] cities = {"amsterdam", null, "rotterdam", null, 23 "leeuwarden", null, "twente", null, "limburg", null}; 24 25 public long getSleepTime() { 26 return randomLong(3000, 5000); 27 } 28 29 public Event pullEvent() { 30 int cityNumber = randomInt(0, (cities.length) / 2 - 1); 31 int nextCityIndex = 2 * cityNumber; 32 33 Event event = Event.createDataEvent("/temperature"); 34 35 event.setField("number", "" + cityNumber); 36 event.setField("city", cities[nextCityIndex]); 37 if (cities[nextCityIndex + 1] == null) { 38 cities[nextCityIndex + 1] = "" + randomInt(5, 10); 39 } 40 int currentCityValue = new Integer (cities[nextCityIndex + 1]).intValue(); 41 int newCityValue = currentCityValue + randomInt(-2, 2); 42 43 event.setField("value", "" + newCityValue); 44 return event; 45 } 46 } 47 48 49 static public class PingEventPullSource extends EventPullSource { 50 public long getSleepTime() { 51 return 3000; 52 } 53 54 public Event pullEvent() { 55 56 return Event.createDataEvent("/pushlet/ping"); 57 } 58 } 59 60 61 static public class SystemStatusEventPullSource extends EventPullSource { 62 Runtime runtime = Runtime.getRuntime(); 63 64 public long getSleepTime() { 65 return 4000; 66 } 67 68 public Event pullEvent() { 69 Event event = Event.createDataEvent("/system/jvm"); 70 event.setField("totalMemory", "" + runtime.totalMemory()); 71 event.setField("freeMemory", "" + runtime.freeMemory()); 72 event.setField("maxMemory", "" + runtime.maxMemory()); 73 int activeCount = Thread.activeCount(); 74 event.setField("threads", "" + activeCount); 75 76 return event; 77 } 78 } 79 80 81 static public class PushletStatusEventPullSource extends EventPullSource { 82 83 public long getSleepTime() { 84 return 5000; 85 } 86 87 public Event pullEvent() { 88 Event event = Event.createDataEvent("/system/pushlet"); 89 event.setField("publisher", "" + SessionManager.getInstance().getStatus()); 91 return event; 92 } 93 } 94 95 96 97 static public class AEXStocksEventPullSource extends EventPullSource { 98 99 String [] stocks = {"abn amro", "26", 100 "aegon", "38", 101 "ahold", "34", 102 "akzo nobel", "51", 103 "asm lith h", "26", 104 "corus plc", "2", 105 "dsm", "40", 106 "elsevier", "14", 107 "fortis (nl)", "32", 108 "getronics", "6", 109 "gucci", "94", 110 "hagemeyer", "25", 111 "heineken", "61", 112 "ing c", "78", 113 "klm", "66", 114 "kon olie", "66", 115 "kpn", "13", 116 "numico c", "44", 117 "philips, kon", "38", 118 "tnt", "26", 119 "unilever c", "62", 120 "vendex kbb", "16", 121 "vnu", "49", 122 "wolt-kluw c", "25"}; 123 124 public long getSleepTime() { 125 return randomLong(2000, 4000); 126 } 127 128 public Event pullEvent() { 129 Event event = Event.createDataEvent("/stocks/aex"); 130 int stockNumber = randomInt(0, (stocks.length) / 2 - 1); 131 int nextStockIndex = 2 * stockNumber; 132 133 event.setField("number", "" + stockNumber); 134 event.setField("name", stocks[nextStockIndex]); 135 if (stocks[nextStockIndex + 1] == null) { 136 stocks[nextStockIndex + 1] = "" + randomInt(50, 150); 137 } 138 int currentStockValue = new Integer (stocks[nextStockIndex + 1]).intValue(); 139 int newStockValue = currentStockValue + randomInt(-2, 2); 140 141 event.setField("rate", "" + newStockValue + "." + randomInt(0, 99)); 142 return event; 143 } 144 145 } 146 147 148 static public class WebPresentationEventPullSource extends EventPullSource { 149 String slideRootDir = "http://www.justobjects.org/cowcatcher/browse/j2ee/slides/"; 150 String [] slideURLs = { 151 "ejb/j2ee/ejbover/slide.0.0.html", 152 "ejb/j2ee/ejbover/slide.0.1.html", 153 "ejb/j2ee/ejbover/slide.0.2.html", 154 "ejb/j2ee/ejbover/slide.0.3.html", 155 "ejb/j2ee/ejbover/slide.0.4.html" 156 }; 157 158 int nextSlideNumber = 0; 159 160 public long getSleepTime() { 161 return 5000; 162 } 163 164 public Event pullEvent() { 165 Event event = Event.createDataEvent("/webpres/auto"); 166 event.setField("url", slideRootDir + slideURLs[nextSlideNumber++]); 167 if (nextSlideNumber == slideURLs.length) { 168 nextSlideNumber = 0; 169 } 170 return event; 172 } 173 } 174 175 176 static public class TestEventPullSource extends EventPullSource { 177 private int number = 0; 178 179 public long getSleepTime() { 180 return 2000; 181 } 182 183 public Event pullEvent() { 184 Event event = Event.createDataEvent("/system/test"); 185 event.setField("nr", "" + (number++)); 187 event.setField("time", "" + System.currentTimeMillis()); 188 return event; 189 } 190 191 } 192 193 194 private static Random random = new Random (); 195 196 197 public static int randomInt(int min, int max) { 198 int nextRandom = random.nextInt(); 199 return min + (nextRandom < 0 ? -nextRandom : nextRandom) % (max - min + 1); 200 } 201 202 203 public static long randomLong(long min, long max) { 204 long nextRandom = random.nextLong(); 205 return min + (nextRandom < 0 ? -nextRandom : nextRandom) % (max - min + 1); 206 } 207 208 209 public static void e(String s) { 210 System.out.println(s); 211 } 212 213 214 public static void p(String s) { 215 } 217 } 218 219 268 | Popular Tags |