1
0

GridServerBase.cs 7.5 KB

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