diff options
Diffstat (limited to 'OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs')
-rw-r--r-- | OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs | 451 |
1 files changed, 451 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs b/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs new file mode 100644 index 0000000..e22328d --- /dev/null +++ b/OpenSim/Server/Handlers/Grid/GridServerPostHandler.cs | |||
@@ -0,0 +1,451 @@ | |||
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 | |||
28 | using Nini.Config; | ||
29 | using log4net; | ||
30 | using System; | ||
31 | using System.Reflection; | ||
32 | using System.IO; | ||
33 | using System.Net; | ||
34 | using System.Text; | ||
35 | using System.Text.RegularExpressions; | ||
36 | using System.Xml; | ||
37 | using System.Xml.Serialization; | ||
38 | using System.Collections.Generic; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
42 | using OpenSim.Framework; | ||
43 | using OpenSim.Framework.Servers.HttpServer; | ||
44 | using OpenMetaverse; | ||
45 | |||
46 | namespace OpenSim.Server.Handlers.Grid | ||
47 | { | ||
48 | public class GridServerPostHandler : BaseStreamHandler | ||
49 | { | ||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
51 | |||
52 | private IGridService m_GridService; | ||
53 | |||
54 | public GridServerPostHandler(IGridService service) : | ||
55 | base("POST", "/grid") | ||
56 | { | ||
57 | m_GridService = service; | ||
58 | } | ||
59 | |||
60 | public override byte[] Handle(string path, Stream requestData, | ||
61 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
62 | { | ||
63 | StreamReader sr = new StreamReader(requestData); | ||
64 | string body = sr.ReadToEnd(); | ||
65 | sr.Close(); | ||
66 | body = body.Trim(); | ||
67 | |||
68 | //m_log.DebugFormat("[XXX]: query String: {0}", body); | ||
69 | |||
70 | Dictionary<string, string> request = | ||
71 | ServerUtils.ParseQueryString(body); | ||
72 | |||
73 | if (!request.ContainsKey("METHOD")) | ||
74 | return FailureResult(); | ||
75 | |||
76 | string method = request["METHOD"]; | ||
77 | |||
78 | switch (method) | ||
79 | { | ||
80 | case "register": | ||
81 | return Register(request); | ||
82 | |||
83 | case "deregister": | ||
84 | return Deregister(request); | ||
85 | |||
86 | case "get_neighbours": | ||
87 | return GetNeighbours(request); | ||
88 | |||
89 | case "get_region_by_uuid": | ||
90 | return GetRegionByUUID(request); | ||
91 | |||
92 | case "get_region_by_position": | ||
93 | return GetRegionByPosition(request); | ||
94 | |||
95 | case "get_region_by_name": | ||
96 | return GetRegionByName(request); | ||
97 | |||
98 | case "get_regions_by_name": | ||
99 | return GetRegionsByName(request); | ||
100 | |||
101 | case "get_region_range": | ||
102 | return GetRegionRange(request); | ||
103 | |||
104 | } | ||
105 | |||
106 | m_log.DebugFormat("[GRID HANDLER]: unknown method {0} request {1}", method.Length, method); | ||
107 | return FailureResult(); | ||
108 | |||
109 | } | ||
110 | |||
111 | #region Method-specific handlers | ||
112 | |||
113 | byte[] Register(Dictionary<string, string> request) | ||
114 | { | ||
115 | UUID scopeID = UUID.Zero; | ||
116 | if (request["SCOPEID"] != null) | ||
117 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
118 | else | ||
119 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region"); | ||
120 | |||
121 | int versionNumberMin = 0, versionNumberMax = 0; | ||
122 | if (request.ContainsKey("VERSIONMIN")) | ||
123 | Int32.TryParse(request["VERSIONMIN"], out versionNumberMin); | ||
124 | else | ||
125 | m_log.WarnFormat("[GRID HANDLER]: no minimum protocol version in request to register region"); | ||
126 | |||
127 | if (request.ContainsKey("VERSIONMAX")) | ||
128 | Int32.TryParse(request["VERSIONMAX"], out versionNumberMax); | ||
129 | else | ||
130 | m_log.WarnFormat("[GRID HANDLER]: no maximum protocol version in request to register region"); | ||
131 | |||
132 | // Check the protocol version | ||
133 | if ((versionNumberMin > ProtocolVersions.ServerProtocolVersionMax && versionNumberMax < ProtocolVersions.ServerProtocolVersionMax)) | ||
134 | { | ||
135 | // Can't do, there is no overlap in the acceptable ranges | ||
136 | return FailureResult(); | ||
137 | } | ||
138 | |||
139 | Dictionary<string, object> rinfoData = new Dictionary<string, object>(); | ||
140 | foreach (KeyValuePair<string, string> kvp in request) | ||
141 | rinfoData[kvp.Key] = kvp.Value; | ||
142 | GridRegion rinfo = new GridRegion(rinfoData); | ||
143 | |||
144 | bool result = m_GridService.RegisterRegion(scopeID, rinfo); | ||
145 | |||
146 | if (result) | ||
147 | return SuccessResult(); | ||
148 | else | ||
149 | return FailureResult(); | ||
150 | } | ||
151 | |||
152 | byte[] Deregister(Dictionary<string, string> request) | ||
153 | { | ||
154 | UUID regionID = UUID.Zero; | ||
155 | if (request["REGIONID"] != null) | ||
156 | UUID.TryParse(request["REGIONID"], out regionID); | ||
157 | else | ||
158 | m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region"); | ||
159 | |||
160 | bool result = m_GridService.DeregisterRegion(regionID); | ||
161 | |||
162 | if (result) | ||
163 | return SuccessResult(); | ||
164 | else | ||
165 | return FailureResult(); | ||
166 | |||
167 | } | ||
168 | |||
169 | byte[] GetNeighbours(Dictionary<string, string> request) | ||
170 | { | ||
171 | UUID scopeID = UUID.Zero; | ||
172 | if (request["SCOPEID"] != null) | ||
173 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
174 | else | ||
175 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours"); | ||
176 | |||
177 | UUID regionID = UUID.Zero; | ||
178 | if (request["REGIONID"] != null) | ||
179 | UUID.TryParse(request["REGIONID"], out regionID); | ||
180 | else | ||
181 | m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours"); | ||
182 | |||
183 | List<GridRegion> rinfos = m_GridService.GetNeighbours(scopeID, regionID); | ||
184 | //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
185 | |||
186 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
187 | if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0))) | ||
188 | result["result"] = "null"; | ||
189 | else | ||
190 | { | ||
191 | int i = 0; | ||
192 | foreach (GridRegion rinfo in rinfos) | ||
193 | { | ||
194 | Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs(); | ||
195 | result["region" + i] = rinfoDict; | ||
196 | i++; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
201 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
202 | UTF8Encoding encoding = new UTF8Encoding(); | ||
203 | return encoding.GetBytes(xmlString); | ||
204 | |||
205 | } | ||
206 | |||
207 | byte[] GetRegionByUUID(Dictionary<string, string> request) | ||
208 | { | ||
209 | UUID scopeID = UUID.Zero; | ||
210 | if (request["SCOPEID"] != null) | ||
211 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
212 | else | ||
213 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours"); | ||
214 | |||
215 | UUID regionID = UUID.Zero; | ||
216 | if (request["REGIONID"] != null) | ||
217 | UUID.TryParse(request["REGIONID"], out regionID); | ||
218 | else | ||
219 | m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours"); | ||
220 | |||
221 | GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID); | ||
222 | //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
223 | |||
224 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
225 | if (rinfo == null) | ||
226 | result["result"] = "null"; | ||
227 | else | ||
228 | result["result"] = rinfo.ToKeyValuePairs(); | ||
229 | |||
230 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
231 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
232 | UTF8Encoding encoding = new UTF8Encoding(); | ||
233 | return encoding.GetBytes(xmlString); | ||
234 | } | ||
235 | |||
236 | byte[] GetRegionByPosition(Dictionary<string, string> request) | ||
237 | { | ||
238 | UUID scopeID = UUID.Zero; | ||
239 | if (request["SCOPEID"] != null) | ||
240 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
241 | else | ||
242 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position"); | ||
243 | |||
244 | int x = 0, y = 0; | ||
245 | if (request["X"] != null) | ||
246 | Int32.TryParse(request["X"], out x); | ||
247 | else | ||
248 | m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position"); | ||
249 | if (request["Y"] != null) | ||
250 | Int32.TryParse(request["Y"], out y); | ||
251 | else | ||
252 | m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position"); | ||
253 | |||
254 | GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y); | ||
255 | //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
256 | |||
257 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
258 | if (rinfo == null) | ||
259 | result["result"] = "null"; | ||
260 | else | ||
261 | result["result"] = rinfo.ToKeyValuePairs(); | ||
262 | |||
263 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
264 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
265 | UTF8Encoding encoding = new UTF8Encoding(); | ||
266 | return encoding.GetBytes(xmlString); | ||
267 | } | ||
268 | |||
269 | byte[] GetRegionByName(Dictionary<string, string> request) | ||
270 | { | ||
271 | UUID scopeID = UUID.Zero; | ||
272 | if (request["SCOPEID"] != null) | ||
273 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
274 | else | ||
275 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name"); | ||
276 | |||
277 | string regionName = string.Empty; | ||
278 | if (request["NAME"] != null) | ||
279 | regionName = request["NAME"]; | ||
280 | else | ||
281 | m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name"); | ||
282 | |||
283 | GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName); | ||
284 | //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
285 | |||
286 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
287 | if (rinfo == null) | ||
288 | result["result"] = "null"; | ||
289 | else | ||
290 | result["result"] = rinfo.ToKeyValuePairs(); | ||
291 | |||
292 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
293 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
294 | UTF8Encoding encoding = new UTF8Encoding(); | ||
295 | return encoding.GetBytes(xmlString); | ||
296 | } | ||
297 | |||
298 | byte[] GetRegionsByName(Dictionary<string, string> request) | ||
299 | { | ||
300 | UUID scopeID = UUID.Zero; | ||
301 | if (request["SCOPEID"] != null) | ||
302 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
303 | else | ||
304 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name"); | ||
305 | |||
306 | string regionName = string.Empty; | ||
307 | if (request["NAME"] != null) | ||
308 | regionName = request["NAME"]; | ||
309 | else | ||
310 | m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name"); | ||
311 | |||
312 | int max = 0; | ||
313 | if (request["MAX"] != null) | ||
314 | Int32.TryParse(request["MAX"], out max); | ||
315 | else | ||
316 | m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name"); | ||
317 | |||
318 | List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max); | ||
319 | //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count); | ||
320 | |||
321 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
322 | if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0))) | ||
323 | result["result"] = "null"; | ||
324 | else | ||
325 | { | ||
326 | int i = 0; | ||
327 | foreach (GridRegion rinfo in rinfos) | ||
328 | { | ||
329 | Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs(); | ||
330 | result["region" + i] = rinfoDict; | ||
331 | i++; | ||
332 | } | ||
333 | } | ||
334 | |||
335 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
336 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
337 | UTF8Encoding encoding = new UTF8Encoding(); | ||
338 | return encoding.GetBytes(xmlString); | ||
339 | } | ||
340 | |||
341 | byte[] GetRegionRange(Dictionary<string, string> request) | ||
342 | { | ||
343 | //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange"); | ||
344 | UUID scopeID = UUID.Zero; | ||
345 | if (request.ContainsKey("SCOPEID")) | ||
346 | UUID.TryParse(request["SCOPEID"], out scopeID); | ||
347 | else | ||
348 | m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range"); | ||
349 | |||
350 | int xmin = 0, xmax = 0, ymin = 0, ymax = 0; | ||
351 | if (request.ContainsKey("XMIN")) | ||
352 | Int32.TryParse(request["XMIN"], out xmin); | ||
353 | else | ||
354 | m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range"); | ||
355 | if (request.ContainsKey("XMAX")) | ||
356 | Int32.TryParse(request["XMAX"], out xmax); | ||
357 | else | ||
358 | m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range"); | ||
359 | if (request.ContainsKey("YMIN")) | ||
360 | Int32.TryParse(request["YMIN"], out ymin); | ||
361 | else | ||
362 | m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range"); | ||
363 | if (request.ContainsKey("YMAX")) | ||
364 | Int32.TryParse(request["YMAX"], out ymax); | ||
365 | else | ||
366 | m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range"); | ||
367 | |||
368 | |||
369 | List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax); | ||
370 | |||
371 | Dictionary<string, object> result = new Dictionary<string, object>(); | ||
372 | if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0))) | ||
373 | result["result"] = "null"; | ||
374 | else | ||
375 | { | ||
376 | int i = 0; | ||
377 | foreach (GridRegion rinfo in rinfos) | ||
378 | { | ||
379 | Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs(); | ||
380 | result["region" + i] = rinfoDict; | ||
381 | i++; | ||
382 | } | ||
383 | } | ||
384 | string xmlString = ServerUtils.BuildXmlResponse(result); | ||
385 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); | ||
386 | UTF8Encoding encoding = new UTF8Encoding(); | ||
387 | return encoding.GetBytes(xmlString); | ||
388 | } | ||
389 | |||
390 | #endregion | ||
391 | |||
392 | #region Misc | ||
393 | |||
394 | private byte[] SuccessResult() | ||
395 | { | ||
396 | XmlDocument doc = new XmlDocument(); | ||
397 | |||
398 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
399 | "", ""); | ||
400 | |||
401 | doc.AppendChild(xmlnode); | ||
402 | |||
403 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
404 | ""); | ||
405 | |||
406 | doc.AppendChild(rootElement); | ||
407 | |||
408 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
409 | result.AppendChild(doc.CreateTextNode("Success")); | ||
410 | |||
411 | rootElement.AppendChild(result); | ||
412 | |||
413 | return DocToBytes(doc); | ||
414 | } | ||
415 | |||
416 | private byte[] FailureResult() | ||
417 | { | ||
418 | XmlDocument doc = new XmlDocument(); | ||
419 | |||
420 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, | ||
421 | "", ""); | ||
422 | |||
423 | doc.AppendChild(xmlnode); | ||
424 | |||
425 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", | ||
426 | ""); | ||
427 | |||
428 | doc.AppendChild(rootElement); | ||
429 | |||
430 | XmlElement result = doc.CreateElement("", "Result", ""); | ||
431 | result.AppendChild(doc.CreateTextNode("Failure")); | ||
432 | |||
433 | rootElement.AppendChild(result); | ||
434 | |||
435 | return DocToBytes(doc); | ||
436 | } | ||
437 | |||
438 | private byte[] DocToBytes(XmlDocument doc) | ||
439 | { | ||
440 | MemoryStream ms = new MemoryStream(); | ||
441 | XmlTextWriter xw = new XmlTextWriter(ms, null); | ||
442 | xw.Formatting = Formatting.Indented; | ||
443 | doc.WriteTo(xw); | ||
444 | xw.Flush(); | ||
445 | |||
446 | return ms.ToArray(); | ||
447 | } | ||
448 | |||
449 | #endregion | ||
450 | } | ||
451 | } | ||