GatekeeperServiceConnector.cs 14 KB

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