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.plugin;
028:
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.Iterator;
032: import java.util.Set;
033:
034: import org.cougaar.core.blackboard.IncrementalSubscription;
035: import org.cougaar.core.logging.LoggingServiceWithPrefix;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.relay.Relay;
038: import org.cougaar.core.service.LoggingService;
039: import org.cougaar.core.service.UIDService;
040: import org.cougaar.core.util.SimpleUniqueObject;
041: import org.cougaar.core.util.UID;
042: import org.cougaar.multicast.AttributeBasedAddress;
043: import org.cougaar.util.UnaryPredicate;
044:
045: /**
046: * This component creates a test {@link AttributeBasedAddress}
047: * {@link Relay}.
048: */
049: public class TestABA extends ComponentPlugin {
050: private IncrementalSubscription relays;
051: private UIDService uidService;
052: private LoggingService logger;
053: private MyRelay myRelay;
054: private UnaryPredicate relayPredicate = new UnaryPredicate() {
055: public boolean execute(Object o) {
056: return o instanceof MyRelay;
057: }
058: };
059:
060: public void setUIDService(UIDService s) {
061: uidService = s;
062: }
063:
064: public void setLoggingService(LoggingService s) {
065: logger = s;
066: logger = LoggingServiceWithPrefix.add(logger,
067: getAgentIdentifier() + ": ");
068: }
069:
070: public void setupSubscriptions() {
071: String cid = getAgentIdentifier().toString();
072: boolean is135ARBN = cid.equals("1-35-ARBN");
073: relays = (IncrementalSubscription) blackboard
074: .subscribe(relayPredicate);
075: if (is135ARBN) {
076: logger.info("Adding relay at " + cid);
077: AttributeBasedAddress target = AttributeBasedAddress
078: .getAttributeBasedAddress("2-BDE-1-AD-COMM",
079: "Role", "Member");
080: myRelay = new MyRelay(Collections.singleton(target));
081: myRelay.setUID(uidService.nextUID());
082: blackboard.publishAdd(myRelay);
083: } else {
084: logger.debug("Waiting at " + cid);
085: }
086: }
087:
088: public void execute() {
089: if (relays.hasChanged()) {
090: int n = relays.size();
091: printList("Added", relays.getAddedCollection(), n);
092: printList("Removed", relays.getRemovedCollection(), n);
093: }
094: }
095:
096: private void printList(String msg, Collection list, int size) {
097: for (Iterator i = list.iterator(); i.hasNext();) {
098: MyRelay relay = (MyRelay) i.next();
099: if (relay == myRelay)
100: continue;
101: MessageAddress src = relay.getSource();
102: logger.info(msg + "(" + size + "): " + src);
103: }
104: }
105:
106: private static class MyRelay extends SimpleUniqueObject implements
107: Relay.Source, Relay.Target, Relay.TargetFactory {
108: transient Set targets;
109: transient MessageAddress source;
110:
111: MyRelay(Set targets) {
112: this .targets = targets;
113: }
114:
115: MyRelay(UID uid, MessageAddress src) {
116: this .targets = Collections.EMPTY_SET;
117: this .source = src;
118: setUID(uid);
119: }
120:
121: public Set getTargets() {
122: return targets;
123: }
124:
125: public Object getContent() {
126: return this ;
127: }
128:
129: public TargetFactory getTargetFactory() {
130: return this ;
131: }
132:
133: public int updateResponse(MessageAddress target, Object response) {
134: return NO_CHANGE;
135: }
136:
137: public MessageAddress getSource() {
138: return source;
139: }
140:
141: public Object getResponse() {
142: return null;
143: }
144:
145: public int updateContent(Object content, Token token) {
146: return CONTENT_CHANGE;
147: }
148:
149: public Relay.Target create(UID uid, MessageAddress source,
150: Object content, Token token) {
151: return new MyRelay(uid, source);
152: }
153: }
154: }
|