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.qos.tmatrix;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.component.ServiceProvider;
031: import org.cougaar.core.mts.AttributeConstants;
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.mts.MessageAttributes;
034: import org.cougaar.core.node.NodeControlService;
035: import org.cougaar.mts.base.CommFailureException;
036: import org.cougaar.mts.base.DestinationLink;
037: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
038: import org.cougaar.mts.base.MessageDeliverer;
039: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
040: import org.cougaar.mts.base.MisdeliveredMessageException;
041: import org.cougaar.mts.base.NameLookupException;
042: import org.cougaar.mts.base.StandardAspect;
043: import org.cougaar.mts.base.UnregisteredNameException;
044: import org.cougaar.mts.std.AttributedMessage;
045:
046: /*
047: * Counts msgs and bytes and adds to local TrafficMatrix.
048: */
049:
050: public class AgentFlowAspect extends StandardAspect implements
051: TrafficMatrixStatisticsService, ServiceProvider,
052: AttributeConstants {
053:
054: private TrafficMatrix trafficMatrix;
055:
056: public AgentFlowAspect() {
057: trafficMatrix = new TrafficMatrix();
058: }
059:
060: public void load() {
061: super .load();
062:
063: ServiceBroker sb = getServiceBroker();
064:
065: NodeControlService ncs = (NodeControlService) sb.getService(
066: this , NodeControlService.class, null);
067:
068: if (ncs != null) {
069: ServiceBroker rootsb = ncs.getRootServiceBroker();
070: rootsb.releaseService(this , NodeControlService.class, ncs);
071: // We provide TrafficMatrixStatisticsService
072: rootsb.addService(TrafficMatrixStatisticsService.class,
073: this );
074:
075: } else {
076: throw new RuntimeException(
077: "AgentFlowAspect can only be used in NodeAgents");
078: }
079:
080: }
081:
082: // ensure there's a TrafficRecord for that map entry
083: private TrafficMatrix.TrafficRecord ensureTrafficRecord(
084: MessageAddress src, MessageAddress dst) {
085: TrafficMatrix.TrafficRecord record = null;
086: synchronized (trafficMatrix) {
087: record = trafficMatrix.getOrMakeRecord(src, dst);
088: }
089: return record;
090: }
091:
092: // TrafficMatricStatisticsService Interface
093: // Deep copy of matrix
094: public TrafficMatrix snapshotMatrix() {
095: return new TrafficMatrix(trafficMatrix);
096: }
097:
098: public void addMatrix(TrafficMatrix matrix) {
099: trafficMatrix.addMatrix(matrix);
100: }
101:
102: // ServiceProvider Interface
103: public Object getService(ServiceBroker sb, Object requestor,
104: Class serviceClass) {
105: if (serviceClass == TrafficMatrixStatisticsService.class) {
106: return this ;
107: } else {
108: return null;
109: }
110: }
111:
112: public void releaseService(ServiceBroker sb, Object requestor,
113: Class serviceClass, Object service) {
114: }
115:
116: // Helper methods
117: boolean delivered(MessageAttributes attributes) {
118: return attributes != null
119: & attributes.getAttribute(DELIVERY_ATTRIBUTE).equals(
120: DELIVERY_STATUS_DELIVERED);
121: }
122:
123: void countMessages(AttributedMessage message, MessageAttributes meta) {
124: if (delivered(meta)) {
125: int msgBytes = 0;
126: Object attr = message.getAttribute(MESSAGE_BYTES_ATTRIBUTE);
127: if (attr != null && (attr instanceof Number))
128: msgBytes = ((Number) attr).intValue();
129:
130: TrafficMatrix.TrafficRecord theRecord = ensureTrafficRecord(
131: message.getOriginator(), message.getTarget());
132:
133: synchronized (theRecord) {
134: theRecord.msgCount++;
135: theRecord.byteCount += msgBytes;
136: }
137:
138: ensureTrafficRecord(message.getOriginator(), message
139: .getTarget());
140:
141: }
142: }
143:
144: //
145: // Aspect Code to implement TrafficRecord Collection
146:
147: public Object getDelegate(Object object, Class type) {
148: if (type == DestinationLink.class) {
149: return new AgentFlowDestinationLink(
150: (DestinationLink) object);
151: // } else if (type == MessageDeliverer.class) {
152: // return new MessageDelivererDelegate((MessageDeliverer) object);
153: } else {
154: return null;
155: }
156: }
157:
158: public class AgentFlowDestinationLink extends
159: DestinationLinkDelegateImplBase {
160:
161: public AgentFlowDestinationLink(DestinationLink link) {
162: super (link);
163: }
164:
165: public MessageAttributes forwardMessage(
166: AttributedMessage message)
167: throws UnregisteredNameException, NameLookupException,
168: CommFailureException, MisdeliveredMessageException {
169: // Attempt to Deliver message
170: MessageAttributes meta = super .forwardMessage(message);
171: countMessages(message, meta);
172: return meta;
173: }
174: }
175:
176: public class MessageDelivererDelegate extends
177: MessageDelivererDelegateImplBase {
178:
179: MessageDelivererDelegate(MessageDeliverer delegatee) {
180: super (delegatee);
181: }
182:
183: public MessageAttributes deliverMessage(
184: AttributedMessage message, MessageAddress dest)
185: throws MisdeliveredMessageException {
186: MessageAttributes meta = super
187: .deliverMessage(message, dest);
188: // No counting on the messages in b
189: //countMessages(message, meta);
190: return meta;
191: }
192:
193: }
194:
195: }
|