diff options
Diffstat (limited to 'OpenSim/Tools/OpenSim.GridLaunch/AppExecutor.cs')
-rw-r--r-- | OpenSim/Tools/OpenSim.GridLaunch/AppExecutor.cs | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/OpenSim/Tools/OpenSim.GridLaunch/AppExecutor.cs b/OpenSim/Tools/OpenSim.GridLaunch/AppExecutor.cs new file mode 100644 index 0000000..2c6ac31 --- /dev/null +++ b/OpenSim/Tools/OpenSim.GridLaunch/AppExecutor.cs | |||
@@ -0,0 +1,247 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | using System; | ||
28 | using System.Collections; | ||
29 | using System.Diagnostics; | ||
30 | using System.IO; | ||
31 | using System.Reflection; | ||
32 | using System.Text; | ||
33 | using System.Threading; | ||
34 | using log4net; | ||
35 | |||
36 | namespace OpenSim.GridLaunch | ||
37 | { | ||
38 | internal partial class AppExecutor : IDisposable | ||
39 | { | ||
40 | // How long to wait for process to shut down by itself | ||
41 | private static readonly int shutdownWaitSeconds = 10; | ||
42 | |||
43 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
44 | |||
45 | //private StreamWriter Input { get { return process.StandardInput; } } | ||
46 | //private StreamReader Output { get { return process.StandardOutput; } } | ||
47 | //private StreamReader Error { get { return process.StandardError; } } | ||
48 | |||
49 | private StreamWriter Input { get; set; } | ||
50 | private StreamReader Output { get; set; } | ||
51 | private StreamReader Error { get; set; } | ||
52 | |||
53 | private object processLock = new object(); | ||
54 | |||
55 | private bool isRunning = false; | ||
56 | public bool IsRunning { get { return isRunning; } } | ||
57 | |||
58 | private string file; | ||
59 | public string File { get { return file; } } | ||
60 | |||
61 | Process process; | ||
62 | |||
63 | public AppExecutor(string File) | ||
64 | { | ||
65 | file = File; | ||
66 | } | ||
67 | |||
68 | #region Dispose of unmanaged resources | ||
69 | ~AppExecutor() | ||
70 | { | ||
71 | Dispose(); | ||
72 | } | ||
73 | private bool isDisposed = false; | ||
74 | public void Dispose() | ||
75 | { | ||
76 | if (!isDisposed) | ||
77 | { | ||
78 | isDisposed = true; | ||
79 | Stop(); | ||
80 | } | ||
81 | } | ||
82 | #endregion | ||
83 | |||
84 | #region Start / Stop process | ||
85 | public void Start() | ||
86 | { | ||
87 | if (isDisposed) | ||
88 | throw new ApplicationException("Attempt to start process in Disposed instance of AppExecutor."); | ||
89 | // Stop before starting | ||
90 | Stop(); | ||
91 | |||
92 | lock (processLock) | ||
93 | { | ||
94 | isRunning = true; | ||
95 | |||
96 | m_log.InfoFormat("Starting \"{0}\".", file); | ||
97 | |||
98 | // Start the process | ||
99 | process = new Process(); | ||
100 | process.StartInfo.FileName = file; | ||
101 | process.StartInfo.Arguments = ""; | ||
102 | process.StartInfo.CreateNoWindow = true; | ||
103 | process.StartInfo.UseShellExecute = false; | ||
104 | process.StartInfo.ErrorDialog = false; | ||
105 | process.EnableRaisingEvents = true; | ||
106 | |||
107 | |||
108 | // Redirect all standard input/output/errors | ||
109 | process.StartInfo.RedirectStandardInput = true; | ||
110 | process.StartInfo.RedirectStandardOutput = true; | ||
111 | process.StartInfo.RedirectStandardError = true; | ||
112 | |||
113 | // Start process | ||
114 | process.Start(); | ||
115 | |||
116 | Input = process.StandardInput; | ||
117 | Output = process.StandardOutput; | ||
118 | Error = process.StandardError; | ||
119 | |||
120 | // Start data copying | ||
121 | timer_Start(); | ||
122 | |||
123 | // We will flush manually | ||
124 | //Input.AutoFlush = false; | ||
125 | |||
126 | } | ||
127 | } | ||
128 | |||
129 | public void Stop() | ||
130 | { | ||
131 | // Shut down process | ||
132 | // We will ignore some exceptions here, against good programming practice... :) | ||
133 | |||
134 | lock (processLock) | ||
135 | { | ||
136 | // Running? | ||
137 | if (!isRunning) | ||
138 | return; | ||
139 | isRunning = false; | ||
140 | |||
141 | timer_Stop(); | ||
142 | |||
143 | m_log.InfoFormat("Stopping \"{0}\".", file); | ||
144 | |||
145 | // Send exit command to console | ||
146 | try | ||
147 | { | ||
148 | if (Input != null) | ||
149 | { | ||
150 | _writeLine(""); | ||
151 | _writeLine("exit"); | ||
152 | _writeLine("quit"); | ||
153 | // Wait for process to exit | ||
154 | process.WaitForExit(1000 * shutdownWaitSeconds); | ||
155 | } | ||
156 | } | ||
157 | catch (Exception ex) | ||
158 | { | ||
159 | m_log.ErrorFormat("Exeption asking \"{0}\" to shut down: {1}", file, ex.ToString()); | ||
160 | } | ||
161 | |||
162 | try | ||
163 | { | ||
164 | // Forcefully kill it | ||
165 | if (process.HasExited != true) | ||
166 | process.Kill(); | ||
167 | } | ||
168 | catch (Exception ex) | ||
169 | { | ||
170 | m_log.ErrorFormat("Exeption killing \"{0}\": {1}", file, ex.ToString()); | ||
171 | } | ||
172 | |||
173 | try | ||
174 | { | ||
175 | // Free resources | ||
176 | process.Close(); | ||
177 | } | ||
178 | catch (Exception ex) | ||
179 | { | ||
180 | m_log.ErrorFormat("Exeption freeing resources for \"{0}\": {1}", file, ex.ToString()); | ||
181 | } | ||
182 | |||
183 | // Dispose of stream and process object | ||
184 | //SafeDisposeOf(Input); | ||
185 | //SafeDisposeOf(Output); | ||
186 | //SafeDisposeOf(Error); | ||
187 | Program.SafeDisposeOf(process); | ||
188 | } | ||
189 | |||
190 | // Done stopping process | ||
191 | } | ||
192 | |||
193 | #endregion | ||
194 | |||
195 | #region Write to stdInput | ||
196 | public void Write(string Text) | ||
197 | { | ||
198 | // Lock so process won't shut down while we write, and that we won't write while proc is shutting down | ||
199 | lock (processLock) | ||
200 | { | ||
201 | _write(Text); | ||
202 | } | ||
203 | } | ||
204 | public void _write(string Text) | ||
205 | { | ||
206 | if (Input != null) | ||
207 | { | ||
208 | try | ||
209 | { | ||
210 | Input.Write(Text); | ||
211 | Input.Flush(); | ||
212 | } | ||
213 | catch (Exception ex) | ||
214 | { | ||
215 | m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString()); | ||
216 | } | ||
217 | |||
218 | } | ||
219 | } | ||
220 | public void WriteLine(string Text) | ||
221 | { | ||
222 | // Lock so process won't shut down while we write, and that we won't write while proc is shutting down | ||
223 | lock (processLock) | ||
224 | { | ||
225 | _writeLine(Text); | ||
226 | } | ||
227 | } | ||
228 | public void _writeLine(string Text) | ||
229 | { | ||
230 | if (Input != null) | ||
231 | { | ||
232 | try | ||
233 | { | ||
234 | m_log.DebugFormat("\"{0}\": Sending: \"{1}\"", file, Text); | ||
235 | Input.WriteLine(Text); | ||
236 | Input.Flush(); | ||
237 | } | ||
238 | catch (Exception ex) | ||
239 | { | ||
240 | m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString()); | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | #endregion | ||
245 | |||
246 | } | ||
247 | } | ||