IGridService.cs 16 KB

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