OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
IGridService.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Net;
32 using System.Net.Sockets;
33 using System.Reflection;
34 
35 using OpenSim.Framework;
36 using OpenMetaverse;
37 
38 using log4net;
39 
40 namespace OpenSim.Services.Interfaces
41 {
42  public interface IGridService
43  {
50  string RegisterRegion(UUID scopeID, GridRegion regionInfos);
51 
58  bool DeregisterRegion(UUID regionID);
59 
66  List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID);
67 
68  GridRegion GetRegionByUUID(UUID scopeID, UUID regionID);
69 
77  GridRegion GetRegionByPosition(UUID scopeID, int x, int y);
78 
85  GridRegion GetRegionByName(UUID scopeID, string regionName);
86 
100  List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber);
101 
102  List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax);
103 
104  List<GridRegion> GetDefaultRegions(UUID scopeID);
105  List<GridRegion> GetDefaultHypergridRegions(UUID scopeID);
106  List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y);
107  List<GridRegion> GetHyperlinks(UUID scopeID);
108 
122  int GetRegionFlags(UUID scopeID, UUID regionID);
123 
124  Dictionary<string,object> GetExtraFeatures();
125  }
126 
127  public interface IHypergridLinker
128  {
129  GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason);
130  bool TryUnlinkRegion(string mapName);
131  }
132 
133  public class GridRegion
134  {
135 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
136 
137 #pragma warning disable 414
138  private static readonly string LogHeader = "[GRID REGION]";
139 #pragma warning restore 414
140 
144  public uint HttpPort { get; set; }
145 
149  public string ServerURI
150  {
151  get {
152  if (!String.IsNullOrEmpty(m_serverURI)) {
153  return m_serverURI;
154  } else {
155  if (HttpPort == 0)
156  return "http://" + m_externalHostName + "/";
157  else
158  return "http://" + m_externalHostName + ":" + HttpPort + "/";
159  }
160  }
161  set {
162  if ( value == null)
163  {
164  m_serverURI = String.Empty;
165  return;
166  }
167 
168  if ( value.EndsWith("/") )
169  {
170 
171  m_serverURI = value;
172  }
173  else
174  {
175  m_serverURI = value + '/';
176  }
177  }
178  }
179 
180  protected string m_serverURI;
181 
185  public string RawServerURI
186  {
187  get { return m_serverURI; }
188  set { m_serverURI = value; }
189  }
190 
191 
192  public string RegionName
193  {
194  get { return m_regionName; }
195  set { m_regionName = value; }
196  }
197  protected string m_regionName = String.Empty;
198 
209 
210  protected string m_externalHostName;
211 
212  protected IPEndPoint m_internalEndPoint;
213 
217  public int RegionCoordX { get { return (int)Util.WorldToRegionLoc((uint)RegionLocX); } }
218 
222  public int RegionCoordY { get { return (int)Util.WorldToRegionLoc((uint)RegionLocY); } }
223 
228  public int RegionLocX
229  {
230  get { return m_regionLocX; }
231  set { m_regionLocX = value; }
232  }
233  protected int m_regionLocX;
234 
235  public int RegionSizeX { get; set; }
236  public int RegionSizeY { get; set; }
237 
242  public int RegionLocY
243  {
244  get { return m_regionLocY; }
245  set { m_regionLocY = value; }
246  }
247  protected int m_regionLocY;
248 
249  protected UUID m_estateOwner;
250 
251  public UUID EstateOwner
252  {
253  get { return m_estateOwner; }
254  set { m_estateOwner = value; }
255  }
256 
257  public UUID RegionID = UUID.Zero;
258  public UUID ScopeID = UUID.Zero;
259 
260  public UUID TerrainImage = UUID.Zero;
261  public UUID ParcelImage = UUID.Zero;
262  public byte Access;
263  public int Maturity;
264  public string RegionSecret = string.Empty;
265  public string Token = string.Empty;
266 
267  public GridRegion()
268  {
269  RegionSizeX = (int)Constants.RegionSize;
270  RegionSizeY = (int)Constants.RegionSize;
271  m_serverURI = string.Empty;
272  }
273 
274  /*
275  public GridRegion(int regionLocX, int regionLocY, IPEndPoint internalEndPoint, string externalUri)
276  {
277  m_regionLocX = regionLocX;
278  m_regionLocY = regionLocY;
279  RegionSizeX = (int)Constants.RegionSize;
280  RegionSizeY = (int)Constants.RegionSize;
281 
282  m_internalEndPoint = internalEndPoint;
283  m_externalHostName = externalUri;
284  }
285 
286  public GridRegion(int regionLocX, int regionLocY, string externalUri, uint port)
287  {
288  m_regionLocX = regionLocX;
289  m_regionLocY = regionLocY;
290  RegionSizeX = (int)Constants.RegionSize;
291  RegionSizeY = (int)Constants.RegionSize;
292 
293  m_externalHostName = externalUri;
294 
295  m_internalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)port);
296  }
297  */
298 
299  public GridRegion(uint xcell, uint ycell)
300  {
301  m_regionLocX = (int)Util.RegionToWorldLoc(xcell);
302  m_regionLocY = (int)Util.RegionToWorldLoc(ycell);
303  RegionSizeX = (int)Constants.RegionSize;
304  RegionSizeY = (int)Constants.RegionSize;
305  }
306 
307  public GridRegion(RegionInfo ConvertFrom)
308  {
309  m_regionName = ConvertFrom.RegionName;
310  m_regionLocX = (int)(ConvertFrom.WorldLocX);
311  m_regionLocY = (int)(ConvertFrom.WorldLocY);
312  RegionSizeX = (int)ConvertFrom.RegionSizeX;
313  RegionSizeY = (int)ConvertFrom.RegionSizeY;
314  m_internalEndPoint = ConvertFrom.InternalEndPoint;
315  m_externalHostName = ConvertFrom.ExternalHostName;
316  HttpPort = ConvertFrom.HttpPort;
317  RegionID = ConvertFrom.RegionID;
318  ServerURI = ConvertFrom.ServerURI;
319  TerrainImage = ConvertFrom.RegionSettings.TerrainImageID;
320  ParcelImage = ConvertFrom.RegionSettings.ParcelImageID;
321  Access = ConvertFrom.AccessLevel;
322  Maturity = ConvertFrom.RegionSettings.Maturity;
323  RegionSecret = ConvertFrom.regionSecret;
324  EstateOwner = ConvertFrom.EstateSettings.EstateOwner;
325  }
326 
327  public GridRegion(GridRegion ConvertFrom)
328  {
329  m_regionName = ConvertFrom.RegionName;
331  m_regionLocX = ConvertFrom.RegionLocX;
332  m_regionLocY = ConvertFrom.RegionLocY;
333  RegionSizeX = ConvertFrom.RegionSizeX;
334  RegionSizeY = ConvertFrom.RegionSizeY;
335  m_internalEndPoint = ConvertFrom.InternalEndPoint;
336  m_externalHostName = ConvertFrom.ExternalHostName;
337  HttpPort = ConvertFrom.HttpPort;
338  RegionID = ConvertFrom.RegionID;
339  ServerURI = ConvertFrom.ServerURI;
340  TerrainImage = ConvertFrom.TerrainImage;
341  ParcelImage = ConvertFrom.ParcelImage;
342  Access = ConvertFrom.Access;
343  Maturity = ConvertFrom.Maturity;
344  RegionSecret = ConvertFrom.RegionSecret;
345  EstateOwner = ConvertFrom.EstateOwner;
346  }
347 
348  public GridRegion(Dictionary<string, object> kvp)
349  {
350  if (kvp.ContainsKey("uuid"))
351  RegionID = new UUID((string)kvp["uuid"]);
352 
353  if (kvp.ContainsKey("locX"))
354  RegionLocX = Convert.ToInt32((string)kvp["locX"]);
355 
356  if (kvp.ContainsKey("locY"))
357  RegionLocY = Convert.ToInt32((string)kvp["locY"]);
358 
359  if (kvp.ContainsKey("sizeX"))
360  RegionSizeX = Convert.ToInt32((string)kvp["sizeX"]);
361  else
362  RegionSizeX = (int)Constants.RegionSize;
363 
364  if (kvp.ContainsKey("sizeY"))
365  RegionSizeY = Convert.ToInt32((string)kvp["sizeY"]);
366  else
367  RegionSizeX = (int)Constants.RegionSize;
368 
369  if (kvp.ContainsKey("regionName"))
370  RegionName = (string)kvp["regionName"];
371 
372  if (kvp.ContainsKey("access"))
373  {
374  byte access = Convert.ToByte((string)kvp["access"]);
375  Access = access;
376  Maturity = (int)Util.ConvertAccessLevelToMaturity(access);
377  }
378 
379  if (kvp.ContainsKey("flags") && kvp["flags"] != null)
380  RegionFlags = (OpenSim.Framework.RegionFlags?)Convert.ToInt32((string)kvp["flags"]);
381 
382  if (kvp.ContainsKey("serverIP"))
383  {
384  //int port = 0;
385  //Int32.TryParse((string)kvp["serverPort"], out port);
386  //IPEndPoint ep = new IPEndPoint(IPAddress.Parse((string)kvp["serverIP"]), port);
387  ExternalHostName = (string)kvp["serverIP"];
388  }
389  else
390  ExternalHostName = "127.0.0.1";
391 
392  if (kvp.ContainsKey("serverPort"))
393  {
394  Int32 port = 0;
395  Int32.TryParse((string)kvp["serverPort"], out port);
396  InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), port);
397  }
398 
399  if (kvp.ContainsKey("serverHttpPort"))
400  {
401  UInt32 port = 0;
402  UInt32.TryParse((string)kvp["serverHttpPort"], out port);
403  HttpPort = port;
404  }
405 
406  if (kvp.ContainsKey("serverURI"))
407  ServerURI = (string)kvp["serverURI"];
408 
409  if (kvp.ContainsKey("regionMapTexture"))
410  UUID.TryParse((string)kvp["regionMapTexture"], out TerrainImage);
411 
412  if (kvp.ContainsKey("parcelMapTexture"))
413  UUID.TryParse((string)kvp["parcelMapTexture"], out ParcelImage);
414 
415  if (kvp.ContainsKey("regionSecret"))
416  RegionSecret =(string)kvp["regionSecret"];
417 
418  if (kvp.ContainsKey("owner_uuid"))
419  EstateOwner = new UUID(kvp["owner_uuid"].ToString());
420 
421  if (kvp.ContainsKey("Token"))
422  Token = kvp["Token"].ToString();
423 
424  // m_log.DebugFormat("{0} New GridRegion. id={1}, loc=<{2},{3}>, size=<{4},{5}>",
425  // LogHeader, RegionID, RegionLocX, RegionLocY, RegionSizeX, RegionSizeY);
426  }
427 
428  public Dictionary<string, object> ToKeyValuePairs()
429  {
430  Dictionary<string, object> kvp = new Dictionary<string, object>();
431  kvp["uuid"] = RegionID.ToString();
432  kvp["locX"] = RegionLocX.ToString();
433  kvp["locY"] = RegionLocY.ToString();
434  kvp["sizeX"] = RegionSizeX.ToString();
435  kvp["sizeY"] = RegionSizeY.ToString();
436  kvp["regionName"] = RegionName;
437 
438  if (RegionFlags != null)
439  kvp["flags"] = ((int)RegionFlags).ToString();
440 
441  kvp["serverIP"] = ExternalHostName; //ExternalEndPoint.Address.ToString();
442  kvp["serverHttpPort"] = HttpPort.ToString();
443  kvp["serverURI"] = ServerURI;
444  kvp["serverPort"] = InternalEndPoint.Port.ToString();
445  kvp["regionMapTexture"] = TerrainImage.ToString();
446  kvp["parcelMapTexture"] = ParcelImage.ToString();
447  kvp["access"] = Access.ToString();
448  kvp["regionSecret"] = RegionSecret;
449  kvp["owner_uuid"] = EstateOwner.ToString();
450  kvp["Token"] = Token.ToString();
451  // Maturity doesn't seem to exist in the DB
452 
453  return kvp;
454  }
455 
456  #region Definition of equality
457 
461  public bool Equals(GridRegion region)
462  {
463  if ((object)region == null)
464  return false;
465  // Return true if the non-zero UUIDs are equal:
466  return (RegionID != UUID.Zero) && RegionID.Equals(region.RegionID);
467  }
468 
469  public override bool Equals(Object obj)
470  {
471  if (obj == null)
472  return false;
473  return Equals(obj as GridRegion);
474  }
475 
476  public override int GetHashCode()
477  {
478  return RegionID.GetHashCode() ^ TerrainImage.GetHashCode() ^ ParcelImage.GetHashCode();
479  }
480 
481  #endregion
482 
488  public IPEndPoint ExternalEndPoint
489  {
490  get
491  {
492  // Old one defaults to IPv6
493  //return new IPEndPoint(Dns.GetHostAddresses(m_externalHostName)[0], m_internalEndPoint.Port);
494 
495  IPAddress ia = null;
496  // If it is already an IP, don't resolve it - just return directly
497  if (IPAddress.TryParse(m_externalHostName, out ia))
498  return new IPEndPoint(ia, m_internalEndPoint.Port);
499 
500  // Reset for next check
501  ia = null;
502  try
503  {
504  foreach (IPAddress Adr in Dns.GetHostAddresses(m_externalHostName))
505  {
506  if (ia == null)
507  ia = Adr;
508 
509  if (Adr.AddressFamily == AddressFamily.InterNetwork)
510  {
511  ia = Adr;
512  break;
513  }
514  }
515  }
516  catch (SocketException e)
517  {
518  /*throw new Exception(
519  "Unable to resolve local hostname " + m_externalHostName + " innerException of type '" +
520  e + "' attached to this exception", e);*/
521  // Don't throw a fatal exception here, instead, return Null and handle it in the caller.
522  // Reason is, on systems such as OSgrid it has occured that known hostnames stop
523  // resolving and thus make surrounding regions crash out with this exception.
524  return null;
525  }
526 
527  return new IPEndPoint(ia, m_internalEndPoint.Port);
528  }
529  }
530 
531  public string ExternalHostName
532  {
533  get { return m_externalHostName; }
534  set { m_externalHostName = value; }
535  }
536 
537  public IPEndPoint InternalEndPoint
538  {
539  get { return m_internalEndPoint; }
540  set { m_internalEndPoint = value; }
541  }
542 
543  public ulong RegionHandle
544  {
545  get { return Util.UIntsToLong((uint)RegionLocX, (uint)RegionLocY); }
546  }
547  }
548 }
Dictionary< string, object > ToKeyValuePairs()
override bool Equals(Object obj)
uint RegionSizeX
X dimension of the region.
Definition: RegionInfo.cs:161
GridRegion(RegionInfo ConvertFrom)
bool Equals(GridRegion region)
Define equality as two regions having the same, non-zero UUID.
GridRegion(GridRegion ConvertFrom)
GridRegion(Dictionary< string, object > kvp)
RegionFlags
Region flags used internally by OpenSimulator to store installation specific information about region...
Definition: RegionFlags.cs:40
Interactive OpenSim region server
Definition: OpenSim.cs:55
GridRegion(uint xcell, uint ycell)