pushlet server

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.core.Event;
7 import nl.justobjects.pushlet.core.EventPullSource;
8 import nl.justobjects.pushlet.core.SessionManager;
9 import nl.justobjects.pushlet.util.Rand;
10
11
12/**
13* Event sources for testing.
14*
15* @author Just van den Broecke - Just Objects &copy;
16* @version $Id: TestEventPullSources.java,v 1.10 2007/11/09 13:16:57 justb Exp $
17*/
18 public class TestEventPullSources {
19
20/**
21* Produces a fake temparature event.
22*/
23 static public class TemperatureEventPullSource extends EventPullSource {
24 String[] cities = {"amsterdam", null, "rotterdam", null,
25 "leeuwarden", null, "twente", null, "limburg", null};
26
27 public long getSleepTime() {
28 return Rand.randomLong(3000, 5000);
29}
30
31 public Event pullEvent() {
32 int cityNumber = Rand.randomInt(0, (cities.length) / 2 - 1);
33 int nextCityIndex = 2 * cityNumber;
34
35 Event event = Event.createDataEvent("/temperature");
36
37 event.setField("number", "" + cityNumber);
38 event.setField("city", cities[nextCityIndex]);
39 if (cities[nextCityIndex + 1] == null) {
40 cities[nextCityIndex + 1] = "" + Rand.randomInt(5, 10);
41}
42 int currentCityValue = new Integer(cities[nextCityIndex + 1]).intValue();
43 int newCityValue = currentCityValue + Rand.randomInt(-2, 2);
44
45 event.setField("value", "" + newCityValue);
46 return event;
47}
48}
49
50/**
51* Produces a ping event.
52*/
53 static public class PingEventPullSource extends EventPullSource {
54 public long getSleepTime() {
55 return 3000;
56}
57
58 public Event pullEvent() {
59
60 return Event.createDataEvent("/pushlet/ping");
61}
62}
63
64/**
65* Produces an event related to the JVM status.
66*/
67 static public class SystemStatusEventPullSource extends EventPullSource {
68 Runtime runtime = Runtime.getRuntime();
69
70 public long getSleepTime() {
71 return 4000;
72}
73
74 public Event pullEvent() {
75 Event event = Event.createDataEvent("/system/jvm");
76 event.setField("totalMemory", "" + runtime.totalMemory());
77 event.setField("freeMemory", "" + runtime.freeMemory());
78 event.setField("maxMemory", "" + runtime.maxMemory());
79 int activeCount = Thread.activeCount();
80 event.setField("threads", "" + activeCount);
81
82 return event;
83}
84}
85
86/**
87* Produces an event related to the Dispatcher.getInstance(). status.
88*/
89 static public class PushletStatusEventPullSource extends EventPullSource {
90
91 public long getSleepTime() {
92 return 5000;
93}
94
95 public Event pullEvent() {
96 Event event = Event.createDataEvent("/system/pushlet");
97// p(Dispatcher.getStatus());
98 event.setField("publisher", "" + SessionManager.getInstance().getStatus());
99 return event;
100}
101}
102
103
104/**
105* Produces events simulating stocks from the AEX.
106*/
107 static public class AEXStocksEventPullSource extends EventPullSource {
108
109 String[] stocks = {"abn amro", "26",
110 "aegon", "38",
111 "ahold", "34",
112 "akzo nobel", "51",
113 "asm lith h", "26",
114 "corus plc", "2",
115 "dsm", "40",
116 "elsevier", "14",
117 "fortis (nl)", "32",
118 "getronics", "6",
119 "gucci", "94",
120 "hagemeyer", "25",
121 "heineken", "61",
122 "ing c", "78",
123 "klm", "66",
124 "kon olie", "66",
125 "kpn", "13",
126 "numico c", "44",
127 "philips, kon", "38",
128 "tnt", "26",
129 "unilever c", "62",
130 "vendex kbb", "16",
131 "vnu", "49",
132 "wolt-kluw c", "25"};
133
134 public long getSleepTime() {
135 return Rand.randomLong(2000, 4000);
136}
137
138 public Event pullEvent() {
139 Event event = Event.createDataEvent("/stocks/aex");
140 int stockNumber = Rand.randomInt(0, (stocks.length) / 2 - 1);
141 int nextStockIndex = 2 * stockNumber;
142
143 event.setField("number", "" + stockNumber);
144 event.setField("name", stocks[nextStockIndex]);
145 if (stocks[nextStockIndex + 1] == null) {
146 stocks[nextStockIndex + 1] = "" + Rand.randomInt(50, 150);
147}
148 int currentStockValue = new Integer(stocks[nextStockIndex + 1]).intValue();
149 int newStockValue = currentStockValue + Rand.randomInt(-2, 2);
150
151 event.setField("rate", "" + newStockValue + "." + Rand.randomInt(0, 99));
152 return event;
153}
154
155}
156
157/**
158* Produces an URL event for automatic webpresentation.
159*/
160 static public class WebPresentationEventPullSource extends EventPullSource {
161 String slideRootDir = "http://www.justobjects.org/cowcatcher/browse/j2ee/slides/";
162 String[] slideURLs = {
163 "ejb/j2ee/ejbover/slide.0.0.html",
164 "ejb/j2ee/ejbover/slide.0.1.html",
165 "ejb/j2ee/ejbover/slide.0.2.html",
166 "ejb/j2ee/ejbover/slide.0.3.html",
167"ejb/j2ee/ejbover/slide.0.4.html"
168};
169
170 int nextSlideNumber = 0;
171
172 public long getSleepTime() {
173 return 5000;
174}
175
176 public Event pullEvent() {
177 Event event = Event.createDataEvent("/webpres/auto");
178 event.setField("url", slideRootDir + slideURLs[nextSlideNumber++]);
179 if (nextSlideNumber == slideURLs.length) {
180 nextSlideNumber = 0;
181}
182// Log.debug("Sending next slide url=" + event.getField("url"));
183 return event;
184}
185}
186
187/**
188* Produces an event related to the Dispatcher.getInstance(). status.
189*/
190 static public class TestEventPullSource extends EventPullSource {
191 private int number = 0;
192
193 public long getSleepTime() {
194 return 2000;
195}
196
197 public Event pullEvent() {
198 Event event = Event.createDataEvent("/system/test");
199// p(Dispatcher.getInstance()..getStatus());
200 event.setField("nr", "" + (number++));
201 event.setField("time", "" + System.currentTimeMillis());
202 return event;
203}
204
205}
206
207/**
208* Util: stderr print method.
209*/
210 public static void e(String s) {
211 System.out.println(s);
212}
213
214/**
215* Util: stdout print method.
216*/
217 public static void p(String s) {
218// System.out.println(s);
219}
220}
221
222/*
223* $Log: TestEventPullSources.java,v $
224* Revision 1.10 2007/11/09 13:16:57 justb
225* use Rand from util package (and and Rand.java to pushlet client jar
226*
227* Revision 1.9 2005/02/28 09:14:56 justb
228* sessmgr/dispatcher factory/singleton support
229*
230* Revision 1.8 2005/02/21 16:59:17 justb
231* SessionManager and session lease introduced
232*
233* Revision 1.7 2005/02/18 10:07:23 justb
234* many renamings of classes (make names compact)
235*
236* Revision 1.6 2005/02/18 09:54:15 justb
237* refactor: rename Publisher Dispatcher.getInstance(). and single Subscriber class
238*
239* Revision 1.5 2004/09/03 22:35:37 justb
240* Almost complete rewrite, just checking in now
241*
242* Revision 1.4 2003/12/03 21:16:58 justb
243* *** empty log message ***
244*
245* Revision 1.3 2003/08/15 08:37:41 justb
246* fix/add Copyright+LGPL file headers and footers
247*
248* Revision 1.2 2003/05/18 16:15:08 justb
249* support for XML encoded Events
250*
251* Revision 1.1.1.1 2002/09/24 21:02:33 justb
252* import to sourceforge
253*
254* Revision 1.1.1.1 2002/09/20 22:48:19 justb
255* import to SF
256*
257* Revision 1.1.1.1 2002/09/20 14:19:01 justb
258* first import into SF
259*
260* Revision 1.6 2002/07/29 10:17:22 just
261* no message
262*
263* Revision 1.5 2001/02/18 23:45:13 just
264* fixes for AEX
265*
266* Revision 1.4 2000/10/30 14:16:09 just
267* no message
268*
269* Revision 1.3 2000/08/31 12:49:50 just
270* added CVS comment tags for log and copyright
271*
272*
273*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值