GatekeeperServiceConnector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.IO;
  32. using System.Net;
  33. using System.Reflection;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Imaging;
  39. using OpenMetaverse.StructuredData;
  40. using Nwc.XmlRpc;
  41. using log4net;
  42. using OpenSim.Services.Connectors.Simulation;
  43. using System.Net.Http;
  44. namespace OpenSim.Services.Connectors.Hypergrid
  45. {
  46. public class GatekeeperServiceConnector : SimulationServiceConnector
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private static UUID m_HGMapImage = new UUID("00000000-0000-1111-9999-000000000013");
  50. private IAssetService m_AssetService;
  51. public GatekeeperServiceConnector()
  52. : base()
  53. {
  54. }
  55. public GatekeeperServiceConnector(IAssetService assService)
  56. {
  57. m_AssetService = assService;
  58. }
  59. protected override string AgentPath()
  60. {
  61. return "foreignagent/";
  62. }
  63. protected override string ObjectPath()
  64. {
  65. return "foreignobject/";
  66. }
  67. public bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string externalName, out string imageURL, out string reason, out int sizeX, out int sizeY)
  68. {
  69. regionID = UUID.Zero;
  70. imageURL = string.Empty;
  71. realHandle = 0;
  72. externalName = string.Empty;
  73. reason = string.Empty;
  74. sizeX = (int)Constants.RegionSize;
  75. sizeY = (int)Constants.RegionSize;
  76. Hashtable hash = new Hashtable();
  77. hash["region_name"] = info.RegionName;
  78. IList paramList = new ArrayList();
  79. paramList.Add(hash);
  80. XmlRpcRequest request = new XmlRpcRequest("link_region", paramList);
  81. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Linking to " + info.ServerURI);
  82. XmlRpcResponse response = null;
  83. try
  84. {
  85. using HttpClient hclient = WebUtil.GetNewGlobalHttpClient(10000);
  86. response = request.Send(info.ServerURI, hclient);
  87. }
  88. catch (Exception e)
  89. {
  90. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  91. reason = "Error contacting remote server";
  92. return false;
  93. }
  94. if (response.IsFault)
  95. {
  96. reason = response.FaultString;
  97. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  98. return false;
  99. }
  100. hash = (Hashtable)response.Value;
  101. //foreach (Object o in hash)
  102. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  103. try
  104. {
  105. bool success = false;
  106. Boolean.TryParse((string)hash["result"], out success);
  107. if (success)
  108. {
  109. UUID.TryParse((string)hash["uuid"], out regionID);
  110. //m_log.Debug(">> HERE, uuid: " + regionID);
  111. if ((string)hash["handle"] != null)
  112. {
  113. realHandle = Convert.ToUInt64((string)hash["handle"]);
  114. //m_log.Debug(">> HERE, realHandle: " + realHandle);
  115. }
  116. if (hash["region_image"] != null)
  117. {
  118. imageURL = (string)hash["region_image"];
  119. //m_log.Debug(">> HERE, imageURL: " + imageURL);
  120. }
  121. if (hash["external_name"] != null)
  122. {
  123. externalName = (string)hash["external_name"];
  124. //m_log.Debug(">> HERE, externalName: " + externalName);
  125. }
  126. if (hash["size_x"] != null)
  127. {
  128. Int32.TryParse((string)hash["size_x"], out sizeX);
  129. }
  130. if (hash["size_y"] != null)
  131. {
  132. Int32.TryParse((string)hash["size_y"], out sizeY);
  133. }
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. reason = "Error parsing return arguments";
  139. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  140. return false;
  141. }
  142. return true;
  143. }
  144. public UUID GetMapImage(UUID regionID, string imageURL, string storagePath)
  145. {
  146. if (m_AssetService == null)
  147. {
  148. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: No AssetService defined. Map tile not retrieved.");
  149. return m_HGMapImage;
  150. }
  151. UUID mapTile = m_HGMapImage;
  152. string filename = string.Empty;
  153. try
  154. {
  155. //m_log.Debug("JPEG: " + imageURL);
  156. string name = regionID.ToString();
  157. filename = Path.Combine(storagePath, name + ".jpg");
  158. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: Map image at {0}, cached at {1}", imageURL, filename);
  159. if (!File.Exists(filename))
  160. {
  161. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: downloading...");
  162. using(WebClient c = new WebClient())
  163. c.DownloadFile(imageURL, filename);
  164. }
  165. else
  166. {
  167. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: using cached image");
  168. }
  169. byte[] imageData = null;
  170. using (Bitmap bitmap = new Bitmap(filename))
  171. {
  172. //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
  173. imageData = OpenJPEG.EncodeFromImage(bitmap, false);
  174. }
  175. AssetBase ass = new AssetBase(UUID.Random(), "region " + name, (sbyte)AssetType.Texture, regionID.ToString());
  176. // !!! for now
  177. //info.RegionSettings.TerrainImageID = ass.FullID;
  178. ass.Data = imageData;
  179. m_AssetService.Store(ass);
  180. // finally
  181. mapTile = ass.FullID;
  182. }
  183. catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
  184. {
  185. m_log.Info("[GATEKEEPER SERVICE CONNECTOR]: Failed getting/storing map image, because it is probably already in the cache");
  186. }
  187. return mapTile;
  188. }
  189. public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID, UUID agentID, string agentHomeURI, out string message)
  190. {
  191. Hashtable hash = new Hashtable();
  192. hash["region_uuid"] = regionID.ToString();
  193. if (!agentID.IsZero())
  194. {
  195. hash["agent_id"] = agentID.ToString();
  196. if (agentHomeURI != null)
  197. hash["agent_home_uri"] = agentHomeURI;
  198. }
  199. IList paramList = new ArrayList();
  200. paramList.Add(hash);
  201. XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
  202. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + gatekeeper.ServerURI);
  203. XmlRpcResponse response = null;
  204. try
  205. {
  206. using HttpClient hclient = WebUtil.GetNewGlobalHttpClient(10000);
  207. response = request.Send(gatekeeper.ServerURI, hclient);
  208. }
  209. catch (Exception e)
  210. {
  211. message = "Error contacting grid.";
  212. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  213. return null;
  214. }
  215. if (response.IsFault)
  216. {
  217. message = "Error contacting grid.";
  218. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  219. return null;
  220. }
  221. hash = (Hashtable)response.Value;
  222. //foreach (Object o in hash)
  223. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  224. try
  225. {
  226. bool success = false;
  227. Boolean.TryParse((string)hash["result"], out success);
  228. if (hash["message"] != null)
  229. message = (string)hash["message"];
  230. else if (success)
  231. message = null;
  232. else
  233. message = "The teleport destination could not be found."; // probably the dest grid is old and doesn't send 'message', but the most common problem is that the region is unavailable
  234. if (success)
  235. {
  236. GridRegion region = new GridRegion();
  237. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  238. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  239. int n = 0;
  240. if (hash["x"] != null)
  241. {
  242. Int32.TryParse((string)hash["x"], out n);
  243. region.RegionLocX = n;
  244. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  245. }
  246. if (hash["y"] != null)
  247. {
  248. Int32.TryParse((string)hash["y"], out n);
  249. region.RegionLocY = n;
  250. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  251. }
  252. if (hash["size_x"] != null)
  253. {
  254. Int32.TryParse((string)hash["size_x"], out n);
  255. region.RegionSizeX = n;
  256. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  257. }
  258. if (hash["size_y"] != null)
  259. {
  260. Int32.TryParse((string)hash["size_y"], out n);
  261. region.RegionSizeY = n;
  262. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  263. }
  264. if (hash["region_name"] != null)
  265. {
  266. region.RegionName = (string)hash["region_name"];
  267. //m_log.Debug(">> HERE, region_name: " + region.RegionName);
  268. }
  269. if (hash["hostname"] != null)
  270. {
  271. region.ExternalHostName = (string)hash["hostname"];
  272. //m_log.Debug(">> HERE, hostname: " + region.ExternalHostName);
  273. }
  274. if (hash["http_port"] != null)
  275. {
  276. uint p = 0;
  277. UInt32.TryParse((string)hash["http_port"], out p);
  278. region.HttpPort = p;
  279. //m_log.Debug(">> HERE, http_port: " + region.HttpPort);
  280. }
  281. if (hash["internal_port"] != null)
  282. {
  283. int p = 0;
  284. Int32.TryParse((string)hash["internal_port"], out p);
  285. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  286. //m_log.Debug(">> HERE, internal_port: " + region.InternalEndPoint);
  287. }
  288. if (hash["server_uri"] != null)
  289. {
  290. region.ServerURI = (string)hash["server_uri"];
  291. //m_log.Debug(">> HERE, server_uri: " + region.ServerURI);
  292. }
  293. // Successful return
  294. return region;
  295. }
  296. }
  297. catch (Exception e)
  298. {
  299. message = "Error parsing response from grid.";
  300. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  301. return null;
  302. }
  303. return null;
  304. }
  305. }
  306. }