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:
027: package org.cougaar.core.adaptivity;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.persist.NotPersistable;
031: import org.cougaar.core.plugin.ServiceUserPlugin;
032: import org.cougaar.core.service.ConditionService;
033:
034: public class CPUTestPlugin extends ServiceUserPlugin {
035: public static final String CPU_CONDITION_NAME = "CPUTestPlugin.CPU";
036:
037: private static final OMCRange[] CPU_RANGES = { new OMCRange(0.0,
038: 1.0) };
039:
040: private static final OMCRangeList CPU_VALUES = new OMCRangeList(
041: CPU_RANGES);
042:
043: private ConditionService conditionService;
044:
045: private static final Double[] cpuValues = { new Double(0.0),
046: new Double(0.1), new Double(0.2), new Double(0.3),
047: new Double(0.4), new Double(0.5), new Double(0.6),
048: new Double(0.7), new Double(0.8), new Double(0.9),
049: new Double(0.8), new Double(0.7), new Double(0.6),
050: new Double(0.7), new Double(0.8), new Double(0.9),
051: new Double(0.8), new Double(0.7), new Double(0.6),
052: new Double(0.5), new Double(0.4), new Double(0.3),
053: new Double(0.2), new Double(0.1), };
054:
055: private int cpuStep = 0;
056:
057: /**
058: * Private inner class precludes use by others to set our
059: * measurement. Others can only reference the base Condition
060: * class which has no setter method.
061: **/
062: private static class CPUTestCondition extends SensorCondition
063: implements NotPersistable {
064: public CPUTestCondition(String name,
065: OMCRangeList allowedValues, Comparable value) {
066: super (name, allowedValues, value);
067: }
068:
069: public void setValue(Comparable newValue) {
070: super .setValue(newValue);
071: }
072: }
073:
074: private static final Class[] requiredServices = { ConditionService.class };
075:
076: public CPUTestPlugin() {
077: super (requiredServices);
078: }
079:
080: public void setupSubscriptions() {
081: CPUTestCondition cpu = new CPUTestCondition(CPU_CONDITION_NAME,
082: CPU_VALUES, cpuValues[0]);
083: getBlackboardService().publishAdd(cpu);
084: if (haveServices())
085: setCPUCondition();
086: }
087:
088: private boolean haveServices() {
089: if (conditionService != null)
090: return true;
091: if (acquireServices()) {
092: ServiceBroker sb = getServiceBroker();
093: conditionService = (ConditionService) sb.getService(this ,
094: ConditionService.class, null);
095: return true;
096: }
097: return false;
098: }
099:
100: public void execute() {
101: if (timerExpired()) {
102: if (haveServices()) {
103: cancelTimer();
104: setCPUCondition();
105: }
106: }
107: }
108:
109: private void setCPUCondition() {
110: CPUTestCondition cpu = (CPUTestCondition) conditionService
111: .getConditionByName(CPU_CONDITION_NAME);
112: if (cpu != null) {
113: if (logger.isInfoEnabled())
114: logger.info("Setting cpu = " + cpuValues[cpuStep]);
115: cpu.setValue(cpuValues[cpuStep]);
116: getBlackboardService().publishChange(cpu);
117: cpuStep++;
118: if (cpuStep == cpuValues.length)
119: cpuStep = 0;
120: }
121: resetTimer(5000);
122: }
123: }
|