IGridService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 interface IHypergridLinker
  113. {
  114. GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason);
  115. bool TryUnlinkRegion(string mapName);
  116. }
  117. public class GridRegion
  118. {
  119. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  120. #pragma warning disable 414
  121. private static readonly string LogHeader = "[GRID REGION]";
  122. #pragma warning restore 414
  123. /// <summary>
  124. /// The port by which http communication occurs with the region
  125. /// </summary>
  126. public uint HttpPort { get; set; }
  127. /// <summary>
  128. /// A well-formed URI for the host region server (namely "http://" + ExternalHostName)
  129. /// </summary>
  130. public string ServerURI
  131. {
  132. get {
  133. if (!String.IsNullOrEmpty(m_serverURI)) {
  134. return m_serverURI;
  135. } else {
  136. if (HttpPort == 0)
  137. return "http://" + m_externalHostName + "/";
  138. else
  139. return "http://" + m_externalHostName + ":" + HttpPort + "/";
  140. }
  141. }
  142. set {
  143. if ( value == null)
  144. {
  145. m_serverURI = String.Empty;
  146. return;
  147. }
  148. if ( value.EndsWith("/") )
  149. {
  150. m_serverURI = value;
  151. }
  152. else
  153. {
  154. m_serverURI = value + '/';
  155. }
  156. }
  157. }
  158. protected string m_serverURI;
  159. /// <summary>
  160. /// Provides direct access to the 'm_serverURI' field, without returning a generated URL if m_serverURI is missing.
  161. /// </summary>
  162. public string RawServerURI
  163. {
  164. get { return m_serverURI; }
  165. set { m_serverURI = value; }
  166. }
  167. public string RegionName
  168. {
  169. get { return m_regionName; }
  170. set { m_regionName = value; }
  171. }
  172. protected string m_regionName = String.Empty;
  173. /// <summary>
  174. /// Region flags.
  175. /// </summary>
  176. /// <remarks>
  177. /// If not set (chiefly if a robust service is running code pre OpenSim 0.8.1) then this will be null and
  178. /// should be ignored. If you require flags information please use the separate IGridService.GetRegionFlags() call
  179. /// XXX: This field is currently ignored when used in RegisterRegion, but could potentially be
  180. /// used to set flags at this point.
  181. /// </remarks>
  182. public OpenSim.Framework.RegionFlags? RegionFlags { get; set; }
  183. protected string m_externalHostName;
  184. protected IPEndPoint m_internalEndPoint;
  185. /// <summary>
  186. /// The co-ordinate of this region in region units.
  187. /// </summary>
  188. public int RegionCoordX { get { return (int)Util.WorldToRegionLoc((uint)RegionLocX); } }
  189. /// <summary>
  190. /// The co-ordinate of this region in region units
  191. /// </summary>
  192. public int RegionCoordY { get { return (int)Util.WorldToRegionLoc((uint)RegionLocY); } }
  193. /// <summary>
  194. /// The location of this region in meters.
  195. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  196. /// </summary>
  197. public int RegionLocX
  198. {
  199. get { return m_regionLocX; }
  200. set { m_regionLocX = value; }
  201. }
  202. protected int m_regionLocX;
  203. public int RegionSizeX { get; set; }
  204. public int RegionSizeY { get; set; }
  205. /// <summary>
  206. /// The location of this region in meters.
  207. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  208. /// </summary>
  209. public int RegionLocY
  210. {
  211. get { return m_regionLocY; }
  212. set { m_regionLocY = value; }
  213. }
  214. protected int m_regionLocY;
  215. protected UUID m_estateOwner;
  216. public UUID EstateOwner
  217. {
  218. get { return m_estateOwner; }
  219. set { m_estateOwner = value; }
  220. }
  221. public UUID RegionID = UUID.Zero;
  222. public UUID ScopeID = UUID.Zero;
  223. public UUID TerrainImage = UUID.Zero;
  224. public UUID ParcelImage = UUID.Zero;
  225. public byte Access;
  226. public int Maturity;
  227. public string RegionSecret = string.Empty;
  228. public string Token = string.Empty;
  229. public GridRegion()
  230. {
  231. RegionSizeX = (int)Constants.RegionSize;
  232. RegionSizeY = (int)Constants.RegionSize;
  233. m_serverURI = string.Empty;
  234. }
  235. /*
  236. public GridRegion(int regionLocX, int regionLocY, IPEndPoint internalEndPoint, string externalUri)
  237. {
  238. m_regionLocX = regionLocX;
  239. m_regionLocY = regionLocY;
  240. RegionSizeX = (int)Constants.RegionSize;
  241. RegionSizeY = (int)Constants.RegionSize;
  242. m_internalEndPoint = internalEndPoint;
  243. m_externalHostName = externalUri;
  244. }
  245. public GridRegion(int regionLocX, int regionLocY, string externalUri, uint port)
  246. {
  247. m_regionLocX = regionLocX;
  248. m_regionLocY = regionLocY;
  249. RegionSizeX = (int)Constants.RegionSize;
  250. RegionSizeY = (int)Constants.RegionSize;
  251. m_externalHostName = externalUri;
  252. m_internalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)port);
  253. }
  254. */
  255. public GridRegion(uint xcell, uint ycell)
  256. {
  257. m_regionLocX = (int)Util.RegionToWorldLoc(xcell);
  258. m_regionLocY = (int)Util.RegionToWorldLoc(ycell);
  259. RegionSizeX = (int)Constants.RegionSize;
  260. RegionSizeY = (int)Constants.RegionSize;
  261. }
  262. public GridRegion(RegionInfo ConvertFrom)
  263. {
  264. m_regionName = ConvertFrom.RegionName;
  265. m_regionLocX = (int)(ConvertFrom.WorldLocX);
  266. m_regionLocY = (int)(ConvertFrom.WorldLocY);
  267. RegionSizeX = (int)ConvertFrom.RegionSizeX;
  268. RegionSizeY = (int)ConvertFrom.RegionSizeY;
  269. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  270. m_externalHostName = ConvertFrom.ExternalHostName;
  271. HttpPort = ConvertFrom.HttpPort;
  272. RegionID = ConvertFrom.RegionID;
  273. ServerURI = ConvertFrom.ServerURI;
  274. TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
  275. ParcelImage = ConvertFrom.RegionSettings.ParcelImageID;
  276. Access = ConvertFrom.AccessLevel;
  277. Maturity = ConvertFrom.RegionSettings.Maturity;
  278. RegionSecret = ConvertFrom.regionSecret;
  279. EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
  280. }
  281. public GridRegion(GridRegion ConvertFrom)
  282. {
  283. m_regionName = ConvertFrom.RegionName;
  284. RegionFlags = ConvertFrom.RegionFlags;
  285. m_regionLocX = ConvertFrom.RegionLocX;
  286. m_regionLocY = ConvertFrom.RegionLocY;
  287. RegionSizeX = ConvertFrom.RegionSizeX;
  288. RegionSizeY = ConvertFrom.RegionSizeY;
  289. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  290. m_externalHostName = ConvertFrom.ExternalHostName;
  291. HttpPort = ConvertFrom.HttpPort;
  292. RegionID = ConvertFrom.RegionID;
  293. ServerURI = ConvertFrom.ServerURI;
  294. TerrainImage = ConvertFrom.TerrainImage;
  295. ParcelImage = ConvertFrom.ParcelImage;
  296. Access = ConvertFrom.Access;
  297. Maturity = ConvertFrom.Maturity;
  298. RegionSecret = ConvertFrom.RegionSecret;
  299. EstateOwner = ConvertFrom.EstateOwner;
  300. }
  301. public GridRegion(Dictionary<string, object> kvp)
  302. {
  303. if (kvp.ContainsKey("uuid"))
  304. RegionID = new UUID((string)kvp["uuid"]);
  305. if (kvp.ContainsKey("locX"))
  306. RegionLocX = Convert.ToInt32((string)kvp["locX"]);
  307. if (kvp.ContainsKey("locY"))
  308. RegionLocY = Convert.ToInt32((string)kvp["locY"]);
  309. if (kvp.ContainsKey("sizeX"))
  310. RegionSizeX = Convert.ToInt32((string)kvp["sizeX"]);
  311. else
  312. RegionSizeX = (int)Constants.RegionSize;
  313. if (kvp.ContainsKey("sizeY"))
  314. RegionSizeY = Convert.ToInt32((string)kvp["sizeY"]);
  315. else
  316. RegionSizeX = (int)Constants.RegionSize;
  317. if (kvp.ContainsKey("regionName"))
  318. RegionName = (string)kvp["regionName"];
  319. if (kvp.ContainsKey("access"))
  320. {
  321. byte access = Convert.ToByte((string)kvp["access"]);
  322. Access = access;
  323. Maturity = (int)Util.ConvertAccessLevelToMaturity(access);
  324. }
  325. if (kvp.ContainsKey("flags") && kvp["flags"] != null)
  326. RegionFlags = (OpenSim.Framework.RegionFlags?)Convert.ToInt32((string)kvp["flags"]);
  327. if (kvp.ContainsKey("serverIP"))
  328. {
  329. //int port = 0;
  330. //Int32.TryParse((string)kvp["serverPort"], out port);
  331. //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
  332. ExternalHostName = (string)kvp["serverIP"];
  333. }
  334. else
  335. ExternalHostName = "127.0.0.1";
  336. if (kvp.ContainsKey("serverPort"))
  337. {
  338. Int32 port = 0;
  339. Int32.TryParse((string)kvp["serverPort"], out port);
  340. InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  341. }
  342. if (kvp.ContainsKey("serverHttpPort"))
  343. {
  344. UInt32 port = 0;
  345. UInt32.TryParse((string)kvp["serverHttpPort"], out port);
  346. HttpPort = port;
  347. }
  348. if (kvp.ContainsKey("serverURI"))
  349. ServerURI = (string)kvp["serverURI"];
  350. if (kvp.ContainsKey("regionMapTexture"))
  351. UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
  352. if (kvp.ContainsKey("parcelMapTexture"))
  353. UUID.TryParse((string)kvp["parcelMapTexture"], out ParcelImage);
  354. if (kvp.ContainsKey("regionSecret"))
  355. RegionSecret =(string)kvp["regionSecret"];
  356. if (kvp.ContainsKey("owner_uuid"))
  357. EstateOwner = new UUID(kvp["owner_uuid"].ToString());
  358. if (kvp.ContainsKey("Token"))
  359. Token = kvp["Token"].ToString();
  360. // m_log.DebugFormat("{0} New GridRegion. id={1}, loc=<{2},{3}>, size=<{4},{5}>",
  361. // LogHeader, RegionID, RegionLocX, RegionLocY, RegionSizeX, RegionSizeY);
  362. }
  363. public Dictionary<string, object> ToKeyValuePairs()
  364. {
  365. Dictionary<string, object> kvp = new Dictionary<string, object>();
  366. kvp["uuid"] = RegionID.ToString();
  367. kvp["locX"] = RegionLocX.ToString();
  368. kvp["locY"] = RegionLocY.ToString();
  369. kvp["sizeX"] = RegionSizeX.ToString();
  370. kvp["sizeY"] = RegionSizeY.ToString();
  371. kvp["regionName"] = RegionName;
  372. if (RegionFlags != null)
  373. kvp["flags"] = ((int)RegionFlags).ToString();
  374. kvp["serverIP"] = ExternalHostName; //ExternalEndPoint.Address.ToString();
  375. kvp["serverHttpPort"] = HttpPort.ToString();
  376. kvp["serverURI"] = ServerURI;
  377. kvp["serverPort"] = InternalEndPoint.Port.ToString();
  378. kvp["regionMapTexture"] = TerrainImage.ToString();
  379. kvp["parcelMapTexture"] = ParcelImage.ToString();
  380. kvp["access"] = Access.ToString();
  381. kvp["regionSecret"] = RegionSecret;
  382. kvp["owner_uuid"] = EstateOwner.ToString();
  383. kvp["Token"] = Token.ToString();
  384. // Maturity doesn't seem to exist in the DB
  385. return kvp;
  386. }
  387. #region Definition of equality
  388. /// <summary>
  389. /// Define equality as two regions having the same, non-zero UUID.
  390. /// </summary>
  391. public bool Equals(GridRegion region)
  392. {
  393. if ((object)region == null)
  394. return false;
  395. // Return true if the non-zero UUIDs are equal:
  396. return (RegionID != UUID.Zero) && RegionID.Equals(region.RegionID);
  397. }
  398. public override bool Equals(Object obj)
  399. {
  400. if (obj == null)
  401. return false;
  402. return Equals(obj as GridRegion);
  403. }
  404. public override int GetHashCode()
  405. {
  406. return RegionID.GetHashCode() ^ TerrainImage.GetHashCode() ^ ParcelImage.GetHashCode();
  407. }
  408. #endregion
  409. /// <value>
  410. /// This accessor can throw all the exceptions that Dns.GetHostAddresses can throw.
  411. ///
  412. /// XXX Isn't this really doing too much to be a simple getter, rather than an explict method?
  413. /// </value>
  414. public IPEndPoint ExternalEndPoint
  415. {
  416. get
  417. {
  418. // Old one defaults to IPv6
  419. //return new IPEndPoint(Dns.GetHostAddresses(m_externalHostName)[0], m_internalEndPoint.Port);
  420. IPAddress ia = null;
  421. // If it is already an IP, don't resolve it - just return directly
  422. if (IPAddress.TryParse(m_externalHostName, out ia))
  423. return new IPEndPoint(ia, m_internalEndPoint.Port);
  424. // Reset for next check
  425. ia = null;
  426. try
  427. {
  428. foreach (IPAddress Adr in Dns.GetHostAddresses(m_externalHostName))
  429. {
  430. if (ia == null)
  431. ia = Adr;
  432. if (Adr.AddressFamily == AddressFamily.InterNetwork)
  433. {
  434. ia = Adr;
  435. break;
  436. }
  437. }
  438. }
  439. catch (SocketException e)
  440. {
  441. /*throw new Exception(
  442. "Unable to resolve local hostname " + m_externalHostName + " innerException of type '" +
  443. e + "' attached to this exception", e);*/
  444. // Don't throw a fatal exception here, instead, return Null and handle it in the caller.
  445. // Reason is, on systems such as OSgrid it has occured that known hostnames stop
  446. // resolving and thus make surrounding regions crash out with this exception.
  447. return null;
  448. }
  449. return new IPEndPoint(ia, m_internalEndPoint.Port);
  450. }
  451. }
  452. public string ExternalHostName
  453. {
  454. get { return m_externalHostName; }
  455. set { m_externalHostName = value; }
  456. }
  457. public IPEndPoint InternalEndPoint
  458. {
  459. get { return m_internalEndPoint; }
  460. set { m_internalEndPoint = value; }
  461. }
  462. public ulong RegionHandle
  463. {
  464. get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
  465. }
  466. }
  467. }