GatekeeperServiceConnector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.Net;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Imaging;
  38. using Nwc.XmlRpc;
  39. using log4net;
  40. using OpenSim.Services.Connectors.Simulation;
  41. namespace OpenSim.Services.Connectors.Hypergrid
  42. {
  43. public class GatekeeperServiceConnector : SimulationServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private static UUID m_HGMapImage = new UUID("00000000-0000-1111-9999-000000000013");
  47. private IAssetService m_AssetService;
  48. public GatekeeperServiceConnector() : base()
  49. {
  50. }
  51. public GatekeeperServiceConnector(IAssetService assService)
  52. {
  53. m_AssetService = assService;
  54. }
  55. protected override string AgentPath()
  56. {
  57. return "/foreignagent/";
  58. }
  59. protected override string ObjectPath()
  60. {
  61. return "/foreignobject/";
  62. }
  63. public bool LinkRegion(GridRegion info, out UUID regionID, out ulong realHandle, out string externalName, out string imageURL, out string reason)
  64. {
  65. regionID = UUID.Zero;
  66. imageURL = string.Empty;
  67. realHandle = 0;
  68. externalName = string.Empty;
  69. reason = string.Empty;
  70. Hashtable hash = new Hashtable();
  71. hash["region_name"] = info.RegionName;
  72. IList paramList = new ArrayList();
  73. paramList.Add(hash);
  74. XmlRpcRequest request = new XmlRpcRequest("link_region", paramList);
  75. string uri = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/";
  76. //m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Linking to " + uri);
  77. XmlRpcResponse response = null;
  78. try
  79. {
  80. response = request.Send(uri, 10000);
  81. }
  82. catch (Exception e)
  83. {
  84. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  85. reason = "Error contacting remote server";
  86. return false;
  87. }
  88. if (response.IsFault)
  89. {
  90. reason = response.FaultString;
  91. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  92. return false;
  93. }
  94. hash = (Hashtable)response.Value;
  95. //foreach (Object o in hash)
  96. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  97. try
  98. {
  99. bool success = false;
  100. Boolean.TryParse((string)hash["result"], out success);
  101. if (success)
  102. {
  103. UUID.TryParse((string)hash["uuid"], out regionID);
  104. //m_log.Debug(">> HERE, uuid: " + uuid);
  105. if ((string)hash["handle"] != null)
  106. {
  107. realHandle = Convert.ToUInt64((string)hash["handle"]);
  108. //m_log.Debug(">> HERE, realHandle: " + realHandle);
  109. }
  110. if (hash["region_image"] != null)
  111. imageURL = (string)hash["region_image"];
  112. if (hash["external_name"] != null)
  113. externalName = (string)hash["external_name"];
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. reason = "Error parsing return arguments";
  119. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  120. return false;
  121. }
  122. return true;
  123. }
  124. UUID m_MissingTexture = new UUID("5748decc-f629-461c-9a36-a35a221fe21f");
  125. public UUID GetMapImage(UUID regionID, string imageURL)
  126. {
  127. if (m_AssetService == null)
  128. return m_MissingTexture;
  129. try
  130. {
  131. WebClient c = new WebClient();
  132. //m_log.Debug("JPEG: " + imageURL);
  133. string filename = regionID.ToString();
  134. c.DownloadFile(imageURL, filename + ".jpg");
  135. Bitmap m = new Bitmap(filename + ".jpg");
  136. //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
  137. byte[] imageData = OpenJPEG.EncodeFromImage(m, true);
  138. AssetBase ass = new AssetBase(UUID.Random(), "region " + filename, (sbyte)AssetType.Texture, regionID.ToString());
  139. // !!! for now
  140. //info.RegionSettings.TerrainImageID = ass.FullID;
  141. ass.Temporary = true;
  142. ass.Local = true;
  143. ass.Data = imageData;
  144. m_AssetService.Store(ass);
  145. // finally
  146. return ass.FullID;
  147. }
  148. catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
  149. {
  150. m_log.Warn("[GATEKEEPER SERVICE CONNECTOR]: Failed getting/storing map image, because it is probably already in the cache");
  151. }
  152. return UUID.Zero;
  153. }
  154. public GridRegion GetHyperlinkRegion(GridRegion gatekeeper, UUID regionID)
  155. {
  156. Hashtable hash = new Hashtable();
  157. hash["region_uuid"] = regionID.ToString();
  158. IList paramList = new ArrayList();
  159. paramList.Add(hash);
  160. XmlRpcRequest request = new XmlRpcRequest("get_region", paramList);
  161. string uri = "http://" + gatekeeper.ExternalEndPoint.Address + ":" + gatekeeper.HttpPort + "/";
  162. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: contacting " + uri);
  163. XmlRpcResponse response = null;
  164. try
  165. {
  166. response = request.Send(uri, 10000);
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Debug("[GATEKEEPER SERVICE CONNECTOR]: Exception " + e.Message);
  171. return null;
  172. }
  173. if (response.IsFault)
  174. {
  175. m_log.ErrorFormat("[GATEKEEPER SERVICE CONNECTOR]: remote call returned an error: {0}", response.FaultString);
  176. return null;
  177. }
  178. hash = (Hashtable)response.Value;
  179. //foreach (Object o in hash)
  180. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  181. try
  182. {
  183. bool success = false;
  184. Boolean.TryParse((string)hash["result"], out success);
  185. if (success)
  186. {
  187. GridRegion region = new GridRegion();
  188. UUID.TryParse((string)hash["uuid"], out region.RegionID);
  189. //m_log.Debug(">> HERE, uuid: " + region.RegionID);
  190. int n = 0;
  191. if (hash["x"] != null)
  192. {
  193. Int32.TryParse((string)hash["x"], out n);
  194. region.RegionLocX = n;
  195. //m_log.Debug(">> HERE, x: " + region.RegionLocX);
  196. }
  197. if (hash["y"] != null)
  198. {
  199. Int32.TryParse((string)hash["y"], out n);
  200. region.RegionLocY = n;
  201. //m_log.Debug(">> HERE, y: " + region.RegionLocY);
  202. }
  203. if (hash["region_name"] != null)
  204. {
  205. region.RegionName = (string)hash["region_name"];
  206. //m_log.Debug(">> HERE, name: " + region.RegionName);
  207. }
  208. if (hash["hostname"] != null)
  209. region.ExternalHostName = (string)hash["hostname"];
  210. if (hash["http_port"] != null)
  211. {
  212. uint p = 0;
  213. UInt32.TryParse((string)hash["http_port"], out p);
  214. region.HttpPort = p;
  215. }
  216. if (hash["internal_port"] != null)
  217. {
  218. int p = 0;
  219. Int32.TryParse((string)hash["internal_port"], out p);
  220. region.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), p);
  221. }
  222. // Successful return
  223. return region;
  224. }
  225. }
  226. catch (Exception e)
  227. {
  228. m_log.Error("[GATEKEEPER SERVICE CONNECTOR]: Got exception while parsing hyperlink response " + e.StackTrace);
  229. return null;
  230. }
  231. return null;
  232. }
  233. }
  234. }