IGridService.cs 13 KB

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