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. string name = regionID.ToString();
  145. filename = Path.Combine(storagePath, name + ".jpg");
  146. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: Map image at {0}, cached at {1}", imageURL, filename);
  147. if (!File.Exists(filename))
  148. {
  149. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: downloading...");
  150. c.DownloadFile(imageURL, filename);
  151. }
  152. else
  153. {
  154. m_log.DebugFormat("[GATEKEEPER SERVICE CONNECTOR]: using cached image");
  155. }
  156. byte[] imageData = null;
  157. using (Bitmap bitmap = new Bitmap(filename))
  158. {
  159. //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
  160. imageData = OpenJPEG.EncodeFromImage(bitmap, true);
  161. }
  162. AssetBase ass = new AssetBase(UUID.Random(), "region " + name, (sbyte)AssetType.Texture, regionID.ToString());
  163. // !!! for now
  164. //info.RegionSettings.TerrainImageID = ass.FullID;
  165. ass.Data = imageData;
  166. mapTile = ass.FullID;
  167. // finally
  168. m_AssetService.Store(ass);
  169. }
  170. catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
  171. {
  172. m_log.Info("[GATEKEEPER SERVICE CONNECTOR]: Failed getting/storing map image, because it is probably already in the cache");
  173. }
  174. return mapTile;
  175. }
  176. public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID, UUID agentID, string agentHomeURI, out string message)
  177. {
  178. Hashtable hash = new Hashtable();
  179. hash["region_uuid"] = regionID.ToString();
  180. if (agentID != UUID.Zero)
  181. {
  182. hash["agent_id"] = agentID.ToString();
  183. if (agentHomeURI != null)
  184. hash["agent_home_uri"] = agentHomeURI;
  185. }
  186. IList paramList = new ArrayList();
  187. paramList.Add(hash);
  188. XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
  189. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + gatekeeper.ServerURI);
  190. XmlRpcResponse response = null;
  191. try
  192. {
  193. response = request.Send(gatekeeper.ServerURI, 10000);
  194. }
  195. catch (Exception e)
  196. {
  197. message = "Error contacting grid.";
  198. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  199. return null;
  200. }
  201. if (response.IsFault)
  202. {
  203. message = "Error contacting grid.";
  204. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  205. return null;
  206. }
  207. hash = (Hashtable)response.Value;
  208. //foreach (Object o in hash)
  209. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  210. try
  211. {
  212. bool success = false;
  213. Boolean.TryParse((string)hash["result"], out success);
  214. if (hash["message"] != null)
  215. message = (string)hash["message"];
  216. else if (success)
  217. message = null;
  218. else
  219. 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
  220. if (success)
  221. {
  222. GridRegion region = new GridRegion();
  223. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  224. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  225. int n = 0;
  226. if (hash["x"] != null)
  227. {
  228. Int32.TryParse((string)hash["x"], out n);
  229. region.RegionLocX = n;
  230. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  231. }
  232. if (hash["y"] != null)
  233. {
  234. Int32.TryParse((string)hash["y"], out n);
  235. region.RegionLocY = n;
  236. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  237. }
  238. if (hash["size_x"] != null)
  239. {
  240. Int32.TryParse((string)hash["size_x"], out n);
  241. region.RegionSizeX = n;
  242. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  243. }
  244. if (hash["size_y"] != null)
  245. {
  246. Int32.TryParse((string)hash["size_y"], out n);
  247. region.RegionSizeY = n;
  248. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  249. }
  250. if (hash["region_name"] != null)
  251. {
  252. region.RegionName = (string)hash["region_name"];
  253. //m_log.Debug(">> HERE, region_name: " + region.RegionName);
  254. }
  255. if (hash["hostname"] != null)
  256. {
  257. region.ExternalHostName = (string)hash["hostname"];
  258. //m_log.Debug(">> HERE, hostname: " + region.ExternalHostName);
  259. }
  260. if (hash["http_port"] != null)
  261. {
  262. uint p = 0;
  263. UInt32.TryParse((string)hash["http_port"], out p);
  264. region.HttpPort = p;
  265. //m_log.Debug(">> HERE, http_port: " + region.HttpPort);
  266. }
  267. if (hash["internal_port"] != null)
  268. {
  269. int p = 0;
  270. Int32.TryParse((string)hash["internal_port"], out p);
  271. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  272. //m_log.Debug(">> HERE, internal_port: " + region.InternalEndPoint);
  273. }
  274. if (hash["server_uri"] != null)
  275. {
  276. region.ServerURI = (string)hash["server_uri"];
  277. //m_log.Debug(">> HERE, server_uri: " + region.ServerURI);
  278. }
  279. // Successful return
  280. return region;
  281. }
  282. }
  283. catch (Exception e)
  284. {
  285. message = "Error parsing response from grid.";
  286. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  287. return null;
  288. }
  289. return null;
  290. }
  291. }
  292. }