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.tools.server.examples;
028:
029: import java.util.*;
030: import java.io.*;
031:
032: import org.cougaar.tools.server.*;
033: import org.cougaar.tools.server.system.ProcessStatus;
034:
035: /**
036: * Minimal "console" for using to test the server.
037: * <p>
038: * Can optionally pass these command-line arguments to
039: * <tt>main(String[])</tt>:<ul>
040: * <li>killMillis=LONG (default=20000)</li>
041: * <li>hostName=STRING (default=localhost)</li>
042: * <li>controlPort=INT (default=8484)</li>
043: * <li>nodeName=STRING (default=MiniNode)</li>
044: * <li>namingAddr=STRING (default=localhost:8888)</li>
045: * </ul>
046: */
047: public class MinConsole {
048:
049: public static void main(String[] args) throws Exception {
050:
051: long killMillis = 20 * 1000;
052:
053: String hostName = "localhost";
054: int controlPort = 8484;
055: String nodeName = "MiniNode";
056: String namingAddr = hostName + ":8888";
057:
058: for (int i = 0; i < args.length; i++) {
059: String ai = args[i];
060: if (ai.startsWith("killMillis=")) {
061: String tmp = ai.substring("killMillis=".length());
062: killMillis = Long.parseLong(tmp);
063: } else if (ai.startsWith("hostName=")) {
064: hostName = ai.substring("hostName=".length());
065: } else if (ai.startsWith("controlPort=")) {
066: String tmp = ai.substring("controlPort=".length());
067: controlPort = Integer.parseInt(tmp);
068: } else if (ai.startsWith("nodeName=")) {
069: nodeName = ai.substring("nodeName=".length());
070: } else if (ai.startsWith("namingAddr=")) {
071: namingAddr = ai.substring("namingAddr=".length());
072: } else {
073: throw new IllegalArgumentException("Illegal arg[" + i
074: + "]: " + ai);
075: }
076: }
077:
078: String procId = nodeName;
079:
080: System.out.println(MinConsole.class.getName() + " {"
081: + "\n killMillis: " + killMillis + "\n hostName: "
082: + hostName + "\n controlPort: " + controlPort
083: + "\n nodeName: " + nodeName + "\n namingAddr: "
084: + namingAddr + "\n procId: " + procId + "\n}");
085:
086: // create a remote-host-registry instance
087: RemoteHostRegistry hostReg = RemoteHostRegistry.getInstance();
088:
089: // contact the host
090: RemoteHost rhost = hostReg.lookupRemoteHost(hostName,
091: controlPort, true);
092:
093: // just to test, let's try a "ping()"
094: ping(rhost);
095:
096: // list the running processes on the server
097: listProcs(rhost);
098:
099: // define the process
100: ProcessDescription desc = createProcessDescription(procId,
101: nodeName, namingAddr);
102:
103: // launch process
104: RemoteProcess proc = createProcess(rhost, desc);
105:
106: // wait a couple seconds
107: Thread.sleep(2 * 1000);
108:
109: // list the processes on the server
110: listProcs(rhost);
111:
112: // let it run for a while
113: System.out
114: .println("Created process \""
115: + procId
116: + "\", run "
117: + ((killMillis <= 0) ? "forever" : "for "
118: + killMillis + "milliseconds")
119: + "\n"
120: + "***************************************************");
121: // list running processes ("ps")
122: listProcessStatus(proc, false);
123:
124: if (killMillis > 0) {
125: Thread.sleep(killMillis);
126:
127: // kill the process
128: System.out
129: .println("****************************************************\n"
130: + "Kill process \"" + procId + "\"");
131: rhost.killRemoteProcess(desc.getName());
132:
133: // list the processes on the server
134: listProcs(rhost);
135:
136: Thread.sleep(2 * 1000);
137:
138: // re-launch process
139: System.out
140: .println("****************************************************\n"
141: + "Re-launch process \"" + procId + "\"");
142: RemoteProcess proc2 = createProcess(rhost, desc);
143:
144: System.out
145: .println("****************************************************\n"
146: + "Run for 5 seconds");
147: Thread.sleep(5 * 1000);
148:
149: System.out.println("exit, force server-side cleanup.");
150:
151: // force exit -- RMI-threads would keep this running
152: System.exit(0);
153: }
154: }
155:
156: private static void ping(RemoteHost rhost) throws Exception {
157: long t1 = System.currentTimeMillis();
158: long t2 = rhost.ping();
159: long t3 = System.currentTimeMillis();
160: System.out.println("Ping (millis): " + "\n sent: " + t1
161: + "\n ret: " + t2 + "\n recv: " + t3
162: + "\n (recv - sent): " + (t3 - t1));
163: }
164:
165: private static void listProcs(RemoteHost rhost) throws Exception {
166: // list the running processes on the server
167: List runningProcs = rhost.listProcessDescriptions();
168: int nRunningProcs = ((runningProcs != null) ? runningProcs
169: .size() : 0);
170: System.out
171: .println("Running processes[" + nRunningProcs + "]: ");
172: for (int i = 0; i < nRunningProcs; i++) {
173: ProcessDescription pdi = (ProcessDescription) runningProcs
174: .get(i);
175: System.out.println(" [" + i + " / " + nRunningProcs
176: + "]: " + pdi);
177: }
178: }
179:
180: private static ProcessDescription createProcessDescription(
181: String procId, String nodeName, String namingAddr)
182: throws Exception {
183: // build a configuration
184: Properties javaProps = new Properties();
185: javaProps.put("org.cougaar.node.name", nodeName);
186: javaProps.put("org.cougaar.name.server", namingAddr);
187: return new ProcessDescription(procId, "group-for-" + procId,
188: javaProps, null);
189: }
190:
191: private static RemoteProcess createProcess(RemoteHost rhost,
192: final ProcessDescription desc) throws Exception {
193: // create an output listener
194: OutputListener ol = new OutputListener() {
195: public void handleOutputBundle(OutputBundle ob) {
196: // just write std-out and std-err
197: DualStreamBuffer dsb = ob.getDualStreamBuffer();
198: try {
199: dsb.writeTo(System.out, System.err);
200: } catch (IOException ioe) {
201: ioe.printStackTrace();
202: }
203: }
204: };
205:
206: // create the output filter/buffer config
207: OutputPolicy op = new OutputPolicy(20);
208:
209: // create a listener config
210: RemoteListenableConfig rlc = new RemoteListenableConfig(ol, op);
211:
212: // create the node
213: try {
214: return rhost.createRemoteProcess(desc, rlc);
215: } catch (Exception e) {
216: System.err.println("Unable to create process:");
217: e.printStackTrace();
218: throw e;
219: }
220: }
221:
222: private static void listProcessStatus(RemoteProcess proc,
223: boolean showAll) throws Exception {
224: // query
225: ProcessStatus[] psa = proc.listProcesses(showAll);
226: // display
227: int n = ((psa != null) ? psa.length : 0);
228: System.out.println(((showAll) ? "All" : "Process's")
229: + " ProcessStatus[" + n + "] for Process \""
230: + proc.getProcessDescription().getName() + "\":");
231: for (int i = 0; i < n; i++) {
232: ProcessStatus pi = psa[i];
233: // show terse details
234: System.out.println(" " + pi.toString(false));
235: }
236: }
237: }
|