1
0

SimulationServiceConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. //private GridRegion m_Region;
  47. public SimulationServiceConnector()
  48. {
  49. }
  50. public SimulationServiceConnector(IConfigSource config)
  51. {
  52. //m_Region = region;
  53. }
  54. public IScene GetScene(ulong regionHandle)
  55. {
  56. return null;
  57. }
  58. public ISimulationService GetInnerService()
  59. {
  60. return null;
  61. }
  62. #region Agents
  63. protected virtual string AgentPath()
  64. {
  65. return "agent/";
  66. }
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string reason)
  71. {
  72. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateAgent start");
  73. reason = String.Empty;
  74. if (destination == null)
  75. {
  76. m_log.Debug("[REMOTE SIMULATION CONNECTOR]: Given destination is null");
  77. return false;
  78. }
  79. string uri = destination.ServerURI + AgentPath() + aCircuit.AgentID + "/";
  80. try
  81. {
  82. OSDMap args = aCircuit.PackAgentCircuitData();
  83. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  84. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  85. args["destination_name"] = OSD.FromString(destination.RegionName);
  86. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  87. args["teleport_flags"] = OSD.FromString(flags.ToString());
  88. OSDMap result = WebUtil.PostToService(uri,args);
  89. if (result["Success"].AsBoolean())
  90. return true;
  91. reason = result["Message"] != null ? result["Message"].AsString() : "error";
  92. return false;
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: CreateAgent failed with exception: " + e.ToString());
  97. reason = e.Message;
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// Send complete data about an agent in this region to a neighbor
  103. /// </summary>
  104. public bool UpdateAgent(GridRegion destination, AgentData data)
  105. {
  106. return UpdateAgent(destination, (IAgentData)data);
  107. }
  108. /// <summary>
  109. /// Send updated position information about an agent in this region to a neighbor
  110. /// This operation may be called very frequently if an avatar is moving about in
  111. /// the region.
  112. /// </summary>
  113. public bool UpdateAgent(GridRegion destination, AgentPosition data)
  114. {
  115. // we need a better throttle for these
  116. // return false;
  117. return UpdateAgent(destination, (IAgentData)data);
  118. }
  119. /// <summary>
  120. /// This is the worker function to send AgentData to a neighbor region
  121. /// </summary>
  122. private bool UpdateAgent(GridRegion destination, IAgentData cAgentData)
  123. {
  124. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: UpdateAgent start");
  125. // Eventually, we want to use a caps url instead of the agentID
  126. string uri = destination.ServerURI + AgentPath() + cAgentData.AgentID + "/";
  127. try
  128. {
  129. OSDMap args = cAgentData.Pack();
  130. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  131. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  132. args["destination_name"] = OSD.FromString(destination.RegionName);
  133. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  134. OSDMap result = WebUtil.PutToService(uri,args);
  135. return result["Success"].AsBoolean();
  136. }
  137. catch (Exception e)
  138. {
  139. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// Not sure what sequence causes this function to be invoked. The only calling
  145. /// path is through the GET method
  146. /// </summary>
  147. public bool RetrieveAgent(GridRegion destination, UUID id, out IAgentData agent)
  148. {
  149. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: RetrieveAgent start");
  150. agent = null;
  151. // Eventually, we want to use a caps url instead of the agentID
  152. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  153. try
  154. {
  155. OSDMap result = WebUtil.GetFromService(uri);
  156. if (result["Success"].AsBoolean())
  157. {
  158. // OSDMap args = Util.GetOSDMap(result["_RawResult"].AsString());
  159. OSDMap args = (OSDMap)result["_Result"];
  160. if (args != null)
  161. {
  162. agent = new CompleteAgentData();
  163. agent.Unpack(args);
  164. return true;
  165. }
  166. }
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Warn("[REMOTE SIMULATION CONNECTOR]: UpdateAgent failed with exception: " + e.ToString());
  171. }
  172. return false;
  173. }
  174. /// <summary>
  175. /// </summary>
  176. public bool QueryAccess(GridRegion destination, UUID id, Vector3 position)
  177. {
  178. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: QueryAccess start, position={0}", position);
  179. IPEndPoint ext = destination.ExternalEndPoint;
  180. if (ext == null) return false;
  181. // Eventually, we want to use a caps url instead of the agentID
  182. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  183. OSDMap request = new OSDMap();
  184. request.Add("position", OSD.FromString(position.ToString()));
  185. try
  186. {
  187. OSDMap result = WebUtil.ServiceOSDRequest(uri, request, "QUERYACCESS", 10000);
  188. bool success = result["Success"].AsBoolean();
  189. if (!success)
  190. {
  191. if (result.ContainsKey("Message"))
  192. {
  193. string message = result["Message"].AsString();
  194. if (message == "Service request failed: [MethodNotAllowed] MethodNotAllowed") // Old style region
  195. {
  196. 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");
  197. return true;
  198. }
  199. }
  200. }
  201. return success;
  202. }
  203. catch (Exception e)
  204. {
  205. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] QueryAcess failed with exception; {0}",e.ToString());
  206. }
  207. return false;
  208. }
  209. /// <summary>
  210. /// </summary>
  211. public bool ReleaseAgent(UUID origin, UUID id, string uri)
  212. {
  213. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: ReleaseAgent start");
  214. try
  215. {
  216. WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000);
  217. }
  218. catch (Exception e)
  219. {
  220. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] ReleaseAgent failed with exception; {0}",e.ToString());
  221. }
  222. return true;
  223. }
  224. /// <summary>
  225. /// </summary>
  226. public bool CloseAgent(GridRegion destination, UUID id)
  227. {
  228. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CloseAgent start");
  229. string uri = destination.ServerURI + AgentPath() + id + "/" + destination.RegionID.ToString() + "/";
  230. try
  231. {
  232. WebUtil.ServiceOSDRequest(uri, null, "DELETE", 10000);
  233. }
  234. catch (Exception e)
  235. {
  236. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CloseAgent failed with exception; {0}",e.ToString());
  237. }
  238. return true;
  239. }
  240. #endregion Agents
  241. #region Objects
  242. protected virtual string ObjectPath()
  243. {
  244. return "object/";
  245. }
  246. /// <summary>
  247. ///
  248. /// </summary>
  249. public bool CreateObject(GridRegion destination, ISceneObject sog, bool isLocalCall)
  250. {
  251. // m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateObject start");
  252. string uri = destination.ServerURI + ObjectPath() + sog.UUID + "/";
  253. try
  254. {
  255. OSDMap args = new OSDMap(2);
  256. args["sog"] = OSD.FromString(sog.ToXml2());
  257. args["extra"] = OSD.FromString(sog.ExtraToXmlString());
  258. args["modified"] = OSD.FromBoolean(sog.HasGroupChanged);
  259. string state = sog.GetStateSnapshot();
  260. if (state.Length > 0)
  261. args["state"] = OSD.FromString(state);
  262. // Add the input general arguments
  263. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  264. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  265. args["destination_name"] = OSD.FromString(destination.RegionName);
  266. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  267. WebUtil.PostToService(uri, args);
  268. }
  269. catch (Exception e)
  270. {
  271. m_log.WarnFormat("[REMOTE SIMULATION CONNECTOR] CreateObject failed with exception; {0}",e.ToString());
  272. }
  273. return true;
  274. }
  275. /// <summary>
  276. ///
  277. /// </summary>
  278. public bool CreateObject(GridRegion destination, UUID userID, UUID itemID)
  279. {
  280. // TODO, not that urgent
  281. return false;
  282. }
  283. #endregion Objects
  284. }
  285. }