1
0

IGridService.cs 18 KB

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