GridServerPostHandler.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Grid
  45. {
  46. public class GridServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IGridService m_GridService;
  50. public GridServerPostHandler(IGridService service) :
  51. base("POST", "/grid")
  52. {
  53. m_GridService = service;
  54. }
  55. public override byte[] Handle(string path, Stream requestData,
  56. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. Dictionary<string, string> request =
  64. ServerUtils.ParseQueryString(body);
  65. if (!request.ContainsKey("METHOD"))
  66. return FailureResult();
  67. string method = request["METHOD"];
  68. switch (method)
  69. {
  70. case "register":
  71. return Register(request);
  72. case "deregister":
  73. return Deregister(request);
  74. case "get_neighbours":
  75. return GetNeighbours(request);
  76. case "get_region_by_uuid":
  77. return GetRegionByUUID(request);
  78. case "get_region_by_position":
  79. return GetRegionByPosition(request);
  80. case "get_region_by_name":
  81. return GetRegionByName(request);
  82. case "get_regions_by_name":
  83. return GetRegionsByName(request);
  84. case "get_region_range":
  85. return GetRegionRange(request);
  86. }
  87. m_log.DebugFormat("[GRID HANDLER]: unknown method {0} request {1}", method.Length, method);
  88. return FailureResult();
  89. }
  90. #region Method-specific handlers
  91. byte[] Register(Dictionary<string, string> request)
  92. {
  93. UUID scopeID = UUID.Zero;
  94. if (request["SCOPEID"] != null)
  95. UUID.TryParse(request["SCOPEID"], out scopeID);
  96. else
  97. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region");
  98. Dictionary<string, object> rinfoData = new Dictionary<string, object>();
  99. foreach (KeyValuePair<string, string> kvp in request)
  100. rinfoData[kvp.Key] = kvp.Value;
  101. GridRegion rinfo = new GridRegion(rinfoData);
  102. bool result = m_GridService.RegisterRegion(scopeID, rinfo);
  103. if (result)
  104. return SuccessResult();
  105. else
  106. return FailureResult();
  107. }
  108. byte[] Deregister(Dictionary<string, string> request)
  109. {
  110. UUID regionID = UUID.Zero;
  111. if (request["REGIONID"] != null)
  112. UUID.TryParse(request["REGIONID"], out regionID);
  113. else
  114. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region");
  115. bool result = m_GridService.DeregisterRegion(regionID);
  116. if (result)
  117. return SuccessResult();
  118. else
  119. return FailureResult();
  120. }
  121. byte[] GetNeighbours(Dictionary<string, string> request)
  122. {
  123. UUID scopeID = UUID.Zero;
  124. if (request["SCOPEID"] != null)
  125. UUID.TryParse(request["SCOPEID"], out scopeID);
  126. else
  127. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  128. UUID regionID = UUID.Zero;
  129. if (request["REGIONID"] != null)
  130. UUID.TryParse(request["REGIONID"], out regionID);
  131. else
  132. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  133. List<GridRegion> rinfos = m_GridService.GetNeighbours(scopeID, regionID);
  134. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  135. Dictionary<string, object> result = new Dictionary<string, object>();
  136. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  137. result["result"] = "null";
  138. else
  139. {
  140. int i = 0;
  141. foreach (GridRegion rinfo in rinfos)
  142. {
  143. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  144. result["region" + i] = rinfoDict;
  145. i++;
  146. }
  147. }
  148. string xmlString = ServerUtils.BuildXmlResponse(result);
  149. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  150. UTF8Encoding encoding = new UTF8Encoding();
  151. return encoding.GetBytes(xmlString);
  152. }
  153. byte[] GetRegionByUUID(Dictionary<string, string> request)
  154. {
  155. UUID scopeID = UUID.Zero;
  156. if (request["SCOPEID"] != null)
  157. UUID.TryParse(request["SCOPEID"], out scopeID);
  158. else
  159. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  160. UUID regionID = UUID.Zero;
  161. if (request["REGIONID"] != null)
  162. UUID.TryParse(request["REGIONID"], out regionID);
  163. else
  164. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  165. GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  166. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  167. Dictionary<string, object> result = new Dictionary<string, object>();
  168. if (rinfo == null)
  169. result["result"] = "null";
  170. else
  171. result["result"] = rinfo.ToKeyValuePairs();
  172. string xmlString = ServerUtils.BuildXmlResponse(result);
  173. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  174. UTF8Encoding encoding = new UTF8Encoding();
  175. return encoding.GetBytes(xmlString);
  176. }
  177. byte[] GetRegionByPosition(Dictionary<string, string> request)
  178. {
  179. UUID scopeID = UUID.Zero;
  180. if (request["SCOPEID"] != null)
  181. UUID.TryParse(request["SCOPEID"], out scopeID);
  182. else
  183. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position");
  184. int x = 0, y = 0;
  185. if (request["X"] != null)
  186. Int32.TryParse(request["X"], out x);
  187. else
  188. m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position");
  189. if (request["Y"] != null)
  190. Int32.TryParse(request["Y"], out y);
  191. else
  192. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position");
  193. GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  194. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  195. Dictionary<string, object> result = new Dictionary<string, object>();
  196. if (rinfo == null)
  197. result["result"] = "null";
  198. else
  199. result["result"] = rinfo.ToKeyValuePairs();
  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. byte[] GetRegionByName(Dictionary<string, string> request)
  206. {
  207. UUID scopeID = UUID.Zero;
  208. if (request["SCOPEID"] != null)
  209. UUID.TryParse(request["SCOPEID"], out scopeID);
  210. else
  211. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name");
  212. string regionName = string.Empty;
  213. if (request["NAME"] != null)
  214. regionName = request["NAME"];
  215. else
  216. m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name");
  217. GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  218. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  219. Dictionary<string, object> result = new Dictionary<string, object>();
  220. if (rinfo == null)
  221. result["result"] = "null";
  222. else
  223. result["result"] = rinfo.ToKeyValuePairs();
  224. string xmlString = ServerUtils.BuildXmlResponse(result);
  225. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  226. UTF8Encoding encoding = new UTF8Encoding();
  227. return encoding.GetBytes(xmlString);
  228. }
  229. byte[] GetRegionsByName(Dictionary<string, string> request)
  230. {
  231. UUID scopeID = UUID.Zero;
  232. if (request["SCOPEID"] != null)
  233. UUID.TryParse(request["SCOPEID"], out scopeID);
  234. else
  235. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name");
  236. string regionName = string.Empty;
  237. if (request["NAME"] != null)
  238. regionName = request["NAME"];
  239. else
  240. m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name");
  241. int max = 0;
  242. if (request["MAX"] != null)
  243. Int32.TryParse(request["MAX"], out max);
  244. else
  245. m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name");
  246. List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max);
  247. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  248. Dictionary<string, object> result = new Dictionary<string, object>();
  249. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  250. result["result"] = "null";
  251. else
  252. {
  253. int i = 0;
  254. foreach (GridRegion rinfo in rinfos)
  255. {
  256. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  257. result["region" + i] = rinfoDict;
  258. i++;
  259. }
  260. }
  261. string xmlString = ServerUtils.BuildXmlResponse(result);
  262. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  263. UTF8Encoding encoding = new UTF8Encoding();
  264. return encoding.GetBytes(xmlString);
  265. }
  266. byte[] GetRegionRange(Dictionary<string, string> request)
  267. {
  268. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  269. UUID scopeID = UUID.Zero;
  270. if (request.ContainsKey("SCOPEID"))
  271. UUID.TryParse(request["SCOPEID"], out scopeID);
  272. else
  273. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  274. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  275. if (request.ContainsKey("XMIN"))
  276. Int32.TryParse(request["XMIN"], out xmin);
  277. else
  278. m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range");
  279. if (request.ContainsKey("XMAX"))
  280. Int32.TryParse(request["XMAX"], out xmax);
  281. else
  282. m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range");
  283. if (request.ContainsKey("YMIN"))
  284. Int32.TryParse(request["YMIN"], out ymin);
  285. else
  286. m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range");
  287. if (request.ContainsKey("YMAX"))
  288. Int32.TryParse(request["YMAX"], out ymax);
  289. else
  290. m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range");
  291. List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  292. Dictionary<string, object> result = new Dictionary<string, object>();
  293. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  294. result["result"] = "null";
  295. else
  296. {
  297. int i = 0;
  298. foreach (GridRegion rinfo in rinfos)
  299. {
  300. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  301. result["region" + i] = rinfoDict;
  302. i++;
  303. }
  304. }
  305. string xmlString = ServerUtils.BuildXmlResponse(result);
  306. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  307. UTF8Encoding encoding = new UTF8Encoding();
  308. return encoding.GetBytes(xmlString);
  309. }
  310. #endregion
  311. #region Misc
  312. private byte[] SuccessResult()
  313. {
  314. XmlDocument doc = new XmlDocument();
  315. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  316. "", "");
  317. doc.AppendChild(xmlnode);
  318. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  319. "");
  320. doc.AppendChild(rootElement);
  321. XmlElement result = doc.CreateElement("", "Result", "");
  322. result.AppendChild(doc.CreateTextNode("Success"));
  323. rootElement.AppendChild(result);
  324. return DocToBytes(doc);
  325. }
  326. private byte[] FailureResult()
  327. {
  328. XmlDocument doc = new XmlDocument();
  329. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  330. "", "");
  331. doc.AppendChild(xmlnode);
  332. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  333. "");
  334. doc.AppendChild(rootElement);
  335. XmlElement result = doc.CreateElement("", "Result", "");
  336. result.AppendChild(doc.CreateTextNode("Failure"));
  337. rootElement.AppendChild(result);
  338. return DocToBytes(doc);
  339. }
  340. private byte[] DocToBytes(XmlDocument doc)
  341. {
  342. MemoryStream ms = new MemoryStream();
  343. XmlTextWriter xw = new XmlTextWriter(ms, null);
  344. xw.Formatting = Formatting.Indented;
  345. doc.WriteTo(xw);
  346. xw.Flush();
  347. return ms.ToArray();
  348. }
  349. #endregion
  350. }
  351. }