001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.logistics.plugin.trans.tools;
027:
028: import java.util.Iterator;
029:
030: import org.cougaar.core.adaptivity.*;
031:
032: import org.cougaar.core.component.BindingSite;
033: import org.cougaar.core.component.Component;
034: import org.cougaar.core.component.Service;
035: import org.cougaar.core.component.ServiceBroker;
036: import org.cougaar.core.component.ServiceProvider;
037:
038: import org.cougaar.core.persist.NotPersistable;
039:
040: import org.cougaar.core.plugin.ComponentPlugin;
041:
042: import org.cougaar.core.service.ConditionService;
043:
044: import org.cougaar.util.GenericStateModelAdapter;
045:
046: public class CPUTestPlugin extends ServiceUserPluginBase {
047: public static final String CPU_CONDITION_NAME = "CPUTestPlugin.CPU";
048:
049: private static final OMCRange[] CPU_RANGES = { new CPURange(0.0,
050: 1.0) };
051:
052: protected static class CPURange extends OMCRange {
053: public CPURange(double a, double b) {
054: super (a, b);
055: }
056: }
057:
058: private static final OMCRangeList CPU_VALUES = new OMCRangeList(
059: CPU_RANGES);
060:
061: private ConditionService conditionService;
062:
063: private static final Double[] cpuValues = { new Double(0.4),
064: new Double(0.7), new Double(0.99) };
065:
066: private int cpuStep = 0;
067:
068: /**
069: * Private inner class precludes use by others to set our
070: * measurement. Others can only reference the base Condition
071: * class which has no setter method.
072: **/
073: private static class CPUTestCondition extends SensorCondition
074: implements NotPersistable {
075: public CPUTestCondition(String name,
076: OMCRangeList allowedValues, Comparable value) {
077: super (name, allowedValues, value);
078: }
079:
080: public void setValue(Comparable newValue) {
081: super .setValue(newValue);
082: }
083: }
084:
085: private static final Class[] requiredServices = { ConditionService.class };
086:
087: public CPUTestPlugin() {
088: super (requiredServices);
089: }
090:
091: public void setupSubscriptions() {
092: if (logger.isDebugEnabled())
093: logger.debug("setupSubscriptions called.");
094: CPUTestCondition cpu = new CPUTestCondition(CPU_CONDITION_NAME,
095: CPU_VALUES, cpuValues[0]);
096: getBlackboardService().publishAdd(cpu);
097:
098: if (logger.isInfoEnabled() && getParameters().isEmpty())
099: logger.info("plugin got NO parametes.");
100:
101: for (Iterator iter = getParameters().iterator(); iter.hasNext();) {
102: String param = (String) iter.next();
103: if (param.equals("alwaysHigh")) {
104: alwaysHigh = true;
105: break;
106: }
107: }
108:
109: if (haveServices())
110: setCPUCondition();
111: }
112:
113: private boolean haveServices() {
114: if (conditionService != null)
115: return true;
116: if (acquireServices()) {
117: if (logger.isDebugEnabled())
118: logger.debug(".haveServices - acquiredServices.");
119: ServiceBroker sb = getServiceBroker();
120: conditionService = (ConditionService) sb.getService(this ,
121: ConditionService.class, null);
122: return true;
123: } else if (logger.isDebugEnabled())
124: logger.debug(".haveServices - did NOT acquire services.");
125: return false;
126: }
127:
128: public void execute() {
129: if (timerExpired()) {
130: if (haveServices()) {
131: cancelTimer();
132: setCPUCondition();
133: } else if (logger.isDebugEnabled())
134: logger.debug(".execute - not all services ready yet.");
135: }
136: }
137:
138: private void setCPUCondition() {
139: CPUTestCondition cpu = (CPUTestCondition) conditionService
140: .getConditionByName(CPU_CONDITION_NAME);
141: if (cpu != null) {
142:
143: if (alwaysHigh) {
144: cpu.setValue(cpuValues[cpuValues.length - 1]);
145: } else {
146: cpu.setValue(cpuValues[cpuStep]);
147: }
148:
149: if (logger.isInfoEnabled())
150: logger.info("Setting cpu = " + cpu.getValue());
151:
152: getBlackboardService().publishChange(cpu);
153: cpuStep++;
154: if (cpuStep == cpuValues.length)
155: cpuStep = 0;
156: }
157: startTimer(10000);
158: }
159:
160: protected boolean alwaysHigh = false;
161: }
|