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.gossip;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.component.ServiceProvider;
031: import org.cougaar.core.mts.MessageAddress;
032: import org.cougaar.core.mts.MessageAttributes;
033: import org.cougaar.core.node.NodeControlService;
034: import org.cougaar.mts.base.CommFailureException;
035: import org.cougaar.mts.base.DestinationLink;
036: import org.cougaar.mts.base.DestinationLinkDelegateImplBase;
037: import org.cougaar.mts.base.MessageDeliverer;
038: import org.cougaar.mts.base.MessageDelivererDelegateImplBase;
039: import org.cougaar.mts.base.MisdeliveredMessageException;
040: import org.cougaar.mts.base.NameLookupException;
041: import org.cougaar.mts.base.RPCLinkProtocol;
042: import org.cougaar.mts.base.StandardAspect;
043: import org.cougaar.mts.base.UnregisteredNameException;
044: import org.cougaar.mts.std.AttributedMessage;
045:
046: /**
047: * Aspect to collect Gossip overhead statistics
048: */
049:
050: final public class GossipStatisticsServiceAspect extends StandardAspect
051: implements ServiceProvider {
052: private GossipTrafficRecord stats;
053: private GossipStatisticsService impl;
054:
055: public GossipStatisticsServiceAspect() {
056: }
057:
058: public void load() {
059: super .load();
060:
061: stats = new GossipTrafficRecord();
062: this .impl = new Impl();
063:
064: ServiceBroker sb = getServiceBroker();
065:
066: NodeControlService ncs = (NodeControlService) sb.getService(
067: this , NodeControlService.class, null);
068:
069: ServiceBroker rootsb = ncs.getRootServiceBroker();
070: sb.releaseService(this , NodeControlService.class, null);
071:
072: rootsb.addService(GossipStatisticsService.class, this );
073:
074: }
075:
076: public Object getReverseDelegate(Object delegatee, Class type) {
077: if (type == MessageDeliverer.class) {
078: return new DelivererDelegate((MessageDeliverer) delegatee);
079: } else {
080: return null;
081: }
082: }
083:
084: public Object getDelegate(Object delegatee, Class type) {
085: if (type == DestinationLink.class) {
086: // RPC Links only!
087: DestinationLink link = (DestinationLink) delegatee;
088: Class cl = link.getProtocolClass();
089: if (RPCLinkProtocol.class.isAssignableFrom(cl)) {
090: return new DestinationLinkDelegate(link);
091: } else {
092: return null;
093: }
094: } else {
095: return null;
096: }
097: }
098:
099: public Object getService(ServiceBroker sb, Object requestor,
100: Class serviceClass) {
101: if (serviceClass == GossipStatisticsService.class) {
102: return impl;
103: } else {
104: return null;
105: }
106: }
107:
108: public void releaseService(ServiceBroker sb, Object requestor,
109: Class serviceClass, Object service) {
110: }
111:
112: void requestsSent(KeyGossip gossip) {
113: stats.requests_sent += gossip.size();
114: }
115:
116: void requestsReceived(KeyGossip gossip) {
117: stats.requests_rcvd += gossip.size();
118: }
119:
120: void valuesSent(ValueGossip gossip) {
121: stats.values_sent += gossip.size();
122: }
123:
124: void valuesReceived(ValueGossip gossip) {
125: stats.values_rcvd += gossip.size();
126: }
127:
128: private boolean hasGossip(Gossip gossip) {
129: return gossip != null && !gossip.isEmpty();
130: }
131:
132: private class Impl implements GossipStatisticsService {
133:
134: Impl() {
135: }
136:
137: public GossipTrafficRecord getStatistics() {
138: return new GossipTrafficRecord(stats);
139: }
140:
141: }
142:
143: private class DestinationLinkDelegate extends
144: DestinationLinkDelegateImplBase {
145: DestinationLinkDelegate(DestinationLink delegatee) {
146: super (delegatee);
147: }
148:
149: public MessageAttributes forwardMessage(
150: AttributedMessage message)
151: throws UnregisteredNameException, NameLookupException,
152: CommFailureException, MisdeliveredMessageException {
153: MessageAttributes result = super .forwardMessage(message);
154: // statistics
155: KeyGossip keyGossip = (KeyGossip) message
156: .getAttribute(GossipAspect.REQUEST_GOSSIP_ATTR);
157: ValueGossip valueGossip = (ValueGossip) message
158: .getAttribute(GossipAspect.VALUE_GOSSIP_ATTR);
159: if (hasGossip(keyGossip))
160: requestsSent(keyGossip);
161: if (hasGossip(valueGossip))
162: valuesSent(valueGossip);
163: ++stats.msg_sent;
164: if (hasGossip(keyGossip) || hasGossip(valueGossip))
165: ++stats.msg_with_gossip_sent;
166: return result;
167: }
168:
169: }
170:
171: private class DelivererDelegate extends
172: MessageDelivererDelegateImplBase {
173: DelivererDelegate(MessageDeliverer delegatee) {
174: super (delegatee);
175: }
176:
177: public MessageAttributes deliverMessage(
178: AttributedMessage message, MessageAddress dest)
179: throws MisdeliveredMessageException {
180: // statistics
181: KeyGossip keyGossip = (KeyGossip) message
182: .getAttribute(GossipAspect.REQUEST_GOSSIP_ATTR);
183: ValueGossip valueGossip = (ValueGossip) message
184: .getAttribute(GossipAspect.VALUE_GOSSIP_ATTR);
185: if (hasGossip(keyGossip))
186: requestsReceived(keyGossip);
187: if (hasGossip(valueGossip))
188: valuesReceived(valueGossip);
189: ++stats.msg_rcvd;
190: if (hasGossip(keyGossip) || hasGossip(valueGossip))
191: ++stats.msg_with_gossip_rcvd;
192: return super.deliverMessage(message, dest);
193: }
194: }
195:
196: }
|