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 autohit.common.AutohitErrorCodes;
24 import autohit.server.ServerException;
25 import autohit.vm.VMProcess;
26
27 /**
28 * The KILL command. It expects a single string that is the PID of the
29 * process to kill. It must be a parsable integer.
30 * <p>
31 * <code>
32 * COMMAND LIST
33 * this.assert(false,false,false,false,true,false)
34 * 0-UNI - OPTIONAL
35 * 1-RESPONSE - OPTIONAL
36 * 2-TARGET - UNUSED
37 * 3-CLASS - UNUSED
38 * 4-COMMAND - REQUIRED - PID of the process to kill. Must be a parsable Ingeter.
39 * 5-OBJECT - UNUSED
40 * </code>
41 *
42 * @author Erich P. Gatejen
43 * @version 1.0
44 * <i>Version History</i>
45 * <code>EPG - Initial - 27Jul03<
46 *
47 */
48 public class CommandKill extends Command {
49
50 final static long serialVersionUID = 1;
51
52 /**
53 * My name
54 */
55 public final static String MY_NAME = "kill";
56
57 /**
58 * Execute the command.
59 * @throws ServerException
60 * @return return the manreadable message for success.
61 */
62 public String execute() throws ServerException {
63
64 String victory = " failed.";
65 int pid;
66
67 // Trap all non-critical errors and just log them to the
68 // responseChannel
69 try {
70
71 // See if the PID is an integer we can use
72 pid = Integer.parseInt(command);
73
74 // Kill it
75 VMProcess currentProcess = (sc.getKernel()).getProcess(pid);
76 if (currentProcess == null) {
77 victory =
78 "kill completed. Process did not exist anyway. PID="
79 + command;
80 } else {
81 // dont actually KILL the process, just stop the running VM
82 currentProcess.vmStop();
83 victory =
84 "kill completed. The process will die when it is done executing current instruction. PID="
85 + command;
86 }
87
88 } catch (NumberFormatException nne) {
89 throw new ServerException(
90 "failed. PID was not a valid number format.",
91 AutohitErrorCodes.CODE_COMMAND_ERROR);
92 } catch (Exception e) {
93 throw new ServerException(
94 "failed. There was a general Exception. message="
95 + e.getMessage(),
96 AutohitErrorCodes.CODE_COMMAND_FAULT);
97 }
98
99 // return the receipt
100 return victory;
101 }
102
103 /**
104 * Verify the Compile command.
105 * @throws ServerException
106 * @return return the manreadable message for accepting the command.
107 */
108 public String verify() throws ServerException {
109 this.assertparam(false, false, false, false, true, false);
110 return "parameters are good.";
111 }
112
113 /**
114 * Get the textual name for the command.
115 * @return return the manreadable name of this command.
116 */
117 public String getName() {
118 return MY_NAME;
119 }
120 }
|