Main.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.Reflection;
  30. using System.Threading;
  31. using System.Timers;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Framework.Interfaces;
  34. using OpenSim.Framework.Servers;
  35. using OpenSim.GenericConfig;
  36. using Timer=System.Timers.Timer;
  37. namespace OpenSim.Grid.GridServer
  38. {
  39. /// <summary>
  40. /// </summary>
  41. public class OpenGrid_Main : conscmd_callback
  42. {
  43. private string ConfigDll = "OpenSim.Grid.GridServer.Config.dll";
  44. private string GridDll = "OpenSim.Framework.Data.MySQL.dll";
  45. public GridConfig Cfg;
  46. public static OpenGrid_Main thegrid;
  47. protected IGenericConfig localXMLConfig;
  48. public static bool setuponly;
  49. //public LLUUID highestUUID;
  50. // private SimProfileManager m_simProfileManager;
  51. private GridManager m_gridManager;
  52. private LogBase m_console;
  53. [STAThread]
  54. public static void Main(string[] args)
  55. {
  56. if (args.Length > 0)
  57. {
  58. if (args[0] == "-setuponly") setuponly = true;
  59. }
  60. Console.WriteLine("Starting...\n");
  61. thegrid = new OpenGrid_Main();
  62. thegrid.Startup();
  63. thegrid.Work();
  64. }
  65. private void Work()
  66. {
  67. m_console.Notice("Enter help for a list of commands\n");
  68. while (true)
  69. {
  70. m_console.MainLogPrompt();
  71. }
  72. }
  73. private OpenGrid_Main()
  74. {
  75. m_console = new LogBase("opengrid-gridserver-console.log", "OpenGrid", this, false);
  76. MainLog.Instance = m_console;
  77. }
  78. public void managercallback(string cmd)
  79. {
  80. switch (cmd)
  81. {
  82. case "shutdown":
  83. RunCmd("shutdown", new string[0]);
  84. break;
  85. }
  86. }
  87. public void Startup()
  88. {
  89. this.localXMLConfig = new XmlConfig("GridServerConfig.xml");
  90. this.localXMLConfig.LoadData();
  91. this.ConfigDB(this.localXMLConfig);
  92. this.localXMLConfig.Close();
  93. m_console.Verbose( "Main.cs:Startup() - Loading configuration");
  94. Cfg = this.LoadConfigDll(this.ConfigDll);
  95. Cfg.InitConfig();
  96. if (setuponly) Environment.Exit(0);
  97. m_console.Verbose( "Main.cs:Startup() - Connecting to Storage Server");
  98. m_gridManager = new GridManager();
  99. m_gridManager.AddPlugin(GridDll); // Made of win
  100. m_gridManager.config = Cfg;
  101. m_console.Verbose( "Main.cs:Startup() - Starting HTTP process");
  102. BaseHttpServer httpServer = new BaseHttpServer(8001);
  103. //GridManagementAgent GridManagerAgent = new GridManagementAgent(httpServer, "gridserver", Cfg.SimSendKey, Cfg.SimRecvKey, managercallback);
  104. httpServer.AddXmlRPCHandler("simulator_login", m_gridManager.XmlRpcSimulatorLoginMethod);
  105. httpServer.AddXmlRPCHandler("simulator_data_request", m_gridManager.XmlRpcSimulatorDataRequestMethod);
  106. httpServer.AddXmlRPCHandler("map_block", m_gridManager.XmlRpcMapBlockMethod);
  107. httpServer.AddStreamHandler(new RestStreamHandler("GET", "/sims/", m_gridManager.RestGetSimMethod ));
  108. httpServer.AddStreamHandler(new RestStreamHandler("POST", "/sims/", m_gridManager.RestSetSimMethod ));
  109. httpServer.AddStreamHandler( new RestStreamHandler("GET", "/regions/", m_gridManager.RestGetRegionMethod ));
  110. httpServer.AddStreamHandler( new RestStreamHandler("POST","/regions/", m_gridManager.RestSetRegionMethod ));
  111. //httpServer.AddRestHandler("GET", "/sims/", m_gridManager.RestGetSimMethod);
  112. //httpServer.AddRestHandler("POST", "/sims/", m_gridManager.RestSetSimMethod);
  113. //httpServer.AddRestHandler("GET", "/regions/", m_gridManager.RestGetRegionMethod);
  114. //httpServer.AddRestHandler("POST", "/regions/", m_gridManager.RestSetRegionMethod);
  115. httpServer.Start();
  116. m_console.Verbose( "Main.cs:Startup() - Starting sim status checker");
  117. Timer simCheckTimer = new Timer(3600000 * 3); // 3 Hours between updates.
  118. simCheckTimer.Elapsed += new ElapsedEventHandler(CheckSims);
  119. simCheckTimer.Enabled = true;
  120. }
  121. private GridConfig LoadConfigDll(string dllName)
  122. {
  123. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  124. GridConfig config = null;
  125. foreach (Type pluginType in pluginAssembly.GetTypes())
  126. {
  127. if (pluginType.IsPublic)
  128. {
  129. if (!pluginType.IsAbstract)
  130. {
  131. Type typeInterface = pluginType.GetInterface("IGridConfig", true);
  132. if (typeInterface != null)
  133. {
  134. IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  135. config = plug.GetConfigObject();
  136. break;
  137. }
  138. typeInterface = null;
  139. }
  140. }
  141. }
  142. pluginAssembly = null;
  143. return config;
  144. }
  145. public void CheckSims(object sender, ElapsedEventArgs e)
  146. {
  147. /*
  148. foreach (SimProfileBase sim in m_simProfileManager.SimProfiles.Values)
  149. {
  150. string SimResponse = "";
  151. try
  152. {
  153. WebRequest CheckSim = WebRequest.Create("http://" + sim.sim_ip + ":" + sim.sim_port.ToString() + "/checkstatus/");
  154. CheckSim.Method = "GET";
  155. CheckSim.ContentType = "text/plaintext";
  156. CheckSim.ContentLength = 0;
  157. StreamWriter stOut = new StreamWriter(CheckSim.GetRequestStream(), System.Text.Encoding.ASCII);
  158. stOut.Write("");
  159. stOut.Close();
  160. StreamReader stIn = new StreamReader(CheckSim.GetResponse().GetResponseStream());
  161. SimResponse = stIn.ReadToEnd();
  162. stIn.Close();
  163. }
  164. catch
  165. {
  166. }
  167. if (SimResponse == "OK")
  168. {
  169. m_simProfileManager.SimProfiles[sim.UUID].online = true;
  170. }
  171. else
  172. {
  173. m_simProfileManager.SimProfiles[sim.UUID].online = false;
  174. }
  175. }
  176. */
  177. }
  178. public void RunCmd(string cmd, string[] cmdparams)
  179. {
  180. switch (cmd)
  181. {
  182. case "help":
  183. m_console.Notice("shutdown - shutdown the grid (USE CAUTION!)");
  184. break;
  185. case "shutdown":
  186. m_console.Close();
  187. Environment.Exit(0);
  188. break;
  189. }
  190. }
  191. public void Show(string ShowWhat)
  192. {
  193. }
  194. private void ConfigDB(IGenericConfig configData)
  195. {
  196. try
  197. {
  198. string attri = "";
  199. attri = configData.GetAttribute("DataBaseProvider");
  200. if (attri == "")
  201. {
  202. GridDll = "OpenSim.Framework.Data.DB4o.dll";
  203. configData.SetAttribute("DataBaseProvider", "OpenSim.Framework.Data.DB4o.dll");
  204. }
  205. else
  206. {
  207. GridDll = attri;
  208. }
  209. configData.Commit();
  210. }
  211. catch
  212. {
  213. }
  214. }
  215. }
  216. }