IGridService.cs 16 KB

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