GridServerPostHandler.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. IOSHttpRequest httpRequest, IOSHttpResponse 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. case "get_default_regions":
  89. return GetDefaultRegions(request);
  90. case "get_fallback_regions":
  91. return GetFallbackRegions(request);
  92. case "get_hyperlinks":
  93. return GetHyperlinks(request);
  94. case "get_region_flags":
  95. return GetRegionFlags(request);
  96. }
  97. m_log.DebugFormat("[GRID HANDLER]: unknown method request {0}", method);
  98. }
  99. catch (Exception e)
  100. {
  101. m_log.ErrorFormat("[GRID HANDLER]: Exception {0} {1}", e.Message, e.StackTrace);
  102. }
  103. return FailureResult();
  104. }
  105. #region Method-specific handlers
  106. byte[] Register(Dictionary<string, object> request)
  107. {
  108. UUID scopeID = UUID.Zero;
  109. if (request.ContainsKey("SCOPEID"))
  110. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  111. else
  112. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region");
  113. int versionNumberMin = 0, versionNumberMax = 0;
  114. if (request.ContainsKey("VERSIONMIN"))
  115. Int32.TryParse(request["VERSIONMIN"].ToString(), out versionNumberMin);
  116. else
  117. m_log.WarnFormat("[GRID HANDLER]: no minimum protocol version in request to register region");
  118. if (request.ContainsKey("VERSIONMAX"))
  119. Int32.TryParse(request["VERSIONMAX"].ToString(), out versionNumberMax);
  120. else
  121. m_log.WarnFormat("[GRID HANDLER]: no maximum protocol version in request to register region");
  122. // Check the protocol version
  123. if ((versionNumberMin > ProtocolVersions.ServerProtocolVersionMax && versionNumberMax < ProtocolVersions.ServerProtocolVersionMax))
  124. {
  125. // Can't do, there is no overlap in the acceptable ranges
  126. return FailureResult();
  127. }
  128. Dictionary<string, object> rinfoData = new Dictionary<string, object>();
  129. GridRegion rinfo = null;
  130. try
  131. {
  132. foreach (KeyValuePair<string, object> kvp in request)
  133. rinfoData[kvp.Key] = kvp.Value.ToString();
  134. rinfo = new GridRegion(rinfoData);
  135. }
  136. catch (Exception e)
  137. {
  138. m_log.DebugFormat("[GRID HANDLER]: exception unpacking region data: {0}", e);
  139. }
  140. string result = "Error communicating with grid service";
  141. if (rinfo != null)
  142. result = m_GridService.RegisterRegion(scopeID, rinfo);
  143. if (result == String.Empty)
  144. return SuccessResult();
  145. else
  146. return FailureResult(result);
  147. }
  148. byte[] Deregister(Dictionary<string, object> request)
  149. {
  150. UUID regionID = UUID.Zero;
  151. if (request.ContainsKey("REGIONID"))
  152. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  153. else
  154. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region");
  155. bool result = m_GridService.DeregisterRegion(regionID);
  156. if (result)
  157. return SuccessResult();
  158. else
  159. return FailureResult();
  160. }
  161. byte[] GetNeighbours(Dictionary<string, object> request)
  162. {
  163. UUID scopeID = UUID.Zero;
  164. if (request.ContainsKey("SCOPEID"))
  165. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  166. else
  167. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  168. UUID regionID = UUID.Zero;
  169. if (request.ContainsKey("REGIONID"))
  170. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  171. else
  172. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  173. List<GridRegion> rinfos = m_GridService.GetNeighbours(scopeID, regionID);
  174. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  175. Dictionary<string, object> result = new Dictionary<string, object>();
  176. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  177. result["result"] = "null";
  178. else
  179. {
  180. int i = 0;
  181. foreach (GridRegion rinfo in rinfos)
  182. {
  183. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  184. result["region" + i] = rinfoDict;
  185. i++;
  186. }
  187. }
  188. string xmlString = ServerUtils.BuildXmlResponse(result);
  189. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  190. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  191. }
  192. byte[] GetRegionByUUID(Dictionary<string, object> request)
  193. {
  194. UUID scopeID = UUID.Zero;
  195. if (request.ContainsKey("SCOPEID"))
  196. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  197. else
  198. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  199. UUID regionID = UUID.Zero;
  200. if (request.ContainsKey("REGIONID"))
  201. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  202. else
  203. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  204. GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  205. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  206. Dictionary<string, object> result = new Dictionary<string, object>();
  207. if (rinfo == null)
  208. result["result"] = "null";
  209. else
  210. result["result"] = rinfo.ToKeyValuePairs();
  211. string xmlString = ServerUtils.BuildXmlResponse(result);
  212. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  213. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  214. }
  215. byte[] GetRegionByPosition(Dictionary<string, object> request)
  216. {
  217. UUID scopeID = UUID.Zero;
  218. if (request.ContainsKey("SCOPEID"))
  219. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  220. else
  221. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position");
  222. int x = 0, y = 0;
  223. if (request.ContainsKey("X"))
  224. Int32.TryParse(request["X"].ToString(), out x);
  225. else
  226. m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position");
  227. if (request.ContainsKey("Y"))
  228. Int32.TryParse(request["Y"].ToString(), out y);
  229. else
  230. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position");
  231. GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  232. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  233. Dictionary<string, object> result = new Dictionary<string, object>();
  234. if (rinfo == null)
  235. result["result"] = "null";
  236. else
  237. result["result"] = rinfo.ToKeyValuePairs();
  238. string xmlString = ServerUtils.BuildXmlResponse(result);
  239. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  240. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  241. }
  242. byte[] GetRegionByName(Dictionary<string, object> request)
  243. {
  244. UUID scopeID = UUID.Zero;
  245. if (request.ContainsKey("SCOPEID"))
  246. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  247. else
  248. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name");
  249. string regionName = string.Empty;
  250. if (request.ContainsKey("NAME"))
  251. regionName = request["NAME"].ToString();
  252. else
  253. m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name");
  254. GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  255. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  256. Dictionary<string, object> result = new Dictionary<string, object>();
  257. if (rinfo == null)
  258. result["result"] = "null";
  259. else
  260. result["result"] = rinfo.ToKeyValuePairs();
  261. string xmlString = ServerUtils.BuildXmlResponse(result);
  262. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  263. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  264. }
  265. byte[] GetRegionsByName(Dictionary<string, object> request)
  266. {
  267. UUID scopeID = UUID.Zero;
  268. if (request.ContainsKey("SCOPEID"))
  269. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  270. else
  271. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name");
  272. string regionName = string.Empty;
  273. if (request.ContainsKey("NAME"))
  274. regionName = request["NAME"].ToString();
  275. else
  276. m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name");
  277. int max = 0;
  278. if (request.ContainsKey("MAX"))
  279. Int32.TryParse(request["MAX"].ToString(), out max);
  280. else
  281. m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name");
  282. List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max);
  283. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  284. Dictionary<string, object> result = new Dictionary<string, object>();
  285. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  286. result["result"] = "null";
  287. else
  288. {
  289. int i = 0;
  290. foreach (GridRegion rinfo in rinfos)
  291. {
  292. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  293. result["region" + i] = rinfoDict;
  294. i++;
  295. }
  296. }
  297. string xmlString = ServerUtils.BuildXmlResponse(result);
  298. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  299. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  300. }
  301. byte[] GetRegionRange(Dictionary<string, object> request)
  302. {
  303. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  304. UUID scopeID = UUID.Zero;
  305. if (request.ContainsKey("SCOPEID"))
  306. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  307. else
  308. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  309. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  310. if (request.ContainsKey("XMIN"))
  311. Int32.TryParse(request["XMIN"].ToString(), out xmin);
  312. else
  313. m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range");
  314. if (request.ContainsKey("XMAX"))
  315. Int32.TryParse(request["XMAX"].ToString(), out xmax);
  316. else
  317. m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range");
  318. if (request.ContainsKey("YMIN"))
  319. Int32.TryParse(request["YMIN"].ToString(), out ymin);
  320. else
  321. m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range");
  322. if (request.ContainsKey("YMAX"))
  323. Int32.TryParse(request["YMAX"].ToString(), out ymax);
  324. else
  325. m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range");
  326. List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  327. Dictionary<string, object> result = new Dictionary<string, object>();
  328. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  329. result["result"] = "null";
  330. else
  331. {
  332. int i = 0;
  333. foreach (GridRegion rinfo in rinfos)
  334. {
  335. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  336. result["region" + i] = rinfoDict;
  337. i++;
  338. }
  339. }
  340. string xmlString = ServerUtils.BuildXmlResponse(result);
  341. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  342. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  343. }
  344. byte[] GetDefaultRegions(Dictionary<string, object> request)
  345. {
  346. //m_log.DebugFormat("[GRID HANDLER]: GetDefaultRegions");
  347. UUID scopeID = UUID.Zero;
  348. if (request.ContainsKey("SCOPEID"))
  349. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  350. else
  351. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  352. List<GridRegion> rinfos = m_GridService.GetDefaultRegions(scopeID);
  353. Dictionary<string, object> result = new Dictionary<string, object>();
  354. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  355. result["result"] = "null";
  356. else
  357. {
  358. int i = 0;
  359. foreach (GridRegion rinfo in rinfos)
  360. {
  361. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  362. result["region" + i] = rinfoDict;
  363. i++;
  364. }
  365. }
  366. string xmlString = ServerUtils.BuildXmlResponse(result);
  367. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  368. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  369. }
  370. byte[] GetFallbackRegions(Dictionary<string, object> request)
  371. {
  372. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  373. UUID scopeID = UUID.Zero;
  374. if (request.ContainsKey("SCOPEID"))
  375. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  376. else
  377. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get fallback regions");
  378. int x = 0, y = 0;
  379. if (request.ContainsKey("X"))
  380. Int32.TryParse(request["X"].ToString(), out x);
  381. else
  382. m_log.WarnFormat("[GRID HANDLER]: no X in request to get fallback regions");
  383. if (request.ContainsKey("Y"))
  384. Int32.TryParse(request["Y"].ToString(), out y);
  385. else
  386. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get fallback regions");
  387. List<GridRegion> rinfos = m_GridService.GetFallbackRegions(scopeID, x, y);
  388. Dictionary<string, object> result = new Dictionary<string, object>();
  389. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  390. result["result"] = "null";
  391. else
  392. {
  393. int i = 0;
  394. foreach (GridRegion rinfo in rinfos)
  395. {
  396. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  397. result["region" + i] = rinfoDict;
  398. i++;
  399. }
  400. }
  401. string xmlString = ServerUtils.BuildXmlResponse(result);
  402. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  403. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  404. }
  405. byte[] GetHyperlinks(Dictionary<string, object> request)
  406. {
  407. //m_log.DebugFormat("[GRID HANDLER]: GetHyperlinks");
  408. UUID scopeID = UUID.Zero;
  409. if (request.ContainsKey("SCOPEID"))
  410. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  411. else
  412. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get linked regions");
  413. List<GridRegion> rinfos = m_GridService.GetHyperlinks(scopeID);
  414. Dictionary<string, object> result = new Dictionary<string, object>();
  415. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  416. result["result"] = "null";
  417. else
  418. {
  419. int i = 0;
  420. foreach (GridRegion rinfo in rinfos)
  421. {
  422. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  423. result["region" + i] = rinfoDict;
  424. i++;
  425. }
  426. }
  427. string xmlString = ServerUtils.BuildXmlResponse(result);
  428. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  429. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  430. }
  431. byte[] GetRegionFlags(Dictionary<string, object> request)
  432. {
  433. UUID scopeID = UUID.Zero;
  434. if (request.ContainsKey("SCOPEID"))
  435. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  436. else
  437. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  438. UUID regionID = UUID.Zero;
  439. if (request.ContainsKey("REGIONID"))
  440. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  441. else
  442. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  443. int flags = m_GridService.GetRegionFlags(scopeID, regionID);
  444. // m_log.DebugFormat("[GRID HANDLER]: flags for region {0}: {1}", regionID, flags);
  445. Dictionary<string, object> result = new Dictionary<string, object>();
  446. result["result"] = flags.ToString();
  447. string xmlString = ServerUtils.BuildXmlResponse(result);
  448. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  449. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  450. }
  451. #endregion
  452. #region Misc
  453. private byte[] SuccessResult()
  454. {
  455. XmlDocument doc = new XmlDocument();
  456. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  457. "", "");
  458. doc.AppendChild(xmlnode);
  459. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  460. "");
  461. doc.AppendChild(rootElement);
  462. XmlElement result = doc.CreateElement("", "Result", "");
  463. result.AppendChild(doc.CreateTextNode("Success"));
  464. rootElement.AppendChild(result);
  465. return DocToBytes(doc);
  466. }
  467. private byte[] FailureResult()
  468. {
  469. return FailureResult(String.Empty);
  470. }
  471. private byte[] FailureResult(string msg)
  472. {
  473. XmlDocument doc = new XmlDocument();
  474. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  475. "", "");
  476. doc.AppendChild(xmlnode);
  477. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  478. "");
  479. doc.AppendChild(rootElement);
  480. XmlElement result = doc.CreateElement("", "Result", "");
  481. result.AppendChild(doc.CreateTextNode("Failure"));
  482. rootElement.AppendChild(result);
  483. XmlElement message = doc.CreateElement("", "Message", "");
  484. message.AppendChild(doc.CreateTextNode(msg));
  485. rootElement.AppendChild(message);
  486. return DocToBytes(doc);
  487. }
  488. private byte[] DocToBytes(XmlDocument doc)
  489. {
  490. MemoryStream ms = new MemoryStream();
  491. XmlTextWriter xw = new XmlTextWriter(ms, null);
  492. xw.Formatting = Formatting.Indented;
  493. doc.WriteTo(xw);
  494. xw.Flush();
  495. return ms.ToArray();
  496. }
  497. #endregion
  498. }
  499. }