001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2006 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.demo.ping;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.text.DecimalFormat;
032: import java.util.ArrayList;
033: import java.util.Collection;
034: import java.util.Collections;
035: import java.util.Comparator;
036: import java.util.List;
037:
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040:
041: import org.cougaar.core.mts.MessageAddress;
042: import org.cougaar.core.relay.SimpleRelay;
043: import org.cougaar.core.service.BlackboardQueryService;
044: import org.cougaar.core.servlet.ComponentServlet;
045: import org.cougaar.util.UnaryPredicate;
046:
047: /**
048: * This servlet shows our ping relays as an HTML page.
049: * <p>
050: * Supports an optional Servlet path parameter, which defaults to "/ping".
051: * <p>
052: * For simplicity, it's easiest to load a copy of this servlet into every
053: * agent.
054: */
055: public class PingServlet extends ComponentServlet {
056:
057: private long loadTime;
058:
059: private BlackboardQueryService blackboard;
060:
061: /** @return a default path if a plugin parameter is not specified */
062: protected String getPath() {
063: String ret = super .getPath();
064: return (ret == null ? "/ping" : ret);
065: }
066:
067: /** This method is called when the agent is created */
068: public void load() {
069: super .load();
070:
071: // Record our load time
072: loadTime = System.currentTimeMillis();
073:
074: // Get our required Cougaar services
075: this .blackboard = (BlackboardQueryService) getServiceBroker()
076: .getService(this , BlackboardQueryService.class, null);
077: }
078:
079: /** This method is called whenever the browser loads our URL. */
080: public void doGet(HttpServletRequest request,
081: HttpServletResponse response) throws IOException {
082:
083: // Begin our HTML page response
084: response.setContentType("text/html");
085: PrintWriter out = response.getWriter();
086: String title = "Agent " + getEncodedAgentName();
087: out.println("<html>" + "<head><title>" + title
088: + "</title></head>" + "<body><h1>" + title + "</h1>");
089:
090: // Write how long we've been running, to make it easy for the user
091: // to calculate the ping throughput
092: long runTime = System.currentTimeMillis() - loadTime;
093: out
094: .println("Milliseconds since agent load: " + runTime
095: + "<p>");
096:
097: // Query the blackboard for relays
098: UnaryPredicate pred = new UnaryPredicate() {
099: public boolean execute(Object o) {
100: return (o instanceof SimpleRelay);
101: }
102: };
103: Collection col = blackboard.query(pred);
104:
105: // Sort by source then target
106: List l = sortRelays(new ArrayList(col));
107:
108: // Write the relays as an HTML table
109: out.println("<table border=1>" + "<tr>" + " <th></th>"
110: + " <th>UID</th>" + " <th>Source</th>"
111: + " <th>Target</th>" + " <th>Content</th>"
112: + " <th>Response</th>"
113: + " <th><i>Pings Per Second</i></th>" + "</tr>");
114: DecimalFormat formatter = new DecimalFormat("0.000");
115: for (int i = 0; i < l.size(); i++) {
116: SimpleRelay relay = (SimpleRelay) l.get(i);
117:
118: double throughput = Double.NaN;
119: if (relay.getQuery() instanceof Number && runTime > 0) {
120: double count = ((Number) relay.getQuery())
121: .doubleValue();
122: double seconds = (runTime / 1000);
123: throughput = (count / seconds);
124: }
125:
126: out.println("<tr align=right>" + " <td>" + i + "</td>"
127: + " <td>" + relay.getUID() + "</td>" + " <td>"
128: + relay.getSource() + "</td>" + " <td>"
129: + relay.getTarget() + "</td>" + " <td>"
130: + relay.getQuery() + "</td>" + " <td>"
131: + relay.getReply() + "</td>" + " <td>"
132: + formatter.format(throughput) + "</td>" + "<tr>");
133: }
134: out.println("</table>");
135:
136: // Create a "reload" button for the user to invoke our servlet again
137: out.println("<form method=\"get\" action=\""
138: + request.getRequestURI() + "\">"
139: + " <input type=\"submit\" value=\"Reload\">"
140: + "</form>");
141:
142: // End our HTML page
143: out.println("</body></html>");
144: }
145:
146: /** Sorting methods */
147: private static List sortRelays(List l) {
148: Comparator c = new Comparator() {
149: public int compare(Object o1, Object o2) {
150: return compareRelays((SimpleRelay) o1, (SimpleRelay) o2);
151: }
152: };
153: Collections.sort(l, c);
154: return l;
155: }
156:
157: private static int compareRelays(SimpleRelay r1, SimpleRelay r2) {
158: int ret;
159: ret = compareAddresses(r1.getSource(), r2.getSource());
160: if (ret != 0)
161: return ret;
162: ret = compareAddresses(r1.getTarget(), r2.getTarget());
163: if (ret != 0)
164: return ret;
165: return compare(r1.getUID(), r2.getUID());
166: }
167:
168: private static int compareAddresses(MessageAddress m1,
169: MessageAddress m2) {
170: String a = (m1 == null ? null : m1.getAddress());
171: String b = (m2 == null ? null : m2.getAddress());
172: return compare(a, b);
173: }
174:
175: private static int compare(Comparable a, Comparable b) {
176: return (a == null ? (b == null ? 0 : 1) : b == null ? -1 : a
177: .compareTo(b));
178: }
179: }
|