AgentHandlers.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. // TODO: add reason if not String.Empty?
  160. responsedata["int_response_code"] = HttpStatusCode.OK;
  161. responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp);
  162. }
  163. // subclasses can override this
  164. protected virtual bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, out string reason)
  165. {
  166. return m_SimulationService.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  167. }
  168. protected void DoAgentPut(Hashtable request, Hashtable responsedata)
  169. {
  170. OSDMap args = Utils.GetOSDMap((string)request["body"]);
  171. if (args == null)
  172. {
  173. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  174. responsedata["str_response_string"] = "Bad request";
  175. return;
  176. }
  177. // retrieve the input arguments
  178. int x = 0, y = 0;
  179. UUID uuid = UUID.Zero;
  180. string regionname = string.Empty;
  181. if (args.ContainsKey("destination_x") && args["destination_x"] != null)
  182. Int32.TryParse(args["destination_x"].AsString(), out x);
  183. if (args.ContainsKey("destination_y") && args["destination_y"] != null)
  184. Int32.TryParse(args["destination_y"].AsString(), out y);
  185. if (args.ContainsKey("destination_uuid") && args["destination_uuid"] != null)
  186. UUID.TryParse(args["destination_uuid"].AsString(), out uuid);
  187. if (args.ContainsKey("destination_name") && args["destination_name"] != null)
  188. regionname = args["destination_name"].ToString();
  189. GridRegion destination = new GridRegion();
  190. destination.RegionID = uuid;
  191. destination.RegionLocX = x;
  192. destination.RegionLocY = y;
  193. destination.RegionName = regionname;
  194. string messageType;
  195. if (args["message_type"] != null)
  196. messageType = args["message_type"].AsString();
  197. else
  198. {
  199. m_log.Warn("[AGENT HANDLER]: Agent Put Message Type not found. ");
  200. messageType = "AgentData";
  201. }
  202. bool result = true;
  203. if ("AgentData".Equals(messageType))
  204. {
  205. AgentData agent = new AgentData();
  206. try
  207. {
  208. agent.Unpack(args);
  209. }
  210. catch (Exception ex)
  211. {
  212. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  213. responsedata["int_response_code"] = HttpStatusCode.BadRequest;
  214. responsedata["str_response_string"] = "Bad request";
  215. return;
  216. }
  217. //agent.Dump();
  218. // This is one of the meanings of PUT agent
  219. result = UpdateAgent(destination, agent);
  220. }
  221. else if ("AgentPosition".Equals(messageType))
  222. {
  223. AgentPosition agent = new AgentPosition();
  224. try
  225. {
  226. agent.Unpack(args);
  227. }
  228. catch (Exception ex)
  229. {
  230. m_log.InfoFormat("[AGENT HANDLER]: exception on unpacking ChildAgentUpdate message {0}", ex.Message);
  231. return;
  232. }
  233. //agent.Dump();
  234. // This is one of the meanings of PUT agent
  235. result = m_SimulationService.UpdateAgent(destination, agent);
  236. }
  237. responsedata["int_response_code"] = HttpStatusCode.OK;
  238. responsedata["str_response_string"] = result.ToString();
  239. //responsedata["str_response_string"] = OSDParser.SerializeJsonString(resp); ??? instead
  240. }
  241. // subclasses can override this
  242. protected virtual bool UpdateAgent(GridRegion destination, AgentData agent)
  243. {
  244. return m_SimulationService.UpdateAgent(destination, agent);
  245. }
  246. protected virtual void DoAgentGet(Hashtable request, Hashtable responsedata, UUID id, UUID regionID)
  247. {
  248. if (m_SimulationService == null)
  249. {
  250. m_log.Debug("[AGENT HANDLER]: Agent GET called. Harmless but useless.");
  251. responsedata["content_type"] = "application/json";
  252. responsedata["int_response_code"] = HttpStatusCode.NotImplemented;
  253. responsedata["str_response_string"] = string.Empty;
  254. return;
  255. }
  256. GridRegion destination = new GridRegion();
  257. destination.RegionID = regionID;
  258. IAgentData agent = null;
  259. bool result = m_SimulationService.RetrieveAgent(destination, id, out agent);
  260. OSDMap map = null;
  261. if (result)
  262. {
  263. if (agent != null) // just to make sure
  264. {
  265. map = agent.Pack();
  266. string strBuffer = "";
  267. try
  268. {
  269. strBuffer = OSDParser.SerializeJsonString(map);
  270. }
  271. catch (Exception e)
  272. {
  273. m_log.WarnFormat("[AGENT HANDLER]: Exception thrown on serialization of DoAgentGet: {0}", e.Message);
  274. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  275. // ignore. buffer will be empty, caller should check.
  276. }
  277. responsedata["content_type"] = "application/json";
  278. responsedata["int_response_code"] = HttpStatusCode.OK;
  279. responsedata["str_response_string"] = strBuffer;
  280. }
  281. else
  282. {
  283. responsedata["int_response_code"] = HttpStatusCode.InternalServerError;
  284. responsedata["str_response_string"] = "Internal error";
  285. }
  286. }
  287. else
  288. {
  289. responsedata["int_response_code"] = HttpStatusCode.NotFound;
  290. responsedata["str_response_string"] = "Not Found";
  291. }
  292. }
  293. protected void DoAgentDelete(Hashtable request, Hashtable responsedata, UUID id, string action, UUID regionID)
  294. {
  295. m_log.Debug(" >>> DoDelete action:" + action + "; RegionID:" + regionID);
  296. GridRegion destination = new GridRegion();
  297. destination.RegionID = regionID;
  298. if (action.Equals("release"))
  299. ReleaseAgent(regionID, id);
  300. else
  301. m_SimulationService.CloseAgent(destination, id);
  302. responsedata["int_response_code"] = HttpStatusCode.OK;
  303. responsedata["str_response_string"] = "OpenSim agent " + id.ToString();
  304. m_log.Debug("[AGENT HANDLER]: Agent Released/Deleted.");
  305. }
  306. protected virtual void ReleaseAgent(UUID regionID, UUID id)
  307. {
  308. m_SimulationService.ReleaseAgent(regionID, id, "");
  309. }
  310. }
  311. }