IGridService.cs 18 KB

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