GatekeeperServiceConnector.cs 13 KB

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