001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.getahead.dwrdemo.gidemo.bus;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Random;
022:
023: import javax.servlet.ServletContext;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.directwebremoting.HubFactory;
028: import org.directwebremoting.ScriptSession;
029: import org.directwebremoting.ServerContext;
030: import org.directwebremoting.ServerContextFactory;
031: import org.directwebremoting.WebContextFactory;
032:
033: /**
034: *
035: * @author Joe Walker [joe at getahead dot ltd dot uk]
036: */
037: public class Simulator implements Runnable {
038: /**
039: *
040: */
041: public Simulator() {
042: ServletContext servletContext = WebContextFactory.get()
043: .getServletContext();
044: serverContext = ServerContextFactory.get(servletContext);
045: setActive(true);
046: }
047:
048: /* (non-Javadoc)
049: * @see java.lang.Runnable#run()
050: */
051: @SuppressWarnings("unchecked")
052: public void run() {
053: try {
054: log.info("Simulator: Starting server-side thread");
055:
056: while (active) {
057: Collection<ScriptSession> sessions = serverContext
058: .getScriptSessionsByPage("/dwr-gi/dwr_oa_gi.html");
059:
060: objectHolder.put("obj", getRandomObject());
061: HubFactory.get().publish("cdf.object", objectHolder);
062:
063: log.info("Simulator: Sent message to "
064: + sessions.size() + " pages");
065:
066: //int timeToSleep = 1000 / publishesPerSecond;
067: int timeToSleep = random.nextInt(500);
068: Thread.sleep(timeToSleep);
069: }
070:
071: log.info("Simulator: Stopping server-side thread");
072: } catch (InterruptedException ex) {
073: ex.printStackTrace();
074: }
075: }
076:
077: /**
078: *
079: */
080: private Map<String, String> getRandomObject() {
081: Map<String, String> reply = new HashMap<String, String>();
082:
083: reply.put("jsxid", "" + random.nextInt(10));
084:
085: for (int i = 1; i <= attributesPerRecord; i++) {
086: reply.put("C" + i, getRandomNumberString());
087: }
088:
089: return reply;
090: }
091:
092: /**
093: *
094: */
095: private String getRandomNumberString() {
096: float base = random.nextInt(100000);
097: base = base / 100;
098: return String.valueOf(base);
099: }
100:
101: public int getAttributesPerRecord() {
102: return attributesPerRecord;
103: }
104:
105: public void setAttributesPerRecord(int attributesPreRecord) {
106: this .attributesPerRecord = attributesPreRecord;
107: }
108:
109: public int getPublishesPerSecond() {
110: return publishesPerSecond;
111: }
112:
113: public void setPublishesPerSecond(int publishesPerSecond) {
114: this .publishesPerSecond = publishesPerSecond;
115: }
116:
117: /**
118: *
119: */
120: public synchronized void setActive(boolean active) {
121: if (active == this .active) {
122: return;
123: }
124:
125: this .active = active;
126: log.warn("Simulator: Setting active state to: " + active);
127:
128: if (this .active) {
129: new Thread(this ).start();
130: }
131: }
132:
133: public boolean isActive() {
134: return active;
135: }
136:
137: private Random random = new Random();
138:
139: private int attributesPerRecord = 5;
140:
141: private int publishesPerSecond = 1;
142:
143: private ServerContext serverContext;
144:
145: private boolean active = false;
146:
147: private Map<String, Map<String, String>> objectHolder = new HashMap<String, Map<String, String>>();
148:
149: /**
150: * The log stream
151: */
152: private static final Log log = LogFactory.getLog(Simulator.class);
153: }
|