AgentHandlers.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using System.Text;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Server.Handlers.Base;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. using Nini.Config;
  42. using log4net;
  43. namespace OpenSim.Server.Handlers.Simulation
  44. {
  45. public class AgentHandler
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private ISimulationService m_SimulationService;
  49. public AgentHandler() { }
  50. public AgentHandler(ISimulationService sim)
  51. {
  52. m_SimulationService = sim;
  53. }
  54. public Hashtable Handler(Hashtable request)
  55. {
  56. // m_log.Debug("[CONNECTION DEBUGGING]: AgentHandler Called");
  57. //
  58. // m_log.Debug("---------------------------");
  59. // m_log.Debug(" >> uri=" + request["uri"]);
  60. // m_log.Debug(" >> content-type=" + request["content-type"]);
  61. // m_log.Debug(" >> http-method=" + request["http-method"]);
  62. // m_log.Debug("---------------------------\n");
  63. Hashtable responsedata = new Hashtable();
  64. responsedata["content_type"] = "text/html";
  65. responsedata["keepalive"] = false;
  66. UUID agentID;
  67. UUID regionID;
  68. string action;
  69. if (!Utils.GetParams((string)request["uri"], out agentID, out regionID, out action))
  70. {
  71. m_log.InfoFormat("[AGENT HANDLER]: Invalid parameters for agent message {0}", request["uri"]);
  72. responsedata["int_response_code"] = 404;
  73. responsedata["str_response_string"] = "false";
  74. return responsedata;
  75. }
  76. // Next, let's parse the verb
  77. string method = (string)request["http-method"];
  78. if (method.Equals("PUT"))
  79. {
  80. DoAgentPut(request, responsedata);
  81. return responsedata;
  82. }
  83. else if (method.Equals("POST"))
  84. {
  85. DoAgentPost(request, responsedata, agentID);
  86. return responsedata;
  87. }
  88. else if (method.Equals("GET"))
  89. {
  90. DoAgentGet(request, responsedata, agentID, regionID);
  91. return responsedata;
  92. }
  93. else if (method.Equals("DELETE"))
  94. {
  95. DoAgentDelete(request, responsedata, agentID, action, regionID);
  96. return responsedata;
  97. }
  98. else
  99. {
  100. m_log.InfoFormat("[AGENT HANDLER]: method {0} not supported in agent message", method);
  101. responsedata["int_response_code"] = HttpStatusCode.MethodNotAllowed;
  102. responsedata["str_response_string"] = "Method not allowed";
  103. return responsedata;
  104. }
  105. }
  106. protected void DoAgentPost(Hashtable request, Hashtable responsedata, UUID id)
  107. {
  108. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  109. if (args == null)
  110. {
  111. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  112. responsedata["str_response_string"] = "Bad request";
  113. return;
  114. }
  115. // retrieve the input arguments
  116. int x = 0, y = 0;
  117. UUID uuid = UUID.Zero;
  118. string regionname = string.Empty;
  119. uint teleportFlags = 0;
  120. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  121. Int32.TryParse(args["destination_x"].AsString(), out x);
  122. else
  123. m_log.WarnFormat(" -- request didn't have destination_x");
  124. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  125. Int32.TryParse(args["destination_y"].AsString(), out y);
  126. else
  127. m_log.WarnFormat(" -- request didn't have destination_y");
  128. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  129. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  130. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  131. regionname = args["destination_name"].ToString();
  132. if (args.ContainsKey("teleport_flags") && args["teleport_flags"] != null)
  133. teleportFlags = args["teleport_flags"].AsUInteger();
  134. GridRegion destination = new GridRegion();
  135. destination.RegionID = uuid;
  136. destination.RegionLocX = x;
  137. destination.RegionLocY = y;
  138. destination.RegionName = regionname;
  139. AgentCircuitData aCircuit = new AgentCircuitData();
  140. try
  141. {
  142. aCircuit.UnpackAgentCircuitData(args);
  143. }
  144. catch (Exception ex)
  145. {
  146. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildCreate message {0}", ex.Message);
  147. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  148. responsedata["str_response_string"] = "Bad request";
  149. return;
  150. }
  151. OSDMap resp = new OSDMap(2);
  152. string reason = String.Empty;
  153. // This is the meaning of POST agent
  154. //m_regionClient.AdjustUserInformation(aCircuit);
  155. //bool result = m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  156. bool result = CreateAgent(destination, aCircuit, teleportFlags, out reason);
  157. resp["reason"] = OSD.FromString(reason);
  158. resp["success"] = OSD.FromBoolean(result);
  159. // Let's also send out the IP address of the caller back to the caller (HG 1.5)
  160. resp["your_ip"] = OSD.FromString(Util.GetCallerIP(request));
  161. // TODO: add reason if not String.Empty?
  162. responsedata["int_response_code"] = HttpStatusCode.OK;
  163. responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
  164. }
  165. // subclasses can override this
  166. protected virtual bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, out string reason)
  167. {
  168. return m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  169. }
  170. protected void DoAgentPut(Hashtable request, Hashtable responsedata)
  171. {
  172. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  173. if (args == null)
  174. {
  175. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  176. responsedata["str_response_string"] = "Bad request";
  177. return;
  178. }
  179. // retrieve the input arguments
  180. int x = 0, y = 0;
  181. UUID uuid = UUID.Zero;
  182. string regionname = string.Empty;
  183. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  184. Int32.TryParse(args["destination_x"].AsString(), out x);
  185. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  186. Int32.TryParse(args["destination_y"].AsString(), out y);
  187. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  188. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  189. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  190. regionname = args["destination_name"].ToString();
  191. GridRegion destination = new GridRegion();
  192. destination.RegionID = uuid;
  193. destination.RegionLocX = x;
  194. destination.RegionLocY = y;
  195. destination.RegionName = regionname;
  196. string messageType;
  197. if (args["message_type"] != null)
  198. messageType = args["message_type"].AsString();
  199. else
  200. {
  201. m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
  202. messageType = "AgentData";
  203. }
  204. bool result = true;
  205. if ("AgentData".Equals(messageType))
  206. {
  207. AgentData agent = new AgentData();
  208. try
  209. {
  210. agent.Unpack(args);
  211. }
  212. catch (Exception ex)
  213. {
  214. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  215. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  216. responsedata["str_response_string"] = "Bad request";
  217. return;
  218. }
  219. //agent.Dump();
  220. // This is one of the meanings of PUT agent
  221. result = UpdateAgent(destination, agent);
  222. }
  223. else if ("AgentPosition".Equals(messageType))
  224. {
  225. AgentPosition agent = new AgentPosition();
  226. try
  227. {
  228. agent.Unpack(args);
  229. }
  230. catch (Exception ex)
  231. {
  232. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  233. return;
  234. }
  235. //agent.Dump();
  236. // This is one of the meanings of PUT agent
  237. result = m_SimulationService.UpdateAgent(destination, agent);
  238. }
  239. responsedata["int_response_code"] = HttpStatusCode.OK;
  240. responsedata["str_response_string"] = result.ToString();
  241. //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
  242. }
  243. // subclasses can override this
  244. protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
  245. {
  246. return m_SimulationService.UpdateAgent(destination, agent);
  247. }
  248. protected virtual void DoAgentGet(Hashtable request, Hashtable responsedata, UUID id, UUID regionID)
  249. {
  250. if (m_SimulationService == null)
  251. {
  252. m_log.Debug("[AGENT HANDLER]: Agent GET called. Harmless but useless.");
  253. responsedata["content_type"] = "application/json";
  254. responsedata["int_response_code"] = HttpStatusCode.NotImplemented;
  255. responsedata["str_response_string"] = string.Empty;
  256. return;
  257. }
  258. GridRegion destination = new GridRegion();
  259. destination.RegionID = regionID;
  260. IAgentData agent = null;
  261. bool result = m_SimulationService.RetrieveAgent(destination, id, out agent);
  262. OSDMap map = null;
  263. if (result)
  264. {
  265. if (agent != null) // just to make sure
  266. {
  267. map = agent.Pack();
  268. string strBuffer = "";
  269. try
  270. {
  271. strBuffer = OSDParser.SerializeJsonString(map);
  272. }
  273. catch (Exception e)
  274. {
  275. m_log.WarnFormat("[AGENT HANDLER]: Exception thrown on serialization of DoAgentGet: {0}", e.Message);
  276. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  277. // ignore. buffer will be empty, caller should check.
  278. }
  279. responsedata["content_type"] = "application/json";
  280. responsedata["int_response_code"] = HttpStatusCode.OK;
  281. responsedata["str_response_string"] = strBuffer;
  282. }
  283. else
  284. {
  285. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  286. responsedata["str_response_string"] = "Internal error";
  287. }
  288. }
  289. else
  290. {
  291. responsedata["int_response_code"] = HttpStatusCode.NotFound;
  292. responsedata["str_response_string"] = "Not Found";
  293. }
  294. }
  295. protected void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, UUID regionID)
  296. {
  297. m_log.Debug(" >>> DoDelete action:" + action + "; RegionID:" + regionID);
  298. GridRegion destination = new GridRegion();
  299. destination.RegionID = regionID;
  300. if (action.Equals("release"))
  301. ReleaseAgent(regionID, id);
  302. else
  303. m_SimulationService.CloseAgent(destination, id);
  304. responsedata["int_response_code"] = HttpStatusCode.OK;
  305. responsedata["str_response_string"] = "OpenSim agent " + id.ToString();
  306. m_log.Debug("[AGENT HANDLER]: Agent Released/Deleted.");
  307. }
  308. protected virtual void ReleaseAgent(UUID regionID, UUID id)
  309. {
  310. m_SimulationService.ReleaseAgent(regionID, id, "");
  311. }
  312. }
  313. }