1
0

IGridService.cs 11 KB

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