GridServerBase.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Generic;
  29. using System.IO;
  30. using System.Timers;
  31. using Mono.Addins;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Servers;
  35. namespace OpenSim.Grid.GridServer
  36. {
  37. /// <summary>
  38. /// </summary>
  39. public class GridServerBase : BaseOpenSimServer, conscmd_callback
  40. {
  41. protected GridConfig m_config;
  42. protected GridManager m_gridManager;
  43. protected List<IGridPlugin> m_plugins = new List<IGridPlugin>();
  44. public void Work()
  45. {
  46. m_console.Notice("Enter help for a list of commands\n");
  47. while (true)
  48. {
  49. m_console.Prompt();
  50. }
  51. }
  52. public GridServerBase()
  53. {
  54. m_console = new ConsoleBase("OpenGrid", this);
  55. MainConsole.Instance = m_console;
  56. }
  57. public void managercallback(string cmd)
  58. {
  59. switch (cmd)
  60. {
  61. case "shutdown":
  62. RunCmd("shutdown", new string[0]);
  63. break;
  64. }
  65. }
  66. public void Startup()
  67. {
  68. m_console.Status("Starting...\n");
  69. Config();
  70. SetupGridManager();
  71. m_console.Status("[GRID]: Starting HTTP process");
  72. m_httpServer = new BaseHttpServer(m_config.HttpPort);
  73. AddHttpHandlers();
  74. LoadGridPlugins( );
  75. m_httpServer.Start();
  76. m_console.Status("[GRID]: Starting sim status checker");
  77. Timer simCheckTimer = new Timer(3600000 * 3); // 3 Hours between updates.
  78. simCheckTimer.Elapsed += new ElapsedEventHandler(CheckSims);
  79. simCheckTimer.Enabled = true;
  80. }
  81. protected void AddHttpHandlers()
  82. {
  83. m_httpServer.AddXmlRPCHandler("simulator_login", m_gridManager.XmlRpcSimulatorLoginMethod);
  84. m_httpServer.AddXmlRPCHandler("simulator_data_request", m_gridManager.XmlRpcSimulatorDataRequestMethod);
  85. m_httpServer.AddXmlRPCHandler("simulator_after_region_moved", m_gridManager.XmlRpcDeleteRegionMethod);
  86. m_httpServer.AddXmlRPCHandler("map_block", m_gridManager.XmlRpcMapBlockMethod);
  87. // Message Server ---> Grid Server
  88. m_httpServer.AddXmlRPCHandler("register_messageserver", m_gridManager.XmlRPCRegisterMessageServer);
  89. m_httpServer.AddXmlRPCHandler("deregister_messageserver", m_gridManager.XmlRPCDeRegisterMessageServer);
  90. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/sims/", m_gridManager.RestGetSimMethod));
  91. m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/sims/", m_gridManager.RestSetSimMethod));
  92. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/regions/", m_gridManager.RestGetRegionMethod));
  93. m_httpServer.AddStreamHandler(new RestStreamHandler("POST", "/regions/", m_gridManager.RestSetRegionMethod));
  94. }
  95. protected void LoadGridPlugins()
  96. {
  97. //m_console.Status("[GRIDPLUGINS]: Looking for plugins");
  98. AddinManager.Initialize(".");
  99. AddinManager.Registry.Update(null);
  100. ExtensionNodeList nodes = AddinManager.GetExtensionNodes("/OpenSim/GridServer");
  101. foreach (TypeExtensionNode node in nodes)
  102. {
  103. m_console.Status("[GRIDPLUGINS]: Loading OpenSim plugin " + node.Path);
  104. IGridPlugin plugin = (IGridPlugin)node.CreateInstance();
  105. plugin.Initialise(this);
  106. m_plugins.Add(plugin);
  107. }
  108. }
  109. protected virtual void SetupGridManager()
  110. {
  111. m_console.Status("[GRID]: Connecting to Storage Server");
  112. m_gridManager = new GridManager();
  113. m_gridManager.AddPlugin(m_config.DatabaseProvider);
  114. m_gridManager.Config = m_config;
  115. }
  116. public void Config()
  117. {
  118. m_config = new GridConfig("GRID SERVER", (Path.Combine(Util.configDir(), "GridServer_Config.xml")));
  119. }
  120. public void CheckSims(object sender, ElapsedEventArgs e)
  121. {
  122. /*
  123. foreach (SimProfileBase sim in m_simProfileManager.SimProfiles.Values)
  124. {
  125. string SimResponse = String.Empty;
  126. try
  127. {
  128. WebRequest CheckSim = WebRequest.Create("http://" + sim.sim_ip + ":" + sim.sim_port.ToString() + "/checkstatus/");
  129. CheckSim.Method = "GET";
  130. CheckSim.ContentType = "text/plaintext";
  131. CheckSim.ContentLength = 0;
  132. StreamWriter stOut = new StreamWriter(CheckSim.GetRequestStream(), System.Text.Encoding.ASCII);
  133. stOut.Write(String.Empty);
  134. stOut.Close();
  135. StreamReader stIn = new StreamReader(CheckSim.GetResponse().GetResponseStream());
  136. SimResponse = stIn.ReadToEnd();
  137. stIn.Close();
  138. }
  139. catch
  140. {
  141. }
  142. if (SimResponse == "OK")
  143. {
  144. m_simProfileManager.SimProfiles[sim.UUID].online = true;
  145. }
  146. else
  147. {
  148. m_simProfileManager.SimProfiles[sim.UUID].online = false;
  149. }
  150. }
  151. */
  152. }
  153. public override void RunCmd(string cmd, string[] cmdparams)
  154. {
  155. base.RunCmd(cmd, cmdparams);
  156. switch (cmd)
  157. {
  158. case "help":
  159. m_console.Notice("shutdown - shutdown the grid (USE CAUTION!)");
  160. break;
  161. case "shutdown":
  162. foreach (IGridPlugin plugin in m_plugins) plugin.Close();
  163. m_console.Close();
  164. Environment.Exit(0);
  165. break;
  166. }
  167. }
  168. }
  169. }