GridServerPostHandler.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. try
  64. {
  65. Dictionary<string, object> request =
  66. ServerUtils.ParseQueryString(body);
  67. if (!request.ContainsKey("METHOD"))
  68. return FailureResult();
  69. string method = request["METHOD"].ToString();
  70. switch (method)
  71. {
  72. case "register":
  73. return Register(request);
  74. case "deregister":
  75. return Deregister(request);
  76. case "get_neighbours":
  77. return GetNeighbours(request);
  78. case "get_region_by_uuid":
  79. return GetRegionByUUID(request);
  80. case "get_region_by_position":
  81. return GetRegionByPosition(request);
  82. case "get_region_by_name":
  83. return GetRegionByName(request);
  84. case "get_regions_by_name":
  85. return GetRegionsByName(request);
  86. case "get_region_range":
  87. return GetRegionRange(request);
  88. }
  89. m_log.DebugFormat("[GRID HANDLER]: unknown method {0} request {1}", method.Length, method);
  90. }
  91. catch (Exception e)
  92. {
  93. m_log.DebugFormat("[GRID HANDLER]: Exception {0}", e);
  94. }
  95. return FailureResult();
  96. }
  97. #region Method-specific handlers
  98. byte[] Register(Dictionary<string, object> request)
  99. {
  100. UUID scopeID = UUID.Zero;
  101. if (request.ContainsKey("SCOPEID"))
  102. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  103. else
  104. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region");
  105. int versionNumberMin = 0, versionNumberMax = 0;
  106. if (request.ContainsKey("VERSIONMIN"))
  107. Int32.TryParse(request["VERSIONMIN"].ToString(), out versionNumberMin);
  108. else
  109. m_log.WarnFormat("[GRID HANDLER]: no minimum protocol version in request to register region");
  110. if (request.ContainsKey("VERSIONMAX"))
  111. Int32.TryParse(request["VERSIONMAX"].ToString(), out versionNumberMax);
  112. else
  113. m_log.WarnFormat("[GRID HANDLER]: no maximum protocol version in request to register region");
  114. // Check the protocol version
  115. if ((versionNumberMin > ProtocolVersions.ServerProtocolVersionMax && versionNumberMax < ProtocolVersions.ServerProtocolVersionMax))
  116. {
  117. // Can't do, there is no overlap in the acceptable ranges
  118. return FailureResult();
  119. }
  120. Dictionary<string, object> rinfoData = new Dictionary<string, object>();
  121. GridRegion rinfo = null;
  122. try
  123. {
  124. foreach (KeyValuePair<string, object> kvp in request)
  125. rinfoData[kvp.Key] = kvp.Value.ToString();
  126. rinfo = new GridRegion(rinfoData);
  127. }
  128. catch (Exception e)
  129. {
  130. m_log.DebugFormat("[GRID HANDLER]: exception unpacking region data: {0}", e);
  131. }
  132. bool result = false;
  133. if (rinfo != null)
  134. result = m_GridService.RegisterRegion(scopeID, rinfo);
  135. if (result)
  136. return SuccessResult();
  137. else
  138. return FailureResult();
  139. }
  140. byte[] Deregister(Dictionary<string, object> request)
  141. {
  142. UUID regionID = UUID.Zero;
  143. if (request.ContainsKey("REGIONID"))
  144. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  145. else
  146. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region");
  147. bool result = m_GridService.DeregisterRegion(regionID);
  148. if (result)
  149. return SuccessResult();
  150. else
  151. return FailureResult();
  152. }
  153. byte[] GetNeighbours(Dictionary<string, object> request)
  154. {
  155. UUID scopeID = UUID.Zero;
  156. if (request.ContainsKey("SCOPEID"))
  157. UUID.TryParse(request["SCOPEID"].ToString(), 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.ContainsKey("REGIONID"))
  162. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  163. else
  164. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  165. List<GridRegion> rinfos = m_GridService.GetNeighbours(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 ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  169. result["result"] = "null";
  170. else
  171. {
  172. int i = 0;
  173. foreach (GridRegion rinfo in rinfos)
  174. {
  175. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  176. result["region" + i] = rinfoDict;
  177. i++;
  178. }
  179. }
  180. string xmlString = ServerUtils.BuildXmlResponse(result);
  181. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  182. UTF8Encoding encoding = new UTF8Encoding();
  183. return encoding.GetBytes(xmlString);
  184. }
  185. byte[] GetRegionByUUID(Dictionary<string, object> request)
  186. {
  187. UUID scopeID = UUID.Zero;
  188. if (request.ContainsKey("SCOPEID"))
  189. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  190. else
  191. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  192. UUID regionID = UUID.Zero;
  193. if (request.ContainsKey("REGIONID"))
  194. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  195. else
  196. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  197. GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  198. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  199. Dictionary<string, object> result = new Dictionary<string, object>();
  200. if (rinfo == null)
  201. result["result"] = "null";
  202. else
  203. result["result"] = rinfo.ToKeyValuePairs();
  204. string xmlString = ServerUtils.BuildXmlResponse(result);
  205. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  206. UTF8Encoding encoding = new UTF8Encoding();
  207. return encoding.GetBytes(xmlString);
  208. }
  209. byte[] GetRegionByPosition(Dictionary<string, object> request)
  210. {
  211. UUID scopeID = UUID.Zero;
  212. if (request.ContainsKey("SCOPEID"))
  213. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  214. else
  215. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position");
  216. int x = 0, y = 0;
  217. if (request.ContainsKey("X"))
  218. Int32.TryParse(request["X"].ToString(), out x);
  219. else
  220. m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position");
  221. if (request.ContainsKey("Y"))
  222. Int32.TryParse(request["Y"].ToString(), out y);
  223. else
  224. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position");
  225. GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  226. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  227. Dictionary<string, object> result = new Dictionary<string, object>();
  228. if (rinfo == null)
  229. result["result"] = "null";
  230. else
  231. result["result"] = rinfo.ToKeyValuePairs();
  232. string xmlString = ServerUtils.BuildXmlResponse(result);
  233. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  234. UTF8Encoding encoding = new UTF8Encoding();
  235. return encoding.GetBytes(xmlString);
  236. }
  237. byte[] GetRegionByName(Dictionary<string, object> request)
  238. {
  239. UUID scopeID = UUID.Zero;
  240. if (request.ContainsKey("SCOPEID"))
  241. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  242. else
  243. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name");
  244. string regionName = string.Empty;
  245. if (request.ContainsKey("NAME"))
  246. regionName = request["NAME"].ToString();
  247. else
  248. m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name");
  249. GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  250. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  251. Dictionary<string, object> result = new Dictionary<string, object>();
  252. if (rinfo == null)
  253. result["result"] = "null";
  254. else
  255. result["result"] = rinfo.ToKeyValuePairs();
  256. string xmlString = ServerUtils.BuildXmlResponse(result);
  257. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  258. UTF8Encoding encoding = new UTF8Encoding();
  259. return encoding.GetBytes(xmlString);
  260. }
  261. byte[] GetRegionsByName(Dictionary<string, object> request)
  262. {
  263. UUID scopeID = UUID.Zero;
  264. if (request.ContainsKey("SCOPEID"))
  265. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  266. else
  267. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name");
  268. string regionName = string.Empty;
  269. if (request.ContainsKey("NAME"))
  270. regionName = request["NAME"].ToString();
  271. else
  272. m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name");
  273. int max = 0;
  274. if (request.ContainsKey("MAX"))
  275. Int32.TryParse(request["MAX"].ToString(), out max);
  276. else
  277. m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name");
  278. List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max);
  279. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  280. Dictionary<string, object> result = new Dictionary<string, object>();
  281. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  282. result["result"] = "null";
  283. else
  284. {
  285. int i = 0;
  286. foreach (GridRegion rinfo in rinfos)
  287. {
  288. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  289. result["region" + i] = rinfoDict;
  290. i++;
  291. }
  292. }
  293. string xmlString = ServerUtils.BuildXmlResponse(result);
  294. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  295. UTF8Encoding encoding = new UTF8Encoding();
  296. return encoding.GetBytes(xmlString);
  297. }
  298. byte[] GetRegionRange(Dictionary<string, object> request)
  299. {
  300. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  301. UUID scopeID = UUID.Zero;
  302. if (request.ContainsKey("SCOPEID"))
  303. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  304. else
  305. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  306. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  307. if (request.ContainsKey("XMIN"))
  308. Int32.TryParse(request["XMIN"].ToString(), out xmin);
  309. else
  310. m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range");
  311. if (request.ContainsKey("XMAX"))
  312. Int32.TryParse(request["XMAX"].ToString(), out xmax);
  313. else
  314. m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range");
  315. if (request.ContainsKey("YMIN"))
  316. Int32.TryParse(request["YMIN"].ToString(), out ymin);
  317. else
  318. m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range");
  319. if (request.ContainsKey("YMAX"))
  320. Int32.TryParse(request["YMAX"].ToString(), out ymax);
  321. else
  322. m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range");
  323. List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  324. Dictionary<string, object> result = new Dictionary<string, object>();
  325. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  326. result["result"] = "null";
  327. else
  328. {
  329. int i = 0;
  330. foreach (GridRegion rinfo in rinfos)
  331. {
  332. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  333. result["region" + i] = rinfoDict;
  334. i++;
  335. }
  336. }
  337. string xmlString = ServerUtils.BuildXmlResponse(result);
  338. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  339. UTF8Encoding encoding = new UTF8Encoding();
  340. return encoding.GetBytes(xmlString);
  341. }
  342. #endregion
  343. #region Misc
  344. private byte[] SuccessResult()
  345. {
  346. XmlDocument doc = new XmlDocument();
  347. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  348. "", "");
  349. doc.AppendChild(xmlnode);
  350. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  351. "");
  352. doc.AppendChild(rootElement);
  353. XmlElement result = doc.CreateElement("", "Result", "");
  354. result.AppendChild(doc.CreateTextNode("Success"));
  355. rootElement.AppendChild(result);
  356. return DocToBytes(doc);
  357. }
  358. private byte[] FailureResult()
  359. {
  360. XmlDocument doc = new XmlDocument();
  361. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  362. "", "");
  363. doc.AppendChild(xmlnode);
  364. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  365. "");
  366. doc.AppendChild(rootElement);
  367. XmlElement result = doc.CreateElement("", "Result", "");
  368. result.AppendChild(doc.CreateTextNode("Failure"));
  369. rootElement.AppendChild(result);
  370. return DocToBytes(doc);
  371. }
  372. private byte[] DocToBytes(XmlDocument doc)
  373. {
  374. MemoryStream ms = new MemoryStream();
  375. XmlTextWriter xw = new XmlTextWriter(ms, null);
  376. xw.Formatting = Formatting.Indented;
  377. doc.WriteTo(xw);
  378. xw.Flush();
  379. return ms.ToArray();
  380. }
  381. #endregion
  382. }
  383. }