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.wp.resolver;
028:
029: import java.util.Map;
030: import org.cougaar.core.mts.MessageAddress;
031: import org.cougaar.core.wp.Timestamp;
032: import org.cougaar.core.wp.WhitePagesMessage;
033:
034: /**
035: * A message from a white pages server to a client, or between
036: * servers.
037: */
038: public final class WPAnswer extends WhitePagesMessage {
039:
040: public static final int LOOKUP = 0;
041: public static final int MODIFY = 1;
042: public static final int FORWARD = 2;
043: public static final int PING = 3;
044:
045: private final long sendTime;
046: private final long replyTime;
047: private final boolean useServerTime;
048: private final int action;
049: private final Map m;
050:
051: public WPAnswer(MessageAddress source, MessageAddress target,
052: long sendTime, long replyTime, boolean useServerTime,
053: int action, Map m) {
054: super (source, target);
055: this .sendTime = sendTime;
056: this .replyTime = replyTime;
057: this .useServerTime = useServerTime;
058: this .action = action;
059: this .m = m;
060: // validate
061: String s = ((sendTime <= 0) ? "invalid send time: " + sendTime
062: : (replyTime <= 0) ? "invalid reply time: " + replyTime
063: : (m == null && action != PING) ? "null map"
064: : (action != LOOKUP && action != MODIFY
065: && action != FORWARD && action != PING) ? "invalid action: "
066: + action
067: : null);
068: if (s != null) {
069: throw new IllegalArgumentException(s);
070: }
071: }
072:
073: /**
074: * The time on the client's clock when the query was sent.
075: */
076: public long getSendTime() {
077: return sendTime;
078: }
079:
080: /**
081: * The time on the server's clock when this response was sent.
082: */
083: public long getReplyTime() {
084: return replyTime;
085: }
086:
087: /**
088: * If true, then all timestamp offsets are relative to the server's
089: * reply time, otherwise they should be based upon the client's
090: * send time plus half the client's measured round-trip-time.
091: * <p>
092: * This flag is chosen by the server. The advantage of this flag:
093: * <ol>
094: * <li><i>true</i> (server time):<br>
095: * This assumes that the client's clock is well
096: * synchronized with the server's clock (e.g. NTP).</li>
097: * <li><i>false</i> (client time):<br>
098: * This avoids clock synchronization issues but assumes
099: * quick message delivery and approximately equal message
100: * send/reply delivery times.</li>
101: * </ol>
102: */
103: public boolean useServerTime() {
104: return useServerTime;
105: }
106:
107: /**
108: * @return the action of request
109: */
110: public int getAction() {
111: return action;
112: }
113:
114: /**
115: * The content of this message.
116: */
117: public Map getMap() {
118: return m;
119: }
120:
121: public String toString() {
122: long now = System.currentTimeMillis();
123: return toString(now);
124: }
125:
126: public String toString(long now) {
127: return "("
128: + (action == LOOKUP ? "lookup"
129: : action == MODIFY ? "modify"
130: : action == FORWARD ? "forward"
131: : "ping") + "-answer from="
132: + getOriginator() + " to=" + getTarget() + " sent="
133: + Timestamp.toString(sendTime, now) + " reply="
134: + Timestamp.toString(replyTime, now) + " useServer="
135: + useServerTime + " " + m + ")";
136: }
137: }
|