1
0

UserAgentServiceConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.Net;
  32. using System.Reflection;
  33. using System.Text;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Services.Connectors.Simulation;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenMetaverse;
  39. using OpenMetaverse.StructuredData;
  40. using log4net;
  41. using Nwc.XmlRpc;
  42. using Nini.Config;
  43. namespace OpenSim.Services.Connectors.Hypergrid
  44. {
  45. public class UserAgentServiceConnector : IUserAgentService
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(
  49. MethodBase.GetCurrentMethod().DeclaringType);
  50. string m_ServerURL;
  51. public UserAgentServiceConnector(string url)
  52. {
  53. m_ServerURL = url;
  54. }
  55. public UserAgentServiceConnector(IConfigSource config)
  56. {
  57. }
  58. public bool LoginAgentToGrid(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination, out string reason)
  59. {
  60. reason = String.Empty;
  61. if (destination == null)
  62. {
  63. reason = "Destination is null";
  64. m_log.Debug("[USER AGENT CONNECTOR]: Given destination is null");
  65. return false;
  66. }
  67. string uri = m_ServerURL + "/homeagent/" + aCircuit.AgentID + "/";
  68. Console.WriteLine(" >>> LoginAgentToGrid <<< " + uri);
  69. HttpWebRequest AgentCreateRequest = (HttpWebRequest)WebRequest.Create(uri);
  70. AgentCreateRequest.Method = "POST";
  71. AgentCreateRequest.ContentType = "application/json";
  72. AgentCreateRequest.Timeout = 10000;
  73. //AgentCreateRequest.KeepAlive = false;
  74. //AgentCreateRequest.Headers.Add("Authorization", authKey);
  75. // Fill it in
  76. OSDMap args = PackCreateAgentArguments(aCircuit, gatekeeper, destination);
  77. string strBuffer = "";
  78. byte[] buffer = new byte[1];
  79. try
  80. {
  81. strBuffer = OSDParser.SerializeJsonString(args);
  82. Encoding str = Util.UTF8;
  83. buffer = str.GetBytes(strBuffer);
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.WarnFormat("[USER AGENT CONNECTOR]: Exception thrown on serialization of ChildCreate: {0}", e.Message);
  88. // ignore. buffer will be empty, caller should check.
  89. }
  90. Stream os = null;
  91. try
  92. { // send the Post
  93. AgentCreateRequest.ContentLength = buffer.Length; //Count bytes to send
  94. os = AgentCreateRequest.GetRequestStream();
  95. os.Write(buffer, 0, strBuffer.Length); //Send it
  96. m_log.InfoFormat("[USER AGENT CONNECTOR]: Posted CreateAgent request to remote sim {0}, region {1}, x={2} y={3}",
  97. uri, destination.RegionName, destination.RegionLocX, destination.RegionLocY);
  98. }
  99. //catch (WebException ex)
  100. catch
  101. {
  102. //m_log.InfoFormat("[USER AGENT CONNECTOR]: Bad send on ChildAgentUpdate {0}", ex.Message);
  103. reason = "cannot contact remote region";
  104. return false;
  105. }
  106. finally
  107. {
  108. if (os != null)
  109. os.Close();
  110. }
  111. // Let's wait for the response
  112. //m_log.Info("[USER AGENT CONNECTOR]: Waiting for a reply after DoCreateChildAgentCall");
  113. WebResponse webResponse = null;
  114. StreamReader sr = null;
  115. try
  116. {
  117. webResponse = AgentCreateRequest.GetResponse();
  118. if (webResponse == null)
  119. {
  120. m_log.Info("[USER AGENT CONNECTOR]: Null reply on DoCreateChildAgentCall post");
  121. }
  122. else
  123. {
  124. sr = new StreamReader(webResponse.GetResponseStream());
  125. string response = sr.ReadToEnd().Trim();
  126. m_log.InfoFormat("[USER AGENT CONNECTOR]: DoCreateChildAgentCall reply was {0} ", response);
  127. if (!String.IsNullOrEmpty(response))
  128. {
  129. try
  130. {
  131. // we assume we got an OSDMap back
  132. OSDMap r = Util.GetOSDMap(response);
  133. bool success = r["success"].AsBoolean();
  134. reason = r["reason"].AsString();
  135. return success;
  136. }
  137. catch (NullReferenceException e)
  138. {
  139. m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", e.Message);
  140. // check for old style response
  141. if (response.ToLower().StartsWith("true"))
  142. return true;
  143. return false;
  144. }
  145. }
  146. }
  147. }
  148. catch (WebException ex)
  149. {
  150. m_log.InfoFormat("[USER AGENT CONNECTOR]: exception on reply of DoCreateChildAgentCall {0}", ex.Message);
  151. reason = "Destination did not reply";
  152. return false;
  153. }
  154. finally
  155. {
  156. if (sr != null)
  157. sr.Close();
  158. }
  159. return true;
  160. }
  161. protected OSDMap PackCreateAgentArguments(AgentCircuitData aCircuit, GridRegion gatekeeper, GridRegion destination)
  162. {
  163. OSDMap args = null;
  164. try
  165. {
  166. args = aCircuit.PackAgentCircuitData();
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Debug("[USER AGENT CONNECTOR]: PackAgentCircuitData failed with exception: " + e.Message);
  171. }
  172. // Add the input arguments
  173. args["gatekeeper_host"] = OSD.FromString(gatekeeper.ExternalHostName);
  174. args["gatekeeper_port"] = OSD.FromString(gatekeeper.HttpPort.ToString());
  175. args["destination_x"] = OSD.FromString(destination.RegionLocX.ToString());
  176. args["destination_y"] = OSD.FromString(destination.RegionLocY.ToString());
  177. args["destination_name"] = OSD.FromString(destination.RegionName);
  178. args["destination_uuid"] = OSD.FromString(destination.RegionID.ToString());
  179. return args;
  180. }
  181. public void SetClientToken(UUID sessionID, string token)
  182. {
  183. // no-op
  184. }
  185. public GridRegion GetHomeRegion(UUID userID, out Vector3 position, out Vector3 lookAt)
  186. {
  187. position = Vector3.UnitY; lookAt = Vector3.UnitY;
  188. Hashtable hash = new Hashtable();
  189. hash["userID"] = userID.ToString();
  190. IList paramList = new ArrayList();
  191. paramList.Add(hash);
  192. XmlRpcRequest request = new XmlRpcRequest("get_home_region", paramList);
  193. XmlRpcResponse response = null;
  194. try
  195. {
  196. response = request.Send(m_ServerURL, 10000);
  197. }
  198. catch (Exception e)
  199. {
  200. return null;
  201. }
  202. if (response.IsFault)
  203. {
  204. return null;
  205. }
  206. hash = (Hashtable)response.Value;
  207. //foreach (Object o in hash)
  208. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  209. try
  210. {
  211. bool success = false;
  212. Boolean.TryParse((string)hash["result"], out success);
  213. if (success)
  214. {
  215. GridRegion region = new GridRegion();
  216. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  217. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  218. int n = 0;
  219. if (hash["x"] != null)
  220. {
  221. Int32.TryParse((string)hash["x"], out n);
  222. region.RegionLocX = n;
  223. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  224. }
  225. if (hash["y"] != null)
  226. {
  227. Int32.TryParse((string)hash["y"], out n);
  228. region.RegionLocY = n;
  229. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  230. }
  231. if (hash["region_name"] != null)
  232. {
  233. region.RegionName = (string)hash["region_name"];
  234. //m_log.Debug(">> HERE, name: " + region.RegionName);
  235. }
  236. if (hash["hostname"] != null)
  237. region.ExternalHostName = (string)hash["hostname"];
  238. if (hash["http_port"] != null)
  239. {
  240. uint p = 0;
  241. UInt32.TryParse((string)hash["http_port"], out p);
  242. region.HttpPort = p;
  243. }
  244. if (hash["internal_port"] != null)
  245. {
  246. int p = 0;
  247. Int32.TryParse((string)hash["internal_port"], out p);
  248. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  249. }
  250. if (hash["position"] != null)
  251. Vector3.TryParse((string)hash["position"], out position);
  252. if (hash["lookAt"] != null)
  253. Vector3.TryParse((string)hash["lookAt"], out lookAt);
  254. // Successful return
  255. return region;
  256. }
  257. }
  258. catch (Exception e)
  259. {
  260. return null;
  261. }
  262. return null;
  263. }
  264. public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
  265. {
  266. Hashtable hash = new Hashtable();
  267. hash["sessionID"] = sessionID.ToString();
  268. hash["externalName"] = thisGridExternalName;
  269. IList paramList = new ArrayList();
  270. paramList.Add(hash);
  271. XmlRpcRequest request = new XmlRpcRequest("agent_is_coming_home", paramList);
  272. string reason = string.Empty;
  273. return GetBoolResponse(request, out reason);
  274. }
  275. public bool VerifyAgent(UUID sessionID, string token)
  276. {
  277. Hashtable hash = new Hashtable();
  278. hash["sessionID"] = sessionID.ToString();
  279. hash["token"] = token;
  280. IList paramList = new ArrayList();
  281. paramList.Add(hash);
  282. XmlRpcRequest request = new XmlRpcRequest("verify_agent", paramList);
  283. string reason = string.Empty;
  284. return GetBoolResponse(request, out reason);
  285. }
  286. public bool VerifyClient(UUID sessionID, string token)
  287. {
  288. Hashtable hash = new Hashtable();
  289. hash["sessionID"] = sessionID.ToString();
  290. hash["token"] = token;
  291. IList paramList = new ArrayList();
  292. paramList.Add(hash);
  293. XmlRpcRequest request = new XmlRpcRequest("verify_client", paramList);
  294. string reason = string.Empty;
  295. return GetBoolResponse(request, out reason);
  296. }
  297. public void LogoutAgent(UUID userID, UUID sessionID)
  298. {
  299. Hashtable hash = new Hashtable();
  300. hash["sessionID"] = sessionID.ToString();
  301. hash["userID"] = userID.ToString();
  302. IList paramList = new ArrayList();
  303. paramList.Add(hash);
  304. XmlRpcRequest request = new XmlRpcRequest("logout_agent", paramList);
  305. string reason = string.Empty;
  306. GetBoolResponse(request, out reason);
  307. }
  308. private bool GetBoolResponse(XmlRpcRequest request, out string reason)
  309. {
  310. //m_log.Debug("[USER AGENT CONNECTOR]: GetBoolResponse from/to " + m_ServerURL);
  311. XmlRpcResponse response = null;
  312. try
  313. {
  314. response = request.Send(m_ServerURL, 10000);
  315. }
  316. catch (Exception e)
  317. {
  318. m_log.DebugFormat("[USER AGENT CONNECTOR]: Unable to contact remote server {0}", m_ServerURL);
  319. reason = "Exception: " + e.Message;
  320. return false;
  321. }
  322. if (response.IsFault)
  323. {
  324. m_log.ErrorFormat("[HGrid]: remote call to {0} returned an error: {1}", m_ServerURL, response.FaultString);
  325. reason = "XMLRPC Fault";
  326. return false;
  327. }
  328. Hashtable hash = (Hashtable)response.Value;
  329. //foreach (Object o in hash)
  330. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  331. try
  332. {
  333. if (hash == null)
  334. {
  335. m_log.ErrorFormat("[USER AGENT CONNECTOR]: Got null response from {0}! THIS IS BAAAAD", m_ServerURL);
  336. reason = "Internal error 1";
  337. return false;
  338. }
  339. bool success = false;
  340. reason = string.Empty;
  341. if (hash.ContainsKey("result"))
  342. Boolean.TryParse((string)hash["result"], out success);
  343. else
  344. {
  345. reason = "Internal error 2";
  346. m_log.WarnFormat("[USER AGENT CONNECTOR]: response from {0} does not have expected key 'result'", m_ServerURL);
  347. }
  348. return success;
  349. }
  350. catch (Exception e)
  351. {
  352. m_log.ErrorFormat("[HGrid]: Got exception on GetBoolResponse response.");
  353. if (hash.ContainsKey("result") && hash["result"] != null)
  354. m_log.ErrorFormat("Reply was ", (string)hash["result"]);
  355. reason = "Exception: " + e.Message;
  356. return false;
  357. }
  358. }
  359. }
  360. }