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