RegionClient.cs 27 KB

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