MainServer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.Reflection;
  30. using System.Net;
  31. using log4net;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Framework.Servers.HttpServer;
  35. namespace OpenSim.Framework.Servers
  36. {
  37. public class MainServer
  38. {
  39. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. private static BaseHttpServer instance = null;
  41. private static Dictionary<uint, BaseHttpServer> m_Servers = new Dictionary<uint, BaseHttpServer>();
  42. /// <summary>
  43. /// Control the printing of certain debug messages.
  44. /// </summary>
  45. /// <remarks>
  46. /// If DebugLevel >= 1, then short warnings are logged when receiving bad input data.
  47. /// If DebugLevel >= 2, then long warnings are logged when receiving bad input data.
  48. /// If DebugLevel >= 3, then short notices about all incoming non-poll HTTP requests are logged.
  49. /// </remarks>
  50. public static int DebugLevel
  51. {
  52. get { return s_debugLevel; }
  53. set
  54. {
  55. s_debugLevel = value;
  56. lock (m_Servers)
  57. foreach (BaseHttpServer server in m_Servers.Values)
  58. server.DebugLevel = s_debugLevel;
  59. }
  60. }
  61. private static int s_debugLevel;
  62. /// <summary>
  63. /// Set the main HTTP server instance.
  64. /// </summary>
  65. /// <remarks>
  66. /// This will be used to register all handlers that listen to the default port.
  67. /// </remarks>
  68. /// <exception cref='Exception'>
  69. /// Thrown if the HTTP server has not already been registered via AddHttpServer()
  70. /// </exception>
  71. public static BaseHttpServer Instance
  72. {
  73. get { return instance; }
  74. set
  75. {
  76. lock (m_Servers)
  77. if (!m_Servers.ContainsValue(value))
  78. throw new Exception("HTTP server must already have been registered to be set as the main instance");
  79. instance = value;
  80. }
  81. }
  82. /// <summary>
  83. /// Get all the registered servers.
  84. /// </summary>
  85. /// <remarks>
  86. /// Returns a copy of the dictionary so this can be iterated through without locking.
  87. /// </remarks>
  88. /// <value></value>
  89. public static Dictionary<uint, BaseHttpServer> Servers
  90. {
  91. get { return new Dictionary<uint, BaseHttpServer>(m_Servers); }
  92. }
  93. public static void RegisterHttpConsoleCommands(ICommandConsole console)
  94. {
  95. console.Commands.AddCommand(
  96. "Debug", false, "debug http", "debug http [<level>]",
  97. "Turn on inbound non-poll http request debugging.",
  98. "If level <= 0, then no extra logging is done.\n"
  99. + "If level >= 1, then short warnings are logged when receiving bad input data.\n"
  100. + "If level >= 2, then long warnings are logged when receiving bad input data.\n"
  101. + "If level >= 3, then short notices about all incoming non-poll HTTP requests are logged.\n"
  102. + "If no level is specified then the current level is returned.",
  103. HandleDebugHttpCommand);
  104. }
  105. /// <summary>
  106. /// Turn on some debugging values for OpenSim.
  107. /// </summary>
  108. /// <param name="args"></param>
  109. private static void HandleDebugHttpCommand(string module, string[] args)
  110. {
  111. if (args.Length == 3)
  112. {
  113. int newDebug;
  114. if (int.TryParse(args[2], out newDebug))
  115. {
  116. MainServer.DebugLevel = newDebug;
  117. MainConsole.Instance.OutputFormat("Debug http level set to {0}", newDebug);
  118. }
  119. }
  120. else if (args.Length == 2)
  121. {
  122. MainConsole.Instance.OutputFormat("Current debug http level is {0}", MainServer.DebugLevel);
  123. }
  124. else
  125. {
  126. MainConsole.Instance.Output("Usage: debug http 0..3");
  127. }
  128. }
  129. /// <summary>
  130. /// Register an already started HTTP server to the collection of known servers.
  131. /// </summary>
  132. /// <param name='server'></param>
  133. public static void AddHttpServer(BaseHttpServer server)
  134. {
  135. lock (m_Servers)
  136. {
  137. if (m_Servers.ContainsKey(server.Port))
  138. throw new Exception(string.Format("HTTP server for port {0} already exists.", server.Port));
  139. m_Servers.Add(server.Port, server);
  140. }
  141. }
  142. /// <summary>
  143. /// Removes the http server listening on the given port.
  144. /// </summary>
  145. /// <remarks>
  146. /// It is the responsibility of the caller to do clean up.
  147. /// </remarks>
  148. /// <param name='port'></param>
  149. /// <returns></returns>
  150. public static bool RemoveHttpServer(uint port)
  151. {
  152. lock (m_Servers)
  153. return m_Servers.Remove(port);
  154. }
  155. /// <summary>
  156. /// Does this collection of servers contain one with the given port?
  157. /// </summary>
  158. /// <remarks>
  159. /// Unlike GetHttpServer, this will not instantiate a server if one does not exist on that port.
  160. /// </remarks>
  161. /// <param name='port'></param>
  162. /// <returns>true if a server with the given port is registered, false otherwise.</returns>
  163. public static bool ContainsHttpServer(uint port)
  164. {
  165. lock (m_Servers)
  166. return m_Servers.ContainsKey(port);
  167. }
  168. /// <summary>
  169. /// Get the default http server or an http server for a specific port.
  170. /// </summary>
  171. /// <remarks>
  172. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  173. /// </remarks>
  174. /// <returns></returns>
  175. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  176. public static IHttpServer GetHttpServer(uint port)
  177. {
  178. return GetHttpServer(port, null);
  179. }
  180. /// <summary>
  181. /// Get the default http server, an http server for a specific port
  182. /// and/or an http server bound to a specific address
  183. /// </summary>
  184. /// <remarks>
  185. /// If the requested HTTP server doesn't already exist then a new one is instantiated and started.
  186. /// </remarks>
  187. /// <returns></returns>
  188. /// <param name='port'>If 0 then the default HTTP server is returned.</param>
  189. /// <param name='ipaddr'>A specific IP address to bind to. If null then the default IP address is used.</param>
  190. public static IHttpServer GetHttpServer(uint port, IPAddress ipaddr)
  191. {
  192. if (port == 0)
  193. return Instance;
  194. if (instance != null && port == Instance.Port)
  195. return Instance;
  196. lock (m_Servers)
  197. {
  198. if (m_Servers.ContainsKey(port))
  199. return m_Servers[port];
  200. m_Servers[port] = new BaseHttpServer(port);
  201. if (ipaddr != null)
  202. m_Servers[port].ListenIPAddress = ipaddr;
  203. m_Servers[port].Start();
  204. return m_Servers[port];
  205. }
  206. }
  207. }
  208. }