GridServerBase.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 OpenSimulator 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.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Grid.Framework;
  38. namespace OpenSim.Grid.GridServer
  39. {
  40. /// <summary>
  41. /// </summary>
  42. public class GridServerBase : BaseOpenSimServer, IGridServiceCore
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. protected GridConfig m_config;
  46. public string m_consoleType = "local";
  47. public IConfigSource m_configSource = null;
  48. public string m_configFile = "GridServer_Config.xml";
  49. public GridConfig Config
  50. {
  51. get { return m_config; }
  52. }
  53. public string Version
  54. {
  55. get { return m_version; }
  56. }
  57. protected List<IGridPlugin> m_plugins = new List<IGridPlugin>();
  58. public void Work()
  59. {
  60. m_console.Output("Enter help for a list of commands\n");
  61. while (true)
  62. {
  63. m_console.Prompt();
  64. }
  65. }
  66. public GridServerBase()
  67. {
  68. }
  69. protected override void StartupSpecific()
  70. {
  71. switch (m_consoleType)
  72. {
  73. case "rest":
  74. m_console = new RemoteConsole("Grid");
  75. break;
  76. case "basic":
  77. m_console = new CommandConsole("Grid");
  78. break;
  79. default:
  80. m_console = new LocalConsole("Grid");
  81. break;
  82. }
  83. MainConsole.Instance = m_console;
  84. m_config = new GridConfig("GRID SERVER", (Path.Combine(Util.configDir(), m_configFile)));
  85. m_log.Info("[GRID]: Starting HTTP process");
  86. m_httpServer = new BaseHttpServer(m_config.HttpPort);
  87. if (m_console is RemoteConsole)
  88. {
  89. RemoteConsole c = (RemoteConsole)m_console;
  90. c.SetServer(m_httpServer);
  91. IConfig netConfig = m_configSource.AddConfig("Network");
  92. netConfig.Set("ConsoleUser", m_config.ConsoleUser);
  93. netConfig.Set("ConsolePass", m_config.ConsolePass);
  94. c.ReadConfig(m_configSource);
  95. }
  96. LoadPlugins();
  97. m_httpServer.Start();
  98. base.StartupSpecific();
  99. }
  100. protected virtual void LoadPlugins()
  101. {
  102. using (PluginLoader<IGridPlugin> loader = new PluginLoader<IGridPlugin>(new GridPluginInitialiser(this)))
  103. {
  104. loader.Load("/OpenSim/GridServer");
  105. m_plugins = loader.Plugins;
  106. }
  107. }
  108. public override void ShutdownSpecific()
  109. {
  110. foreach (IGridPlugin plugin in m_plugins) plugin.Dispose();
  111. }
  112. #region IServiceCore
  113. protected Dictionary<Type, object> m_moduleInterfaces = new Dictionary<Type, object>();
  114. /// <summary>
  115. /// Register an Module interface.
  116. /// </summary>
  117. /// <typeparam name="T"></typeparam>
  118. /// <param name="iface"></param>
  119. public void RegisterInterface<T>(T iface)
  120. {
  121. lock (m_moduleInterfaces)
  122. {
  123. if (!m_moduleInterfaces.ContainsKey(typeof(T)))
  124. {
  125. m_moduleInterfaces.Add(typeof(T), iface);
  126. }
  127. }
  128. }
  129. public bool TryGet<T>(out T iface)
  130. {
  131. if (m_moduleInterfaces.ContainsKey(typeof(T)))
  132. {
  133. iface = (T)m_moduleInterfaces[typeof(T)];
  134. return true;
  135. }
  136. iface = default(T);
  137. return false;
  138. }
  139. public T Get<T>()
  140. {
  141. return (T)m_moduleInterfaces[typeof(T)];
  142. }
  143. public BaseHttpServer GetHttpServer()
  144. {
  145. return m_httpServer;
  146. }
  147. #endregion
  148. }
  149. }