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.util.GenericStateModel;
030:
031: /**
032: * Handle an acknowledged move.
033: */
034: public class AckHandler extends AbstractHandler {
035:
036: private GenericStateModel model;
037:
038: public AckHandler(MobilitySupport support, GenericStateModel model) {
039: super (support);
040: this .model = model;
041: }
042:
043: public void run() {
044: ack();
045: }
046:
047: /**
048: * Received an ACK response from the destination node.
049: */
050: private void ack() {
051:
052: // FIXME race condition between move & agent-add!
053:
054: // FIXME do handshake
055:
056: if (log.isInfoEnabled()) {
057: log.info("Received acknowledgement from node "
058: + moveTicket.getDestinationNode()
059: + " for move of agent " + id);
060: }
061:
062: try {
063:
064: stopAgent();
065: unloadAgent();
066: removeAgent();
067:
068: } catch (Exception e) {
069:
070: if (log.isErrorEnabled()) {
071: log.error("Agent removal after move failed", e);
072: }
073:
074: }
075:
076: // fill in success
077: //
078: // note that this travels from the origin node
079: // to the just-moved agent on the remote destination
080: // node.
081:
082: onArrival();
083:
084: // agent will be GC'ed now
085:
086: if (log.isInfoEnabled()) {
087: log.info("Agent " + id + " has successfully moved to node "
088: + moveTicket.getDestinationNode()
089: + " and has been removed from node " + nodeId);
090: }
091:
092: }
093:
094: private void stopAgent() {
095: if (log.isInfoEnabled()) {
096: log.info("Stop agent " + id);
097: }
098: model.stop();
099: if (log.isInfoEnabled()) {
100: log.info("Stopped agent " + id);
101: }
102: }
103:
104: private void unloadAgent() {
105: if (log.isInfoEnabled()) {
106: log.info("Unload agent " + id);
107: }
108: model.unload();
109: if (log.isInfoEnabled()) {
110: log.info("Unloaded agent " + id);
111: }
112: }
113:
114: protected void removeAgent() {
115: if (log.isInfoEnabled()) {
116: log.info("Remove agent " + id);
117: }
118: super .removeAgent();
119: if (log.isInfoEnabled()) {
120: log.info("Removed agent " + id);
121: }
122: }
123:
124: public String toString() {
125: return "Move (ack) of agent " + id;
126: }
127: }
|