GatekeeperServiceConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.Drawing;
  31. using System.Net;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Imaging;
  38. using OpenMetaverse.StructuredData;
  39. using Nwc.XmlRpc;
  40. using log4net;
  41. using OpenSim.Services.Connectors.Simulation;
  42. namespace OpenSim.Services.Connectors.Hypergrid
  43. {
  44. public class GatekeeperServiceConnector : SimulationServiceConnector
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. // private static UUID m_HGMapImage = new UUID("00000000-0000-1111-9999-000000000013");
  48. private IAssetService m_AssetService;
  49. public GatekeeperServiceConnector() : base()
  50. {
  51. }
  52. public GatekeeperServiceConnector(IAssetService assService)
  53. {
  54. m_AssetService = assService;
  55. }
  56. protected override string AgentPath()
  57. {
  58. return "/foreignagent/";
  59. }
  60. protected override string ObjectPath()
  61. {
  62. return "/foreignobject/";
  63. }
  64. public bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string externalName, out string imageURL, out string reason)
  65. {
  66. regionID = UUID.Zero;
  67. imageURL = string.Empty;
  68. realHandle = 0;
  69. externalName = string.Empty;
  70. reason = string.Empty;
  71. Hashtable hash = new Hashtable();
  72. hash["region_name"] = info.RegionName;
  73. IList paramList = new ArrayList();
  74. paramList.Add(hash);
  75. XmlRpcRequest request = new XmlRpcRequest("link_region", paramList);
  76. string uri = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/";
  77. //m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Linking to " + uri);
  78. XmlRpcResponse response = null;
  79. try
  80. {
  81. response = request.Send(uri, 10000);
  82. }
  83. catch (Exception e)
  84. {
  85. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  86. reason = "Error contacting remote server";
  87. return false;
  88. }
  89. if (response.IsFault)
  90. {
  91. reason = response.FaultString;
  92. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  93. return false;
  94. }
  95. hash = (Hashtable)response.Value;
  96. //foreach (Object o in hash)
  97. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  98. try
  99. {
  100. bool success = false;
  101. Boolean.TryParse((string)hash["result"], out success);
  102. if (success)
  103. {
  104. UUID.TryParse((string)hash["uuid"], out regionID);
  105. //m_log.Debug(">> HERE, uuid: " + uuid);
  106. if ((string)hash["handle"] != null)
  107. {
  108. realHandle = Convert.ToUInt64((string)hash["handle"]);
  109. //m_log.Debug(">> HERE, realHandle: " + realHandle);
  110. }
  111. if (hash["region_image"] != null)
  112. imageURL = (string)hash["region_image"];
  113. if (hash["external_name"] != null)
  114. externalName = (string)hash["external_name"];
  115. }
  116. }
  117. catch (Exception e)
  118. {
  119. reason = "Error parsing return arguments";
  120. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  121. return false;
  122. }
  123. return true;
  124. }
  125. UUID m_MissingTexture = new UUID("5748decc-f629-461c-9a36-a35a221fe21f");
  126. public UUID GetMapImage(UUID regionID, string imageURL)
  127. {
  128. if (m_AssetService == null)
  129. return m_MissingTexture;
  130. try
  131. {
  132. WebClient c = new WebClient();
  133. //m_log.Debug("JPEG: " + imageURL);
  134. string filename = regionID.ToString();
  135. c.DownloadFile(imageURL, filename + ".jpg");
  136. Bitmap m = new Bitmap(filename + ".jpg");
  137. //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
  138. byte[] imageData = OpenJPEG.EncodeFromImage(m, true);
  139. AssetBase ass = new AssetBase(UUID.Random(), "region " + filename, (sbyte)AssetType.Texture, regionID.ToString());
  140. // !!! for now
  141. //info.RegionSettings.TerrainImageID = ass.FullID;
  142. ass.Temporary = true;
  143. ass.Local = true;
  144. ass.Data = imageData;
  145. m_AssetService.Store(ass);
  146. // finally
  147. return ass.FullID;
  148. }
  149. catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
  150. {
  151. m_log.Warn("[GATEKEEPER SERVICE CONNECTOR]: Failed getting/storing map image, because it is probably already in the cache");
  152. }
  153. return UUID.Zero;
  154. }
  155. public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID)
  156. {
  157. Hashtable hash = new Hashtable();
  158. hash["region_uuid"] = regionID.ToString();
  159. IList paramList = new ArrayList();
  160. paramList.Add(hash);
  161. XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
  162. string uri = "http://" + gatekeeper.ExternalEndPoint.Address + ":" + gatekeeper.HttpPort + "/";
  163. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + uri);
  164. XmlRpcResponse response = null;
  165. try
  166. {
  167. response = request.Send(uri, 10000);
  168. }
  169. catch (Exception e)
  170. {
  171. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  172. return null;
  173. }
  174. if (response.IsFault)
  175. {
  176. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  177. return null;
  178. }
  179. hash = (Hashtable)response.Value;
  180. //foreach (Object o in hash)
  181. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  182. try
  183. {
  184. bool success = false;
  185. Boolean.TryParse((string)hash["result"], out success);
  186. if (success)
  187. {
  188. GridRegion region = new GridRegion();
  189. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  190. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  191. int n = 0;
  192. if (hash["x"] != null)
  193. {
  194. Int32.TryParse((string)hash["x"], out n);
  195. region.RegionLocX = n;
  196. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  197. }
  198. if (hash["y"] != null)
  199. {
  200. Int32.TryParse((string)hash["y"], out n);
  201. region.RegionLocY = n;
  202. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  203. }
  204. if (hash["region_name"] != null)
  205. {
  206. region.RegionName = (string)hash["region_name"];
  207. //m_log.Debug(">> HERE, name: " + region.RegionName);
  208. }
  209. if (hash["hostname"] != null)
  210. region.ExternalHostName = (string)hash["hostname"];
  211. if (hash["http_port"] != null)
  212. {
  213. uint p = 0;
  214. UInt32.TryParse((string)hash["http_port"], out p);
  215. region.HttpPort = p;
  216. }
  217. if (hash["internal_port"] != null)
  218. {
  219. int p = 0;
  220. Int32.TryParse((string)hash["internal_port"], out p);
  221. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  222. }
  223. // Successful return
  224. return region;
  225. }
  226. }
  227. catch (Exception e)
  228. {
  229. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  230. return null;
  231. }
  232. return null;
  233. }
  234. public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint flags, out string myipaddress, out string reason)
  235. {
  236. HttpWebRequest AgentCreateRequest = null;
  237. myipaddress = String.Empty;
  238. reason = String.Empty;
  239. if (SendRequest(destination, aCircuit, flags, out reason, out AgentCreateRequest))
  240. {
  241. string response = GetResponse(AgentCreateRequest, out reason);
  242. bool success = true;
  243. UnpackResponse(response, out success, out reason, out myipaddress);
  244. return success;
  245. }
  246. return false;
  247. }
  248. protected void UnpackResponse(string response, out bool result, out string reason, out string ipaddress)
  249. {
  250. result = true;
  251. reason = string.Empty;
  252. ipaddress = string.Empty;
  253. if (!String.IsNullOrEmpty(response))
  254. {
  255. try
  256. {
  257. // we assume we got an OSDMap back
  258. OSDMap r = Util.GetOSDMap(response);
  259. result = r["success"].AsBoolean();
  260. reason = r["reason"].AsString();
  261. ipaddress = r["your_ip"].AsString();
  262. }
  263. catch (NullReferenceException e)
  264. {
  265. m_log.InfoFormat("[GATEKEEPER SERVICE CONNECTOR]: exception on UnpackResponse of DoCreateChildAgentCall {0}", e.Message);
  266. reason = "Internal error";
  267. result = false;
  268. }
  269. }
  270. }
  271. }
  272. }