HypergridServiceConnector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.Text;
  31. using System.Drawing;
  32. using System.Net;
  33. using System.Reflection;
  34. using OpenSim.Services.Interfaces;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenSim.Framework;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Imaging;
  39. using log4net;
  40. using Nwc.XmlRpc;
  41. namespace OpenSim.Services.Connectors.Grid
  42. {
  43. public class HypergridServiceConnector
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private IAssetService m_AssetService;
  47. public HypergridServiceConnector(IAssetService assService)
  48. {
  49. m_AssetService = assService;
  50. }
  51. public UUID LinkRegion(GridRegion info, out ulong realHandle)
  52. {
  53. UUID uuid = UUID.Zero;
  54. realHandle = 0;
  55. Hashtable hash = new Hashtable();
  56. hash["region_name"] = info.RegionName;
  57. IList paramList = new ArrayList();
  58. paramList.Add(hash);
  59. XmlRpcRequest request = new XmlRpcRequest("link_region", paramList);
  60. string uri = "http://" + info.ExternalEndPoint.Address + ":" + info.HttpPort + "/";
  61. m_log.Debug("[HGrid]: Linking to " + uri);
  62. XmlRpcResponse response = null;
  63. try
  64. {
  65. response = request.Send(uri, 10000);
  66. }
  67. catch (Exception e)
  68. {
  69. m_log.Debug("[HGrid]: Exception " + e.Message);
  70. return uuid;
  71. }
  72. if (response.IsFault)
  73. {
  74. m_log.ErrorFormat("[HGrid]: remote call returned an error: {0}", response.FaultString);
  75. }
  76. else
  77. {
  78. hash = (Hashtable)response.Value;
  79. //foreach (Object o in hash)
  80. // m_log.Debug(">> " + ((DictionaryEntry)o).Key + ":" + ((DictionaryEntry)o).Value);
  81. try
  82. {
  83. UUID.TryParse((string)hash["uuid"], out uuid);
  84. //m_log.Debug(">> HERE, uuid: " + uuid);
  85. info.RegionID = uuid;
  86. if ((string)hash["handle"] != null)
  87. {
  88. realHandle = Convert.ToUInt64((string)hash["handle"]);
  89. //m_log.Debug(">> HERE, realHandle: " + realHandle);
  90. }
  91. //if (hash["region_image"] != null)
  92. //{
  93. // UUID img = UUID.Zero;
  94. // UUID.TryParse((string)hash["region_image"], out img);
  95. // info.RegionSettings.TerrainImageID = img;
  96. //}
  97. if (hash["region_name"] != null)
  98. {
  99. info.RegionName = (string)hash["region_name"];
  100. //m_log.Debug(">> " + info.RegionName);
  101. }
  102. if (hash["internal_port"] != null)
  103. {
  104. int port = Convert.ToInt32((string)hash["internal_port"]);
  105. info.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  106. //m_log.Debug(">> " + info.InternalEndPoint.ToString());
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. m_log.Error("[HGrid]: Got exception while parsing hyperlink response " + e.StackTrace);
  112. }
  113. }
  114. return uuid;
  115. }
  116. public void GetMapImage(GridRegion info)
  117. {
  118. try
  119. {
  120. string regionimage = "regionImage" + info.RegionID.ToString();
  121. regionimage = regionimage.Replace("-", "");
  122. WebClient c = new WebClient();
  123. string uri = "http://" + info.ExternalHostName + ":" + info.HttpPort + "/index.php?method=" + regionimage;
  124. //m_log.Debug("JPEG: " + uri);
  125. c.DownloadFile(uri, info.RegionID.ToString() + ".jpg");
  126. Bitmap m = new Bitmap(info.RegionID.ToString() + ".jpg");
  127. //m_log.Debug("Size: " + m.PhysicalDimension.Height + "-" + m.PhysicalDimension.Width);
  128. byte[] imageData = OpenJPEG.EncodeFromImage(m, true);
  129. AssetBase ass = new AssetBase(UUID.Random(), "region " + info.RegionID.ToString(), (sbyte)AssetType.Texture);
  130. // !!! for now
  131. //info.RegionSettings.TerrainImageID = ass.FullID;
  132. ass.Temporary = true;
  133. ass.Local = true;
  134. ass.Data = imageData;
  135. m_AssetService.Store(ass);
  136. // finally
  137. info.TerrainImage = ass.FullID;
  138. }
  139. catch // LEGIT: Catching problems caused by OpenJPEG p/invoke
  140. {
  141. m_log.Warn("[HGrid]: Failed getting/storing map image, because it is probably already in the cache");
  142. }
  143. }
  144. public bool InformRegionOfUser(GridRegion regInfo, AgentCircuitData agentData, GridRegion home, string userServer, string assetServer, string inventoryServer)
  145. {
  146. string capsPath = agentData.CapsPath;
  147. Hashtable loginParams = new Hashtable();
  148. loginParams["session_id"] = agentData.SessionID.ToString();
  149. loginParams["firstname"] = agentData.firstname;
  150. loginParams["lastname"] = agentData.lastname;
  151. loginParams["agent_id"] = agentData.AgentID.ToString();
  152. loginParams["circuit_code"] = agentData.circuitcode.ToString();
  153. loginParams["startpos_x"] = agentData.startpos.X.ToString();
  154. loginParams["startpos_y"] = agentData.startpos.Y.ToString();
  155. loginParams["startpos_z"] = agentData.startpos.Z.ToString();
  156. loginParams["caps_path"] = capsPath;
  157. if (home != null)
  158. {
  159. loginParams["region_uuid"] = home.RegionID.ToString();
  160. loginParams["regionhandle"] = home.RegionHandle.ToString();
  161. loginParams["home_address"] = home.ExternalHostName;
  162. loginParams["home_port"] = home.HttpPort.ToString();
  163. loginParams["internal_port"] = home.InternalEndPoint.Port.ToString();
  164. m_log.Debug(" --------- Home -------");
  165. m_log.Debug(" >> " + loginParams["home_address"] + " <<");
  166. m_log.Debug(" >> " + loginParams["region_uuid"] + " <<");
  167. m_log.Debug(" >> " + loginParams["regionhandle"] + " <<");
  168. m_log.Debug(" >> " + loginParams["home_port"] + " <<");
  169. m_log.Debug(" --------- ------------ -------");
  170. }
  171. else
  172. m_log.WarnFormat("[HGrid]: Home region not found for {0} {1}", agentData.firstname, agentData.lastname);
  173. loginParams["userserver_id"] = userServer;
  174. loginParams["assetserver_id"] = assetServer;
  175. loginParams["inventoryserver_id"] = inventoryServer;
  176. ArrayList SendParams = new ArrayList();
  177. SendParams.Add(loginParams);
  178. // Send
  179. string uri = "http://" + regInfo.ExternalHostName + ":" + regInfo.HttpPort + "/";
  180. //m_log.Debug("XXX uri: " + uri);
  181. XmlRpcRequest request = new XmlRpcRequest("expect_hg_user", SendParams);
  182. XmlRpcResponse reply;
  183. try
  184. {
  185. reply = request.Send(uri, 6000);
  186. }
  187. catch (Exception e)
  188. {
  189. m_log.Warn("[HGrid]: Failed to notify region about user. Reason: " + e.Message);
  190. return false;
  191. }
  192. if (!reply.IsFault)
  193. {
  194. bool responseSuccess = true;
  195. if (reply.Value != null)
  196. {
  197. Hashtable resp = (Hashtable)reply.Value;
  198. if (resp.ContainsKey("success"))
  199. {
  200. if ((string)resp["success"] == "FALSE")
  201. {
  202. responseSuccess = false;
  203. }
  204. }
  205. }
  206. if (responseSuccess)
  207. {
  208. m_log.Info("[HGrid]: Successfully informed remote region about user " + agentData.AgentID);
  209. return true;
  210. }
  211. else
  212. {
  213. m_log.ErrorFormat("[HGrid]: Region responded that it is not available to receive clients");
  214. return false;
  215. }
  216. }
  217. else
  218. {
  219. m_log.ErrorFormat("[HGrid]: XmlRpc request to region failed with message {0}, code {1} ", reply.FaultString, reply.FaultCode);
  220. return false;
  221. }
  222. }
  223. }
  224. }