AgentHandlers.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. m_log.Debug("[AGENT HANDLER]: ProcessRequest called with null Simulation Service");
  183. httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError;
  184. httpResponse.RawBuffer = Utils.falseStrBytes;
  185. return;
  186. }
  187. if (!Utils.GetParams(httpRequest.UriPath, out UUID agentID, out UUID regionID, out string action))
  188. {
  189. m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", httpRequest.UriPath);
  190. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  191. httpResponse.RawBuffer = Utils.falseStrBytes;
  192. return;
  193. }
  194. switch(httpRequest.HttpMethod)
  195. {
  196. case "QUERYACCESS":
  197. {
  198. if (agentID == UUID.Zero || regionID == UUID.Zero)
  199. {
  200. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  201. httpResponse.RawBuffer = Utils.falseStrBytes;
  202. return;
  203. }
  204. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  205. if (args == null)
  206. {
  207. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  208. httpResponse.RawBuffer = Utils.falseStrBytes;
  209. return;
  210. }
  211. DoQueryAccess(args, httpResponse, agentID, regionID);
  212. break;
  213. }
  214. case "PUT":
  215. {
  216. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  217. if (args == null)
  218. {
  219. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  220. httpResponse.RawBuffer = Utils.falseStrBytes;
  221. return;
  222. }
  223. DoAgentPut(args, httpResponse);
  224. break;
  225. }
  226. case "POST":
  227. {
  228. if (agentID == UUID.Zero)
  229. {
  230. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  231. httpResponse.RawBuffer = Utils.falseStrBytes;
  232. return;
  233. }
  234. OSDMap args = Utils.DeserializeJSONOSMap(httpRequest);
  235. if (args == null)
  236. {
  237. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  238. httpResponse.RawBuffer = Utils.falseStrBytes;
  239. return;
  240. }
  241. DoAgentPost(args, httpRequest, httpResponse, agentID);
  242. break;
  243. }
  244. case "DELETE":
  245. {
  246. if (agentID == UUID.Zero || regionID == UUID.Zero)
  247. {
  248. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  249. httpResponse.RawBuffer = Utils.falseStrBytes;
  250. return;
  251. }
  252. httpRequest.QueryAsDictionary.TryGetValue("auth", out string auth_token);
  253. DoAgentDelete(httpRequest, httpResponse, agentID, action, regionID, auth_token);
  254. break;
  255. }
  256. default:
  257. {
  258. httpResponse.StatusCode = (int)HttpStatusCode.MethodNotAllowed;
  259. httpResponse.RawBuffer = Utils.falseStrBytes;
  260. return;
  261. }
  262. }
  263. }
  264. protected virtual void DoQueryAccess(OSDMap args, IOSHttpResponse httpResponse, UUID agentID, UUID regionID)
  265. {
  266. bool viaTeleport = true;
  267. OSD tmpOSD;
  268. if (args.TryGetValue("viaTeleport", out tmpOSD))
  269. viaTeleport = tmpOSD.AsBoolean();
  270. Vector3 position = Vector3.Zero;
  271. if (args.TryGetValue("position", out tmpOSD))
  272. position = Vector3.Parse(tmpOSD.AsString());
  273. string agentHomeURI = null;
  274. if (args.TryGetValue("agent_home_uri", out tmpOSD))
  275. agentHomeURI = tmpOSD.AsString();
  276. // Decode the legacy (string) version and extract the number
  277. float theirVersion = 0f;
  278. if (args.TryGetValue("my_version", out tmpOSD))
  279. {
  280. string theirVersionStr = tmpOSD.AsString();
  281. string[] parts = theirVersionStr.Split(new char[] { '/' });
  282. if (parts.Length > 1)
  283. theirVersion = float.Parse(parts[1], Culture.FormatProvider);
  284. }
  285. EntityTransferContext ctx = new EntityTransferContext();
  286. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  287. ctx.Unpack((OSDMap)tmpOSD);
  288. // Decode the new versioning data
  289. float minVersionRequired = 0f;
  290. float maxVersionRequired = 0f;
  291. float minVersionProvided = 0f;
  292. float maxVersionProvided = 0f;
  293. if (args.TryGetValue("simulation_service_supported_min", out tmpOSD))
  294. minVersionProvided = (float)tmpOSD.AsReal();
  295. if (args.TryGetValue("simulation_service_supported_max", out tmpOSD))
  296. maxVersionProvided = (float)tmpOSD.AsReal();
  297. if (args.TryGetValue("simulation_service_accepted_min", out tmpOSD))
  298. minVersionRequired = (float)tmpOSD.AsReal();
  299. if (args.TryGetValue("simulation_service_accepted_max", out tmpOSD))
  300. maxVersionRequired = (float)tmpOSD.AsReal();
  301. OSDMap resp = new OSDMap(3);
  302. float version = 0f;
  303. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  304. float outboundVersion = 0f;
  305. float inboundVersion = 0f;
  306. if (minVersionProvided == 0f) // string version or older
  307. {
  308. // If there is no version in the packet at all we're looking at 0.6 or
  309. // even more ancient. Refuse it.
  310. if (theirVersion == 0f)
  311. {
  312. resp["success"] = OSD.FromBoolean(false);
  313. resp["reason"] = OSD.FromString("Your region is running a old version of opensim no longer supported. Consider updating it");
  314. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  315. return;
  316. }
  317. version = theirVersion;
  318. if (version < VersionInfo.SimulationServiceVersionAcceptedMin ||
  319. version > VersionInfo.SimulationServiceVersionAcceptedMax)
  320. {
  321. resp["success"] = OSD.FromBoolean(false);
  322. 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));
  323. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  324. return;
  325. }
  326. }
  327. else
  328. {
  329. // Test for no overlap
  330. if (minVersionProvided > VersionInfo.SimulationServiceVersionAcceptedMax ||
  331. maxVersionProvided < VersionInfo.SimulationServiceVersionAcceptedMin)
  332. {
  333. resp["success"] = OSD.FromBoolean(false);
  334. 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));
  335. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  336. return;
  337. }
  338. if (minVersionRequired > VersionInfo.SimulationServiceVersionSupportedMax ||
  339. maxVersionRequired < VersionInfo.SimulationServiceVersionSupportedMin)
  340. {
  341. resp["success"] = OSD.FromBoolean(false);
  342. 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));
  343. httpResponse.RawBuffer = Util.UTF8.GetBytes(OSDParser.SerializeJsonString(resp, true));
  344. return;
  345. }
  346. // Determine versions to use
  347. // This is intentionally inverted. Inbound and Outbound refer to the direction of the transfer.
  348. // Therefore outbound means from the sender to the receier and inbound means from the receiver to the sender.
  349. // So outbound is what we will accept and inbound is what we will send. Confused yet?
  350. outboundVersion = Math.Min(maxVersionProvided, VersionInfo.SimulationServiceVersionAcceptedMax);
  351. inboundVersion = Math.Min(maxVersionRequired, VersionInfo.SimulationServiceVersionSupportedMax);
  352. }
  353. List<UUID> features = new List<UUID>();
  354. if (args.TryGetValue("features", out tmpOSD) && tmpOSD is OSDArray)
  355. {
  356. OSDArray array = (OSDArray)tmpOSD;
  357. foreach (OSD o in array)
  358. features.Add(new UUID(o.AsString()));
  359. }
  360. GridRegion destination = new GridRegion();
  361. destination.RegionID = regionID;
  362. string reason;
  363. // We're sending the version numbers down to the local connector to do the varregion check.
  364. ctx.InboundVersion = inboundVersion;
  365. ctx.OutboundVersion = outboundVersion;
  366. if (minVersionProvided == 0f)
  367. {
  368. ctx.InboundVersion = version;
  369. ctx.OutboundVersion = version;
  370. }
  371. bool result = m_SimulationService.QueryAccess(destination, agentID, agentHomeURI, viaTeleport, position, features, ctx, out reason);
  372. m_log.DebugFormat("[AGENT HANDLER]: QueryAccess returned {0} ({1}). Version={2}, {3}/{4}",
  373. result, reason, version, inboundVersion, outboundVersion);
  374. resp["success"] = OSD.FromBoolean(result);
  375. resp["reason"] = OSD.FromString(reason);
  376. string legacyVersion = String.Format(Culture.FormatProvider, "SIMULATION/{0}", version);
  377. resp["version"] = OSD.FromString(legacyVersion);
  378. resp["negotiated_inbound_version"] = OSD.FromReal(inboundVersion);
  379. resp["negotiated_outbound_version"] = OSD.FromReal(outboundVersion);
  380. OSDArray featuresWanted = new OSDArray();
  381. foreach (UUID feature in features)
  382. featuresWanted.Add(OSD.FromString(feature.ToString()));
  383. resp["features"] = featuresWanted;
  384. httpResponse.KeepAlive = result;
  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, 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. httpResponse.KeepAlive = (data.flags & (uint)(TeleportFlags.ViaLogin | TeleportFlags.ViaHGLogin)) != (uint)TeleportFlags.ViaLogin;
  458. }
  459. protected virtual AgentDestinationData CreateAgentDestinationData()
  460. {
  461. return new AgentDestinationData();
  462. }
  463. protected virtual void UnpackData(OSDMap args, AgentDestinationData data)
  464. {
  465. OSD tmpOSD;
  466. // retrieve the input arguments
  467. if (args.TryGetValue("destination_x", out tmpOSD) && tmpOSD != null)
  468. Int32.TryParse(tmpOSD.AsString(), out data.x);
  469. else
  470. m_log.WarnFormat(" -- request didn't have destination_x");
  471. if (args.TryGetValue("destination_y", out tmpOSD) && tmpOSD != null)
  472. Int32.TryParse(tmpOSD.AsString(), out data.y);
  473. else
  474. m_log.WarnFormat(" -- request didn't have destination_y");
  475. if (args.TryGetValue("destination_uuid", out tmpOSD) && tmpOSD != null)
  476. UUID.TryParse(tmpOSD.AsString(), out data.uuid);
  477. if (args.TryGetValue("destination_name", out tmpOSD) && tmpOSD != null)
  478. data.name = tmpOSD.ToString();
  479. if (args.TryGetValue("teleport_flags", out tmpOSD) && tmpOSD != null)
  480. data.flags = tmpOSD.AsUInteger();
  481. }
  482. protected virtual GridRegion ExtractGatekeeper(AgentDestinationData data)
  483. {
  484. return null;
  485. }
  486. // subclasses can override this
  487. protected virtual bool CreateAgent(GridRegion source, GridRegion gatekeeper, GridRegion destination,
  488. AgentCircuitData aCircuit, uint teleportFlags, EntityTransferContext ctx, out string reason)
  489. {
  490. reason = string.Empty;
  491. bool ret = m_SimulationService.CreateAgent(source, destination, aCircuit, teleportFlags, ctx, out reason);
  492. // m_log.DebugFormat("[AGENT HANDLER]: SYNC CreateAgent {0} {1}", ret.ToString(), reason);
  493. return ret;
  494. }
  495. protected void DoAgentPut(OSDMap args, IOSHttpResponse httpResponse)
  496. {
  497. // retrieve the input arguments
  498. OSD tmpOSD;
  499. EntityTransferContext ctx = new EntityTransferContext();
  500. int x = 0, y = 0;
  501. UUID uuid = UUID.Zero;
  502. string regionname = string.Empty;
  503. if (args.TryGetValue("destination_x", out tmpOSD) && tmpOSD != null)
  504. Int32.TryParse(tmpOSD.AsString(), out x);
  505. if (args.TryGetValue("destination_y", out tmpOSD) && tmpOSD != null)
  506. Int32.TryParse(tmpOSD.AsString(), out y);
  507. if (args.TryGetValue("destination_uuid", out tmpOSD) && tmpOSD != null)
  508. UUID.TryParse(tmpOSD.AsString(), out uuid);
  509. if (args.TryGetValue("destination_name", out tmpOSD) && tmpOSD != null)
  510. regionname = tmpOSD.ToString();
  511. if (args.TryGetValue("context", out tmpOSD) && tmpOSD is OSDMap)
  512. ctx.Unpack((OSDMap)tmpOSD);
  513. GridRegion destination = new GridRegion();
  514. destination.RegionID = uuid;
  515. destination.RegionLocX = x;
  516. destination.RegionLocY = y;
  517. destination.RegionName = regionname;
  518. string messageType;
  519. if (args["message_type"] != null)
  520. messageType = args["message_type"].AsString();
  521. else
  522. {
  523. m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
  524. messageType = "AgentData";
  525. }
  526. bool result = true;
  527. if ("AgentData".Equals(messageType))
  528. {
  529. AgentData agent = new AgentData();
  530. try
  531. {
  532. agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
  533. }
  534. catch (Exception ex)
  535. {
  536. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  537. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  538. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  539. return;
  540. }
  541. //agent.Dump();
  542. // This is one of the meanings of PUT agent
  543. result = UpdateAgent(destination, agent);
  544. }
  545. else if ("AgentPosition".Equals(messageType))
  546. {
  547. AgentPosition agent = new AgentPosition();
  548. try
  549. {
  550. agent.Unpack(args, m_SimulationService.GetScene(destination.RegionID), ctx);
  551. }
  552. catch (Exception ex)
  553. {
  554. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  555. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  556. httpResponse.RawBuffer = Util.UTF8.GetBytes("false");
  557. return;
  558. }
  559. //agent.Dump();
  560. // This is one of the meanings of PUT agent
  561. result = m_SimulationService.UpdateAgent(destination, agent);
  562. }
  563. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  564. httpResponse.RawBuffer = Util.UTF8.GetBytes(result.ToString());
  565. //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
  566. }
  567. // subclasses can override this
  568. protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
  569. {
  570. // The data and protocols are already defined so this is just a dummy to satisfy the interface
  571. // TODO: make this end-to-end
  572. return m_SimulationService.UpdateAgent(destination, agent, new EntityTransferContext());
  573. }
  574. }
  575. public class AgentDestinationData
  576. {
  577. public int x;
  578. public int y;
  579. public string name;
  580. public UUID uuid;
  581. public uint flags;
  582. public bool fromLogin;
  583. }
  584. }