001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.lib.aggagent.test;
027:
028: import java.util.Iterator;
029: import java.util.Vector;
030:
031: import org.cougaar.core.agent.service.alarm.Alarm;
032: import org.cougaar.core.plugin.ComponentPlugin;
033:
034: public class EffortWaster extends ComponentPlugin {
035: // Parameters of performance for the EffortWaster Plugin. The variables are
036: // initialized with their default values.
037: private int maxCycles = 5;
038: private long pauseInterval = 5000;
039: private long initialWait = 10000;
040:
041: private Vector cycles = new Vector();
042: private int nextCycle = 1;
043: private int initialValue = 1;
044:
045: public void setMaxCycles(String v) {
046: try {
047: maxCycles = Integer.parseInt(v);
048: } catch (NumberFormatException nfe) {
049: System.out
050: .println("EffortWaster::setMaxCycles: bad number format \""
051: + v + "\"");
052: }
053: }
054:
055: public void setInitialValue(String v) {
056: try {
057: initialValue = Integer.parseInt(v);
058: } catch (NumberFormatException nfe) {
059: System.out
060: .println("EffortWaster::setInitialValue: bad number format \""
061: + v + "\"");
062: }
063: }
064:
065: public void setPauseInterval(String v) {
066: try {
067: pauseInterval = Integer.parseInt(v);
068: } catch (NumberFormatException nfe) {
069: System.out
070: .println("EffortWaster::setPauseInterval: bad number format \""
071: + v + "\"");
072: }
073: }
074:
075: public void setInitialWait(String v) {
076: try {
077: initialWait = Long.parseLong(v);
078: } catch (NumberFormatException nfe) {
079: System.out
080: .println("EffortWaster::setInitialWait: bad number format \""
081: + v + "\"");
082: }
083: }
084:
085: public void setupSubscriptions() {
086: Pair p = new Pair();
087: if (getParameters() != null) {
088: for (Iterator i = getParameters().iterator(); i.hasNext();) {
089: p.load(i.next());
090: if ("maxCycles".equals(p.name))
091: setMaxCycles(p.value);
092: else if ("pauseInterval".equals(p.name))
093: setPauseInterval(p.value);
094: else if ("initialValue".equals(p.name))
095: setInitialValue(p.value);
096: else
097: System.out
098: .println("EffortWaster: unrecognized parameter \""
099: + p.name + "\"");
100: }
101: }
102:
103: wakeAfter(initialWait);
104: }
105:
106: private static class Pair {
107: public String name = null;
108: public String value = null;
109:
110: public void load(Object o) {
111: name = null;
112: value = null;
113: if (o != null) {
114: String s = o.toString();
115: int i = s.indexOf('=');
116: if (i == -1) {
117: name = s;
118: } else {
119: name = s.substring(0, i);
120: value = s.substring(i + 1);
121: }
122: }
123: }
124: }
125:
126: public void execute() {
127: if (!wasAwakened()) {
128: return;
129: }
130:
131: Vector additions = new Vector();
132: if (cycles.size() < maxCycles) {
133: NumberCycle nc = new NumberCycle(nextCycle++, initialValue);
134: additions.add(nc);
135: getBlackboardService().publishAdd(nc);
136: }
137:
138: Vector removals = new Vector();
139: for (Iterator i = cycles.iterator(); i.hasNext();) {
140: NumberCycle nc = (NumberCycle) i.next();
141: if (nc.increment()) {
142: getBlackboardService().publishChange(nc);
143: } else {
144: getBlackboardService().publishRemove(nc);
145: removals.add(nc);
146: }
147: }
148: cycles.removeAll(removals);
149: cycles.addAll(additions);
150: wakeAfter(pauseInterval);
151: }
152:
153: private void wakeAfter(long pauseInterval) {
154: long now = System.currentTimeMillis();
155: long wakeAt = now + pauseInterval;
156: Alarm alarm = new PluginAlarm(wakeAt);
157: getAlarmService().addRealTimeAlarm(alarm);
158: }
159:
160: public class PluginAlarm implements Alarm {
161: private long expiresAt;
162: private boolean expired = false;
163:
164: public PluginAlarm(long expirationTime) {
165: expiresAt = expirationTime;
166: }
167:
168: public long getExpirationTime() {
169: return expiresAt;
170: }
171:
172: public synchronized void expire() {
173: if (!expired) {
174: expired = true;
175: {
176: org.cougaar.core.service.BlackboardService bbs = getBlackboardService();
177: if (bbs != null)
178: bbs.signalClientActivity();
179: }
180: }
181: }
182:
183: public boolean hasExpired() {
184: return expired;
185: }
186:
187: public synchronized boolean cancel() {
188: boolean was = expired;
189: expired = true;
190: return was;
191: }
192:
193: public String toString() {
194: return "<PluginAlarm " + expiresAt
195: + (expired ? "(Expired) " : " ") + "for "
196: + EffortWaster.this .toString() + ">";
197: }
198: }
199:
200: }
|