001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.plugin.freeze;
028:
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Set;
032:
033: import org.cougaar.core.blackboard.Subscription;
034: import org.cougaar.core.component.ServiceBroker;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.service.UIDService;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /**
040: * This component gathers and integrates freeze information from agents
041: * in a society to determine the "completion" of the freeze operation.
042: * In most agents, it gathers the information and forwards the frozen
043: * status of the agent to another agent. This process continues
044: * through a hierarchy of such plugins until the plugin at the root of
045: * the tree is reached. When the root determines that ice has been
046: * acheived, that is reflected in the freeze control servlet
047: * <p>
048: * NOTE: This is part of the older mechanism for freezing the society. The
049: * current mechanism uses FreezeServlet located on every agent in the society,
050: * and depends on some external process to tell all agents to freeze. This older
051: * mechanism has not been removed so that people can continue to use a single servlet
052: * to freeze the entire society, but the FreezeServlet mechanism is preferred now.
053: */
054: public abstract class FreezeSourcePlugin extends FreezePlugin {
055: protected UIDService uidService;
056: private Subscription relaySubscription;
057: private FreezeRelaySource relay; // The relay we sent
058:
059: public void unload() {
060: if (uidService != null) {
061: ServiceBroker sb = getServiceBroker();
062: sb.releaseService(this , UIDService.class, uidService);
063: uidService = null;
064: }
065: super .unload();
066: }
067:
068: public void setupSubscriptions() {
069: super .setupSubscriptions();
070: ServiceBroker sb = getServiceBroker();
071: uidService = (UIDService) sb.getService(this , UIDService.class,
072: null);
073: relaySubscription = blackboard.subscribe(sourceRelayPredicate);
074: }
075:
076: public void execute() {
077: if (relaySubscription.hasChanged()) {
078: if (relay != null) {
079: setUnfrozenAgents(relay.getUnfrozenAgents());
080: }
081: }
082: }
083:
084: protected abstract Set getTargetNames();
085:
086: protected abstract void setUnfrozenAgents(Set unfrozenAgents);
087:
088: protected synchronized void freeze() {
089: if (relay != null)
090: return; // Already frozen
091: if (logger.isDebugEnabled())
092: logger.debug("freeze");
093: MessageAddress me = getAgentIdentifier();
094: Set names = getTargetNames();
095: Set targets = new HashSet(names.size());
096: for (Iterator i = names.iterator(); i.hasNext();) {
097: MessageAddress cid = MessageAddress
098: .getMessageAddress((String) i.next());
099: if (!cid.equals(me))
100: targets.add(cid);
101: }
102: relay = new FreezeRelaySource(targets);
103: relay.setUID(uidService.nextUID());
104: blackboard.publishAdd(relay);
105: relaySubscription = blackboard.subscribe(new UnaryPredicate() {
106: public boolean execute(Object o) {
107: return o == relay;
108: }
109: });
110: setUnfrozenAgents(names);
111: }
112:
113: protected synchronized void thaw() {
114: if (relay == null)
115: return; // not frozen
116: if (logger.isDebugEnabled())
117: logger.debug("thaw");
118: blackboard.publishRemove(relay);
119: blackboard.unsubscribe(relaySubscription);
120: relay = null;
121: }
122: }
|