1
0

GatekeeperServiceConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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)
  177. {
  178. Hashtable hash = new Hashtable();
  179. hash["region_uuid"] = regionID.ToString();
  180. IList paramList = new ArrayList();
  181. paramList.Add(hash);
  182. XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
  183. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + gatekeeper.ServerURI);
  184. XmlRpcResponse response = null;
  185. try
  186. {
  187. response = request.Send(gatekeeper.ServerURI, 10000);
  188. }
  189. catch (Exception e)
  190. {
  191. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  192. return null;
  193. }
  194. if (response.IsFault)
  195. {
  196. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  197. return null;
  198. }
  199. hash = (Hashtable)response.Value;
  200. //foreach (Object o in hash)
  201. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  202. try
  203. {
  204. bool success = false;
  205. Boolean.TryParse((string)hash["result"], out success);
  206. if (success)
  207. {
  208. GridRegion region = new GridRegion();
  209. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  210. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  211. int n = 0;
  212. if (hash["x"] != null)
  213. {
  214. Int32.TryParse((string)hash["x"], out n);
  215. region.RegionLocX = n;
  216. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  217. }
  218. if (hash["y"] != null)
  219. {
  220. Int32.TryParse((string)hash["y"], out n);
  221. region.RegionLocY = n;
  222. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  223. }
  224. if (hash["region_name"] != null)
  225. {
  226. region.RegionName = (string)hash["region_name"];
  227. //m_log.Debug(">> HERE, region_name: " + region.RegionName);
  228. }
  229. if (hash["hostname"] != null)
  230. {
  231. region.ExternalHostName = (string)hash["hostname"];
  232. //m_log.Debug(">> HERE, hostname: " + region.ExternalHostName);
  233. }
  234. if (hash["http_port"] != null)
  235. {
  236. uint p = 0;
  237. UInt32.TryParse((string)hash["http_port"], out p);
  238. region.HttpPort = p;
  239. //m_log.Debug(">> HERE, http_port: " + region.HttpPort);
  240. }
  241. if (hash["internal_port"] != null)
  242. {
  243. int p = 0;
  244. Int32.TryParse((string)hash["internal_port"], out p);
  245. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  246. //m_log.Debug(">> HERE, internal_port: " + region.InternalEndPoint);
  247. }
  248. if (hash["server_uri"] != null)
  249. {
  250. region.ServerURI = (string)hash["server_uri"];
  251. //m_log.Debug(">> HERE, server_uri: " + region.ServerURI);
  252. }
  253. // Successful return
  254. return region;
  255. }
  256. }
  257. catch (Exception e)
  258. {
  259. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  260. return null;
  261. }
  262. return null;
  263. }
  264. }
  265. }