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:
027: package org.cougaar.core.node;
028:
029: import java.util.Collections;
030: import java.util.HashSet;
031: import java.util.Set;
032: import org.cougaar.core.component.Component;
033: import org.cougaar.core.component.ServiceBroker;
034: import org.cougaar.core.component.ServiceProvider;
035: import org.cougaar.core.component.ServiceRevokedListener;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.service.AgentIdentificationService;
038: import org.cougaar.core.service.LoggingService;
039: import org.cougaar.util.GenericStateModelAdapter;
040:
041: /**
042: * This component advertises the {@link NodeBusyService} to all
043: * agents.
044: */
045: public final class NodeBusyComponent extends GenericStateModelAdapter
046: implements Component {
047:
048: private ServiceBroker sb;
049: private ServiceBroker rootsb;
050:
051: private LoggingService log;
052:
053: private ServiceProvider nbsp;
054:
055: public void setServiceBroker(ServiceBroker sb) {
056: this .sb = sb;
057: }
058:
059: public void load() {
060: super .load();
061:
062: log = (LoggingService) sb.getService(this ,
063: LoggingService.class, null);
064:
065: NodeControlService ncs = (NodeControlService) sb.getService(
066: this , NodeControlService.class, null);
067: if (ncs == null) {
068: throw new RuntimeException(
069: "Unable to obtain NodeControlService");
070: }
071: rootsb = ncs.getRootServiceBroker();
072: sb.releaseService(this , NodeControlService.class, ncs);
073:
074: nbsp = new NodeBusyServiceProvider(log);
075: rootsb.addService(NodeBusyService.class, nbsp);
076: }
077:
078: public void unload() {
079: super .unload();
080:
081: rootsb.revokeService(NodeBusyService.class, nbsp);
082: nbsp = null;
083: }
084:
085: private static class NodeBusyServiceProvider implements
086: ServiceProvider {
087: private final LoggingService log;
088: private final Set busyAgents = new HashSet();
089:
090: public NodeBusyServiceProvider(LoggingService log) {
091: this .log = log;
092: }
093:
094: public Object getService(ServiceBroker xsb, Object requestor,
095: Class serviceClass) {
096: if (serviceClass != NodeBusyService.class) {
097: throw new IllegalArgumentException(
098: "Can only provide NodeBusyService!");
099: }
100: return new NodeBusyService() {
101: MessageAddress me = null;
102:
103: public void setAgentIdentificationService(
104: AgentIdentificationService ais) {
105: me = ais.getMessageAddress();
106: }
107:
108: public void setAgentBusy(boolean busy) {
109: if (me == null) {
110: throw new RuntimeException(
111: "AgentIdentificationService has not been set");
112: }
113: if (log.isDebugEnabled()) {
114: log.debug("setAgentBusy(" + me + ", " + busy
115: + ")");
116: }
117: if (busy) {
118: busyAgents.add(me);
119: } else {
120: busyAgents.remove(me);
121: }
122: }
123:
124: public boolean isAgentBusy(MessageAddress agent) {
125: return busyAgents.contains(agent);
126: }
127:
128: public Set getBusyAgents() {
129: return Collections.unmodifiableSet(busyAgents);
130: }
131: };
132: }
133:
134: public void releaseService(ServiceBroker xsb, Object requestor,
135: Class serviceClass, Object service) {
136: }
137: }
138: }
|