IGridService.cs 16 KB

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