SimulationServiceConnector.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using log4net;
  39. using Nini.Config;
  40. namespace OpenSim.Services.Connectors.Simulation
  41. {
  42. public class SimulationServiceConnector : ISimulationService
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. //private GridRegion m_Region;
  46. public SimulationServiceConnector()
  47. {
  48. }
  49. public SimulationServiceConnector(IConfigSource config)
  50. {
  51. //m_Region = region;
  52. }
  53. public IScene GetScene(ulong regionHandle)
  54. {
  55. return null;
  56. }
  57. public ISimulationService GetInnerService()
  58. {
  59. return null;
  60. }
  61. #region Agents
  62. protected virtual string AgentPath()
  63. {
  64. return "/agent/";
  65. }
  66. public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
  67. {
  68. reason = String.Empty;
  69. if (destination == null)
  70. {
  71. reason = "Destination is null";
  72. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
  73. return false;
  74. }
  75. // Eventually, we want to use a caps url instead of the agentID
  76. string uri = string.Empty;
  77. try
  78. {
  79. uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + aCircuit.AgentID + "/";
  80. }
  81. catch (Exception e)
  82. {
  83. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent create. Reason: " + e.Message);
  84. reason = e.Message;
  85. return false;
  86. }
  87. //Console.WriteLine(" >>> DoCreateChildAgentCall <<< " + uri);
  88. HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
  89. AgentCreateRequest.Method = "POST";
  90. AgentCreateRequest.ContentType = "application/json";
  91. AgentCreateRequest.Timeout = 10000;
  92. //AgentCreateRequest.KeepAlive = false;
  93. //AgentCreateRequest.Headers.Add("Authorization", authKey);
  94. // Fill it in
  95. OSDMap args = PackCreateAgentArguments(aCircuit, destination, flags);
  96. if (args == null)
  97. return false;
  98. string strBuffer = "";
  99. byte[] buffer = new byte[1];
  100. try
  101. {
  102. strBuffer = OSDParser.SerializeJsonString(args);
  103. Encoding str = Util.UTF8;
  104. buffer = str.GetBytes(strBuffer);
  105. }
  106. catch (Exception e)
  107. {
  108. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
  109. // ignore. buffer will be empty, caller should check.
  110. }
  111. Stream os = null;
  112. try
  113. { // send the Post
  114. AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  115. os = AgentCreateRequest.GetRequestStream();
  116. os.Write(buffer, 0, strBuffer.Length); //Send it
  117. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
  118. uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
  119. }
  120. //catch (WebException ex)
  121. catch
  122. {
  123. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
  124. reason = "cannot contact remote region";
  125. return false;
  126. }
  127. finally
  128. {
  129. if (os != null)
  130. os.Close();
  131. }
  132. // Let's wait for the response
  133. //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
  134. WebResponse webResponse = null;
  135. StreamReader sr = null;
  136. try
  137. {
  138. webResponse = AgentCreateRequest.GetResponse();
  139. if (webResponse == null)
  140. {
  141. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on DoCreateChildAgentCall post");
  142. }
  143. else
  144. {
  145. sr = new StreamReader(webResponse.GetResponseStream());
  146. string response = sr.ReadToEnd().Trim();
  147. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
  148. if (!String.IsNullOrEmpty(response))
  149. {
  150. try
  151. {
  152. // we assume we got an OSDMap back
  153. OSDMap r = Util.GetOSDMap(response);
  154. bool success = r["success"].AsBoolean();
  155. reason = r["reason"].AsString();
  156. return success;
  157. }
  158. catch (NullReferenceException e)
  159. {
  160. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
  161. // check for old style response
  162. if (response.ToLower().StartsWith("true"))
  163. return true;
  164. return false;
  165. }
  166. }
  167. }
  168. }
  169. catch (WebException ex)
  170. {
  171. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
  172. reason = "Destination did not reply";
  173. return false;
  174. }
  175. finally
  176. {
  177. if (sr != null)
  178. sr.Close();
  179. }
  180. return true;
  181. }
  182. protected virtual OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion destination, uint flags)
  183. {
  184. OSDMap args = null;
  185. try
  186. {
  187. args = aCircuit.PackAgentCircuitData();
  188. }
  189. catch (Exception e)
  190. {
  191. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message);
  192. return null;
  193. }
  194. // Add the input arguments
  195. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  196. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  197. args["destination_name"] = OSD.FromString(destination.RegionName);
  198. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  199. args["teleport_flags"] = OSD.FromString(flags.ToString());
  200. return args;
  201. }
  202. public bool UpdateAgent(GridRegion destination, AgentData data)
  203. {
  204. return UpdateAgent(destination, (IAgentData)data);
  205. }
  206. public bool UpdateAgent(GridRegion destination, AgentPosition data)
  207. {
  208. return UpdateAgent(destination, (IAgentData)data);
  209. }
  210. private bool UpdateAgent(GridRegion destination, IAgentData cAgentData)
  211. {
  212. // Eventually, we want to use a caps url instead of the agentID
  213. string uri = string.Empty;
  214. try
  215. {
  216. uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + cAgentData.AgentID + "/";
  217. }
  218. catch (Exception e)
  219. {
  220. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent update. Reason: " + e.Message);
  221. return false;
  222. }
  223. //Console.WriteLine(" >>> DoAgentUpdateCall <<< " + uri);
  224. HttpWebRequest ChildUpdateRequest = (HttpWebRequest)WebRequest.Create(uri);
  225. ChildUpdateRequest.Method = "PUT";
  226. ChildUpdateRequest.ContentType = "application/json";
  227. ChildUpdateRequest.Timeout = 10000;
  228. //ChildUpdateRequest.KeepAlive = false;
  229. // Fill it in
  230. OSDMap args = null;
  231. try
  232. {
  233. args = cAgentData.Pack();
  234. }
  235. catch (Exception e)
  236. {
  237. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: PackUpdateMessage failed with exception: " + e.Message);
  238. }
  239. // Add the input arguments
  240. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  241. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  242. args["destination_name"] = OSD.FromString(destination.RegionName);
  243. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  244. string strBuffer = "";
  245. byte[] buffer = new byte[1];
  246. try
  247. {
  248. strBuffer = OSDParser.SerializeJsonString(args);
  249. Encoding str = Util.UTF8;
  250. buffer = str.GetBytes(strBuffer);
  251. }
  252. catch (Exception e)
  253. {
  254. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of ChildUpdate: {0}", e.Message);
  255. // ignore. buffer will be empty, caller should check.
  256. }
  257. Stream os = null;
  258. try
  259. { // send the Post
  260. ChildUpdateRequest.ContentLength = buffer.Length; //Count bytes to send
  261. os = ChildUpdateRequest.GetRequestStream();
  262. os.Write(buffer, 0, strBuffer.Length); //Send it
  263. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted AgentUpdate request to remote sim {0}", uri);
  264. }
  265. catch (WebException ex)
  266. //catch
  267. {
  268. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on AgentUpdate {0}", ex.Message);
  269. return false;
  270. }
  271. finally
  272. {
  273. if (os != null)
  274. os.Close();
  275. }
  276. // Let's wait for the response
  277. //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after ChildAgentUpdate");
  278. WebResponse webResponse = null;
  279. StreamReader sr = null;
  280. try
  281. {
  282. webResponse = ChildUpdateRequest.GetResponse();
  283. if (webResponse == null)
  284. {
  285. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on ChilAgentUpdate post");
  286. }
  287. sr = new StreamReader(webResponse.GetResponseStream());
  288. //reply = sr.ReadToEnd().Trim();
  289. sr.ReadToEnd().Trim();
  290. sr.Close();
  291. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
  292. }
  293. catch (WebException ex)
  294. {
  295. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ChilAgentUpdate {0}", ex.Message);
  296. // ignore, really
  297. }
  298. finally
  299. {
  300. if (sr != null)
  301. sr.Close();
  302. }
  303. return true;
  304. }
  305. public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
  306. {
  307. agent = null;
  308. // Eventually, we want to use a caps url instead of the agentID
  309. string uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  310. //Console.WriteLine(" >>> DoRetrieveRootAgentCall <<< " + uri);
  311. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  312. request.Method = "GET";
  313. request.Timeout = 10000;
  314. //request.Headers.Add("authorization", ""); // coming soon
  315. HttpWebResponse webResponse = null;
  316. string reply = string.Empty;
  317. StreamReader sr = null;
  318. try
  319. {
  320. webResponse = (HttpWebResponse)request.GetResponse();
  321. if (webResponse == null)
  322. {
  323. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on agent get ");
  324. }
  325. sr = new StreamReader(webResponse.GetResponseStream());
  326. reply = sr.ReadToEnd().Trim();
  327. //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was " + reply);
  328. }
  329. catch (WebException ex)
  330. {
  331. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent get {0}", ex.Message);
  332. // ignore, really
  333. return false;
  334. }
  335. finally
  336. {
  337. if (sr != null)
  338. sr.Close();
  339. }
  340. if (webResponse.StatusCode == HttpStatusCode.OK)
  341. {
  342. // we know it's jason
  343. OSDMap args = Util.GetOSDMap(reply);
  344. if (args == null)
  345. {
  346. //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: Error getting OSDMap from reply");
  347. return false;
  348. }
  349. agent = new CompleteAgentData();
  350. agent.Unpack(args);
  351. return true;
  352. }
  353. //Console.WriteLine("[REMOTE SIMULATION CONNECTOR]: DoRetrieveRootAgentCall returned status " + webResponse.StatusCode);
  354. return false;
  355. }
  356. public bool ReleaseAgent(UUID origin, UUID id, string uri)
  357. {
  358. WebRequest request = WebRequest.Create(uri);
  359. request.Method = "DELETE";
  360. request.Timeout = 10000;
  361. StreamReader sr = null;
  362. try
  363. {
  364. WebResponse webResponse = request.GetResponse();
  365. if (webResponse == null)
  366. {
  367. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on ReleaseAgent");
  368. }
  369. sr = new StreamReader(webResponse.GetResponseStream());
  370. //reply = sr.ReadToEnd().Trim();
  371. sr.ReadToEnd().Trim();
  372. sr.Close();
  373. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
  374. }
  375. catch (WebException ex)
  376. {
  377. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of ReleaseAgent {0}", ex.Message);
  378. return false;
  379. }
  380. finally
  381. {
  382. if (sr != null)
  383. sr.Close();
  384. }
  385. return true;
  386. }
  387. public bool CloseAgent(GridRegion destination, UUID id)
  388. {
  389. string uri = string.Empty;
  390. try
  391. {
  392. uri = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  393. }
  394. catch (Exception e)
  395. {
  396. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Unable to resolve external endpoint on agent close. Reason: " + e.Message);
  397. return false;
  398. }
  399. //Console.WriteLine(" >>> DoCloseAgentCall <<< " + uri);
  400. WebRequest request = WebRequest.Create(uri);
  401. request.Method = "DELETE";
  402. request.Timeout = 10000;
  403. StreamReader sr = null;
  404. try
  405. {
  406. WebResponse webResponse = request.GetResponse();
  407. if (webResponse == null)
  408. {
  409. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on agent delete ");
  410. }
  411. sr = new StreamReader(webResponse.GetResponseStream());
  412. //reply = sr.ReadToEnd().Trim();
  413. sr.ReadToEnd().Trim();
  414. sr.Close();
  415. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: ChilAgentUpdate reply was {0} ", reply);
  416. }
  417. catch (WebException ex)
  418. {
  419. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of agent delete from {0}: {1}", destination.RegionName, ex.Message);
  420. return false;
  421. }
  422. finally
  423. {
  424. if (sr != null)
  425. sr.Close();
  426. }
  427. return true;
  428. }
  429. #endregion Agents
  430. #region Objects
  431. protected virtual string ObjectPath()
  432. {
  433. return "/object/";
  434. }
  435. public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
  436. {
  437. string uri
  438. = "http://" + destination.ExternalEndPoint.Address + ":" + destination.HttpPort + ObjectPath() + sog.UUID + "/";
  439. //m_log.Debug(" >>> DoCreateObjectCall <<< " + uri);
  440. WebRequest ObjectCreateRequest = WebRequest.Create(uri);
  441. ObjectCreateRequest.Method = "POST";
  442. ObjectCreateRequest.ContentType = "application/json";
  443. ObjectCreateRequest.Timeout = 10000;
  444. OSDMap args = new OSDMap(2);
  445. args["sog"] = OSD.FromString(sog.ToXml2());
  446. args["extra"] = OSD.FromString(sog.ExtraToXmlString());
  447. string state = sog.GetStateSnapshot();
  448. if (state.Length > 0)
  449. args["state"] = OSD.FromString(state);
  450. // Add the input general arguments
  451. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  452. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  453. args["destination_name"] = OSD.FromString(destination.RegionName);
  454. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  455. string strBuffer = "";
  456. byte[] buffer = new byte[1];
  457. try
  458. {
  459. strBuffer = OSDParser.SerializeJsonString(args);
  460. Encoding str = Util.UTF8;
  461. buffer = str.GetBytes(strBuffer);
  462. }
  463. catch (Exception e)
  464. {
  465. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR]: Exception thrown on serialization of CreateObject: {0}", e.Message);
  466. // ignore. buffer will be empty, caller should check.
  467. }
  468. Stream os = null;
  469. try
  470. { // send the Post
  471. ObjectCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  472. os = ObjectCreateRequest.GetRequestStream();
  473. os.Write(buffer, 0, strBuffer.Length); //Send it
  474. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Posted CreateObject request to remote sim {0}", uri);
  475. }
  476. catch (WebException ex)
  477. {
  478. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: Bad send on CreateObject {0}", ex.Message);
  479. return false;
  480. }
  481. finally
  482. {
  483. if (os != null)
  484. os.Close();
  485. }
  486. // Let's wait for the response
  487. //m_log.Info("[REMOTE SIMULATION CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
  488. StreamReader sr = null;
  489. try
  490. {
  491. WebResponse webResponse = ObjectCreateRequest.GetResponse();
  492. if (webResponse == null)
  493. {
  494. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Null reply on CreateObject post");
  495. return false;
  496. }
  497. sr = new StreamReader(webResponse.GetResponseStream());
  498. //reply = sr.ReadToEnd().Trim();
  499. sr.ReadToEnd().Trim();
  500. //m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: DoCreateChildAgentCall reply was {0} ", reply);
  501. }
  502. catch (WebException ex)
  503. {
  504. m_log.InfoFormat("[REMOTE SIMULATION CONNECTOR]: exception on reply of CreateObject {0}", ex.Message);
  505. return false;
  506. }
  507. finally
  508. {
  509. if (sr != null)
  510. sr.Close();
  511. }
  512. return true;
  513. }
  514. public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
  515. {
  516. // TODO, not that urgent
  517. return false;
  518. }
  519. #endregion Objects
  520. }
  521. }