Main.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) OpenSim project, http://osgrid.org/
  3. * All rights reserved.
  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 <organization> 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 <copyright holder> ``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 <copyright holder> 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.IO;
  29. using System.Text;
  30. using System.Timers;
  31. using System.Net;
  32. using System.Reflection;
  33. using libsecondlife;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Sims;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Interfaces;
  38. namespace OpenGridServices.GridServer
  39. {
  40. /// <summary>
  41. /// </summary>
  42. public class OpenGrid_Main : conscmd_callback
  43. {
  44. private string ConfigDll = "OpenGrid.Config.GridConfigDb4o.dll";
  45. private GridConfig Cfg;
  46. public static OpenGrid_Main thegrid;
  47. public string GridOwner;
  48. public string DefaultStartupMsg;
  49. public string DefaultAssetServer;
  50. public string AssetSendKey;
  51. public string AssetRecvKey;
  52. public string DefaultUserServer;
  53. public string UserSendKey;
  54. public string UserRecvKey;
  55. public string SimSendKey;
  56. public string SimRecvKey;
  57. public LLUUID highestUUID;
  58. public GridHTTPServer _httpd;
  59. public SimProfileManager _regionmanager;
  60. private ConsoleBase m_console;
  61. private Timer SimCheckTimer;
  62. [STAThread]
  63. public static void Main(string[] args)
  64. {
  65. Console.WriteLine("Starting...\n");
  66. thegrid = new OpenGrid_Main();
  67. thegrid.Startup();
  68. thegrid.Work();
  69. }
  70. private void Work()
  71. {
  72. m_console.WriteLine("\nEnter help for a list of commands\n");
  73. while (true)
  74. {
  75. m_console.MainConsolePrompt();
  76. }
  77. }
  78. private OpenGrid_Main()
  79. {
  80. m_console = new ConsoleBase("opengrid-gridserver-console.log", "OpenGrid", this);
  81. MainConsole.Instance = m_console;
  82. }
  83. public void Startup()
  84. {
  85. m_console.WriteLine("Main.cs:Startup() - Loading configuration");
  86. Cfg = this.LoadConfigDll(this.ConfigDll);
  87. Cfg.InitConfig();
  88. m_console.WriteLine("Main.cs:Startup() - Loading sim profiles from database");
  89. this._regionmanager = new SimProfileManager();
  90. _regionmanager.LoadProfiles();
  91. m_console.WriteLine("Main.cs:Startup() - Starting HTTP process");
  92. _httpd = new GridHTTPServer();
  93. m_console.WriteLine("Main.cs:Startup() - Starting sim status checker");
  94. SimCheckTimer = new Timer();
  95. SimCheckTimer.Interval = 300000; // 5 minutes
  96. SimCheckTimer.Elapsed+=new ElapsedEventHandler(CheckSims);
  97. SimCheckTimer.Enabled=true;
  98. }
  99. private GridConfig LoadConfigDll(string dllName)
  100. {
  101. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  102. GridConfig config = null;
  103. foreach (Type pluginType in pluginAssembly.GetTypes())
  104. {
  105. if (pluginType.IsPublic)
  106. {
  107. if (!pluginType.IsAbstract)
  108. {
  109. Type typeInterface = pluginType.GetInterface("IGridConfig", true);
  110. if (typeInterface != null)
  111. {
  112. IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  113. config = plug.GetConfigObject();
  114. break;
  115. }
  116. typeInterface = null;
  117. }
  118. }
  119. }
  120. pluginAssembly = null;
  121. return config;
  122. }
  123. public void CheckSims(object sender, ElapsedEventArgs e) {
  124. foreach(SimProfileBase sim in _regionmanager.SimProfiles.Values) {
  125. string SimResponse="";
  126. try {
  127. WebRequest CheckSim = WebRequest.Create("http://" + sim.sim_ip + ":" + sim.sim_port.ToString() + "/checkstatus/");
  128. CheckSim.Method = "GET";
  129. CheckSim.ContentType = "text/plaintext";
  130. CheckSim.ContentLength = 0;
  131. StreamWriter stOut = new StreamWriter(CheckSim.GetRequestStream(), System.Text.Encoding.ASCII);
  132. stOut.Write("");
  133. stOut.Close();
  134. StreamReader stIn = new StreamReader(CheckSim.GetResponse().GetResponseStream());
  135. SimResponse = stIn.ReadToEnd();
  136. stIn.Close();
  137. } catch(Exception exception) {
  138. }
  139. if(SimResponse=="OK") {
  140. _regionmanager.SimProfiles[sim.UUID].online=true;
  141. } else {
  142. _regionmanager.SimProfiles[sim.UUID].online=false;
  143. }
  144. }
  145. }
  146. public void RunCmd(string cmd, string[] cmdparams)
  147. {
  148. switch (cmd)
  149. {
  150. case "help":
  151. m_console.WriteLine("shutdown - shutdown the grid (USE CAUTION!)");
  152. break;
  153. case "shutdown":
  154. m_console.Close();
  155. Environment.Exit(0);
  156. break;
  157. }
  158. }
  159. public void Show(string ShowWhat)
  160. {
  161. }
  162. }
  163. }