SimulationServiceConnector.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 System.Collections;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. using log4net;
  40. using Nini.Config;
  41. namespace OpenSim.Services.Connectors.Simulation
  42. {
  43. public class SimulationServiceConnector : ISimulationService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. // we use this dictionary to track the pending updateagent requests, maps URI --> position update
  47. private Dictionary<string,AgentPosition> m_updateAgentQueue = new Dictionary<string,AgentPosition>();
  48. //private GridRegion m_Region;
  49. public SimulationServiceConnector()
  50. {
  51. }
  52. public SimulationServiceConnector(IConfigSource config)
  53. {
  54. //m_Region = region;
  55. }
  56. public IScene GetScene(ulong regionHandle)
  57. {
  58. return null;
  59. }
  60. public ISimulationService GetInnerService()
  61. {
  62. return null;
  63. }
  64. #region Agents
  65. protected virtual string AgentPath()
  66. {
  67. return "agent/";
  68. }
  69. public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
  70. {
  71. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateAgent start");
  72. reason = String.Empty;
  73. if (destination == null)
  74. {
  75. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
  76. return false;
  77. }
  78. string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/";
  79. try
  80. {
  81. OSDMap args = aCircuit.PackAgentCircuitData();
  82. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  83. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  84. args["destination_name"] = OSD.FromString(destination.RegionName);
  85. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  86. args["teleport_flags"] = OSD.FromString(flags.ToString());
  87. OSDMap result = WebUtil.PostToServiceCompressed(uri, args, 30000);
  88. bool success = result["success"].AsBoolean();
  89. if (success && result.ContainsKey("_Result"))
  90. {
  91. OSDMap data = (OSDMap)result["_Result"];
  92. reason = data["reason"].AsString();
  93. success = data["success"].AsBoolean();
  94. return success;
  95. }
  96. // Try the old version, uncompressed
  97. result = WebUtil.PostToService(uri, args, 30000);
  98. if (result["Success"].AsBoolean())
  99. {
  100. if (result.ContainsKey("_Result"))
  101. {
  102. OSDMap data = (OSDMap)result["_Result"];
  103. reason = data["reason"].AsString();
  104. success = data["success"].AsBoolean();
  105. m_log.WarnFormat(
  106. "[REMOTE SIMULATION CONNECTOR]: Remote simulator {0} did not accept compressed transfer, suggest updating it.", destination.RegionName);
  107. return success;
  108. }
  109. }
  110. m_log.WarnFormat(
  111. "[REMOTE SIMULATION CONNECTOR]: Failed to create agent {0} {1} at remote simulator {2}",
  112. aCircuit.firstname, aCircuit.lastname, destination.RegionName);
  113. reason = result["Message"] != null ? result["Message"].AsString() : "error";
  114. return false;
  115. }
  116. catch (Exception e)
  117. {
  118. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: CreateAgent failed with exception: " + e.ToString());
  119. reason = e.Message;
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Send complete data about an agent in this region to a neighbor
  125. /// </summary>
  126. public bool UpdateAgent(GridRegion destination, AgentData data)
  127. {
  128. return UpdateAgent(destination, (IAgentData)data, 200000); // yes, 200 seconds
  129. }
  130. /// <summary>
  131. /// Send updated position information about an agent in this region to a neighbor
  132. /// This operation may be called very frequently if an avatar is moving about in
  133. /// the region.
  134. /// </summary>
  135. public bool UpdateAgent(GridRegion destination, AgentPosition data)
  136. {
  137. // The basic idea of this code is that the first thread that needs to
  138. // send an update for a specific avatar becomes the worker for any subsequent
  139. // requests until there are no more outstanding requests. Further, only send the most
  140. // recent update; this *should* never be needed but some requests get
  141. // slowed down and once that happens the problem with service end point
  142. // limits kicks in and nothing proceeds
  143. string uri = destination.ServerURI + AgentPath() + data.AgentID + "/";
  144. lock (m_updateAgentQueue)
  145. {
  146. if (m_updateAgentQueue.ContainsKey(uri))
  147. {
  148. // Another thread is already handling
  149. // updates for this simulator, just update
  150. // the position and return, overwrites are
  151. // not a problem since we only care about the
  152. // last update anyway
  153. m_updateAgentQueue[uri] = data;
  154. return true;
  155. }
  156. // Otherwise update the reference and start processing
  157. m_updateAgentQueue[uri] = data;
  158. }
  159. AgentPosition pos = null;
  160. while (true)
  161. {
  162. lock (m_updateAgentQueue)
  163. {
  164. // save the position
  165. AgentPosition lastpos = pos;
  166. pos = m_updateAgentQueue[uri];
  167. // this is true if no one put a new
  168. // update in the map since the last
  169. // one we processed, if thats the
  170. // case then we are done
  171. if (pos == lastpos)
  172. {
  173. m_updateAgentQueue.Remove(uri);
  174. return true;
  175. }
  176. }
  177. UpdateAgent(destination, (IAgentData)pos, 10000);
  178. }
  179. // unreachable
  180. // return true;
  181. }
  182. /// <summary>
  183. /// This is the worker function to send AgentData to a neighbor region
  184. /// </summary>
  185. private bool UpdateAgent(GridRegion destination, IAgentData cAgentData, int timeout)
  186. {
  187. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: UpdateAgent start");
  188. // Eventually, we want to use a caps url instead of the agentID
  189. string uri = destination.ServerURI + AgentPath() + cAgentData.AgentID + "/";
  190. try
  191. {
  192. OSDMap args = cAgentData.Pack();
  193. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  194. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  195. args["destination_name"] = OSD.FromString(destination.RegionName);
  196. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  197. OSDMap result = WebUtil.PutToServiceCompressed(uri, args, timeout);
  198. if (result["Success"].AsBoolean())
  199. return true;
  200. result = WebUtil.PutToService(uri, args, timeout);
  201. return result["Success"].AsBoolean();
  202. }
  203. catch (Exception e)
  204. {
  205. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
  206. }
  207. return false;
  208. }
  209. /// <summary>
  210. /// Not sure what sequence causes this function to be invoked. The only calling
  211. /// path is through the GET method
  212. /// </summary>
  213. public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
  214. {
  215. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: RetrieveAgent start");
  216. agent = null;
  217. // Eventually, we want to use a caps url instead of the agentID
  218. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  219. try
  220. {
  221. OSDMap result = WebUtil.GetFromService(uri, 10000);
  222. if (result["Success"].AsBoolean())
  223. {
  224. // OSDMap args = Util.GetOSDMap(result["_RawResult"].AsString());
  225. OSDMap args = (OSDMap)result["_Result"];
  226. if (args != null)
  227. {
  228. agent = new CompleteAgentData();
  229. agent.Unpack(args, null);
  230. return true;
  231. }
  232. }
  233. }
  234. catch (Exception e)
  235. {
  236. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
  237. }
  238. return false;
  239. }
  240. /// <summary>
  241. /// </summary>
  242. public bool QueryAccess(GridRegion destination, UUID id, Vector3 position, out string version, out string reason)
  243. {
  244. reason = "Failed to contact destination";
  245. version = "Unknown";
  246. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: QueryAccess start, position={0}", position);
  247. IPEndPoint ext = destination.ExternalEndPoint;
  248. if (ext == null) return false;
  249. // Eventually, we want to use a caps url instead of the agentID
  250. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  251. OSDMap request = new OSDMap();
  252. request.Add("position", OSD.FromString(position.ToString()));
  253. try
  254. {
  255. OSDMap result = WebUtil.ServiceOSDRequest(uri, request, "QUERYACCESS", 10000, false);
  256. bool success = result["success"].AsBoolean();
  257. if (result.ContainsKey("_Result"))
  258. {
  259. OSDMap data = (OSDMap)result["_Result"];
  260. reason = data["reason"].AsString();
  261. if (data["version"] != null && data["version"].AsString() != string.Empty)
  262. version = data["version"].AsString();
  263. m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: QueryAccess to {0} returned {1} version {2} ({3})", uri, success, version, data["version"].AsString());
  264. }
  265. if (!success)
  266. {
  267. if (result.ContainsKey("Message"))
  268. {
  269. string message = result["Message"].AsString();
  270. if (message == "Service request failed: [MethodNotAllowed] MethodNotAllowed") // Old style region
  271. {
  272. m_log.Info("[REMOTE SIMULATION CONNECTOR]: The above web util error was caused by a TP to a sim that doesn't support QUERYACCESS and can be ignored");
  273. return true;
  274. }
  275. reason = result["Message"];
  276. }
  277. else
  278. {
  279. reason = "Communications failure";
  280. }
  281. return false;
  282. }
  283. return success;
  284. }
  285. catch (Exception e)
  286. {
  287. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] QueryAcess failed with exception; {0}",e.ToString());
  288. }
  289. return false;
  290. }
  291. /// <summary>
  292. /// </summary>
  293. public bool ReleaseAgent(UUID origin, UUID id, string uri)
  294. {
  295. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: ReleaseAgent start");
  296. try
  297. {
  298. WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false);
  299. }
  300. catch (Exception e)
  301. {
  302. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] ReleaseAgent failed with exception; {0}",e.ToString());
  303. }
  304. return true;
  305. }
  306. /// <summary>
  307. /// </summary>
  308. public bool CloseAgent(GridRegion destination, UUID id)
  309. {
  310. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CloseAgent start");
  311. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  312. try
  313. {
  314. WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000, false);
  315. }
  316. catch (Exception e)
  317. {
  318. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CloseAgent failed with exception; {0}",e.ToString());
  319. }
  320. return true;
  321. }
  322. #endregion Agents
  323. #region Objects
  324. protected virtual string ObjectPath()
  325. {
  326. return "object/";
  327. }
  328. /// <summary>
  329. ///
  330. /// </summary>
  331. public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
  332. {
  333. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateObject start");
  334. string uri = destination.ServerURI + ObjectPath() + sog.UUID + "/";
  335. try
  336. {
  337. OSDMap args = new OSDMap(2);
  338. args["sog"] = OSD.FromString(sog.ToXml2());
  339. args["extra"] = OSD.FromString(sog.ExtraToXmlString());
  340. args["modified"] = OSD.FromBoolean(sog.HasGroupChanged);
  341. string state = sog.GetStateSnapshot();
  342. if (state.Length > 0)
  343. args["state"] = OSD.FromString(state);
  344. // Add the input general arguments
  345. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  346. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  347. args["destination_name"] = OSD.FromString(destination.RegionName);
  348. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  349. WebUtil.PostToService(uri, args, 40000);
  350. }
  351. catch (Exception e)
  352. {
  353. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CreateObject failed with exception; {0}",e.ToString());
  354. }
  355. return true;
  356. }
  357. /// <summary>
  358. ///
  359. /// </summary>
  360. public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
  361. {
  362. // TODO, not that urgent
  363. return false;
  364. }
  365. #endregion Objects
  366. }
  367. }