IGridService.cs 14 KB

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