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.plugin.ComponentPlugin;
031: import org.cougaar.core.qos.metrics.Constants;
032: import org.cougaar.core.service.LoggingService;
033: import org.cougaar.core.service.ThreadService;
034: import org.cougaar.core.thread.Schedulable;
035:
036: /*
037: * Simple TrafficMatrix example client, meant to show usage.
038: * Loaded on the Robustness Manager Agent, which snapshots the matrix at intervals
039: * and logs them at LEVEL=DEBUG
040: */
041: public class TMatrixTracePlugin extends ComponentPlugin implements
042: Runnable, Constants {
043: private static final int BASE_PERIOD = 10; //10SecAVG
044: private LoggingService logging;
045: private CommunityTrafficMatrixService agentFlowService;
046: private Schedulable schedulable;
047:
048: public TMatrixTracePlugin() {
049: }
050:
051: // Component
052: public void load() {
053: super .load();
054:
055: ServiceBroker sb = getServiceBroker();
056:
057: logging = (LoggingService) sb.getService(this ,
058: LoggingService.class, null);
059:
060: // TrafficMatrix accessor service
061: agentFlowService = (CommunityTrafficMatrixService) sb
062: .getService(this , CommunityTrafficMatrixService.class,
063: null);
064: if (agentFlowService == null) {
065: if (logging.isErrorEnabled()) {
066: logging
067: .error("Can't find CommunityTrafficMatrixService. This plugin must be loaded at Low priority");
068: }
069: return;
070: }
071:
072: ThreadService threadService = (ThreadService) sb.getService(
073: this , ThreadService.class, null);
074: schedulable = threadService.getThread(this , this ,
075: "TMatrixTracePlugin");
076: schedulable.schedule(5000, BASE_PERIOD * 1000);
077: sb.releaseService(this , ThreadService.class, threadService);
078: }
079:
080: // Examples - Get the TrafficMatrix & print out at an interval
081: public void run() {
082: TrafficMatrix agentFlowSnapshot = agentFlowService
083: .snapshotMatrix();
084: /*
085: // Ex.#1: print out matrix using TrafficIterator
086: if(agentFlowSnapshot != null) {
087: if(logging.isDebugEnabled()) {
088: logging.debug("Printing out traffic matrix using the TrafficIterator: ");
089: }
090: TrafficMatrix.TrafficIterator iter = agentFlowSnapshot.getIterator();
091: while(iter.hasNext()) {
092: TrafficMatrix.TrafficRecord newRecord = (TrafficMatrix.TrafficRecord) iter.next();
093: MessageAddress orig = iter.getOrig();
094: MessageAddress target = iter.getTarget();
095: if(logging.isDebugEnabled()) {
096: logging.debug("Orig="+orig+", Target="+target+": "+newRecord);
097: }
098: }
099:
100:
101: // Ex.#2: get data between two individual & print out
102: double msgCt = agentFlowSnapshot.getMsgCount(MessageAddress.getMessageAddress("src1"), MessageAddress.getMessageAddress("sink1"));
103: double byteCt = agentFlowSnapshot.getByteCount(MessageAddress.getMessageAddress("src1"), MessageAddress.getMessageAddress("sink1"));
104: if(logging.isDebugEnabled()) {
105: logging.debug("msgCount & byteCount bewteen src1 -> sink1: "+ msgCt + ", "+ byteCt);
106: }
107: }
108: */
109:
110: if (logging.isDebugEnabled()) {
111: logging.debug(agentFlowSnapshot.toPrettyString());
112: }
113: }
114:
115: // Cougaar Plugin requirement methods
116: protected void setupSubscriptions() {
117: }
118:
119: protected void execute() {
120: }
121: }
|