Main.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Threading;
  33. using System.Reflection;
  34. using libsecondlife;
  35. using OpenGrid.Framework.Manager;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Sims;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Interfaces;
  40. using OpenSim.Servers;
  41. using OpenSim.GenericConfig;
  42. namespace OpenGridServices.GridServer
  43. {
  44. /// <summary>
  45. /// </summary>
  46. public class OpenGrid_Main : BaseServer, conscmd_callback
  47. {
  48. private string ConfigDll = "OpenGrid.Config.GridConfigDb4o.dll";
  49. private string GridDll = "OpenGrid.Framework.Data.DB4o.dll";
  50. public GridConfig Cfg;
  51. public static OpenGrid_Main thegrid;
  52. protected IGenericConfig localXMLConfig;
  53. public static bool setuponly;
  54. //public LLUUID highestUUID;
  55. // private SimProfileManager m_simProfileManager;
  56. private GridManager m_gridManager;
  57. private ConsoleBase m_console;
  58. [STAThread]
  59. public static void Main(string[] args)
  60. {
  61. if (args.Length > 0)
  62. {
  63. if (args[0] == "-setuponly") setuponly = true;
  64. }
  65. Console.WriteLine("Starting...\n");
  66. thegrid = new OpenGrid_Main();
  67. thegrid.Startup();
  68. thegrid.Work();
  69. }
  70. private void Work()
  71. {
  72. while (true)
  73. {
  74. Thread.Sleep(5000);
  75. // should flush the DB etc here
  76. }
  77. }
  78. private OpenGrid_Main()
  79. {
  80. m_console = new ConsoleBase("opengrid-gridserver-console.log", "OpenGrid", this, false);
  81. MainConsole.Instance = m_console;
  82. }
  83. public void managercallback(string cmd)
  84. {
  85. switch (cmd)
  86. {
  87. case "shutdown":
  88. RunCmd("shutdown", new string[0]);
  89. break;
  90. }
  91. }
  92. public void Startup()
  93. {
  94. this.localXMLConfig = new XmlConfig("GridServerConfig.xml");
  95. this.localXMLConfig.LoadData();
  96. this.ConfigDB(this.localXMLConfig);
  97. this.localXMLConfig.Close();
  98. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Loading configuration");
  99. Cfg = this.LoadConfigDll(this.ConfigDll);
  100. Cfg.InitConfig();
  101. if (setuponly) Environment.Exit(0);
  102. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Connecting to Storage Server");
  103. m_gridManager = new GridManager();
  104. m_gridManager.AddPlugin(GridDll); // Made of win
  105. m_gridManager.config = Cfg;
  106. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Starting HTTP process");
  107. BaseHttpServer httpServer = new BaseHttpServer(8001);
  108. GridManagementAgent GridManagerAgent = new GridManagementAgent(httpServer, "gridserver", Cfg.SimSendKey, Cfg.SimRecvKey, managercallback);
  109. httpServer.AddXmlRPCHandler("simulator_login", m_gridManager.XmlRpcLoginToSimulatorMethod);
  110. httpServer.AddXmlRPCHandler("map_block", m_gridManager.XmlRpcMapBlockMethod);
  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. // lbsa71 : This code snippet taken from old http server.
  116. // I have no idea what this was supposed to do - looks like an infinite recursion to me.
  117. // case "regions":
  118. //// DIRTY HACK ALERT
  119. //Console.WriteLine("/regions/ accessed");
  120. //TheSim = OpenGrid_Main.thegrid._regionmanager.GetProfileByHandle((ulong)Convert.ToUInt64(rest_params[1]));
  121. //respstring = ParseREST("/regions/" + rest_params[1], requestBody, HTTPmethod);
  122. //break;
  123. // lbsa71 : I guess these were never used?
  124. //Listener.Prefixes.Add("http://+:8001/gods/");
  125. //Listener.Prefixes.Add("http://+:8001/highestuuid/");
  126. //Listener.Prefixes.Add("http://+:8001/uuidblocks/");
  127. httpServer.Start();
  128. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Starting sim status checker");
  129. System.Timers.Timer simCheckTimer = new System.Timers.Timer(300000); // 5 minutes
  130. simCheckTimer.Elapsed += new ElapsedEventHandler(CheckSims);
  131. simCheckTimer.Enabled = true;
  132. }
  133. private GridConfig LoadConfigDll(string dllName)
  134. {
  135. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  136. GridConfig config = null;
  137. foreach (Type pluginType in pluginAssembly.GetTypes())
  138. {
  139. if (pluginType.IsPublic)
  140. {
  141. if (!pluginType.IsAbstract)
  142. {
  143. Type typeInterface = pluginType.GetInterface("IGridConfig", true);
  144. if (typeInterface != null)
  145. {
  146. IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  147. config = plug.GetConfigObject();
  148. break;
  149. }
  150. typeInterface = null;
  151. }
  152. }
  153. }
  154. pluginAssembly = null;
  155. return config;
  156. }
  157. public void CheckSims(object sender, ElapsedEventArgs e)
  158. {
  159. /*
  160. foreach (SimProfileBase sim in m_simProfileManager.SimProfiles.Values)
  161. {
  162. string SimResponse = "";
  163. try
  164. {
  165. WebRequest CheckSim = WebRequest.Create("http://" + sim.sim_ip + ":" + sim.sim_port.ToString() + "/checkstatus/");
  166. CheckSim.Method = "GET";
  167. CheckSim.ContentType = "text/plaintext";
  168. CheckSim.ContentLength = 0;
  169. StreamWriter stOut = new StreamWriter(CheckSim.GetRequestStream(), System.Text.Encoding.ASCII);
  170. stOut.Write("");
  171. stOut.Close();
  172. StreamReader stIn = new StreamReader(CheckSim.GetResponse().GetResponseStream());
  173. SimResponse = stIn.ReadToEnd();
  174. stIn.Close();
  175. }
  176. catch
  177. {
  178. }
  179. if (SimResponse == "OK")
  180. {
  181. m_simProfileManager.SimProfiles[sim.UUID].online = true;
  182. }
  183. else
  184. {
  185. m_simProfileManager.SimProfiles[sim.UUID].online = false;
  186. }
  187. }
  188. */
  189. }
  190. public void RunCmd(string cmd, string[] cmdparams)
  191. {
  192. switch (cmd)
  193. {
  194. case "help":
  195. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "shutdown - shutdown the grid (USE CAUTION!)");
  196. break;
  197. case "shutdown":
  198. m_console.Close();
  199. Environment.Exit(0);
  200. break;
  201. }
  202. }
  203. public void Show(string ShowWhat)
  204. {
  205. }
  206. private void ConfigDB(IGenericConfig configData)
  207. {
  208. try
  209. {
  210. string attri = "";
  211. attri = configData.GetAttribute("DataBaseProvider");
  212. if (attri == "")
  213. {
  214. GridDll = "OpenGrid.Framework.Data.DB4o.dll";
  215. configData.SetAttribute("DataBaseProvider", "OpenGrid.Framework.Data.DB4o.dll");
  216. }
  217. else
  218. {
  219. GridDll = attri;
  220. }
  221. configData.Commit();
  222. }
  223. catch (Exception e)
  224. {
  225. }
  226. }
  227. }
  228. }