HttpServerBase.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Threading;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Console;
  33. using OpenSim.Framework.Servers.HttpServer;
  34. using log4net;
  35. using Nini.Config;
  36. namespace OpenSim.Server.Base
  37. {
  38. public class HttpServerBase : ServicesServerBase
  39. {
  40. // Logger
  41. //
  42. private static readonly ILog m_Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. // The http server instance
  44. //
  45. protected BaseHttpServer m_HttpServer = null;
  46. protected uint m_Port = 0;
  47. protected Dictionary<uint, BaseHttpServer> m_Servers =
  48. new Dictionary<uint, BaseHttpServer>();
  49. public IHttpServer HttpServer
  50. {
  51. get { return m_HttpServer; }
  52. }
  53. public uint DefaultPort
  54. {
  55. get { return m_Port; }
  56. }
  57. public IHttpServer GetHttpServer(uint port)
  58. {
  59. m_Log.InfoFormat("[SERVER]: Requested port {0}", port);
  60. if (port == m_Port)
  61. return HttpServer;
  62. if (m_Servers.ContainsKey(port))
  63. return m_Servers[port];
  64. m_Servers[port] = new BaseHttpServer(port);
  65. m_Servers[port].Start();
  66. return m_Servers[port];
  67. }
  68. // Handle all the automagical stuff
  69. //
  70. public HttpServerBase(string prompt, string[] args) : base(prompt, args)
  71. {
  72. }
  73. protected override void ReadConfig()
  74. {
  75. IConfig networkConfig = m_Config.Configs["Network"];
  76. if (networkConfig == null)
  77. {
  78. System.Console.WriteLine("Section 'Network' not found, server can't start");
  79. Thread.CurrentThread.Abort();
  80. }
  81. uint port = (uint)networkConfig.GetInt("port", 0);
  82. if (port == 0)
  83. {
  84. System.Console.WriteLine("Port number not specified or 0, server can't start");
  85. Thread.CurrentThread.Abort();
  86. }
  87. m_Port = port;
  88. m_HttpServer = new BaseHttpServer(port);
  89. MainServer.Instance = m_HttpServer;
  90. }
  91. protected override void Initialise()
  92. {
  93. m_HttpServer.Start();
  94. if (MainConsole.Instance is RemoteConsole)
  95. {
  96. ((RemoteConsole)MainConsole.Instance).SetServer(m_HttpServer);
  97. }
  98. }
  99. }
  100. }