GridServerBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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 GridConfig Config
  47. {
  48. get { return m_config; }
  49. }
  50. public string Version
  51. {
  52. get { return m_version; }
  53. }
  54. protected List<IGridPlugin> m_plugins = new List<IGridPlugin>();
  55. public void Work()
  56. {
  57. m_console.Output("Enter help for a list of commands\n");
  58. while (true)
  59. {
  60. m_console.Prompt();
  61. }
  62. }
  63. public GridServerBase()
  64. {
  65. m_console = new LocalConsole("Grid");
  66. MainConsole.Instance = m_console;
  67. }
  68. protected override void StartupSpecific()
  69. {
  70. m_config = new GridConfig("GRID SERVER", (Path.Combine(Util.configDir(), "GridServer_Config.xml")));
  71. m_log.Info("[GRID]: Starting HTTP process");
  72. m_httpServer = new BaseHttpServer(m_config.HttpPort);
  73. LoadPlugins();
  74. m_httpServer.Start();
  75. base.StartupSpecific();
  76. }
  77. protected virtual void LoadPlugins()
  78. {
  79. PluginLoader<IGridPlugin> loader =
  80. new PluginLoader<IGridPlugin>(new GridPluginInitialiser(this));
  81. loader.Load("/OpenSim/GridServer");
  82. m_plugins = loader.Plugins;
  83. }
  84. public override void ShutdownSpecific()
  85. {
  86. foreach (IGridPlugin plugin in m_plugins) plugin.Dispose();
  87. }
  88. #region IServiceCore
  89. protected Dictionary<Type, object> m_moduleInterfaces = new Dictionary<Type, object>();
  90. /// <summary>
  91. /// Register an Module interface.
  92. /// </summary>
  93. /// <typeparam name="T"></typeparam>
  94. /// <param name="iface"></param>
  95. public void RegisterInterface<T>(T iface)
  96. {
  97. lock (m_moduleInterfaces)
  98. {
  99. if (!m_moduleInterfaces.ContainsKey(typeof(T)))
  100. {
  101. m_moduleInterfaces.Add(typeof(T), iface);
  102. }
  103. }
  104. }
  105. public bool TryGet<T>(out T iface)
  106. {
  107. if (m_moduleInterfaces.ContainsKey(typeof(T)))
  108. {
  109. iface = (T)m_moduleInterfaces[typeof(T)];
  110. return true;
  111. }
  112. iface = default(T);
  113. return false;
  114. }
  115. public T Get<T>()
  116. {
  117. return (T)m_moduleInterfaces[typeof(T)];
  118. }
  119. public BaseHttpServer GetHttpServer()
  120. {
  121. return m_httpServer;
  122. }
  123. #endregion
  124. }
  125. }