IGridService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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.Net.Sockets;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenMetaverse;
  35. using log4net;
  36. namespace OpenSim.Services.Interfaces
  37. {
  38. public interface IGridService
  39. {
  40. /// <summary>
  41. /// Register a region with the grid service.
  42. /// </summary>
  43. /// <param name="regionInfos"> </param>
  44. /// <returns></returns>
  45. /// <exception cref="System.Exception">Thrown if region registration failed</exception>
  46. string RegisterRegion(UUID scopeID, GridRegion regionInfos);
  47. /// <summary>
  48. /// Deregister a region with the grid service.
  49. /// </summary>
  50. /// <param name="regionID"></param>
  51. /// <returns></returns>
  52. /// <exception cref="System.Exception">Thrown if region deregistration failed</exception>
  53. bool DeregisterRegion(UUID regionID);
  54. /// <summary>
  55. /// Get information about the regions neighbouring the given co-ordinates (in meters).
  56. /// </summary>
  57. /// <param name="x"></param>
  58. /// <param name="y"></param>
  59. /// <returns></returns>
  60. List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID);
  61. GridRegion GetRegionByUUID(UUID scopeID, UUID regionID);
  62. /// <summary>
  63. /// Get the region at the given position (in meters)
  64. /// </summary>
  65. /// <param name="scopeID"></param>
  66. /// <param name="x"></param>
  67. /// <param name="y"></param>
  68. /// <returns></returns>
  69. GridRegion GetRegionByPosition(UUID scopeID, int x, int y);
  70. /// <summary>
  71. /// Get information about a region which exactly matches the name given.
  72. /// </summary>
  73. /// <param name="scopeID"></param>
  74. /// <param name="regionName"></param>
  75. /// <returns>Returns the region information if the name matched. Null otherwise.</returns>
  76. GridRegion GetRegionByName(UUID scopeID, string regionName);
  77. /// <summary>
  78. /// Get information about regions starting with the provided name.
  79. /// </summary>
  80. /// <param name="name">
  81. /// The name to match against.
  82. /// </param>
  83. /// <param name="maxNumber">
  84. /// The maximum number of results to return.
  85. /// </param>
  86. /// <returns>
  87. /// A list of <see cref="RegionInfo"/>s of regions with matching name. If the
  88. /// grid-server couldn't be contacted or returned an error, return null.
  89. /// </returns>
  90. List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber);
  91. List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax);
  92. List<GridRegion> GetDefaultRegions(UUID scopeID);
  93. List<GridRegion> GetDefaultHypergridRegions(UUID scopeID);
  94. List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y);
  95. List<GridRegion> GetHyperlinks(UUID scopeID);
  96. /// <summary>
  97. /// Get internal OpenSimulator region flags.
  98. /// </summary>
  99. /// <remarks>
  100. /// See OpenSimulator.Framework.RegionFlags. These are not returned in the GridRegion structure -
  101. /// they currently need to be requested separately. Possibly this should change to avoid multiple service calls
  102. /// in some situations.
  103. /// </remarks>
  104. /// <returns>
  105. /// The region flags.
  106. /// </returns>
  107. /// <param name='scopeID'></param>
  108. /// <param name='regionID'></param>
  109. int GetRegionFlags(UUID scopeID, UUID regionID);
  110. Dictionary<string,object> GetExtraFeatures();
  111. }
  112. public class GridRegion
  113. {
  114. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  115. #pragma warning disable 414
  116. private static readonly string LogHeader = "[GRID REGION]";
  117. #pragma warning restore 414
  118. /// <summary>
  119. /// The port by which http communication occurs with the region
  120. /// </summary>
  121. public uint HttpPort { get; set; }
  122. /// <summary>
  123. /// A well-formed URI for the host region server (namely "http://" + ExternalHostName)
  124. /// </summary>
  125. public string ServerURI
  126. {
  127. get {
  128. if (!String.IsNullOrEmpty(m_serverURI)) {
  129. return m_serverURI;
  130. } else {
  131. if (HttpPort == 0)
  132. return "http://" + m_externalHostName + "/";
  133. else
  134. return "http://" + m_externalHostName + ":" + HttpPort + "/";
  135. }
  136. }
  137. set {
  138. if (value.EndsWith("/")) {
  139. m_serverURI = value;
  140. } else {
  141. m_serverURI = value + '/';
  142. }
  143. }
  144. }
  145. protected string m_serverURI;
  146. /// <summary>
  147. /// Provides direct access to the 'm_serverURI' field, without returning a generated URL if m_serverURI is missing.
  148. /// </summary>
  149. public string RawServerURI
  150. {
  151. get { return m_serverURI; }
  152. set { m_serverURI = value; }
  153. }
  154. public string RegionName
  155. {
  156. get { return m_regionName; }
  157. set { m_regionName = value; }
  158. }
  159. protected string m_regionName = String.Empty;
  160. /// <summary>
  161. /// Region flags.
  162. /// </summary>
  163. /// <remarks>
  164. /// If not set (chiefly if a robust service is running code pre OpenSim 0.8.1) then this will be null and
  165. /// should be ignored. If you require flags information please use the separate IGridService.GetRegionFlags() call
  166. /// XXX: This field is currently ignored when used in RegisterRegion, but could potentially be
  167. /// used to set flags at this point.
  168. /// </remarks>
  169. public OpenSim.Framework.RegionFlags? RegionFlags { get; set; }
  170. protected string m_externalHostName;
  171. protected IPEndPoint m_internalEndPoint;
  172. /// <summary>
  173. /// The co-ordinate of this region in region units.
  174. /// </summary>
  175. public int RegionCoordX { get { return (int)Util.WorldToRegionLoc((uint)RegionLocX); } }
  176. /// <summary>
  177. /// The co-ordinate of this region in region units
  178. /// </summary>
  179. public int RegionCoordY { get { return (int)Util.WorldToRegionLoc((uint)RegionLocY); } }
  180. /// <summary>
  181. /// The location of this region in meters.
  182. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  183. /// </summary>
  184. public int RegionLocX
  185. {
  186. get { return m_regionLocX; }
  187. set { m_regionLocX = value; }
  188. }
  189. protected int m_regionLocX;
  190. public int RegionSizeX { get; set; }
  191. public int RegionSizeY { get; set; }
  192. /// <summary>
  193. /// The location of this region in meters.
  194. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  195. /// </summary>
  196. public int RegionLocY
  197. {
  198. get { return m_regionLocY; }
  199. set { m_regionLocY = value; }
  200. }
  201. protected int m_regionLocY;
  202. protected UUID m_estateOwner;
  203. public UUID EstateOwner
  204. {
  205. get { return m_estateOwner; }
  206. set { m_estateOwner = value; }
  207. }
  208. public UUID RegionID = UUID.Zero;
  209. public UUID ScopeID = UUID.Zero;
  210. public UUID TerrainImage = UUID.Zero;
  211. public UUID ParcelImage = UUID.Zero;
  212. public byte Access;
  213. public int Maturity;
  214. public string RegionSecret = string.Empty;
  215. public string Token = string.Empty;
  216. public GridRegion()
  217. {
  218. RegionSizeX = (int)Constants.RegionSize;
  219. RegionSizeY = (int)Constants.RegionSize;
  220. m_serverURI = string.Empty;
  221. }
  222. /*
  223. public GridRegion(int regionLocX, int regionLocY, IPEndPoint internalEndPoint, string externalUri)
  224. {
  225. m_regionLocX = regionLocX;
  226. m_regionLocY = regionLocY;
  227. RegionSizeX = (int)Constants.RegionSize;
  228. RegionSizeY = (int)Constants.RegionSize;
  229. m_internalEndPoint = internalEndPoint;
  230. m_externalHostName = externalUri;
  231. }
  232. public GridRegion(int regionLocX, int regionLocY, string externalUri, uint port)
  233. {
  234. m_regionLocX = regionLocX;
  235. m_regionLocY = regionLocY;
  236. RegionSizeX = (int)Constants.RegionSize;
  237. RegionSizeY = (int)Constants.RegionSize;
  238. m_externalHostName = externalUri;
  239. m_internalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)port);
  240. }
  241. */
  242. public GridRegion(uint xcell, uint ycell)
  243. {
  244. m_regionLocX = (int)Util.RegionToWorldLoc(xcell);
  245. m_regionLocY = (int)Util.RegionToWorldLoc(ycell);
  246. RegionSizeX = (int)Constants.RegionSize;
  247. RegionSizeY = (int)Constants.RegionSize;
  248. }
  249. public GridRegion(RegionInfo ConvertFrom)
  250. {
  251. m_regionName = ConvertFrom.RegionName;
  252. m_regionLocX = (int)(ConvertFrom.WorldLocX);
  253. m_regionLocY = (int)(ConvertFrom.WorldLocY);
  254. RegionSizeX = (int)ConvertFrom.RegionSizeX;
  255. RegionSizeY = (int)ConvertFrom.RegionSizeY;
  256. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  257. m_externalHostName = ConvertFrom.ExternalHostName;
  258. HttpPort = ConvertFrom.HttpPort;
  259. RegionID = ConvertFrom.RegionID;
  260. ServerURI = ConvertFrom.ServerURI;
  261. TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
  262. ParcelImage = ConvertFrom.RegionSettings.ParcelImageID;
  263. Access = ConvertFrom.AccessLevel;
  264. Maturity = ConvertFrom.RegionSettings.Maturity;
  265. RegionSecret = ConvertFrom.regionSecret;
  266. EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
  267. }
  268. public GridRegion(GridRegion ConvertFrom)
  269. {
  270. m_regionName = ConvertFrom.RegionName;
  271. RegionFlags = ConvertFrom.RegionFlags;
  272. m_regionLocX = ConvertFrom.RegionLocX;
  273. m_regionLocY = ConvertFrom.RegionLocY;
  274. RegionSizeX = ConvertFrom.RegionSizeX;
  275. RegionSizeY = ConvertFrom.RegionSizeY;
  276. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  277. m_externalHostName = ConvertFrom.ExternalHostName;
  278. HttpPort = ConvertFrom.HttpPort;
  279. RegionID = ConvertFrom.RegionID;
  280. ServerURI = ConvertFrom.ServerURI;
  281. TerrainImage = ConvertFrom.TerrainImage;
  282. ParcelImage = ConvertFrom.ParcelImage;
  283. Access = ConvertFrom.Access;
  284. Maturity = ConvertFrom.Maturity;
  285. RegionSecret = ConvertFrom.RegionSecret;
  286. EstateOwner = ConvertFrom.EstateOwner;
  287. }
  288. public GridRegion(Dictionary<string, object> kvp)
  289. {
  290. if (kvp.ContainsKey("uuid"))
  291. RegionID = new UUID((string)kvp["uuid"]);
  292. if (kvp.ContainsKey("locX"))
  293. RegionLocX = Convert.ToInt32((string)kvp["locX"]);
  294. if (kvp.ContainsKey("locY"))
  295. RegionLocY = Convert.ToInt32((string)kvp["locY"]);
  296. if (kvp.ContainsKey("sizeX"))
  297. RegionSizeX = Convert.ToInt32((string)kvp["sizeX"]);
  298. else
  299. RegionSizeX = (int)Constants.RegionSize;
  300. if (kvp.ContainsKey("sizeY"))
  301. RegionSizeY = Convert.ToInt32((string)kvp["sizeY"]);
  302. else
  303. RegionSizeX = (int)Constants.RegionSize;
  304. if (kvp.ContainsKey("regionName"))
  305. RegionName = (string)kvp["regionName"];
  306. if (kvp.ContainsKey("flags") && kvp["flags"] != null)
  307. RegionFlags = (OpenSim.Framework.RegionFlags?)Convert.ToInt32((string)kvp["flags"]);
  308. if (kvp.ContainsKey("serverIP"))
  309. {
  310. //int port = 0;
  311. //Int32.TryParse((string)kvp["serverPort"], out port);
  312. //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
  313. ExternalHostName = (string)kvp["serverIP"];
  314. }
  315. else
  316. ExternalHostName = "127.0.0.1";
  317. if (kvp.ContainsKey("serverPort"))
  318. {
  319. Int32 port = 0;
  320. Int32.TryParse((string)kvp["serverPort"], out port);
  321. InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  322. }
  323. if (kvp.ContainsKey("serverHttpPort"))
  324. {
  325. UInt32 port = 0;
  326. UInt32.TryParse((string)kvp["serverHttpPort"], out port);
  327. HttpPort = port;
  328. }
  329. if (kvp.ContainsKey("serverURI"))
  330. ServerURI = (string)kvp["serverURI"];
  331. if (kvp.ContainsKey("regionMapTexture"))
  332. UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
  333. if (kvp.ContainsKey("parcelMapTexture"))
  334. UUID.TryParse((string)kvp["parcelMapTexture"], out ParcelImage);
  335. if (kvp.ContainsKey("access"))
  336. Access = Byte.Parse((string)kvp["access"]);
  337. if (kvp.ContainsKey("regionSecret"))
  338. RegionSecret =(string)kvp["regionSecret"];
  339. if (kvp.ContainsKey("owner_uuid"))
  340. EstateOwner = new UUID(kvp["owner_uuid"].ToString());
  341. if (kvp.ContainsKey("Token"))
  342. Token = kvp["Token"].ToString();
  343. // m_log.DebugFormat("{0} New GridRegion. id={1}, loc=<{2},{3}>, size=<{4},{5}>",
  344. // LogHeader, RegionID, RegionLocX, RegionLocY, RegionSizeX, RegionSizeY);
  345. }
  346. public Dictionary<string, object> ToKeyValuePairs()
  347. {
  348. Dictionary<string, object> kvp = new Dictionary<string, object>();
  349. kvp["uuid"] = RegionID.ToString();
  350. kvp["locX"] = RegionLocX.ToString();
  351. kvp["locY"] = RegionLocY.ToString();
  352. kvp["sizeX"] = RegionSizeX.ToString();
  353. kvp["sizeY"] = RegionSizeY.ToString();
  354. kvp["regionName"] = RegionName;
  355. if (RegionFlags != null)
  356. kvp["flags"] = ((int)RegionFlags).ToString();
  357. kvp["serverIP"] = ExternalHostName; //ExternalEndPoint.Address.ToString();
  358. kvp["serverHttpPort"] = HttpPort.ToString();
  359. kvp["serverURI"] = ServerURI;
  360. kvp["serverPort"] = InternalEndPoint.Port.ToString();
  361. kvp["regionMapTexture"] = TerrainImage.ToString();
  362. kvp["parcelMapTexture"] = ParcelImage.ToString();
  363. kvp["access"] = Access.ToString();
  364. kvp["regionSecret"] = RegionSecret;
  365. kvp["owner_uuid"] = EstateOwner.ToString();
  366. kvp["Token"] = Token.ToString();
  367. // Maturity doesn't seem to exist in the DB
  368. return kvp;
  369. }
  370. #region Definition of equality
  371. /// <summary>
  372. /// Define equality as two regions having the same, non-zero UUID.
  373. /// </summary>
  374. public bool Equals(GridRegion region)
  375. {
  376. if ((object)region == null)
  377. return false;
  378. // Return true if the non-zero UUIDs are equal:
  379. return (RegionID != UUID.Zero) && RegionID.Equals(region.RegionID);
  380. }
  381. public override bool Equals(Object obj)
  382. {
  383. if (obj == null)
  384. return false;
  385. return Equals(obj as GridRegion);
  386. }
  387. public override int GetHashCode()
  388. {
  389. return RegionID.GetHashCode() ^ TerrainImage.GetHashCode() ^ ParcelImage.GetHashCode();
  390. }
  391. #endregion
  392. /// <value>
  393. /// This accessor can throw all the exceptions that Dns.GetHostAddresses can throw.
  394. ///
  395. /// XXX Isn't this really doing too much to be a simple getter, rather than an explict method?
  396. /// </value>
  397. public IPEndPoint ExternalEndPoint
  398. {
  399. get
  400. {
  401. // Old one defaults to IPv6
  402. //return new IPEndPoint(Dns.GetHostAddresses(m_externalHostName)[0], m_internalEndPoint.Port);
  403. IPAddress ia = null;
  404. // If it is already an IP, don't resolve it - just return directly
  405. if (IPAddress.TryParse(m_externalHostName, out ia))
  406. return new IPEndPoint(ia, m_internalEndPoint.Port);
  407. // Reset for next check
  408. ia = null;
  409. try
  410. {
  411. foreach (IPAddress Adr in Dns.GetHostAddresses(m_externalHostName))
  412. {
  413. if (ia == null)
  414. ia = Adr;
  415. if (Adr.AddressFamily == AddressFamily.InterNetwork)
  416. {
  417. ia = Adr;
  418. break;
  419. }
  420. }
  421. }
  422. catch (SocketException e)
  423. {
  424. throw new Exception(
  425. "Unable to resolve local hostname " + m_externalHostName + " innerException of type '" +
  426. e + "' attached to this exception", e);
  427. }
  428. return new IPEndPoint(ia, m_internalEndPoint.Port);
  429. }
  430. }
  431. public string ExternalHostName
  432. {
  433. get { return m_externalHostName; }
  434. set { m_externalHostName = value; }
  435. }
  436. public IPEndPoint InternalEndPoint
  437. {
  438. get { return m_internalEndPoint; }
  439. set { m_internalEndPoint = value; }
  440. }
  441. public ulong RegionHandle
  442. {
  443. get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
  444. }
  445. }
  446. }