1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.server.command;
22
23 import java.util.List;
24 import java.util.ListIterator;
25
26 import autohit.common.AutohitErrorCodes;
27 import autohit.server.ServerException;
28 import autohit.vm.VM;
29 import autohit.vm.VMProcess;
30
31 /**
32 * The PS command. It will dump the process list. It will be in the form of
33 * cmdid|pid|state numeric|state name|root program.
34 * <p>
35 * <code>
36 * <code>
37 * COMMAND LIST
38 * this.assert(false,false,false,false,true,false)
39 * 0-UNI - OPTIONAL
40 * 1-RESPONSE - OPTIONAL
41 * 2-TARGET - OPTIONAL
42 * 3-CLASS - UNUSED
43 * 4-COMMAND - UNUSED
44 * 5-OBJECT - UNUSED
45 * </code>
46 * </code>
47 *
48 * @author Erich P. Gatejen
49 * @version 1.0 <i>Version History</i><code>EPG - Initial - 27Jul03<
50 */
51 public class CommandPS extends Command {
52
53 final static long serialVersionUID = 1;
54
55 /**
56 * My name */
57 public final static String MY_NAME = "ps";
58 public final static String LIST_HEADER = "PROCESSLIST" + RESPONSE_ELEMENT_SEPERATOR;
59
60 /**
61 * Execute the command.
62 *
63 * @throws ServerException
64 * @return return the manreadable message for success.
65 */
66 public String execute() throws ServerException {
67
68 String victory = "failed.";
69
70 VMProcess currentProcess;
71 StringBuffer text;
72 boolean started = false;
73
74 // Trap all non-critical errors and just log them to the
75 // responseChannel
76 try {
77
78 // Get the process list and chug through it
79 // Send FINAL_RESULTS on the last item or if the list is empty;
80 List pcbList = (sc.getKernel()).getProcessList();
81 if ((pcbList == null) || (pcbList.size() <= 0)) {
82
83 sendTarget(
84 LIST_HEADER
85 + uniqueID
86 + RESPONSE_ELEMENT_SEPERATOR
87 + "X"
88 + RESPONSE_ELEMENT_SEPERATOR
89 + "No processes are running.",
90 AutohitErrorCodes.EVENT_COMMAND_FINAL_RESULTS,
91 null);
92
93 } else {
94
95 ListIterator listi = pcbList.listIterator();
96 while (listi.hasNext()) {
97
98 // build entry
99 currentProcess = (VMProcess) listi.next();
100 text = new StringBuffer(LIST_HEADER);
101 text.append(uniqueID);
102 text.append(RESPONSE_ELEMENT_SEPERATOR);
103 text.append(currentProcess.getPID());
104 text.append(RESPONSE_ELEMENT_SEPERATOR);
105 switch (currentProcess.getState()) {
106 case VM.STATE_NEW :
107 text.append("new");
108 break;
109 case VM.STATE_PAUSED :
110 text.append("paused");
111 break;
112 case VM.STATE_RUNNING :
113 text.append("running");
114 break;
115 default :
116 text.append("unknown");
117 break;
118 }
119 text.append(RESPONSE_ELEMENT_SEPERATOR);
120 text.append(currentProcess.getRootProgram());
121
122 // is it the last one?
123 if (listi.hasNext()) {
124 sendTarget(text.toString(), AutohitErrorCodes.EVENT_COMMAND_PARTIAL_RESULTS, null);
125 } else {
126 sendTarget(text.toString(), AutohitErrorCodes.EVENT_COMMAND_FINAL_RESULTS, null);
127 }
128 started = true;
129
130 } // end while
131
132 } // end if empty
133
134 // Ok, it's working
135 victory = "ps dispatched.";
136
137 } catch (Exception e) {
138
139 if (started == true) {
140 // We got some results out. Send a message to the target
141 try {
142 sendTarget(
143 LIST_HEADER
144 + uniqueID
145 + RESPONSE_ELEMENT_SEPERATOR
146 + "ABORTED! Exception while listing processes.",
147 AutohitErrorCodes.EVENT_COMMAND_FAULTED,
148 null);
149 } catch (Exception eee) {
150 // no point. it WILL happen again
151 }
152 }
153 throw new ServerException(
154 "failed. There was a general Exception. message=" + e.getMessage(),
155 AutohitErrorCodes.EVENT_COMMAND_FAILED);
156 }
157
158 // return
159 return victory;
160 }
161
162 /**
163 * Verify the Compile command.
164 *
165 * @throws ServerException
166 * @return return the manreadable message for accepting the command.
167 */
168 public String verify() throws ServerException {
169 this.assertparam(false, false, false, false, false, false);
170 return "parameters are good.";
171 }
172
173 /**
174 * Get the textual name for the command.
175 *
176 * @return return the manreadable name of this command.
177 */
178 public String getName() {
179 return MY_NAME;
180 }
181 }
|