1
0

GridServerBase.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 System.Timers;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Grid.Framework;
  39. namespace OpenSim.Grid.GridServer
  40. {
  41. /// <summary>
  42. /// </summary>
  43. public class GridServerBase : BaseOpenSimServer, IGridServiceCore
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. protected GridConfig m_config;
  47. public string m_consoleType = "local";
  48. public IConfigSource m_configSource = null;
  49. public string m_configFile = "GridServer_Config.xml";
  50. public GridConfig Config
  51. {
  52. get { return m_config; }
  53. }
  54. public string Version
  55. {
  56. get { return m_version; }
  57. }
  58. protected List<IGridPlugin> m_plugins = new List<IGridPlugin>();
  59. public void Work()
  60. {
  61. m_console.Output("Enter help for a list of commands\n");
  62. while (true)
  63. {
  64. m_console.Prompt();
  65. }
  66. }
  67. public GridServerBase()
  68. {
  69. }
  70. protected override void StartupSpecific()
  71. {
  72. switch (m_consoleType)
  73. {
  74. case "rest":
  75. m_console = new RemoteConsole("Grid");
  76. break;
  77. case "basic":
  78. m_console = new CommandConsole("Grid");
  79. break;
  80. default:
  81. m_console = new LocalConsole("Grid");
  82. break;
  83. }
  84. MainConsole.Instance = m_console;
  85. m_config = new GridConfig("GRID SERVER", (Path.Combine(Util.configDir(), m_configFile)));
  86. m_log.Info("[GRID]: Starting HTTP process");
  87. m_httpServer = new BaseHttpServer(m_config.HttpPort);
  88. if (m_console is RemoteConsole)
  89. {
  90. RemoteConsole c = (RemoteConsole)m_console;
  91. c.SetServer(m_httpServer);
  92. IConfig netConfig = m_configSource.AddConfig("Network");
  93. netConfig.Set("ConsoleUser", m_config.ConsoleUser);
  94. netConfig.Set("ConsolePass", m_config.ConsolePass);
  95. c.ReadConfig(m_configSource);
  96. }
  97. LoadPlugins();
  98. m_httpServer.Start();
  99. base.StartupSpecific();
  100. }
  101. protected virtual void LoadPlugins()
  102. {
  103. PluginLoader<IGridPlugin> loader =
  104. new PluginLoader<IGridPlugin>(new GridPluginInitialiser(this));
  105. loader.Load("/OpenSim/GridServer");
  106. m_plugins = loader.Plugins;
  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. }