Program.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.Collections.Generic;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using log4net.Appender;
  34. using log4net.Repository.Hierarchy;
  35. using OpenSim.GridLaunch.GUI;
  36. using OpenSim.GridLaunch.GUI.Network;
  37. namespace OpenSim.GridLaunch
  38. {
  39. class Program
  40. {
  41. public static readonly string ConfigFile = "OpenSim.GridLaunch.ini";
  42. internal static Dictionary<string, AppExecutor> AppList = new Dictionary<string, AppExecutor>();
  43. private static readonly int delayBetweenExecuteSeconds = 10;
  44. //private static readonly int consoleReadIntervalMilliseconds = 50;
  45. ////private static readonly Timer readTimer = new Timer(readConsole, null, Timeout.Infinite, Timeout.Infinite);
  46. //private static Thread timerThread;
  47. //private static object timerThreadLock = new object();
  48. private static IGUI GUIModule;
  49. private static string GUIModuleName = "";
  50. public static readonly CommandProcessor Command = new CommandProcessor();
  51. public static readonly Settings Settings = new Settings();
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. public delegate void AppConsoleOutputDelegate(string App, string Text);
  54. public static event AppConsoleOutputDelegate AppConsoleOutput;
  55. public delegate void AppConsoleErrorDelegate(string App, string Text);
  56. public static event AppConsoleErrorDelegate AppConsoleError;
  57. public delegate void AppCreatedDelegate(string App);
  58. public static event AppCreatedDelegate AppCreated;
  59. public delegate void AppRemovedDelegate(string App);
  60. public static event AppRemovedDelegate AppRemoved;
  61. internal static void FireAppConsoleOutput(string App, string Text)
  62. {
  63. if (AppConsoleOutput != null)
  64. AppConsoleOutput(App, Text);
  65. }
  66. internal static void FireAppConsoleError(string App, string Text)
  67. {
  68. if (AppConsoleError != null)
  69. AppConsoleError(App, Text);
  70. }
  71. private static readonly object startStopLock = new object();
  72. public static string Name { get { return "OpenSim Grid executor"; } }
  73. #region Start/Shutdown
  74. static void Main(string[] args)
  75. {
  76. log4net.Config.XmlConfigurator.Configure();
  77. // Startup
  78. m_log.Info(Name);
  79. m_log.Info(new string('-', Name.Length));
  80. // Read settings
  81. Settings.LoadConfig(ConfigFile);
  82. // Command line arguments override settings
  83. Settings.ParseCommandArguments(args);
  84. // Start GUI module
  85. StartGUIModule();
  86. // Start the processes
  87. ThreadPool.QueueUserWorkItem(startProcesses);
  88. // Hand over thread control to whatever GUI module
  89. GUIModule.StartGUI();
  90. // GUI module returned, we are done
  91. Shutdown();
  92. }
  93. private static void StartGUIModule()
  94. {
  95. // Create GUI module
  96. GUIModuleName = Settings["GUI"];
  97. switch (GUIModuleName.ToLower())
  98. {
  99. case "winform":
  100. GUIModuleName = "WinForm";
  101. GUIModule = new GUI.WinForm.ProcessPanel();
  102. break;
  103. case "service":
  104. GUIModuleName = "Service";
  105. GUIModule = new Service();
  106. break;
  107. case "tcpd":
  108. GUIModuleName = "TCPD";
  109. GUIModule = new TCPD();
  110. break;
  111. case "console":
  112. default:
  113. GUIModuleName = "Console";
  114. GUIModule = new GUI.Console.Console();
  115. break;
  116. }
  117. m_log.Info("GUI type: " + GUIModuleName);
  118. }
  119. internal static void Shutdown()
  120. {
  121. // Stop the processes
  122. stopProcesses();
  123. lock (startStopLock)
  124. {
  125. // Stop GUI module
  126. if (GUIModule != null)
  127. {
  128. GUIModule.StopGUI();
  129. GUIModule = null;
  130. }
  131. }
  132. }
  133. internal static void SafeDisposeOf(object obj)
  134. {
  135. IDisposable o = obj as IDisposable;
  136. try
  137. {
  138. if (o != null)
  139. o.Dispose();
  140. }
  141. catch { }
  142. }
  143. #endregion
  144. #region Start / Stop applications
  145. private static void startProcesses(Object stateInfo)
  146. {
  147. // Stop before starting
  148. stopProcesses();
  149. // Start console read timer
  150. //timer_Start();
  151. // Start the applications
  152. foreach (string file in new ArrayList(Settings.Components.Keys))
  153. {
  154. // Is this file marked for startup?
  155. if (Settings.Components[file])
  156. {
  157. AppExecutor app = new AppExecutor(file);
  158. app.Start();
  159. AppList.Add(file, app);
  160. if (AppCreated != null)
  161. AppCreated(app.File);
  162. System.Threading.Thread.Sleep(1000*delayBetweenExecuteSeconds);
  163. }
  164. }
  165. }
  166. private static void stopProcesses()
  167. {
  168. // Stop timer
  169. //timer_Stop();
  170. // Lock so we don't collide with any timer still executing on AppList
  171. lock (AppList)
  172. {
  173. // Start the applications
  174. foreach (AppExecutor app in AppList.Values)
  175. {
  176. try
  177. {
  178. m_log.Info("Stopping: " + app.File);
  179. app.Stop();
  180. }
  181. catch (Exception ex)
  182. {
  183. m_log.ErrorFormat("Exception while stopping \"{0}\": {1}", app.File, ex.ToString());
  184. }
  185. finally
  186. {
  187. if (AppRemoved != null)
  188. AppRemoved(app.File);
  189. app.Dispose();
  190. }
  191. }
  192. AppList.Clear();
  193. }
  194. }
  195. #endregion
  196. public static void Write(string App, string Text)
  197. {
  198. // Check if it is a commands
  199. bool isCommand = Command.Process(App, Text);
  200. // Write to stdInput of app
  201. if (!isCommand && AppList.ContainsKey(App))
  202. AppList[App].Write(Text);
  203. }
  204. public static void WriteLine(string App, string Text)
  205. {
  206. // Check if it is a commands
  207. bool isCommand = Command.Process(App, Text);
  208. // Write to stdInput of app
  209. if (!isCommand && AppList.ContainsKey(App))
  210. AppList[App].WriteLine(Text);
  211. }
  212. }
  213. }