RegionClient.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using log4net;
  37. namespace OpenSim.Framework.Communications.Clients
  38. {
  39. public class RegionClient
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public bool DoCreateChildAgentCall(GridRegion region, AgentCircuitData aCircuit, string authKey, out string reason)
  43. {
  44. reason = String.Empty;
  45. // Eventually, we want to use a caps url instead of the agentID
  46. string uri = string.Empty;
  47. try
  48. {
  49. uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + aCircuit.AgentID + "/";
  50. }
  51. catch (Exception e)
  52. {
  53. m_log.Debug("[REST COMMS]: Unable to resolve external endpoint on agent create. Reason: " + e.Message);
  54. reason = e.Message;
  55. return false;
  56. }
  57. //Console.WriteLine(" >>> DoCreateChildAgentCall <<< " + uri);
  58. HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
  59. AgentCreateRequest.Method = "POST";
  60. AgentCreateRequest.ContentType = "application/json";
  61. AgentCreateRequest.Timeout = 10000;
  62. //AgentCreateRequest.KeepAlive = false;
  63. AgentCreateRequest.Headers.Add("Authorization", authKey);
  64. // Fill it in
  65. OSDMap args = null;
  66. try
  67. {
  68. args = aCircuit.PackAgentCircuitData();
  69. }
  70. catch (Exception e)
  71. {
  72. m_log.Debug("[REST COMMS]: PackAgentCircuitData failed with exception: " + e.Message);
  73. }
  74. // Add the regionhandle of the destination region
  75. ulong regionHandle = GetRegionHandle(region.RegionHandle);
  76. args["destination_handle"] = OSD.FromString(regionHandle.ToString());
  77. string strBuffer = "";
  78. byte[] buffer = new byte[1];
  79. try
  80. {
  81. strBuffer = OSDParser.SerializeJsonString(args);
  82. Encoding str = Util.UTF8;
  83. buffer = str.GetBytes(strBuffer);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
  88. // ignore. buffer will be empty, caller should check.
  89. }
  90. Stream os = null;
  91. try
  92. { // send the Post
  93. AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  94. os = AgentCreateRequest.GetRequestStream();
  95. os.Write(buffer, 0, strBuffer.Length); //Send it
  96. //m_log.InfoFormat("[REST COMMS]: Posted CreateChildAgent request to remote sim {0}", uri);
  97. }
  98. //catch (WebException ex)
  99. catch
  100. {
  101. //m_log.InfoFormat("[REST COMMS]: Bad send on ChildAgentUpdate {0}", ex.Message);
  102. reason = "cannot contact remote region";
  103. return false;
  104. }
  105. finally
  106. {
  107. if (os != null)
  108. os.Close();
  109. }
  110. // Let's wait for the response
  111. //m_log.Info("[REST COMMS]: Waiting for a reply after DoCreateChildAgentCall");
  112. WebResponse webResponse = null;
  113. StreamReader sr = null;
  114. try
  115. {
  116. webResponse = AgentCreateRequest.GetResponse();
  117. if (webResponse == null)
  118. {
  119. m_log.Info("[REST COMMS]: Null reply on DoCreateChildAgentCall post");
  120. }
  121. else
  122. {
  123. sr = new StreamReader(webResponse.GetResponseStream());
  124. string response = sr.ReadToEnd().Trim();
  125. m_log.InfoFormat("[REST COMMS]: DoCreateChildAgentCall reply was {0} ", response);
  126. if (!String.IsNullOrEmpty(response))
  127. {
  128. try
  129. {
  130. // we assume we got an OSDMap back
  131. OSDMap r = GetOSDMap(response);
  132. bool success = r["success"].AsBoolean();
  133. reason = r["reason"].AsString();
  134. return success;
  135. }
  136. catch (NullReferenceException e)
  137. {
  138. m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
  139. // check for old style response
  140. if (response.ToLower().StartsWith("true"))
  141. return true;
  142. return false;
  143. }
  144. }
  145. }
  146. }
  147. catch (WebException ex)
  148. {
  149. m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
  150. // ignore, really
  151. }
  152. finally
  153. {
  154. if (sr != null)
  155. sr.Close();
  156. }
  157. return true;
  158. }
  159. public bool DoChildAgentUpdateCall(GridRegion region, IAgentData cAgentData)
  160. {
  161. // Eventually, we want to use a caps url instead of the agentID
  162. string uri = string.Empty;
  163. try
  164. {
  165. uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + cAgentData.AgentID + "/";
  166. }
  167. catch (Exception e)
  168. {
  169. m_log.Debug("[REST COMMS]: Unable to resolve external endpoint on agent update. Reason: " + e.Message);
  170. return false;
  171. }
  172. //Console.WriteLine(" >>> DoChildAgentUpdateCall <<< " + uri);
  173. HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
  174. ChildUpdateRequest.Method = "PUT";
  175. ChildUpdateRequest.ContentType = "application/json";
  176. ChildUpdateRequest.Timeout = 10000;
  177. //ChildUpdateRequest.KeepAlive = false;
  178. // Fill it in
  179. OSDMap args = null;
  180. try
  181. {
  182. args = cAgentData.Pack();
  183. }
  184. catch (Exception e)
  185. {
  186. m_log.Debug("[REST COMMS]: PackUpdateMessage failed with exception: " + e.Message);
  187. }
  188. // Add the regionhandle of the destination region
  189. ulong regionHandle = GetRegionHandle(region.RegionHandle);
  190. args["destination_handle"] = OSD.FromString(regionHandle.ToString());
  191. string strBuffer = "";
  192. byte[] buffer = new byte[1];
  193. try
  194. {
  195. strBuffer = OSDParser.SerializeJsonString(args);
  196. Encoding str = Util.UTF8;
  197. buffer = str.GetBytes(strBuffer);
  198. }
  199. catch (Exception e)
  200. {
  201. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
  202. // ignore. buffer will be empty, caller should check.
  203. }
  204. Stream os = null;
  205. try
  206. { // send the Post
  207. ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send
  208. os = ChildUpdateRequest.GetRequestStream();
  209. os.Write(buffer, 0, strBuffer.Length); //Send it
  210. //m_log.InfoFormat("[REST COMMS]: Posted ChildAgentUpdate request to remote sim {0}", uri);
  211. }
  212. //catch (WebException ex)
  213. catch
  214. {
  215. //m_log.InfoFormat("[REST COMMS]: Bad send on ChildAgentUpdate {0}", ex.Message);
  216. return false;
  217. }
  218. finally
  219. {
  220. if (os != null)
  221. os.Close();
  222. }
  223. // Let's wait for the response
  224. //m_log.Info("[REST COMMS]: Waiting for a reply after ChildAgentUpdate");
  225. WebResponse webResponse = null;
  226. StreamReader sr = null;
  227. try
  228. {
  229. webResponse = ChildUpdateRequest.GetResponse();
  230. if (webResponse == null)
  231. {
  232. m_log.Info("[REST COMMS]: Null reply on ChilAgentUpdate post");
  233. }
  234. sr = new StreamReader(webResponse.GetResponseStream());
  235. //reply = sr.ReadToEnd().Trim();
  236. sr.ReadToEnd().Trim();
  237. sr.Close();
  238. //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
  239. }
  240. catch (WebException ex)
  241. {
  242. m_log.InfoFormat("[REST COMMS]: exception on reply of ChilAgentUpdate {0}", ex.Message);
  243. // ignore, really
  244. }
  245. finally
  246. {
  247. if (sr != null)
  248. sr.Close();
  249. }
  250. return true;
  251. }
  252. public bool DoRetrieveRootAgentCall(GridRegion region, UUID id, out IAgentData agent)
  253. {
  254. agent = null;
  255. // Eventually, we want to use a caps url instead of the agentID
  256. string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + id + "/" + region.RegionHandle.ToString() + "/";
  257. //Console.WriteLine(" >>> DoRetrieveRootAgentCall <<< " + uri);
  258. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  259. request.Method = "GET";
  260. request.Timeout = 10000;
  261. //request.Headers.Add("authorization", ""); // coming soon
  262. HttpWebResponse webResponse = null;
  263. string reply = string.Empty;
  264. StreamReader sr = null;
  265. try
  266. {
  267. webResponse = (HttpWebResponse)request.GetResponse();
  268. if (webResponse == null)
  269. {
  270. m_log.Info("[REST COMMS]: Null reply on agent get ");
  271. }
  272. sr = new StreamReader(webResponse.GetResponseStream());
  273. reply = sr.ReadToEnd().Trim();
  274. //Console.WriteLine("[REST COMMS]: ChilAgentUpdate reply was " + reply);
  275. }
  276. catch (WebException ex)
  277. {
  278. m_log.InfoFormat("[REST COMMS]: exception on reply of agent get {0}", ex.Message);
  279. // ignore, really
  280. return false;
  281. }
  282. finally
  283. {
  284. if (sr != null)
  285. sr.Close();
  286. }
  287. if (webResponse.StatusCode == HttpStatusCode.OK)
  288. {
  289. // we know it's jason
  290. OSDMap args = GetOSDMap(reply);
  291. if (args == null)
  292. {
  293. //Console.WriteLine("[REST COMMS]: Error getting OSDMap from reply");
  294. return false;
  295. }
  296. agent = new CompleteAgentData();
  297. agent.Unpack(args);
  298. return true;
  299. }
  300. //Console.WriteLine("[REST COMMS]: DoRetrieveRootAgentCall returned status " + webResponse.StatusCode);
  301. return false;
  302. }
  303. public bool DoReleaseAgentCall(ulong regionHandle, UUID id, string uri)
  304. {
  305. //m_log.Debug(" >>> DoReleaseAgentCall <<< " + uri);
  306. WebRequest request = WebRequest.Create(uri);
  307. request.Method = "DELETE";
  308. request.Timeout = 10000;
  309. StreamReader sr = null;
  310. try
  311. {
  312. WebResponse webResponse = request.GetResponse();
  313. if (webResponse == null)
  314. {
  315. m_log.Info("[REST COMMS]: Null reply on agent delete ");
  316. }
  317. sr = new StreamReader(webResponse.GetResponseStream());
  318. //reply = sr.ReadToEnd().Trim();
  319. sr.ReadToEnd().Trim();
  320. sr.Close();
  321. //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
  322. }
  323. catch (WebException ex)
  324. {
  325. m_log.InfoFormat("[REST COMMS]: exception on reply of agent delete {0}", ex.Message);
  326. // ignore, really
  327. }
  328. finally
  329. {
  330. if (sr != null)
  331. sr.Close();
  332. }
  333. return true;
  334. }
  335. public bool DoCloseAgentCall(GridRegion region, UUID id)
  336. {
  337. string uri = string.Empty;
  338. try
  339. {
  340. uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/agent/" + id + "/" + region.RegionHandle.ToString() + "/";
  341. }
  342. catch (Exception e)
  343. {
  344. m_log.Debug("[REST COMMS]: Unable to resolve external endpoint on agent close. Reason: " + e.Message);
  345. return false;
  346. }
  347. //Console.WriteLine(" >>> DoCloseAgentCall <<< " + uri);
  348. WebRequest request = WebRequest.Create(uri);
  349. request.Method = "DELETE";
  350. request.Timeout = 10000;
  351. StreamReader sr = null;
  352. try
  353. {
  354. WebResponse webResponse = request.GetResponse();
  355. if (webResponse == null)
  356. {
  357. m_log.Info("[REST COMMS]: Null reply on agent delete ");
  358. }
  359. sr = new StreamReader(webResponse.GetResponseStream());
  360. //reply = sr.ReadToEnd().Trim();
  361. sr.ReadToEnd().Trim();
  362. sr.Close();
  363. //m_log.InfoFormat("[REST COMMS]: ChilAgentUpdate reply was {0} ", reply);
  364. }
  365. catch (WebException ex)
  366. {
  367. m_log.InfoFormat("[REST COMMS]: exception on reply of agent delete {0}", ex.Message);
  368. // ignore, really
  369. }
  370. finally
  371. {
  372. if (sr != null)
  373. sr.Close();
  374. }
  375. return true;
  376. }
  377. public bool DoCreateObjectCall(GridRegion region, ISceneObject sog, string sogXml2, bool allowScriptCrossing)
  378. {
  379. ulong regionHandle = GetRegionHandle(region.RegionHandle);
  380. string uri
  381. = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort
  382. + "/object/" + sog.UUID + "/" + regionHandle.ToString() + "/";
  383. //m_log.Debug(" >>> DoCreateChildAgentCall <<< " + uri);
  384. WebRequest ObjectCreateRequest = WebRequest.Create(uri);
  385. ObjectCreateRequest.Method = "POST";
  386. ObjectCreateRequest.ContentType = "application/json";
  387. ObjectCreateRequest.Timeout = 10000;
  388. OSDMap args = new OSDMap(2);
  389. args["sog"] = OSD.FromString(sogXml2);
  390. args["extra"] = OSD.FromString(sog.ExtraToXmlString());
  391. if (allowScriptCrossing)
  392. {
  393. string state = sog.GetStateSnapshot();
  394. if (state.Length > 0)
  395. args["state"] = OSD.FromString(state);
  396. }
  397. string strBuffer = "";
  398. byte[] buffer = new byte[1];
  399. try
  400. {
  401. strBuffer = OSDParser.SerializeJsonString(args);
  402. Encoding str = Util.UTF8;
  403. buffer = str.GetBytes(strBuffer);
  404. }
  405. catch (Exception e)
  406. {
  407. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of CreateObject: {0}", e.Message);
  408. // ignore. buffer will be empty, caller should check.
  409. }
  410. Stream os = null;
  411. try
  412. { // send the Post
  413. ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  414. os = ObjectCreateRequest.GetRequestStream();
  415. os.Write(buffer, 0, strBuffer.Length); //Send it
  416. m_log.InfoFormat("[REST COMMS]: Posted ChildAgentUpdate request to remote sim {0}", uri);
  417. }
  418. //catch (WebException ex)
  419. catch
  420. {
  421. // m_log.InfoFormat("[REST COMMS]: Bad send on CreateObject {0}", ex.Message);
  422. return false;
  423. }
  424. finally
  425. {
  426. if (os != null)
  427. os.Close();
  428. }
  429. // Let's wait for the response
  430. //m_log.Info("[REST COMMS]: Waiting for a reply after DoCreateChildAgentCall");
  431. StreamReader sr = null;
  432. try
  433. {
  434. WebResponse webResponse = ObjectCreateRequest.GetResponse();
  435. if (webResponse == null)
  436. {
  437. m_log.Info("[REST COMMS]: Null reply on DoCreateObjectCall post");
  438. }
  439. sr = new StreamReader(webResponse.GetResponseStream());
  440. //reply = sr.ReadToEnd().Trim();
  441. sr.ReadToEnd().Trim();
  442. //m_log.InfoFormat("[REST COMMS]: DoCreateChildAgentCall reply was {0} ", reply);
  443. }
  444. catch (WebException ex)
  445. {
  446. m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateObjectCall {0}", ex.Message);
  447. // ignore, really
  448. }
  449. finally
  450. {
  451. if (sr != null)
  452. sr.Close();
  453. }
  454. return true;
  455. }
  456. public bool DoCreateObjectCall(GridRegion region, UUID userID, UUID itemID)
  457. {
  458. ulong regionHandle = GetRegionHandle(region.RegionHandle);
  459. string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/object/" + UUID.Zero + "/" + regionHandle.ToString() + "/";
  460. //m_log.Debug(" >>> DoCreateChildAgentCall <<< " + uri);
  461. WebRequest ObjectCreateRequest = WebRequest.Create(uri);
  462. ObjectCreateRequest.Method = "PUT";
  463. ObjectCreateRequest.ContentType = "application/json";
  464. ObjectCreateRequest.Timeout = 10000;
  465. OSDMap args = new OSDMap(2);
  466. args["userid"] = OSD.FromUUID(userID);
  467. args["itemid"] = OSD.FromUUID(itemID);
  468. string strBuffer = "";
  469. byte[] buffer = new byte[1];
  470. try
  471. {
  472. strBuffer = OSDParser.SerializeJsonString(args);
  473. Encoding str = Util.UTF8;
  474. buffer = str.GetBytes(strBuffer);
  475. }
  476. catch (Exception e)
  477. {
  478. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of CreateObject: {0}", e.Message);
  479. // ignore. buffer will be empty, caller should check.
  480. }
  481. Stream os = null;
  482. try
  483. { // send the Post
  484. ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  485. os = ObjectCreateRequest.GetRequestStream();
  486. os.Write(buffer, 0, strBuffer.Length); //Send it
  487. //m_log.InfoFormat("[REST COMMS]: Posted CreateObject request to remote sim {0}", uri);
  488. }
  489. //catch (WebException ex)
  490. catch
  491. {
  492. // m_log.InfoFormat("[REST COMMS]: Bad send on CreateObject {0}", ex.Message);
  493. return false;
  494. }
  495. finally
  496. {
  497. if (os != null)
  498. os.Close();
  499. }
  500. // Let's wait for the response
  501. //m_log.Info("[REST COMMS]: Waiting for a reply after DoCreateChildAgentCall");
  502. StreamReader sr = null;
  503. try
  504. {
  505. WebResponse webResponse = ObjectCreateRequest.GetResponse();
  506. if (webResponse == null)
  507. {
  508. m_log.Info("[REST COMMS]: Null reply on DoCreateObjectCall post");
  509. }
  510. sr = new StreamReader(webResponse.GetResponseStream());
  511. sr.ReadToEnd().Trim();
  512. sr.ReadToEnd().Trim();
  513. //m_log.InfoFormat("[REST COMMS]: DoCreateChildAgentCall reply was {0} ", reply);
  514. }
  515. catch (WebException ex)
  516. {
  517. m_log.InfoFormat("[REST COMMS]: exception on reply of DoCreateObjectCall {0}", ex.Message);
  518. // ignore, really
  519. }
  520. finally
  521. {
  522. if (sr != null)
  523. sr.Close();
  524. }
  525. return true;
  526. }
  527. public bool DoHelloNeighbourCall(RegionInfo region, RegionInfo thisRegion)
  528. {
  529. string uri = "http://" + region.ExternalEndPoint.Address + ":" + region.HttpPort + "/region/" + thisRegion.RegionID + "/";
  530. //m_log.Debug(" >>> DoHelloNeighbourCall <<< " + uri);
  531. WebRequest HelloNeighbourRequest = WebRequest.Create(uri);
  532. HelloNeighbourRequest.Method = "POST";
  533. HelloNeighbourRequest.ContentType = "application/json";
  534. HelloNeighbourRequest.Timeout = 10000;
  535. // Fill it in
  536. OSDMap args = null;
  537. try
  538. {
  539. args = thisRegion.PackRegionInfoData();
  540. }
  541. catch (Exception e)
  542. {
  543. m_log.Debug("[REST COMMS]: PackRegionInfoData failed with exception: " + e.Message);
  544. }
  545. // Add the regionhandle of the destination region
  546. ulong regionHandle = GetRegionHandle(region.RegionHandle);
  547. args["destination_handle"] = OSD.FromString(regionHandle.ToString());
  548. string strBuffer = "";
  549. byte[] buffer = new byte[1];
  550. try
  551. {
  552. strBuffer = OSDParser.SerializeJsonString(args);
  553. Encoding str = Util.UTF8;
  554. buffer = str.GetBytes(strBuffer);
  555. }
  556. catch (Exception e)
  557. {
  558. m_log.WarnFormat("[REST COMMS]: Exception thrown on serialization of HelloNeighbour: {0}", e.Message);
  559. // ignore. buffer will be empty, caller should check.
  560. }
  561. Stream os = null;
  562. try
  563. { // send the Post
  564. HelloNeighbourRequest.ContentLength = buffer.Length; //Count bytes to send
  565. os = HelloNeighbourRequest.GetRequestStream();
  566. os.Write(buffer, 0, strBuffer.Length); //Send it
  567. //m_log.InfoFormat("[REST COMMS]: Posted HelloNeighbour request to remote sim {0}", uri);
  568. }
  569. //catch (WebException ex)
  570. catch
  571. {
  572. //m_log.InfoFormat("[REST COMMS]: Bad send on HelloNeighbour {0}", ex.Message);
  573. return false;
  574. }
  575. finally
  576. {
  577. if (os != null)
  578. os.Close();
  579. }
  580. // Let's wait for the response
  581. //m_log.Info("[REST COMMS]: Waiting for a reply after DoHelloNeighbourCall");
  582. StreamReader sr = null;
  583. try
  584. {
  585. WebResponse webResponse = HelloNeighbourRequest.GetResponse();
  586. if (webResponse == null)
  587. {
  588. m_log.Info("[REST COMMS]: Null reply on DoHelloNeighbourCall post");
  589. }
  590. sr = new StreamReader(webResponse.GetResponseStream());
  591. //reply = sr.ReadToEnd().Trim();
  592. sr.ReadToEnd().Trim();
  593. //m_log.InfoFormat("[REST COMMS]: DoHelloNeighbourCall reply was {0} ", reply);
  594. }
  595. catch (WebException ex)
  596. {
  597. m_log.InfoFormat("[REST COMMS]: exception on reply of DoHelloNeighbourCall {0}", ex.Message);
  598. // ignore, really
  599. }
  600. finally
  601. {
  602. if (sr != null)
  603. sr.Close();
  604. }
  605. return true;
  606. }
  607. #region Hyperlinks
  608. public virtual ulong GetRegionHandle(ulong handle)
  609. {
  610. return handle;
  611. }
  612. public virtual bool IsHyperlink(ulong handle)
  613. {
  614. return false;
  615. }
  616. public virtual void SendUserInformation(GridRegion regInfo, AgentCircuitData aCircuit)
  617. {
  618. }
  619. public virtual void AdjustUserInformation(AgentCircuitData aCircuit)
  620. {
  621. }
  622. #endregion /* Hyperlinks */
  623. public static OSDMap GetOSDMap(string data)
  624. {
  625. OSDMap args = null;
  626. try
  627. {
  628. OSD buffer;
  629. // We should pay attention to the content-type, but let's assume we know it's Json
  630. buffer = OSDParser.DeserializeJson(data);
  631. if (buffer.Type == OSDType.Map)
  632. {
  633. args = (OSDMap)buffer;
  634. return args;
  635. }
  636. else
  637. {
  638. // uh?
  639. System.Console.WriteLine("[REST COMMS]: Got OSD of type " + buffer.Type.ToString());
  640. return null;
  641. }
  642. }
  643. catch (Exception ex)
  644. {
  645. System.Console.WriteLine("[REST COMMS]: exception on parse of REST message " + ex.Message);
  646. return null;
  647. }
  648. }
  649. }
  650. }