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.mts.MessageAddress;
031: import org.cougaar.core.plugin.ServiceUserPlugin;
032: import org.cougaar.core.service.UIDService;
033:
034: /**
035: * A plugin to exercise the Relay mechanism by setting a
036: * RelayedOperatingMode that is targeted to the Provider agent. The
037: * target manifestation of the operating mode is a Sensor that is used
038: * by the adaptivity engine to select plays.
039: **/
040: public class CPURemoteTestPlugin extends ServiceUserPlugin {
041: /** The name of the OperatingMode and Condition **/
042: public static final String CPU_CONDITION_NAME = "CPURemoteTestPlugin.CPU";
043:
044: /** A range from 0.0 thru 1.0 **/
045: private static final OMCRange[] CPU_RANGES = { new OMCRange(0.0,
046: 1.0) };
047:
048: /** A value list with just one range from 0.0 thru 1.0 **/
049: private static final OMCRangeList CPU_VALUES = new OMCRangeList(
050: CPU_RANGES);
051:
052: private UIDService uidService;
053:
054: private InterAgentCondition cpu;
055:
056: private static final Double[] cpuValues = { new Double(1.0),
057: new Double(1.0), new Double(1.0), new Double(1.0),
058: new Double(1.0), new Double(1.0), new Double(1.0),
059: new Double(1.0), new Double(1.0), new Double(1.0),
060: new Double(0.1), new Double(0.1), new Double(0.1),
061: new Double(0.1), new Double(0.1), new Double(0.1),
062: new Double(0.1), new Double(0.1), new Double(0.1),
063: new Double(0.1), };
064:
065: private int cpuStep = 0;
066:
067: private static final Class[] requiredServices = { UIDService.class };
068:
069: public CPURemoteTestPlugin() {
070: super (requiredServices);
071: }
072:
073: public void setupSubscriptions() {
074: cpu = new InterAgentCondition(CPU_CONDITION_NAME, CPU_VALUES,
075: cpuValues[0]);
076: cpu.setTarget(MessageAddress.getMessageAddress("Provider"));
077: getBlackboardService().publishAdd(cpu);
078: if (haveServices()) {
079: uidService.registerUniqueObject(cpu);
080: setCPUCondition();
081: }
082: }
083:
084: private boolean haveServices() {
085: if (uidService != null)
086: return true;
087: if (acquireServices()) {
088: ServiceBroker sb = getServiceBroker();
089: uidService = (UIDService) sb.getService(this ,
090: UIDService.class, null);
091: return true;
092: }
093: return false;
094: }
095:
096: public void execute() {
097: if (timerExpired()) {
098: if (haveServices()) {
099: cancelTimer();
100: setCPUCondition();
101: }
102: }
103: }
104:
105: private void setCPUCondition() {
106: if (logger.isInfoEnabled())
107: logger.info("Setting cpu = " + cpuValues[cpuStep]);
108: cpu.setValue(cpuValues[cpuStep]);
109: getBlackboardService().publishChange(cpu);
110: cpuStep++;
111: if (cpuStep == cpuValues.length)
112: cpuStep = 0;
113: resetTimer(60000);
114: }
115: }
|