1
0

IGridService.cs 18 KB

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