AgentHandlers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.IO.Compression;
  32. using System.Reflection;
  33. using System.Net;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using log4net;
  41. namespace OpenSim.Server.Handlers.Simulation
  42. {
  43. //this is only for hg homeagent and gatekeeperagent
  44. public class AgentPostHandler : SimpleStreamHandler
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. protected bool m_Proxy = false;
  48. public AgentPostHandler(string path) : base(path)
  49. {
  50. }
  51. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  52. {
  53. // m_log.DebugFormat("[SIMULATION]: Stream handler called");
  54. httpResponse.ContentType = "text/html"; //??
  55. httpResponse.KeepAlive = false;
  56. if(httpRequest.HttpMethod != "POST")
  57. {
  58. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  59. httpResponse.RawBuffer = Utils.falseStrBytes;
  60. }
  61. if (httpRequest.ContentType != "application/json" && httpRequest.ContentType != "application/x-gzip")
  62. {
  63. httpResponse.StatusCode = (int)HttpStatusCode.NotAcceptable;
  64. httpResponse.RawBuffer = Utils.falseStrBytes;
  65. }
  66. UUID agentID;
  67. UUID regionID;
  68. string action;
  69. if (!Utils.GetParams(httpRequest.UriPath, out agentID, out regionID, out action))
  70. {
  71. m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", httpRequest.RawUrl);
  72. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  73. httpResponse.RawBuffer = Utils.falseStrBytes;
  74. }
  75. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  76. if (args == null)
  77. {
  78. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  79. httpResponse.RawBuffer = Utils.falseStrBytes;
  80. }
  81. DoAgentPost(args, httpRequest.RemoteIPEndPoint.Address.ToString(), httpResponse, agentID);
  82. httpResponse.StatusCode = 200;
  83. }
  84. protected void DoAgentPost(OSDMap args, string remoteAddress, IOSHttpResponse response, UUID id)
  85. {
  86. OSD tmpOSD;
  87. EntityTransferContext ctx = new EntityTransferContext();
  88. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  89. ctx.Unpack((OSDMap)tmpOSD);
  90. AgentDestinationData data = CreateAgentDestinationData();
  91. UnpackData(args, data, remoteAddress);
  92. GridRegion destination = new GridRegion();
  93. destination.RegionID = data.uuid;
  94. destination.RegionLocX = data.x;
  95. destination.RegionLocY = data.y;
  96. destination.RegionName = data.name;
  97. GridRegion gatekeeper = ExtractGatekeeper(data);
  98. AgentCircuitData aCircuit = new AgentCircuitData();
  99. try
  100. {
  101. aCircuit.UnpackAgentCircuitData(args);
  102. }
  103. catch (Exception ex)
  104. {
  105. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
  106. response.StatusCode = (int)HttpStatusCode.BadRequest;
  107. return;
  108. }
  109. GridRegion source = null;
  110. if (args.TryGetValue("source_uuid", out tmpOSD))
  111. {
  112. source = new GridRegion();
  113. source.RegionID = UUID.Parse(tmpOSD.AsString());
  114. source.RegionLocX = Int32.Parse(args["source_x"].AsString());
  115. source.RegionLocY = Int32.Parse(args["source_y"].AsString());
  116. source.RegionName = args["source_name"].AsString();
  117. if (args.TryGetValue("source_server_uri", out tmpOSD))
  118. source.RawServerURI = tmpOSD.AsString();
  119. else
  120. source.RawServerURI = null;
  121. }
  122. OSDMap resp = new OSDMap(2);
  123. string reason = string.Empty;
  124. bool result = CreateAgent(source, gatekeeper, destination, aCircuit, data.flags, data.fromLogin, ctx, out reason);
  125. resp["reason"] = OSD.FromString(reason);
  126. resp["success"] = OSD.FromBoolean(result);
  127. // Let's also send out the IP address of the caller back to the caller (HG 1.5)
  128. resp["your_ip"] = remoteAddress;
  129. response.StatusCode = (int)HttpStatusCode.OK;
  130. response.RawBuffer = OSDParser.SerializeJsonToBytes(resp);
  131. }
  132. protected virtual AgentDestinationData CreateAgentDestinationData()
  133. {
  134. return new AgentDestinationData();
  135. }
  136. protected virtual void UnpackData(OSDMap args, AgentDestinationData data, string remoteAddress)
  137. {
  138. OSD tmpOSD;
  139. // retrieve the input arguments
  140. if (args.TryGetValue("destination_x", out tmpOSD) && tmpOSD != null)
  141. Int32.TryParse(tmpOSD.AsString(), out data.x);
  142. else
  143. m_log.WarnFormat(" -- request didn't have destination_x");
  144. if (args.TryGetValue("destination_y", out tmpOSD) && tmpOSD != null)
  145. Int32.TryParse(tmpOSD.AsString(), out data.y);
  146. else
  147. m_log.WarnFormat(" -- request didn't have destination_y");
  148. if (args.TryGetValue("destination_uuid", out tmpOSD) && tmpOSD != null)
  149. UUID.TryParse(tmpOSD.AsString(), out data.uuid);
  150. if (args.TryGetValue("destination_name", out tmpOSD) && tmpOSD != null)
  151. data.name = tmpOSD.ToString();
  152. if (args.TryGetValue("teleport_flags", out tmpOSD) && tmpOSD != null)
  153. data.flags = tmpOSD.AsUInteger();
  154. }
  155. protected virtual GridRegion ExtractGatekeeper(AgentDestinationData data)
  156. {
  157. return null;
  158. }
  159. // subclasses must override this
  160. protected virtual bool CreateAgent(GridRegion source, GridRegion gatekeeper, GridRegion destination,
  161. AgentCircuitData aCircuit, uint teleportFlags, bool fromLogin, EntityTransferContext ctx, out string reason)
  162. {
  163. reason = "Configuration issues, plz mantis";
  164. return false;
  165. }
  166. }
  167. public class AgentSimpleHandler : SimpleStreamHandler
  168. {
  169. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  170. private ISimulationService m_SimulationService;
  171. protected bool m_Proxy = false;
  172. public AgentSimpleHandler(ISimulationService service) : base("/agent")
  173. {
  174. m_SimulationService = service;
  175. }
  176. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  177. {
  178. httpResponse.KeepAlive = false;
  179. httpResponse.ContentType = "application/json";
  180. if (m_SimulationService == null)
  181. {
  182. httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError;
  183. httpResponse.RawBuffer = Utils.falseStrBytes;
  184. return;
  185. }
  186. if (!Utils.GetParams(httpRequest.UriPath, out UUID agentID, out UUID regionID, out string action))
  187. {
  188. m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", httpRequest.UriPath);
  189. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  190. httpResponse.RawBuffer = Utils.falseStrBytes;
  191. return;
  192. }
  193. switch(httpRequest.HttpMethod)
  194. {
  195. case "QUERYACCESS":
  196. {
  197. if (agentID == UUID.Zero || regionID == UUID.Zero)
  198. {
  199. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  200. httpResponse.RawBuffer = Utils.falseStrBytes;
  201. return;
  202. }
  203. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  204. if (args == null)
  205. {
  206. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  207. httpResponse.RawBuffer = Utils.falseStrBytes;
  208. return;
  209. }
  210. DoQueryAccess(args, httpResponse, agentID, regionID);
  211. break;
  212. }
  213. case "PUT":
  214. {
  215. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  216. if (args == null)
  217. {
  218. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  219. httpResponse.RawBuffer = Utils.falseStrBytes;
  220. return;
  221. }
  222. DoAgentPut(args, httpResponse);
  223. break;
  224. }
  225. case "POST":
  226. {
  227. if (agentID == UUID.Zero)
  228. {
  229. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  230. httpResponse.RawBuffer = Utils.falseStrBytes;
  231. return;
  232. }
  233. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  234. if (args == null)
  235. {
  236. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  237. httpResponse.RawBuffer = Utils.falseStrBytes;
  238. return;
  239. }
  240. DoAgentPost(args, httpRequest, httpResponse, agentID);
  241. break;
  242. }
  243. case "DELETE":
  244. {
  245. if (agentID == UUID.Zero || regionID == UUID.Zero)
  246. {
  247. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  248. httpResponse.RawBuffer = Utils.falseStrBytes;
  249. return;
  250. }
  251. httpRequest.QueryAsDictionary.TryGetValue("auth", out string auth_token);
  252. DoAgentDelete(httpRequest, httpResponse, agentID, action, regionID, auth_token);
  253. break;
  254. }
  255. default:
  256. {
  257. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  258. httpResponse.RawBuffer = Utils.falseStrBytes;
  259. return;
  260. }
  261. }
  262. }
  263. protected virtual void DoQueryAccess(OSDMap args, IOSHttpResponse httpResponse, UUID agentID, UUID regionID)
  264. {
  265. bool viaTeleport = true;
  266. OSD tmpOSD;
  267. if (args.TryGetValue("viaTeleport", out tmpOSD))
  268. viaTeleport = tmpOSD.AsBoolean();
  269. Vector3 position = Vector3.Zero;
  270. if (args.TryGetValue("position", out tmpOSD))
  271. position = Vector3.Parse(tmpOSD.AsString());
  272. string agentHomeURI = null;
  273. if (args.TryGetValue("agent_home_uri", out tmpOSD))
  274. agentHomeURI = tmpOSD.AsString();
  275. // Decode the legacy (string) version and extract the number
  276. float theirVersion = 0f;
  277. if (args.TryGetValue("my_version", out tmpOSD))
  278. {
  279. string theirVersionStr = tmpOSD.AsString();
  280. string[] parts = theirVersionStr.Split(new char[] { '/' });
  281. if (parts.Length > 1)
  282. theirVersion = float.Parse(parts[1], Culture.FormatProvider);
  283. }
  284. EntityTransferContext ctx = new EntityTransferContext();
  285. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  286. ctx.Unpack((OSDMap)tmpOSD);
  287. // Decode the new versioning data
  288. float minVersionRequired = 0f;
  289. float maxVersionRequired = 0f;
  290. float minVersionProvided = 0f;
  291. float maxVersionProvided = 0f;
  292. if (args.TryGetValue("simulation_service_supported_min", out tmpOSD))
  293. minVersionProvided = (float)tmpOSD.AsReal();
  294. if (args.TryGetValue("simulation_service_supported_max", out tmpOSD))
  295. maxVersionProvided = (float)tmpOSD.AsReal();
  296. if (args.TryGetValue("simulation_service_accepted_min", out tmpOSD))
  297. minVersionRequired = (float)tmpOSD.AsReal();
  298. if (args.TryGetValue("simulation_service_accepted_max", out tmpOSD))
  299. maxVersionRequired = (float)tmpOSD.AsReal();
  300. OSDMap resp = new OSDMap(3);
  301. float version = 0f;
  302. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  303. float outboundVersion = 0f;
  304. float inboundVersion = 0f;
  305. if (minVersionProvided == 0f) // string version or older
  306. {
  307. // If there is no version in the packet at all we're looking at 0.6 or
  308. // even more ancient. Refuse it.
  309. if (theirVersion == 0f)
  310. {
  311. resp["success"] = OSD.FromBoolean(false);
  312. resp["reason"] = OSD.FromString("Your region is running a old version of opensim no longer supported. Consider updating it");
  313. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  314. return;
  315. }
  316. version = theirVersion;
  317. if (version < VersionInfo.SimulationServiceVersionAcceptedMin ||
  318. version > VersionInfo.SimulationServiceVersionAcceptedMax)
  319. {
  320. resp["success"] = OSD.FromBoolean(false);
  321. resp["reason"] = OSD.FromString(String.Format("Your region protocol version is {0} and we accept only {1} - {2}. No version overlap.", theirVersion, VersionInfo.SimulationServiceVersionAcceptedMin, VersionInfo.SimulationServiceVersionAcceptedMax));
  322. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  323. return;
  324. }
  325. }
  326. else
  327. {
  328. // Test for no overlap
  329. if (minVersionProvided > VersionInfo.SimulationServiceVersionAcceptedMax ||
  330. maxVersionProvided < VersionInfo.SimulationServiceVersionAcceptedMin)
  331. {
  332. resp["success"] = OSD.FromBoolean(false);
  333. resp["reason"] = OSD.FromString(String.Format("Your region provide protocol versions {0} - {1} and we accept only {2} - {3}. No version overlap.", minVersionProvided, maxVersionProvided, VersionInfo.SimulationServiceVersionAcceptedMin, VersionInfo.SimulationServiceVersionAcceptedMax));
  334. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  335. return;
  336. }
  337. if (minVersionRequired > VersionInfo.SimulationServiceVersionSupportedMax ||
  338. maxVersionRequired < VersionInfo.SimulationServiceVersionSupportedMin)
  339. {
  340. resp["success"] = OSD.FromBoolean(false);
  341. resp["reason"] = OSD.FromString(String.Format("You require region protocol versions {0} - {1} and we provide only {2} - {3}. No version overlap.", minVersionRequired, maxVersionRequired, VersionInfo.SimulationServiceVersionSupportedMin, VersionInfo.SimulationServiceVersionSupportedMax));
  342. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  343. return;
  344. }
  345. // Determine versions to use
  346. // This is intentionally inverted. Inbound and Outbound refer to the direction of the transfer.
  347. // Therefore outbound means from the sender to the receier and inbound means from the receiver to the sender.
  348. // So outbound is what we will accept and inbound is what we will send. Confused yet?
  349. outboundVersion = Math.Min(maxVersionProvided, VersionInfo.SimulationServiceVersionAcceptedMax);
  350. inboundVersion = Math.Min(maxVersionRequired, VersionInfo.SimulationServiceVersionSupportedMax);
  351. }
  352. List<UUID> features = new List<UUID>();
  353. if (args.TryGetValue("features", out tmpOSD) && tmpOSD is OSDArray)
  354. {
  355. OSDArray array = (OSDArray)tmpOSD;
  356. foreach (OSD o in array)
  357. features.Add(new UUID(o.AsString()));
  358. }
  359. GridRegion destination = new GridRegion();
  360. destination.RegionID = regionID;
  361. string reason;
  362. // We're sending the version numbers down to the local connector to do the varregion check.
  363. ctx.InboundVersion = inboundVersion;
  364. ctx.OutboundVersion = outboundVersion;
  365. if (minVersionProvided == 0f)
  366. {
  367. ctx.InboundVersion = version;
  368. ctx.OutboundVersion = version;
  369. }
  370. bool result = m_SimulationService.QueryAccess(destination, agentID, agentHomeURI, viaTeleport, position, features, ctx, out reason);
  371. m_log.DebugFormat("[AGENT HANDLER]: QueryAccess returned {0} ({1}). Version={2}, {3}/{4}",
  372. result, reason, version, inboundVersion, outboundVersion);
  373. resp["success"] = OSD.FromBoolean(result);
  374. resp["reason"] = OSD.FromString(reason);
  375. string legacyVersion = String.Format(Culture.FormatProvider, "SIMULATION/{0}", version);
  376. resp["version"] = OSD.FromString(legacyVersion);
  377. resp["negotiated_inbound_version"] = OSD.FromReal(inboundVersion);
  378. resp["negotiated_outbound_version"] = OSD.FromReal(outboundVersion);
  379. OSDArray featuresWanted = new OSDArray();
  380. foreach (UUID feature in features)
  381. featuresWanted.Add(OSD.FromString(feature.ToString()));
  382. resp["features"] = featuresWanted;
  383. if(result)
  384. httpResponse.KeepAlive = true;
  385. // We must preserve defaults here, otherwise a false "success" will not be put into the JSON map!
  386. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  387. // console.WriteLine("str_response_string [{0}]", responsedata["str_response_string"]);
  388. }
  389. protected void DoAgentDelete(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID agentID, string action, UUID regionID, string auth_token)
  390. {
  391. if (string.IsNullOrEmpty(action))
  392. m_log.DebugFormat("[AGENT HANDLER]: >>> DELETE <<< RegionID: {0}; from: {1}; auth_code: {2}",
  393. regionID, httpRequest.RemoteIPEndPoint.Address.ToString(), auth_token);
  394. else
  395. m_log.DebugFormat("[AGENT HANDLER]: Release {0} to RegionID: {1}", agentID, regionID);
  396. if (action.Equals("release"))
  397. m_SimulationService.ReleaseAgent(regionID, agentID, "");
  398. else
  399. {
  400. GridRegion destination = new GridRegion();
  401. destination.RegionID = regionID;
  402. Util.FireAndForget(
  403. o => m_SimulationService.CloseAgent(destination, agentID, auth_token), null, "AgentHandler.DoAgentDelete");
  404. }
  405. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  406. httpResponse.RawBuffer = Util.UTF8.GetBytes("OpenSim agent " + agentID.ToString());
  407. //m_log.DebugFormat("[AGENT HANDLER]: Agent {0} Released/Deleted from region {1}", id, regionID);
  408. }
  409. protected void DoAgentPost(OSDMap args, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID agentID)
  410. {
  411. OSD tmpOSD;
  412. EntityTransferContext ctx = new EntityTransferContext();
  413. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  414. ctx.Unpack((OSDMap)tmpOSD);
  415. AgentDestinationData data = CreateAgentDestinationData();
  416. UnpackData(args, data);
  417. GridRegion destination = new GridRegion();
  418. destination.RegionID = data.uuid;
  419. destination.RegionLocX = data.x;
  420. destination.RegionLocY = data.y;
  421. destination.RegionName = data.name;
  422. GridRegion gatekeeper = ExtractGatekeeper(data);
  423. AgentCircuitData aCircuit = new AgentCircuitData();
  424. try
  425. {
  426. aCircuit.UnpackAgentCircuitData(args);
  427. }
  428. catch (Exception ex)
  429. {
  430. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
  431. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  432. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  433. return;
  434. }
  435. GridRegion source = null;
  436. if (args.TryGetValue("source_uuid", out tmpOSD))
  437. {
  438. source = new GridRegion();
  439. source.RegionID = UUID.Parse(tmpOSD.AsString());
  440. source.RegionLocX = Int32.Parse(args["source_x"].AsString());
  441. source.RegionLocY = Int32.Parse(args["source_y"].AsString());
  442. source.RegionName = args["source_name"].AsString();
  443. if (args.TryGetValue("source_server_uri", out tmpOSD))
  444. source.RawServerURI = tmpOSD.AsString();
  445. else
  446. source.RawServerURI = null;
  447. }
  448. OSDMap resp = new OSDMap(2);
  449. string reason = string.Empty;
  450. bool result = CreateAgent(source, gatekeeper, destination, aCircuit, data.flags, data.fromLogin, ctx, out reason);
  451. resp["reason"] = OSD.FromString(reason);
  452. resp["success"] = OSD.FromBoolean(result);
  453. // Let's also send out the IP address of the caller back to the caller (HG 1.5)
  454. resp["your_ip"] = OSD.FromString(httpRequest.RemoteIPEndPoint.Address.ToString());
  455. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  456. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp));
  457. }
  458. protected virtual AgentDestinationData CreateAgentDestinationData()
  459. {
  460. return new AgentDestinationData();
  461. }
  462. protected virtual void UnpackData(OSDMap args, AgentDestinationData data)
  463. {
  464. OSD tmpOSD;
  465. // retrieve the input arguments
  466. if (args.TryGetValue("destination_x", out tmpOSD) && tmpOSD != null)
  467. Int32.TryParse(tmpOSD.AsString(), out data.x);
  468. else
  469. m_log.WarnFormat(" -- request didn't have destination_x");
  470. if (args.TryGetValue("destination_y", out tmpOSD) && tmpOSD != null)
  471. Int32.TryParse(tmpOSD.AsString(), out data.y);
  472. else
  473. m_log.WarnFormat(" -- request didn't have destination_y");
  474. if (args.TryGetValue("destination_uuid", out tmpOSD) && tmpOSD != null)
  475. UUID.TryParse(tmpOSD.AsString(), out data.uuid);
  476. if (args.TryGetValue("destination_name", out tmpOSD) && tmpOSD != null)
  477. data.name = tmpOSD.ToString();
  478. if (args.TryGetValue("teleport_flags", out tmpOSD) && tmpOSD != null)
  479. data.flags = tmpOSD.AsUInteger();
  480. }
  481. protected virtual GridRegion ExtractGatekeeper(AgentDestinationData data)
  482. {
  483. return null;
  484. }
  485. // subclasses can override this
  486. protected virtual bool CreateAgent(GridRegion source, GridRegion gatekeeper, GridRegion destination,
  487. AgentCircuitData aCircuit, uint teleportFlags, bool fromLogin, EntityTransferContext ctx, out string reason)
  488. {
  489. reason = string.Empty;
  490. bool ret = m_SimulationService.CreateAgent(source, destination, aCircuit, teleportFlags, ctx, out reason);
  491. // m_log.DebugFormat("[AGENT HANDLER]: SYNC CreateAgent {0} {1}", ret.ToString(), reason);
  492. return ret;
  493. }
  494. protected void DoAgentPut(OSDMap args, IOSHttpResponse httpResponse)
  495. {
  496. // retrieve the input arguments
  497. OSD tmpOSD;
  498. EntityTransferContext ctx = new EntityTransferContext();
  499. int x = 0, y = 0;
  500. UUID uuid = UUID.Zero;
  501. string regionname = string.Empty;
  502. if (args.TryGetValue("destination_x", out tmpOSD) && tmpOSD != null)
  503. Int32.TryParse(tmpOSD.AsString(), out x);
  504. if (args.TryGetValue("destination_y", out tmpOSD) && tmpOSD != null)
  505. Int32.TryParse(tmpOSD.AsString(), out y);
  506. if (args.TryGetValue("destination_uuid", out tmpOSD) && tmpOSD != null)
  507. UUID.TryParse(tmpOSD.AsString(), out uuid);
  508. if (args.TryGetValue("destination_name", out tmpOSD) && tmpOSD != null)
  509. regionname = tmpOSD.ToString();
  510. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  511. ctx.Unpack((OSDMap)tmpOSD);
  512. GridRegion destination = new GridRegion();
  513. destination.RegionID = uuid;
  514. destination.RegionLocX = x;
  515. destination.RegionLocY = y;
  516. destination.RegionName = regionname;
  517. string messageType;
  518. if (args["message_type"] != null)
  519. messageType = args["message_type"].AsString();
  520. else
  521. {
  522. m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
  523. messageType = "AgentData";
  524. }
  525. bool result = true;
  526. if ("AgentData".Equals(messageType))
  527. {
  528. AgentData agent = new AgentData();
  529. try
  530. {
  531. agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
  532. }
  533. catch (Exception ex)
  534. {
  535. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  536. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  537. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  538. return;
  539. }
  540. //agent.Dump();
  541. // This is one of the meanings of PUT agent
  542. result = UpdateAgent(destination, agent);
  543. }
  544. else if ("AgentPosition".Equals(messageType))
  545. {
  546. AgentPosition agent = new AgentPosition();
  547. try
  548. {
  549. agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
  550. }
  551. catch (Exception ex)
  552. {
  553. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  554. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  555. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  556. return;
  557. }
  558. //agent.Dump();
  559. // This is one of the meanings of PUT agent
  560. result = m_SimulationService.UpdateAgent(destination, agent);
  561. }
  562. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  563. httpResponse.RawBuffer = Util.UTF8.GetBytes(result.ToString());
  564. //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
  565. }
  566. // subclasses can override this
  567. protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
  568. {
  569. // The data and protocols are already defined so this is just a dummy to satisfy the interface
  570. // TODO: make this end-to-end
  571. return m_SimulationService.UpdateAgent(destination, agent, new EntityTransferContext());
  572. }
  573. }
  574. public class AgentDestinationData
  575. {
  576. public int x;
  577. public int y;
  578. public string name;
  579. public UUID uuid;
  580. public uint flags;
  581. public bool fromLogin;
  582. }
  583. }