GatekeeperServiceConnector.cs 13 KB

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