1
0

GridClient.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using OpenMetaverse;
  34. using Nwc.XmlRpc;
  35. namespace OpenSim.Framework.Communications.Clients
  36. {
  37. public class GridClient
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public bool RegisterRegion(
  41. string gridServerURL, string sendKey, string receiveKey, RegionInfo regionInfo, out bool forcefulBanLines)
  42. {
  43. m_log.InfoFormat(
  44. "[GRID CLIENT]: Registering region {0} with grid at {1}", regionInfo.RegionName, gridServerURL);
  45. forcefulBanLines = true;
  46. Hashtable GridParams = new Hashtable();
  47. // Login / Authentication
  48. GridParams["authkey"] = sendKey;
  49. GridParams["recvkey"] = receiveKey;
  50. GridParams["UUID"] = regionInfo.RegionID.ToString();
  51. GridParams["sim_ip"] = regionInfo.ExternalHostName;
  52. GridParams["sim_port"] = regionInfo.InternalEndPoint.Port.ToString();
  53. GridParams["region_locx"] = regionInfo.RegionLocX.ToString();
  54. GridParams["region_locy"] = regionInfo.RegionLocY.ToString();
  55. GridParams["sim_name"] = regionInfo.RegionName;
  56. GridParams["http_port"] = regionInfo.HttpPort.ToString();
  57. GridParams["remoting_port"] = ConfigSettings.DefaultRegionRemotingPort.ToString();
  58. GridParams["map-image-id"] = regionInfo.RegionSettings.TerrainImageID.ToString();
  59. GridParams["originUUID"] = regionInfo.originRegionID.ToString();
  60. GridParams["server_uri"] = regionInfo.ServerURI;
  61. GridParams["region_secret"] = regionInfo.regionSecret;
  62. GridParams["major_interface_version"] = VersionInfo.MajorInterfaceVersion.ToString();
  63. if (regionInfo.MasterAvatarAssignedUUID != UUID.Zero)
  64. GridParams["master_avatar_uuid"] = regionInfo.MasterAvatarAssignedUUID.ToString();
  65. else
  66. GridParams["master_avatar_uuid"] = regionInfo.EstateSettings.EstateOwner.ToString();
  67. // Package into an XMLRPC Request
  68. ArrayList SendParams = new ArrayList();
  69. SendParams.Add(GridParams);
  70. // Send Request
  71. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_login", SendParams);
  72. XmlRpcResponse GridResp;
  73. try
  74. {
  75. // The timeout should always be significantly larger than the timeout for the grid server to request
  76. // the initial status of the region before confirming registration.
  77. GridResp = GridReq.Send(gridServerURL, 90000);
  78. }
  79. catch (Exception e)
  80. {
  81. Exception e2
  82. = new Exception(
  83. String.Format(
  84. "Unable to register region with grid at {0}. Grid service not running?",
  85. gridServerURL),
  86. e);
  87. throw e2;
  88. }
  89. Hashtable GridRespData = (Hashtable)GridResp.Value;
  90. // Hashtable griddatahash = GridRespData;
  91. // Process Response
  92. if (GridRespData.ContainsKey("error"))
  93. {
  94. string errorstring = (string)GridRespData["error"];
  95. Exception e = new Exception(
  96. String.Format("Unable to connect to grid at {0}: {1}", gridServerURL, errorstring));
  97. throw e;
  98. }
  99. else
  100. {
  101. // m_knownRegions = RequestNeighbours(regionInfo.RegionLocX, regionInfo.RegionLocY);
  102. if (GridRespData.ContainsKey("allow_forceful_banlines"))
  103. {
  104. if ((string)GridRespData["allow_forceful_banlines"] != "TRUE")
  105. {
  106. forcefulBanLines = false;
  107. }
  108. }
  109. }
  110. return true;
  111. }
  112. public bool DeregisterRegion(string gridServerURL, string sendKey, string receiveKey, RegionInfo regionInfo, out string errorMsg)
  113. {
  114. errorMsg = "";
  115. Hashtable GridParams = new Hashtable();
  116. GridParams["UUID"] = regionInfo.RegionID.ToString();
  117. // Package into an XMLRPC Request
  118. ArrayList SendParams = new ArrayList();
  119. SendParams.Add(GridParams);
  120. // Send Request
  121. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_after_region_moved", SendParams);
  122. XmlRpcResponse GridResp = null;
  123. try
  124. {
  125. GridResp = GridReq.Send(gridServerURL, 10000);
  126. }
  127. catch (Exception e)
  128. {
  129. Exception e2
  130. = new Exception(
  131. String.Format(
  132. "Unable to deregister region with grid at {0}. Grid service not running?",
  133. gridServerURL),
  134. e);
  135. throw e2;
  136. }
  137. Hashtable GridRespData = (Hashtable)GridResp.Value;
  138. // Hashtable griddatahash = GridRespData;
  139. // Process Response
  140. if (GridRespData != null && GridRespData.ContainsKey("error"))
  141. {
  142. errorMsg = (string)GridRespData["error"];
  143. return false;
  144. }
  145. return true;
  146. }
  147. public bool RequestNeighborInfo(
  148. string gridServerURL, string sendKey, string receiveKey, UUID regionUUID,
  149. out RegionInfo regionInfo, out string errorMsg)
  150. {
  151. // didn't find it so far, we have to go the long way
  152. regionInfo = null;
  153. errorMsg = string.Empty;
  154. Hashtable requestData = new Hashtable();
  155. requestData["region_UUID"] = regionUUID.ToString();
  156. requestData["authkey"] = sendKey;
  157. ArrayList SendParams = new ArrayList();
  158. SendParams.Add(requestData);
  159. XmlRpcRequest gridReq = new XmlRpcRequest("simulator_data_request", SendParams);
  160. XmlRpcResponse gridResp = null;
  161. try
  162. {
  163. gridResp = gridReq.Send(gridServerURL, 3000);
  164. }
  165. catch (Exception e)
  166. {
  167. errorMsg = e.Message;
  168. return false;
  169. }
  170. Hashtable responseData = (Hashtable)gridResp.Value;
  171. if (responseData.ContainsKey("error"))
  172. {
  173. errorMsg = (string)responseData["error"];
  174. return false; ;
  175. }
  176. regionInfo = BuildRegionInfo(responseData, String.Empty);
  177. return true;
  178. }
  179. public bool RequestNeighborInfo(
  180. string gridServerURL, string sendKey, string receiveKey, ulong regionHandle,
  181. out RegionInfo regionInfo, out string errorMsg)
  182. {
  183. // didn't find it so far, we have to go the long way
  184. regionInfo = null;
  185. errorMsg = string.Empty;
  186. try
  187. {
  188. Hashtable requestData = new Hashtable();
  189. requestData["region_handle"] = regionHandle.ToString();
  190. requestData["authkey"] = sendKey;
  191. ArrayList SendParams = new ArrayList();
  192. SendParams.Add(requestData);
  193. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_data_request", SendParams);
  194. XmlRpcResponse GridResp = GridReq.Send(gridServerURL, 3000);
  195. Hashtable responseData = (Hashtable)GridResp.Value;
  196. if (responseData.ContainsKey("error"))
  197. {
  198. errorMsg = (string)responseData["error"];
  199. return false;
  200. }
  201. uint regX = Convert.ToUInt32((string)responseData["region_locx"]);
  202. uint regY = Convert.ToUInt32((string)responseData["region_locy"]);
  203. string externalHostName = (string)responseData["sim_ip"];
  204. uint simPort = Convert.ToUInt32(responseData["sim_port"]);
  205. string regionName = (string)responseData["region_name"];
  206. UUID regionID = new UUID((string)responseData["region_UUID"]);
  207. uint remotingPort = Convert.ToUInt32((string)responseData["remoting_port"]);
  208. uint httpPort = 9000;
  209. if (responseData.ContainsKey("http_port"))
  210. {
  211. httpPort = Convert.ToUInt32((string)responseData["http_port"]);
  212. }
  213. // Ok, so this is definitively the wrong place to do this, way too hard coded, but it doesn't seem we GET this info?
  214. string simURI = "http://" + externalHostName + ":" + simPort;
  215. // string externalUri = (string) responseData["sim_uri"];
  216. //IPEndPoint neighbourInternalEndPoint = new IPEndPoint(IPAddress.Parse(internalIpStr), (int) port);
  217. regionInfo = RegionInfo.Create(regionID, regionName, regX, regY, externalHostName, httpPort, simPort, remotingPort, simURI);
  218. }
  219. catch (Exception e)
  220. {
  221. errorMsg = e.Message;
  222. return false;
  223. }
  224. return true;
  225. }
  226. public bool RequestClosestRegion(
  227. string gridServerURL, string sendKey, string receiveKey, string regionName,
  228. out RegionInfo regionInfo, out string errorMsg)
  229. {
  230. regionInfo = null;
  231. errorMsg = string.Empty;
  232. try
  233. {
  234. Hashtable requestData = new Hashtable();
  235. requestData["region_name_search"] = regionName;
  236. requestData["authkey"] = sendKey;
  237. ArrayList SendParams = new ArrayList();
  238. SendParams.Add(requestData);
  239. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_data_request", SendParams);
  240. XmlRpcResponse GridResp = GridReq.Send(gridServerURL, 3000);
  241. Hashtable responseData = (Hashtable)GridResp.Value;
  242. if (responseData.ContainsKey("error"))
  243. {
  244. errorMsg = (string)responseData["error"];
  245. return false;
  246. }
  247. regionInfo = BuildRegionInfo(responseData, "");
  248. }
  249. catch (Exception e)
  250. {
  251. errorMsg = e.Message;
  252. return false;
  253. }
  254. return true;
  255. }
  256. /// <summary>
  257. /// Performs a XML-RPC query against the grid server returning mapblock information in the specified coordinates
  258. /// </summary>
  259. /// <remarks>REDUNDANT - OGS1 is to be phased out in favour of OGS2</remarks>
  260. /// <param name="minX">Minimum X value</param>
  261. /// <param name="minY">Minimum Y value</param>
  262. /// <param name="maxX">Maximum X value</param>
  263. /// <param name="maxY">Maximum Y value</param>
  264. /// <returns>Hashtable of hashtables containing map data elements</returns>
  265. public bool MapBlockQuery(
  266. string gridServerURL, int minX, int minY, int maxX, int maxY, out Hashtable respData, out string errorMsg)
  267. {
  268. respData = new Hashtable();
  269. errorMsg = string.Empty;
  270. Hashtable param = new Hashtable();
  271. param["xmin"] = minX;
  272. param["ymin"] = minY;
  273. param["xmax"] = maxX;
  274. param["ymax"] = maxY;
  275. IList parameters = new ArrayList();
  276. parameters.Add(param);
  277. try
  278. {
  279. XmlRpcRequest req = new XmlRpcRequest("map_block", parameters);
  280. XmlRpcResponse resp = req.Send(gridServerURL, 10000);
  281. respData = (Hashtable)resp.Value;
  282. return true;
  283. }
  284. catch (Exception e)
  285. {
  286. errorMsg = e.Message;
  287. return false;
  288. }
  289. }
  290. public bool SearchRegionByName(string gridServerURL, IList parameters, out Hashtable respData, out string errorMsg)
  291. {
  292. respData = null;
  293. errorMsg = string.Empty;
  294. try
  295. {
  296. XmlRpcRequest request = new XmlRpcRequest("search_for_region_by_name", parameters);
  297. XmlRpcResponse resp = request.Send(gridServerURL, 10000);
  298. respData = (Hashtable)resp.Value;
  299. if (respData != null && respData.Contains("faultCode"))
  300. {
  301. errorMsg = (string)respData["faultString"];
  302. return false;
  303. }
  304. return true;
  305. }
  306. catch (Exception e)
  307. {
  308. errorMsg = e.Message;
  309. return false;
  310. }
  311. }
  312. public RegionInfo BuildRegionInfo(Hashtable responseData, string prefix)
  313. {
  314. uint regX = Convert.ToUInt32((string)responseData[prefix + "region_locx"]);
  315. uint regY = Convert.ToUInt32((string)responseData[prefix + "region_locy"]);
  316. string internalIpStr = (string)responseData[prefix + "sim_ip"];
  317. uint port = Convert.ToUInt32(responseData[prefix + "sim_port"]);
  318. IPEndPoint neighbourInternalEndPoint = new IPEndPoint(Util.GetHostFromDNS(internalIpStr), (int)port);
  319. RegionInfo regionInfo = new RegionInfo(regX, regY, neighbourInternalEndPoint, internalIpStr);
  320. regionInfo.RemotingPort = Convert.ToUInt32((string)responseData[prefix + "remoting_port"]);
  321. regionInfo.RemotingAddress = internalIpStr;
  322. if (responseData.ContainsKey(prefix + "http_port"))
  323. {
  324. regionInfo.HttpPort = Convert.ToUInt32((string)responseData[prefix + "http_port"]);
  325. }
  326. regionInfo.RegionID = new UUID((string)responseData[prefix + "region_UUID"]);
  327. regionInfo.RegionName = (string)responseData[prefix + "region_name"];
  328. regionInfo.RegionSettings.TerrainImageID = new UUID((string)responseData[prefix + "map_UUID"]);
  329. return regionInfo;
  330. }
  331. }
  332. }