001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2006 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.demo.ping;
028:
029: import java.util.Iterator;
030:
031: import org.cougaar.bootstrap.SystemProperties;
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.core.plugin.ComponentPlugin;
034: import org.cougaar.core.relay.SimpleRelay;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.util.Arguments;
037: import org.cougaar.util.UnaryPredicate;
038:
039: /**
040: * This plugin is an example ping target that receives relays and sends
041: * back a reply.
042: * <p>
043: * A "verbose=<i>boolean</i>" plugin parameter and System property is
044: * supported, exactly as documented in {@link PingSender}.
045: * <p>
046: * There must be one instance of this plugin in every agent that will
047: * receive {@link PingSender} relays. For simplicity, it's easiest to load
048: * a copy of this plugin into every agent.
049: *
050: * @property org.cougaar.demo.ping.PingReceiver.verbose=true
051: * PingReceiver should output SHOUT-level logging messages, if not set as
052: * a plugin parameter.
053: *
054: * @see PingSender Remote plugin that sends the ping relays to this plugin
055: *
056: * @see PingServlet Optional browser-based GUI.
057: */
058: public class PingReceiver extends ComponentPlugin {
059:
060: private LoggingService log;
061:
062: private boolean verbose;
063:
064: private IncrementalSubscription sub;
065:
066: /** This method is called when the agent is constructed. */
067: public void setArguments(Arguments args) {
068: verbose = args.getBoolean("verbose", true);
069: }
070:
071: /** This method is called when the agent loads. */
072: public void load() {
073: super .load();
074:
075: // Get our required Cougaar services
076: log = (LoggingService) getServiceBroker().getService(this ,
077: LoggingService.class, null);
078: }
079:
080: /** This method is called when the agent starts. */
081: protected void setupSubscriptions() {
082: // Subscribe to all relays sent to our agent
083: sub = (IncrementalSubscription) blackboard
084: .subscribe(createPredicate());
085:
086: // When a relay arrives on our blackboard, our "execute()" method
087: // will be called.
088: }
089:
090: /** This method is called whenever a subscription changes. */
091: protected void execute() {
092: // Observe added relays by looking at our subscription's add list
093: for (Iterator iter = sub.getAddedCollection().iterator(); iter
094: .hasNext();) {
095: SimpleRelay relay = (SimpleRelay) iter.next();
096: replyTo(relay);
097: }
098: }
099:
100: /** Create our subscription filter */
101: private UnaryPredicate createPredicate() {
102: // Matches any relay sent to our agent
103: return new UnaryPredicate() {
104: public boolean execute(Object o) {
105: return ((o instanceof SimpleRelay) && agentId
106: .equals(((SimpleRelay) o).getTarget()));
107: }
108: };
109: }
110:
111: private void replyTo(SimpleRelay relay) {
112: // Send back the same content as our response
113: Object content = relay.getQuery();
114: Object response = content;
115: relay.setReply(response);
116: if (verbose && log.isShoutEnabled()) {
117: log.shout("Responding to ping " + response + " from "
118: + relay.getSource());
119: }
120: blackboard.publishChange(relay);
121: }
122: }
|