IGridService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. /// <summary>
  80. /// Get information about regions starting with the provided name.
  81. /// </summary>
  82. /// <param name="name">
  83. /// The name to match against.
  84. /// </param>
  85. /// <param name="maxNumber">
  86. /// The maximum number of results to return.
  87. /// </param>
  88. /// <returns>
  89. /// A list of <see cref="RegionInfo"/>s of regions with matching name. If the
  90. /// grid-server couldn't be contacted or returned an error, return null.
  91. /// </returns>
  92. List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber);
  93. List<GridRegion> GetRegionsByURI(UUID scopeID, RegionURI uri, int maxNumber);
  94. List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax);
  95. List<GridRegion> GetDefaultRegions(UUID scopeID);
  96. List<GridRegion> GetDefaultHypergridRegions(UUID scopeID);
  97. List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y);
  98. List<GridRegion> GetHyperlinks(UUID scopeID);
  99. /// <summary>
  100. /// Get internal OpenSimulator region flags.
  101. /// </summary>
  102. /// <remarks>
  103. /// See OpenSimulator.Framework.RegionFlags. These are not returned in the GridRegion structure -
  104. /// they currently need to be requested separately. Possibly this should change to avoid multiple service calls
  105. /// in some situations.
  106. /// </remarks>
  107. /// <returns>
  108. /// The region flags.
  109. /// </returns>
  110. /// <param name='scopeID'></param>
  111. /// <param name='regionID'></param>
  112. int GetRegionFlags(UUID scopeID, UUID regionID);
  113. Dictionary<string,object> GetExtraFeatures();
  114. }
  115. public interface IHypergridLinker
  116. {
  117. GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason);
  118. bool TryUnlinkRegion(string mapName);
  119. }
  120. public class GridRegion
  121. {
  122. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  123. #pragma warning disable 414
  124. private static readonly string LogHeader = "[GRID REGION]";
  125. #pragma warning restore 414
  126. /// <summary>
  127. /// The port by which http communication occurs with the region
  128. /// </summary>
  129. public uint HttpPort { get; set; }
  130. /// <summary>
  131. /// A well-formed URI for the host region server (namely "http://" + ExternalHostName)
  132. /// </summary>
  133. public string ServerURI
  134. {
  135. get {
  136. if (!String.IsNullOrEmpty(m_serverURI)) {
  137. return m_serverURI;
  138. } else {
  139. if (HttpPort == 0)
  140. return "http://" + m_externalHostName + "/";
  141. else
  142. return "http://" + m_externalHostName + ":" + HttpPort + "/";
  143. }
  144. }
  145. set {
  146. if ( value == null)
  147. {
  148. m_serverURI = String.Empty;
  149. return;
  150. }
  151. if ( value.EndsWith("/") )
  152. {
  153. m_serverURI = value;
  154. }
  155. else
  156. {
  157. m_serverURI = value + '/';
  158. }
  159. }
  160. }
  161. protected string m_serverURI;
  162. /// <summary>
  163. /// Provides direct access to the 'm_serverURI' field, without returning a generated URL if m_serverURI is missing.
  164. /// </summary>
  165. public string RawServerURI
  166. {
  167. get { return m_serverURI; }
  168. set { m_serverURI = value; }
  169. }
  170. public string RegionName
  171. {
  172. get { return m_regionName; }
  173. set { m_regionName = value; }
  174. }
  175. protected string m_regionName = String.Empty;
  176. /// <summary>
  177. /// Region flags.
  178. /// </summary>
  179. /// <remarks>
  180. /// If not set (chiefly if a robust service is running code pre OpenSim 0.8.1) then this will be null and
  181. /// should be ignored. If you require flags information please use the separate IGridService.GetRegionFlags() call
  182. /// XXX: This field is currently ignored when used in RegisterRegion, but could potentially be
  183. /// used to set flags at this point.
  184. /// </remarks>
  185. public OpenSim.Framework.RegionFlags? RegionFlags { get; set; }
  186. protected string m_externalHostName;
  187. protected IPEndPoint m_internalEndPoint;
  188. /// <summary>
  189. /// The co-ordinate of this region in region units.
  190. /// </summary>
  191. public int RegionCoordX { get { return (int)Util.WorldToRegionLoc((uint)RegionLocX); } }
  192. /// <summary>
  193. /// The co-ordinate of this region in region units
  194. /// </summary>
  195. public int RegionCoordY { get { return (int)Util.WorldToRegionLoc((uint)RegionLocY); } }
  196. /// <summary>
  197. /// The location of this region in meters.
  198. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  199. /// </summary>
  200. public int RegionLocX
  201. {
  202. get { return m_regionLocX; }
  203. set { m_regionLocX = value; }
  204. }
  205. protected int m_regionLocX;
  206. public int RegionSizeX { get; set; }
  207. public int RegionSizeY { get; set; }
  208. /// <summary>
  209. /// The location of this region in meters.
  210. /// DANGER DANGER! Note that this name means something different in RegionInfo.
  211. /// </summary>
  212. public int RegionLocY
  213. {
  214. get { return m_regionLocY; }
  215. set { m_regionLocY = value; }
  216. }
  217. protected int m_regionLocY;
  218. protected UUID m_estateOwner;
  219. public UUID EstateOwner
  220. {
  221. get { return m_estateOwner; }
  222. set { m_estateOwner = value; }
  223. }
  224. public UUID RegionID = UUID.Zero;
  225. public UUID ScopeID = UUID.Zero;
  226. public UUID TerrainImage = UUID.Zero;
  227. public UUID ParcelImage = UUID.Zero;
  228. public byte Access;
  229. public int Maturity;
  230. public string RegionSecret = string.Empty;
  231. public string Token = string.Empty;
  232. public GridRegion()
  233. {
  234. RegionSizeX = (int)Constants.RegionSize;
  235. RegionSizeY = (int)Constants.RegionSize;
  236. m_serverURI = string.Empty;
  237. }
  238. public GridRegion(uint xcell, uint ycell)
  239. {
  240. m_regionLocX = (int)Util.RegionToWorldLoc(xcell);
  241. m_regionLocY = (int)Util.RegionToWorldLoc(ycell);
  242. RegionSizeX = (int)Constants.RegionSize;
  243. RegionSizeY = (int)Constants.RegionSize;
  244. }
  245. public GridRegion(RegionInfo ConvertFrom)
  246. {
  247. m_regionName = ConvertFrom.RegionName;
  248. m_regionLocX = (int)(ConvertFrom.WorldLocX);
  249. m_regionLocY = (int)(ConvertFrom.WorldLocY);
  250. RegionSizeX = (int)ConvertFrom.RegionSizeX;
  251. RegionSizeY = (int)ConvertFrom.RegionSizeY;
  252. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  253. m_externalHostName = ConvertFrom.ExternalHostName;
  254. HttpPort = ConvertFrom.HttpPort;
  255. RegionID = ConvertFrom.RegionID;
  256. ServerURI = ConvertFrom.ServerURI;
  257. TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
  258. ParcelImage = ConvertFrom.RegionSettings.ParcelImageID;
  259. Access = ConvertFrom.AccessLevel;
  260. Maturity = ConvertFrom.RegionSettings.Maturity;
  261. RegionSecret = ConvertFrom.regionSecret;
  262. EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
  263. }
  264. public GridRegion(GridRegion ConvertFrom)
  265. {
  266. m_regionName = ConvertFrom.RegionName;
  267. RegionFlags = ConvertFrom.RegionFlags;
  268. m_regionLocX = ConvertFrom.RegionLocX;
  269. m_regionLocY = ConvertFrom.RegionLocY;
  270. RegionSizeX = ConvertFrom.RegionSizeX;
  271. RegionSizeY = ConvertFrom.RegionSizeY;
  272. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  273. m_externalHostName = ConvertFrom.ExternalHostName;
  274. HttpPort = ConvertFrom.HttpPort;
  275. RegionID = ConvertFrom.RegionID;
  276. ServerURI = ConvertFrom.ServerURI;
  277. TerrainImage = ConvertFrom.TerrainImage;
  278. ParcelImage = ConvertFrom.ParcelImage;
  279. Access = ConvertFrom.Access;
  280. Maturity = ConvertFrom.Maturity;
  281. RegionSecret = ConvertFrom.RegionSecret;
  282. EstateOwner = ConvertFrom.EstateOwner;
  283. }
  284. public GridRegion(Dictionary<string, object> kvp)
  285. {
  286. if (kvp.ContainsKey("uuid"))
  287. RegionID = new UUID((string)kvp["uuid"]);
  288. if (kvp.ContainsKey("locX"))
  289. RegionLocX = Convert.ToInt32((string)kvp["locX"]);
  290. if (kvp.ContainsKey("locY"))
  291. RegionLocY = Convert.ToInt32((string)kvp["locY"]);
  292. if (kvp.ContainsKey("sizeX"))
  293. RegionSizeX = Convert.ToInt32((string)kvp["sizeX"]);
  294. else
  295. RegionSizeX = (int)Constants.RegionSize;
  296. if (kvp.ContainsKey("sizeY"))
  297. RegionSizeY = Convert.ToInt32((string)kvp["sizeY"]);
  298. else
  299. RegionSizeX = (int)Constants.RegionSize;
  300. if (kvp.ContainsKey("regionName"))
  301. RegionName = (string)kvp["regionName"];
  302. if (kvp.ContainsKey("access"))
  303. {
  304. byte access = Convert.ToByte((string)kvp["access"]);
  305. Access = access;
  306. Maturity = (int)Util.ConvertAccessLevelToMaturity(access);
  307. }
  308. if (kvp.ContainsKey("flags") && kvp["flags"] != null)
  309. RegionFlags = (OpenSim.Framework.RegionFlags?)Convert.ToInt32((string)kvp["flags"]);
  310. if (kvp.ContainsKey("serverIP"))
  311. {
  312. //int port = 0;
  313. //Int32.TryParse((string)kvp["serverPort"], out port);
  314. //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
  315. ExternalHostName = (string)kvp["serverIP"];
  316. }
  317. else
  318. ExternalHostName = "127.0.0.1";
  319. if (kvp.ContainsKey("serverPort"))
  320. {
  321. Int32 port = 0;
  322. Int32.TryParse((string)kvp["serverPort"], out port);
  323. InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  324. }
  325. if (kvp.ContainsKey("serverHttpPort"))
  326. {
  327. UInt32 port = 0;
  328. UInt32.TryParse((string)kvp["serverHttpPort"], out port);
  329. HttpPort = port;
  330. }
  331. if (kvp.ContainsKey("serverURI"))
  332. ServerURI = (string)kvp["serverURI"];
  333. if (kvp.ContainsKey("regionMapTexture"))
  334. UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
  335. if (kvp.ContainsKey("parcelMapTexture"))
  336. UUID.TryParse((string)kvp["parcelMapTexture"], out ParcelImage);
  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 { return Util.getEndPoint(m_externalHostName, m_internalEndPoint.Port); }
  400. }
  401. public string ExternalHostName
  402. {
  403. get { return m_externalHostName; }
  404. set { m_externalHostName = value; }
  405. }
  406. public IPEndPoint InternalEndPoint
  407. {
  408. get { return m_internalEndPoint; }
  409. set { m_internalEndPoint = value; }
  410. }
  411. public ulong RegionHandle
  412. {
  413. get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
  414. }
  415. }
  416. }