001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.planning.plugin.completion;
028:
029: import java.io.IOException;
030: import java.io.PrintWriter;
031: import java.lang.reflect.Constructor;
032: import java.text.SimpleDateFormat;
033: import java.util.Date;
034:
035: import javax.servlet.Servlet;
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.cougaar.core.service.AlarmService;
041: import org.cougaar.core.service.DemoControlService;
042: import org.cougaar.core.servlet.BaseServletComponent;
043:
044: /**
045: * This plugin gathers and integrates completion information from
046: * nodes in a society to determine the "completion" of the current
047: * tasks. This plugin should be included only in one agent at the root
048: * of the society such as NCA. When the root determines that
049: * completion has been acheived (or is never going to be achieved), it
050: * advances the clock with the expectation that the advancement will
051: * engender additional activity and waits for the completion of that
052: * work.
053: **/
054:
055: public class SimpleTimeAdvanceComponent extends BaseServletComponent {
056: private static final Class[] stringArgType = { String.class };
057:
058: private int DAYS = 1;
059:
060: private int HOURS = 0;
061:
062: private AlarmService alarmService;
063:
064: private DemoControlService demoControlService;
065:
066: public void setAlarmService(AlarmService as) {
067: alarmService = as;
068: }
069:
070: public void setDemoControlService(DemoControlService dcs) {
071: demoControlService = dcs;
072: }
073:
074: public SimpleTimeAdvanceComponent() {
075: super ();
076: }
077:
078: protected String getPath() {
079: return "/" + getRelativePath();
080: }
081:
082: protected String getRelativePath() {
083: return "timeAdvance";
084: }
085:
086: protected Servlet createServlet() {
087: try {
088: return new SimpleTimeAdvanceServlet();
089: } catch (RuntimeException e) {
090: e.printStackTrace();
091: throw e;
092: }
093: }
094:
095: private class SimpleTimeAdvanceServlet extends HttpServlet {
096: protected void doGet(HttpServletRequest request,
097: HttpServletResponse response) throws IOException {
098: doPostOrGet(request, response);
099: }
100:
101: protected void doPost(HttpServletRequest request,
102: HttpServletResponse response) throws IOException {
103: doPostOrGet(request, response);
104: }
105:
106: protected void doPostOrGet(HttpServletRequest request,
107: HttpServletResponse response) throws IOException {
108: boolean doUpdate = ("Advance".equals(request
109: .getParameter("submit")));
110: PrintWriter out = response.getWriter();
111: response.setContentType("text/html");
112: out
113: .println("<html>\n <head>\n <title>Time Advance Control</title>\n </head>");
114: out.println(" <body>\n <h1>Time Advance Control</h1>"
115: + " <form action=\"" + getRelativePath()
116: + "\" method=\"get\">");
117: out.println(" <table>");
118: out.println(" <tr><td>Scenario Time</td><td>"
119: + formatDate(alarmService.currentTimeMillis())
120: + "</td></tr>");
121: DAYS = handleField("days", "Advance Days",
122: "The days for each advancement", new Integer(DAYS),
123: request, out).intValue();
124: HOURS = handleField("hours", "Advance Hours",
125: "The hours for each advancement",
126: new Integer(HOURS), request, out).intValue();
127: out.println(" </table>");
128: out
129: .println("<input type=\"submit\" name=\"submit\" value=\"Advance\">");
130: out
131: .println("<input type=\"submit\" name=\"submit\" value=\"Refresh\">");
132: if (doUpdate) {
133: long TIME_STEP = DAYS * 86400000L + HOURS * 3600000L;
134: long newTime = alarmService.currentTimeMillis()
135: + TIME_STEP;
136: long quantization = 1L;
137: if (TIME_STEP >= 86400000L) {
138: // Quantize to nearest day if step is a multiple of one day
139: if ((TIME_STEP % 86400000L) == 0L) {
140: quantization = 86400000L;
141: }
142: } else if ((TIME_STEP % 3600000L) == 0L) {
143: // Quantize to nearest hour if step is a multiple of one hour
144: quantization = 3600000;
145: }
146: newTime = (newTime / quantization) * quantization;
147: demoControlService.setSocietyTime(newTime, true);
148: out.println("Advancing to " + formatDate(newTime));
149: }
150: out.println(" </form>\n </body>\n</html>");
151: }
152:
153: private Number handleField(String name, String label,
154: String description, Number currentValue,
155: HttpServletRequest request, PrintWriter out) {
156: Number newValue = createValue(currentValue, request
157: .getParameter(name));
158: if (newValue != null && !newValue.equals(currentValue)) {
159: currentValue = newValue;
160: }
161: out.println(" <tr><td>" + label + "</td><td>"
162: + "<input name=\"" + name
163: + "\" type=\"text\" value=\""
164: + currentValue.toString() + "\"></td></tr>");
165: return currentValue;
166: }
167:
168: private Number createValue(Number currentValue, String v) {
169: try {
170: Constructor constructor = currentValue.getClass()
171: .getConstructor(stringArgType);
172: Object[] args = { v };
173: return (Number) constructor.newInstance(args);
174: } catch (Exception e) {
175: return currentValue;
176: }
177: }
178: }
179:
180: private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
181: "MM/dd/yyyy HH:mm:ss");
182: private static Date fdate = new Date();
183:
184: public static String formatDate(long time) {
185: synchronized (fdate) {
186: fdate.setTime(time);
187: return dateFormat.format(fdate);
188: }
189: }
190: }
|