001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 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.agent.service.community;
028:
029: import java.util.Collection;
030: import org.cougaar.core.blackboard.IncrementalSubscription;
031: import org.cougaar.core.plugin.ComponentPlugin;
032: import org.cougaar.core.relay.Relay;
033: import org.cougaar.core.relay.SimpleRelay;
034: import org.cougaar.core.relay.SimpleRelaySource;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.core.service.UIDService;
037: import org.cougaar.multicast.AttributeBasedAddress;
038: import org.cougaar.util.Arguments;
039: import org.cougaar.util.UnaryPredicate;
040:
041: /**
042: * This plugin sends a simple ABA Relay to a target community.
043: */
044: public class TestABA extends ComponentPlugin {
045:
046: private Arguments args = Arguments.EMPTY_INSTANCE;
047:
048: private LoggingService log;
049: private UIDService uids;
050:
051: private IncrementalSubscription sub;
052:
053: public void setParameter(Object o) {
054: args = new Arguments(o);
055: }
056:
057: public void setLoggingService(LoggingService log) {
058: this .log = log;
059: }
060:
061: public void setUIDService(UIDService uids) {
062: this .uids = uids;
063: }
064:
065: protected void setupSubscriptions() {
066: sub = (IncrementalSubscription) blackboard
067: .subscribe(new UnaryPredicate() {
068: public boolean execute(Object o) {
069: return (o instanceof Relay);
070: }
071: });
072:
073: String community = args.getString("target");
074: if (community != null) {
075: if (log.isInfoEnabled()) {
076: log.info("Sending ABA Relay to " + community);
077: }
078: SimpleRelay sr = new SimpleRelaySource(uids.nextUID(),
079: agentId, AttributeBasedAddress
080: .getAttributeBasedAddress(community,
081: "Role", "Member"), "(Test from \""
082: + agentId + "\" to \"" + community + "\")");
083: blackboard.publishAdd(sr);
084: }
085: }
086:
087: protected void execute() {
088: if (log.isInfoEnabled() && sub.hasChanged()) {
089: for (int i = 0; i < 3; i++) {
090: Collection c = (i == 0 ? sub.getAddedCollection()
091: : i == 1 ? sub.getChangedCollection() : sub
092: .getRemovedCollection());
093: if (c.isEmpty())
094: continue;
095: log
096: .info("Observed "
097: + (i == 0 ? "add" : i == 1 ? "change"
098: : "remove") + "[" + c.size()
099: + "]:" + c);
100: }
101: }
102: }
103: }
|