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.mts.std;
028:
029: import org.cougaar.core.mts.AttributeConstants;
030: import org.cougaar.core.mts.MessageAddress;
031: import org.cougaar.core.mts.MessageAttributes;
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.mts.base.CommFailureException;
034: import org.cougaar.mts.base.DestinationLink;
035: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
036: import org.cougaar.mts.base.MisdeliveredMessageException;
037: import org.cougaar.mts.base.NameLookupException;
038: import org.cougaar.mts.base.SendLink;
039: import org.cougaar.mts.base.SendLinkDelegateImplBase;
040: import org.cougaar.mts.base.StandardAspect;
041: import org.cougaar.mts.base.UnregisteredNameException;
042:
043: /**
044: * This test Aspect logs message delivery elapsed time.
045: */
046: public class MessageTraceAspect extends StandardAspect implements
047: AttributeConstants {
048: private static final String SUBMISSION_TIME_ATTRIBUTE = "SUBMIT!";
049:
050: public void load() {
051: super .load();
052: }
053:
054: public Object getDelegate(Object delegatee, Class type) {
055: if (type == DestinationLink.class) {
056: // forward time
057: return new DestinationLinkDelegate(
058: (DestinationLink) delegatee);
059: } else if (type == SendLink.class) {
060: // note submit time
061: return new SendLinkDelegate((SendLink) delegatee);
062: } else {
063: return null;
064: }
065: }
066:
067: private class SendLinkDelegate extends SendLinkDelegateImplBase {
068: SendLinkDelegate(SendLink link) {
069: super (link);
070: }
071:
072: public void sendMessage(AttributedMessage msg) {
073: long now = System.currentTimeMillis();
074: msg.setLocalAttribute(SUBMISSION_TIME_ATTRIBUTE, new Long(
075: now));
076: super .sendMessage(msg);
077: }
078: }
079:
080: private class DestinationLinkDelegate extends
081: DestinationLinkDelegateImplBase {
082:
083: public DestinationLinkDelegate(DestinationLink link) {
084: super (link);
085: }
086:
087: private void logSuccess(AttributedMessage msg, long forward_time) {
088: logEntry(msg, forward_time, "Success");
089: }
090:
091: private void logFailure(AttributedMessage msg,
092: long forward_time, Exception reason) {
093: logEntry(msg, forward_time, "Exception "
094: + reason.getClass().getName());
095: }
096:
097: private void logEntry(AttributedMessage msg, long forward_time,
098: String tag) {
099: long submit_time = ((Long) msg
100: .getAttribute(SUBMISSION_TIME_ATTRIBUTE))
101: .longValue();
102: long now = System.currentTimeMillis();
103: long delta1 = now - submit_time; // cleint send time
104: long delta2 = now - forward_time; // link send time
105: MessageAddress from = msg.getOriginator();
106: MessageAddress to = msg.getTarget();
107: Integer msg_size = (Integer) msg
108: .getAttribute(MESSAGE_BYTES_ATTRIBUTE);
109: int message_size = msg_size == null ? 0 : msg_size
110: .intValue();
111: Integer hdr_size = (Integer) msg
112: .getAttribute(HEADER_BYTES_ATTRIBUTE);
113: int header_size = hdr_size == null ? 0 : hdr_size
114: .intValue();
115:
116: LoggingService lsvc = getLoggingService();
117: lsvc.info(tag + "," + from + "," + to + "," + submit_time
118: + "," + delta1 + "," + delta2 + "," + message_size
119: + "," + header_size);
120: }
121:
122: public MessageAttributes forwardMessage(AttributedMessage msg)
123: throws UnregisteredNameException, NameLookupException,
124: CommFailureException, MisdeliveredMessageException
125:
126: {
127: long forward_time = System.currentTimeMillis();
128: try {
129: MessageAttributes result = super .forwardMessage(msg);
130:
131: logSuccess(msg, forward_time);
132: return result;
133: } catch (UnregisteredNameException ex1) {
134: logFailure(msg, forward_time, ex1);
135: throw ex1;
136: } catch (NameLookupException ex2) {
137: logFailure(msg, forward_time, ex2);
138: throw ex2;
139: } catch (CommFailureException ex3) {
140: logFailure(msg, forward_time, ex3);
141: throw ex3;
142: } catch (MisdeliveredMessageException ex4) {
143: logFailure(msg, forward_time, ex4);
144: throw ex4;
145: }
146: }
147:
148: }
149:
150: }
|