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.mobility.arch;
028:
029: import org.cougaar.core.component.ComponentDescription;
030: import org.cougaar.core.mobility.MoveTicket;
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.service.LoggingService;
033:
034: /**
035: * Handle a request to accept a mobile agent at the target node.
036: */
037: public class ArrivalHandler extends AbstractHandler {
038:
039: private ComponentDescription desc;
040:
041: public ArrivalHandler(MobilitySupport support,
042: ComponentDescription desc) {
043: super (support);
044: this .desc = desc;
045: }
046:
047: public void run() {
048: arrival();
049: }
050:
051: private void arrival() {
052:
053: // FIXME race condition between move & agent-add!
054:
055: // FIXME do handshake
056:
057: if (log.isInfoEnabled()) {
058: log.info("Received request to add agent " + id
059: + ", which is moving from node "
060: + moveTicket.getOriginNode() + " to local node "
061: + nodeId);
062: }
063:
064: try {
065:
066: addAgent(desc);
067:
068: } catch (Exception e) {
069:
070: if (log.isErrorEnabled()) {
071: log.error("Unable to add moved agent " + id
072: + " to node " + nodeId, e);
073: }
074:
075: sendNack(e);
076:
077: return;
078: }
079:
080: try {
081:
082: sendAck();
083:
084: } catch (Exception e) {
085:
086: // too late now!
087:
088: if (log.isErrorEnabled()) {
089: log.error(
090: "Unable to send acknowledgement for"
091: + " move of agent " + id + " to node "
092: + nodeId, e);
093: }
094:
095: }
096:
097: if (log.isInfoEnabled()) {
098: log.info("Sent acknowledgement back to node "
099: + moveTicket.getOriginNode() + ": agent " + id
100: + " has successfully moved to node " + nodeId);
101: }
102:
103: }
104:
105: protected void addAgent(ComponentDescription desc) {
106: if (log.isInfoEnabled()) {
107: log.info("Add agent " + id);
108: }
109: super .addAgent(desc);
110: if (log.isInfoEnabled()) {
111: log.info("Added agent " + id);
112: }
113: }
114:
115: public String toString() {
116: return "Move (arrival) of agent " + id;
117: }
118: }
|