1
0

IGridService.cs 16 KB

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