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 */
18public 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;
00        }
01    }
02
03
04    /**
05     * Produces events simulating stocks from the AEX.
06     */
07    static public class AEXStocksEventPullSource extends EventPullSource {
08
09        String[] stocks = {"abn amro", "26",
10                "aegon", "38",
11                "ahold", "34",
12                "akzo nobel", "51",
13                "asm lith h", "26",
14                "corus plc", "2",
15                "dsm", "40",
16                "elsevier", "14",
17                "fortis (nl)", "32",
18                "getronics", "6",
19                "gucci", "94",
20                "hagemeyer", "25",
21                "heineken", "61",
22                "ing c", "78",
23                "klm", "66",
24                "kon olie", "66",
25                "kpn", "13",
26                "numico c", "44",
27                "philips, kon", "38",
28                "tnt", "26",
29                "unilever c", "62",
30                "vendex kbb", "16",
31                "vnu", "49",
32                "wolt-kluw c", "25"};
33
34        public long getSleepTime() {
35            return Rand.randomLong(2000, 4000);
36        }
37
38        public Event pullEvent() {
39            Event event = Event.createDataEvent("/stocks/aex");
40            int stockNumber = Rand.randomInt(0, (stocks.length) / 2 - 1);
41            int nextStockIndex = 2 * stockNumber;
42
43            event.setField("number", "" + stockNumber);
44            event.setField("name", stocks[nextStockIndex]);
45            if (stocks[nextStockIndex + 1] == null) {
46                stocks[nextStockIndex + 1] = "" + Rand.randomInt(50, 150);
47            }
48            int currentStockValue = new Integer(stocks[nextStockIndex + 1]).intValue();
49            int newStockValue = currentStockValue + Rand.randomInt(-2, 2);
50
51            event.setField("rate", "" + newStockValue + "." + Rand.randomInt(0, 99));
52            return event;
53        }
54
55    }
56
57    /**
58     * Produces an URL event for automatic webpresentation.
59     */
60    static public class WebPresentationEventPullSource extends EventPullSource {
61        String slideRootDir = "http://www.justobjects.org/cowcatcher/browse/j2ee/slides/";
62        String[] slideURLs = {
63                "ejb/j2ee/ejbover/slide.0.0.html",
64                "ejb/j2ee/ejbover/slide.0.1.html",
65                "ejb/j2ee/ejbover/slide.0.2.html",
66                "ejb/j2ee/ejbover/slide.0.3.html",
67                "ejb/j2ee/ejbover/slide.0.4.html"
68        };
69
70        int nextSlideNumber = 0;
71
72        public long getSleepTime() {
73            return 5000;
74        }
75
76        public Event pullEvent() {
77            Event event = Event.createDataEvent("/webpres/auto");
78            event.setField("url", slideRootDir + slideURLs[nextSlideNumber++]);
79            if (nextSlideNumber == slideURLs.length) {
80                nextSlideNumber = 0;
81            }
82            // Log.debug("Sending next slide url=" + event.getField("url"));
83            return event;
84        }
85    }
86
87    /**
88     * Produces an event related to the Dispatcher.getInstance(). status.
89     */
90    static public class TestEventPullSource extends EventPullSource {
91        private int number = 0;
92
93        public long getSleepTime() {
94            return 2000;
95        }
96
97        public Event pullEvent() {
98            Event event = Event.createDataEvent("/system/test");
99            // p(Dispatcher.getInstance()..getStatus());
00            event.setField("nr", "" + (number++));
01            event.setField("time", "" + System.currentTimeMillis());
02            return event;
03        }
04
05    }
06
07    /**
08     * Util: stderr print method.
09     */
10    public static void e(String s) {
11        System.out.println(s);
12    }
13
14    /**
15     * Util: stdout print method.
16     */
17    public static void p(String s) {
18        // System.out.println(s);
19    }
20}
21
22/*
23 * $Log: TestEventPullSources.java,v $
24 * Revision 1.10  2007/11/09 13:16:57  justb
25 * use Rand from util package (and and Rand.java to pushlet client jar
26 *
27 * Revision 1.9  2005/02/28 09:14:56  justb
28 * sessmgr/dispatcher factory/singleton support
29 *
30 * Revision 1.8  2005/02/21 16:59:17  justb
31 * SessionManager and session lease introduced
32 *
33 * Revision 1.7  2005/02/18 10:07:23  justb
34 * many renamings of classes (make names compact)
35 *
36 * Revision 1.6  2005/02/18 09:54:15  justb
37 * refactor: rename Publisher Dispatcher.getInstance(). and single Subscriber class
38 *
39 * Revision 1.5  2004/09/03 22:35:37  justb
40 * Almost complete rewrite, just checking in now
41 *
42 * Revision 1.4  2003/12/03 21:16:58  justb
43 * *** empty log message ***
44 *
45 * Revision 1.3  2003/08/15 08:37:41  justb
46 * fix/add Copyright+LGPL file headers and footers
47 *
48 * Revision 1.2  2003/05/18 16:15:08  justb
49 * support for XML encoded Events
50 *
51 * Revision 1.1.1.1  2002/09/24 21:02:33  justb
52 * import to sourceforge
53 *
54 * Revision 1.1.1.1  2002/09/20 22:48:19  justb
55 * import to SF
56 *
57 * Revision 1.1.1.1  2002/09/20 14:19:01  justb
58 * first import into SF
59 *
60 * Revision 1.6  2002/07/29 10:17:22  just
61 * no message
62 *
63 * Revision 1.5  2001/02/18 23:45:13  just
64 * fixes for AEX
65 *
66 * Revision 1.4  2000/10/30 14:16:09  just
67 * no message
68 *
69 * Revision 1.3  2000/08/31 12:49:50  just
70 * added CVS comment tags for log and copyright
71 *
72 *
73 */
74