IGridService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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
  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 { return m_serverURI; }
  105. set { m_serverURI = value; }
  106. }
  107. protected string m_serverURI;
  108. public string RegionName
  109. {
  110. get { return m_regionName; }
  111. set { m_regionName = value; }
  112. }
  113. protected string m_regionName = String.Empty;
  114. protected string m_externalHostName;
  115. protected IPEndPoint m_internalEndPoint;
  116. public int RegionLocX
  117. {
  118. get { return m_regionLocX; }
  119. set { m_regionLocX = value; }
  120. }
  121. protected int m_regionLocX;
  122. public int RegionLocY
  123. {
  124. get { return m_regionLocY; }
  125. set { m_regionLocY = value; }
  126. }
  127. protected int m_regionLocY;
  128. protected UUID m_estateOwner;
  129. public UUID EstateOwner
  130. {
  131. get { return m_estateOwner; }
  132. set { m_estateOwner = value; }
  133. }
  134. public UUID RegionID = UUID.Zero;
  135. public UUID ScopeID = UUID.Zero;
  136. public UUID TerrainImage = UUID.Zero;
  137. public byte Access;
  138. public int Maturity;
  139. public string RegionSecret = string.Empty;
  140. public string Token = string.Empty;
  141. public GridRegion()
  142. {
  143. }
  144. public GridRegion(int regionLocX, int regionLocY, IPEndPoint internalEndPoint, string externalUri)
  145. {
  146. m_regionLocX = regionLocX;
  147. m_regionLocY = regionLocY;
  148. m_internalEndPoint = internalEndPoint;
  149. m_externalHostName = externalUri;
  150. }
  151. public GridRegion(int regionLocX, int regionLocY, string externalUri, uint port)
  152. {
  153. m_regionLocX = regionLocX;
  154. m_regionLocY = regionLocY;
  155. m_externalHostName = externalUri;
  156. m_internalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)port);
  157. }
  158. public GridRegion(uint xcell, uint ycell)
  159. {
  160. m_regionLocX = (int)(xcell * Constants.RegionSize);
  161. m_regionLocY = (int)(ycell * Constants.RegionSize);
  162. }
  163. public GridRegion(RegionInfo ConvertFrom)
  164. {
  165. m_regionName = ConvertFrom.RegionName;
  166. m_regionLocX = (int)(ConvertFrom.RegionLocX * Constants.RegionSize);
  167. m_regionLocY = (int)(ConvertFrom.RegionLocY * Constants.RegionSize);
  168. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  169. m_externalHostName = ConvertFrom.ExternalHostName;
  170. m_httpPort = ConvertFrom.HttpPort;
  171. RegionID = ConvertFrom.RegionID;
  172. ServerURI = ConvertFrom.ServerURI;
  173. TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
  174. Access = ConvertFrom.AccessLevel;
  175. Maturity = ConvertFrom.RegionSettings.Maturity;
  176. RegionSecret = ConvertFrom.regionSecret;
  177. EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
  178. }
  179. public GridRegion(GridRegion ConvertFrom)
  180. {
  181. m_regionName = ConvertFrom.RegionName;
  182. m_regionLocX = ConvertFrom.RegionLocX;
  183. m_regionLocY = ConvertFrom.RegionLocY;
  184. m_internalEndPoint = ConvertFrom.InternalEndPoint;
  185. m_externalHostName = ConvertFrom.ExternalHostName;
  186. m_httpPort = ConvertFrom.HttpPort;
  187. RegionID = ConvertFrom.RegionID;
  188. ServerURI = ConvertFrom.ServerURI;
  189. TerrainImage = ConvertFrom.TerrainImage;
  190. Access = ConvertFrom.Access;
  191. Maturity = ConvertFrom.Maturity;
  192. RegionSecret = ConvertFrom.RegionSecret;
  193. EstateOwner = ConvertFrom.EstateOwner;
  194. }
  195. /// <value>
  196. /// This accessor can throw all the exceptions that Dns.GetHostAddresses can throw.
  197. ///
  198. /// XXX Isn't this really doing too much to be a simple getter, rather than an explict method?
  199. /// </value>
  200. public IPEndPoint ExternalEndPoint
  201. {
  202. get
  203. {
  204. // Old one defaults to IPv6
  205. //return new IPEndPoint(Dns.GetHostAddresses(m_externalHostName)[0], m_internalEndPoint.Port);
  206. IPAddress ia = null;
  207. // If it is already an IP, don't resolve it - just return directly
  208. if (IPAddress.TryParse(m_externalHostName, out ia))
  209. return new IPEndPoint(ia, m_internalEndPoint.Port);
  210. // Reset for next check
  211. ia = null;
  212. try
  213. {
  214. foreach (IPAddress Adr in Dns.GetHostAddresses(m_externalHostName))
  215. {
  216. if (ia == null)
  217. ia = Adr;
  218. if (Adr.AddressFamily == AddressFamily.InterNetwork)
  219. {
  220. ia = Adr;
  221. break;
  222. }
  223. }
  224. }
  225. catch (SocketException e)
  226. {
  227. throw new Exception(
  228. "Unable to resolve local hostname " + m_externalHostName + " innerException of type '" +
  229. e + "' attached to this exception", e);
  230. }
  231. return new IPEndPoint(ia, m_internalEndPoint.Port);
  232. }
  233. }
  234. public string ExternalHostName
  235. {
  236. get { return m_externalHostName; }
  237. set { m_externalHostName = value; }
  238. }
  239. public IPEndPoint InternalEndPoint
  240. {
  241. get { return m_internalEndPoint; }
  242. set { m_internalEndPoint = value; }
  243. }
  244. public ulong RegionHandle
  245. {
  246. get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
  247. }
  248. public Dictionary<string, object> ToKeyValuePairs()
  249. {
  250. Dictionary<string, object> kvp = new Dictionary<string, object>();
  251. kvp["uuid"] = RegionID.ToString();
  252. kvp["locX"] = RegionLocX.ToString();
  253. kvp["locY"] = RegionLocY.ToString();
  254. kvp["regionName"] = RegionName;
  255. kvp["serverIP"] = ExternalHostName; //ExternalEndPoint.Address.ToString();
  256. kvp["serverHttpPort"] = HttpPort.ToString();
  257. kvp["serverURI"] = ServerURI;
  258. kvp["serverPort"] = InternalEndPoint.Port.ToString();
  259. kvp["regionMapTexture"] = TerrainImage.ToString();
  260. kvp["access"] = Access.ToString();
  261. kvp["regionSecret"] = RegionSecret;
  262. kvp["owner_uuid"] = EstateOwner.ToString();
  263. kvp["Token"] = Token.ToString();
  264. // Maturity doesn't seem to exist in the DB
  265. return kvp;
  266. }
  267. public GridRegion(Dictionary<string, object> kvp)
  268. {
  269. if (kvp.ContainsKey("uuid"))
  270. RegionID = new UUID((string)kvp["uuid"]);
  271. if (kvp.ContainsKey("locX"))
  272. RegionLocX = Convert.ToInt32((string)kvp["locX"]);
  273. if (kvp.ContainsKey("locY"))
  274. RegionLocY = Convert.ToInt32((string)kvp["locY"]);
  275. if (kvp.ContainsKey("regionName"))
  276. RegionName = (string)kvp["regionName"];
  277. if (kvp.ContainsKey("serverIP"))
  278. {
  279. //int port = 0;
  280. //Int32.TryParse((string)kvp["serverPort"], out port);
  281. //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
  282. ExternalHostName = (string)kvp["serverIP"];
  283. }
  284. else
  285. ExternalHostName = "127.0.0.1";
  286. if (kvp.ContainsKey("serverPort"))
  287. {
  288. Int32 port = 0;
  289. Int32.TryParse((string)kvp["serverPort"], out port);
  290. InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
  291. }
  292. if (kvp.ContainsKey("serverHttpPort"))
  293. {
  294. UInt32 port = 0;
  295. UInt32.TryParse((string)kvp["serverHttpPort"], out port);
  296. HttpPort = port;
  297. }
  298. if (kvp.ContainsKey("serverURI"))
  299. ServerURI = (string)kvp["serverURI"];
  300. if (kvp.ContainsKey("regionMapTexture"))
  301. UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
  302. if (kvp.ContainsKey("access"))
  303. Access = Byte.Parse((string)kvp["access"]);
  304. if (kvp.ContainsKey("regionSecret"))
  305. RegionSecret =(string)kvp["regionSecret"];
  306. if (kvp.ContainsKey("owner_uuid"))
  307. EstateOwner = new UUID(kvp["owner_uuid"].ToString());
  308. if (kvp.ContainsKey("Token"))
  309. Token = kvp["Token"].ToString();
  310. }
  311. }
  312. }