1
0

IGridService.cs 15 KB

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