123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Threading;
- using log4net;
- namespace OpenSim.GridLaunch
- {
- internal partial class AppExecutor : IDisposable
- {
-
- private static readonly int shutdownWaitSeconds = 10;
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
-
-
-
- private StreamWriter Input { get; set; }
- private StreamReader Output { get; set; }
- private StreamReader Error { get; set; }
- private object processLock = new object();
- private bool isRunning = false;
- public bool IsRunning { get { return isRunning; } }
- private string file;
- public string File { get { return file; } }
- Process process;
- public AppExecutor(string File)
- {
- file = File;
- }
- #region Dispose of unmanaged resources
- ~AppExecutor()
- {
- Dispose();
- }
- private bool isDisposed = false;
- public void Dispose()
- {
- if (!isDisposed)
- {
- isDisposed = true;
- Stop();
- }
- }
- #endregion
- #region Start / Stop process
- public void Start()
- {
- if (isDisposed)
- throw new ApplicationException("Attempt to start process in Disposed instance of AppExecutor.");
-
- Stop();
- lock (processLock)
- {
- isRunning = true;
- m_log.InfoFormat("Starting \"{0}\".", file);
-
- process = new Process();
- process.StartInfo.FileName = file;
- process.StartInfo.Arguments = "";
- process.StartInfo.CreateNoWindow = true;
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.ErrorDialog = false;
- process.EnableRaisingEvents = true;
-
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
-
- process.Start();
- Input = process.StandardInput;
- Output = process.StandardOutput;
- Error = process.StandardError;
-
- timer_Start();
-
-
- }
- }
- public void Stop()
- {
-
-
- lock (processLock)
- {
-
- if (!isRunning)
- return;
- isRunning = false;
- timer_Stop();
- m_log.InfoFormat("Stopping \"{0}\".", file);
-
- try
- {
- if (Input != null)
- {
- _writeLine("");
- _writeLine("exit");
- _writeLine("quit");
-
- process.WaitForExit(1000 * shutdownWaitSeconds);
- }
- }
- catch (Exception ex)
- {
- m_log.ErrorFormat("Exeption asking \"{0}\" to shut down: {1}", file, ex.ToString());
- }
- try
- {
-
- if (process.HasExited != true)
- process.Kill();
- }
- catch (Exception ex)
- {
- m_log.ErrorFormat("Exeption killing \"{0}\": {1}", file, ex.ToString());
- }
- try
- {
-
- process.Close();
- }
- catch (Exception ex)
- {
- m_log.ErrorFormat("Exeption freeing resources for \"{0}\": {1}", file, ex.ToString());
- }
-
-
-
-
- Program.SafeDisposeOf(process);
- }
-
- }
- #endregion
- #region Write to stdInput
- public void Write(string Text)
- {
-
- lock (processLock)
- {
- _write(Text);
- }
- }
- public void _write(string Text)
- {
- if (Input != null)
- {
- try
- {
- Input.Write(Text);
- Input.Flush();
- }
- catch (Exception ex)
- {
- m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString());
- }
- }
- }
- public void WriteLine(string Text)
- {
-
- lock (processLock)
- {
- _writeLine(Text);
- }
- }
- public void _writeLine(string Text)
- {
- if (Input != null)
- {
- try
- {
- m_log.DebugFormat("\"{0}\": Sending: \"{1}\"", file, Text);
- Input.WriteLine(Text);
- Input.Flush();
- }
- catch (Exception ex)
- {
- m_log.ErrorFormat("Exeption sending text \"{0}\" to \"{1}\": {2}", file, Text, ex.ToString());
- }
- }
- }
- #endregion
- }
- }
|