001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.mobility.plugin;
028:
029: import java.util.Enumeration;
030: import java.util.Iterator;
031:
032: import org.cougaar.core.blackboard.IncrementalSubscription;
033: import org.cougaar.core.mobility.AddTicket;
034: import org.cougaar.core.mobility.ldm.AgentControl;
035: import org.cougaar.core.mobility.ldm.MobilityFactory;
036: import org.cougaar.core.mts.MessageAddress;
037: import org.cougaar.core.plugin.ComponentPlugin;
038: import org.cougaar.core.service.DomainService;
039: import org.cougaar.util.UnaryPredicate;
040:
041: /**
042: * This component is an example plugin demonstrating how to use an
043: * {@link AgentControl} to add an agent to the local node.
044: * <p>
045: * First create an AddTicket and use the MobilitySupport to create an
046: * AgentControl request, the object through which all
047: * adds/moves/removes are recognized by the mobility API and handled
048: * correctly and securely.
049: * <p>
050: * This plugin also shows how one would subscribe to the changed
051: * AgentControl objects and their status.
052: */
053: public class AddAgentExamplePlugin extends ComponentPlugin {
054:
055: protected UnaryPredicate AGENT_CONTROL_PRED = new UnaryPredicate() {
056: public boolean execute(Object o) {
057: return (o instanceof AgentControl);
058: }
059: };
060:
061: protected MobilityFactory mobilityFactory;
062: DomainService domain;
063:
064: IncrementalSubscription sub;
065:
066: public void setDomainService(DomainService domain) {
067: this .domain = domain;
068: mobilityFactory = (MobilityFactory) domain
069: .getFactory("mobility");
070: }
071:
072: protected void setupSubscriptions() {
073: if (mobilityFactory == null) {
074: throw new RuntimeException(
075: "Mobility factory (and domain) not enabled");
076: }
077:
078: Iterator iter = getParameters().iterator();
079: String newAgent = (String) iter.next();
080: String destNode = (String) iter.next();
081:
082: // add the AgentControl request
083: addAgent(newAgent, destNode);
084:
085: sub = (IncrementalSubscription) blackboard
086: .subscribe(AGENT_CONTROL_PRED);
087: }
088:
089: public void execute() {
090: if (sub.hasChanged()) {
091: for (Enumeration en = sub.getAddedList(); en
092: .hasMoreElements();) {
093: AgentControl ac = (AgentControl) en.nextElement();
094: System.out.println("ADDED " + ac);
095: }
096: for (Enumeration en = sub.getChangedList(); en
097: .hasMoreElements();) {
098: AgentControl ac = (AgentControl) en.nextElement();
099: System.out.println("CHANGED " + ac);
100: }
101: for (Enumeration en = sub.getRemovedList(); en
102: .hasMoreElements();) {
103: AgentControl ac = (AgentControl) en.nextElement();
104: System.out.println("REMOVED " + ac);
105: }
106: }
107: }
108:
109: protected void addAgent(String newAgent, String destNode) {
110:
111: MessageAddress newAgentAddr = null;
112: MessageAddress destNodeAddr = null;
113: if (newAgent != null) {
114: newAgentAddr = MessageAddress.getMessageAddress(newAgent);
115: }
116: if (destNode != null) {
117: destNodeAddr = MessageAddress.getMessageAddress(destNode);
118: }
119: Object ticketId = mobilityFactory.createTicketIdentifier();
120: AddTicket addTicket = new AddTicket(ticketId, newAgentAddr,
121: destNodeAddr);
122:
123: AgentControl ac = mobilityFactory.createAgentControl(null,
124: destNodeAddr, addTicket);
125:
126: System.out.println("CREATED " + ac);
127: blackboard.publishAdd(ac);
128: }
129:
130: }
|