aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/GridService
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Services/GridService')
-rw-r--r--OpenSim/Services/GridService/GridService.cs285
-rw-r--r--OpenSim/Services/GridService/HypergridLinker.cs634
2 files changed, 917 insertions, 2 deletions
diff --git a/OpenSim/Services/GridService/GridService.cs b/OpenSim/Services/GridService/GridService.cs
index 7749c37..1368e46 100644
--- a/OpenSim/Services/GridService/GridService.cs
+++ b/OpenSim/Services/GridService/GridService.cs
@@ -34,6 +34,7 @@ using log4net;
34using OpenSim.Framework; 34using OpenSim.Framework;
35using OpenSim.Framework.Console; 35using OpenSim.Framework.Console;
36using OpenSim.Data; 36using OpenSim.Data;
37using OpenSim.Server.Base;
37using OpenSim.Services.Interfaces; 38using OpenSim.Services.Interfaces;
38using GridRegion = OpenSim.Services.Interfaces.GridRegion; 39using GridRegion = OpenSim.Services.Interfaces.GridRegion;
39using OpenMetaverse; 40using OpenMetaverse;
@@ -46,17 +47,58 @@ namespace OpenSim.Services.GridService
46 LogManager.GetLogger( 47 LogManager.GetLogger(
47 MethodBase.GetCurrentMethod().DeclaringType); 48 MethodBase.GetCurrentMethod().DeclaringType);
48 49
50 private bool m_DeleteOnUnregister = true;
51 private static GridService m_RootInstance = null;
52 protected IConfigSource m_config;
53 protected HypergridLinker m_HypergridLinker;
54
55 protected IAuthenticationService m_AuthenticationService = null;
49 protected bool m_AllowDuplicateNames = false; 56 protected bool m_AllowDuplicateNames = false;
57 protected bool m_AllowHypergridMapSearch = false;
50 58
51 public GridService(IConfigSource config) 59 public GridService(IConfigSource config)
52 : base(config) 60 : base(config)
53 { 61 {
54 m_log.DebugFormat("[GRID SERVICE]: Starting..."); 62 m_log.DebugFormat("[GRID SERVICE]: Starting...");
55 63
64 m_config = config;
56 IConfig gridConfig = config.Configs["GridService"]; 65 IConfig gridConfig = config.Configs["GridService"];
57 if (gridConfig != null) 66 if (gridConfig != null)
58 { 67 {
68 m_DeleteOnUnregister = gridConfig.GetBoolean("DeleteOnUnregister", true);
69
70 string authService = gridConfig.GetString("AuthenticationService", String.Empty);
71
72 if (authService != String.Empty)
73 {
74 Object[] args = new Object[] { config };
75 m_AuthenticationService = ServerUtils.LoadPlugin<IAuthenticationService>(authService, args);
76 }
59 m_AllowDuplicateNames = gridConfig.GetBoolean("AllowDuplicateNames", m_AllowDuplicateNames); 77 m_AllowDuplicateNames = gridConfig.GetBoolean("AllowDuplicateNames", m_AllowDuplicateNames);
78 m_AllowHypergridMapSearch = gridConfig.GetBoolean("AllowHypergridMapSearch", m_AllowHypergridMapSearch);
79 }
80
81 if (m_RootInstance == null)
82 {
83 m_RootInstance = this;
84
85 if (MainConsole.Instance != null)
86 {
87 MainConsole.Instance.Commands.AddCommand("grid", true,
88 "show region",
89 "show region <Region name>",
90 "Show details on a region",
91 String.Empty,
92 HandleShowRegion);
93
94 MainConsole.Instance.Commands.AddCommand("grid", true,
95 "set region flags",
96 "set region flags <Region name> <flags>",
97 "Set database flags for region",
98 String.Empty,
99 HandleSetFlags);
100 }
101 m_HypergridLinker = new HypergridLinker(m_config, this, m_Database);
60 } 102 }
61 } 103 }
62 104
@@ -64,9 +106,48 @@ namespace OpenSim.Services.GridService
64 106
65 public string RegisterRegion(UUID scopeID, GridRegion regionInfos) 107 public string RegisterRegion(UUID scopeID, GridRegion regionInfos)
66 { 108 {
109 IConfig gridConfig = m_config.Configs["GridService"];
67 // This needs better sanity testing. What if regionInfo is registering in 110 // This needs better sanity testing. What if regionInfo is registering in
68 // overlapping coords? 111 // overlapping coords?
69 RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID); 112 RegionData region = m_Database.Get(regionInfos.RegionLocX, regionInfos.RegionLocY, scopeID);
113 if (region != null)
114 {
115 // There is a preexisting record
116 //
117 // Get it's flags
118 //
119 OpenSim.Data.RegionFlags rflags = (OpenSim.Data.RegionFlags)Convert.ToInt32(region.Data["flags"]);
120
121 // Is this a reservation?
122 //
123 if ((rflags & OpenSim.Data.RegionFlags.Reservation) != 0)
124 {
125 // Regions reserved for the null key cannot be taken.
126 //
127 if (region.Data["PrincipalID"] == UUID.Zero.ToString())
128 return "Region location us reserved";
129
130 // Treat it as an auth request
131 //
132 // NOTE: Fudging the flags value here, so these flags
133 // should not be used elsewhere. Don't optimize
134 // this with the later retrieval of the same flags!
135 //
136 rflags |= OpenSim.Data.RegionFlags.Authenticate;
137 }
138
139 if ((rflags & OpenSim.Data.RegionFlags.Authenticate) != 0)
140 {
141 // Can we authenticate at all?
142 //
143 if (m_AuthenticationService == null)
144 return "No authentication possible";
145
146 if (!m_AuthenticationService.Verify(new UUID(region.Data["PrincipalID"].ToString()), regionInfos.Token, 30))
147 return "Bad authentication";
148 }
149 }
150
70 if ((region != null) && (region.RegionID != regionInfos.RegionID)) 151 if ((region != null) && (region.RegionID != regionInfos.RegionID))
71 { 152 {
72 m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.", 153 m_log.WarnFormat("[GRID SERVICE]: Region {0} tried to register in coordinates {1}, {2} which are already in use in scope {3}.",
@@ -76,6 +157,9 @@ namespace OpenSim.Services.GridService
76 if ((region != null) && (region.RegionID == regionInfos.RegionID) && 157 if ((region != null) && (region.RegionID == regionInfos.RegionID) &&
77 ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY))) 158 ((region.posX != regionInfos.RegionLocX) || (region.posY != regionInfos.RegionLocY)))
78 { 159 {
160 if ((Convert.ToInt32(region.Data["flags"]) & (int)OpenSim.Data.RegionFlags.NoMove) != 0)
161 return "Can't move this region";
162
79 // Region reregistering in other coordinates. Delete the old entry 163 // Region reregistering in other coordinates. Delete the old entry
80 m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.", 164 m_log.DebugFormat("[GRID SERVICE]: Region {0} ({1}) was previously registered at {2}-{3}. Deleting old entry.",
81 regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY); 165 regionInfos.RegionName, regionInfos.RegionID, regionInfos.RegionLocX, regionInfos.RegionLocY);
@@ -110,8 +194,37 @@ namespace OpenSim.Services.GridService
110 // Everything is ok, let's register 194 // Everything is ok, let's register
111 RegionData rdata = RegionInfo2RegionData(regionInfos); 195 RegionData rdata = RegionInfo2RegionData(regionInfos);
112 rdata.ScopeID = scopeID; 196 rdata.ScopeID = scopeID;
197
198 if (region != null)
199 {
200 int oldFlags = Convert.ToInt32(region.Data["flags"]);
201 if ((oldFlags & (int)OpenSim.Data.RegionFlags.LockedOut) != 0)
202 return "Region locked out";
203
204 oldFlags &= ~(int)OpenSim.Data.RegionFlags.Reservation;
205
206 rdata.Data["flags"] = oldFlags.ToString(); // Preserve flags
207 }
208 else
209 {
210 rdata.Data["flags"] = "0";
211 if ((gridConfig != null) && rdata.RegionName != string.Empty)
212 {
213 int newFlags = 0;
214 string regionName = rdata.RegionName.Trim().Replace(' ', '_');
215 newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + regionName, String.Empty));
216 newFlags = ParseFlags(newFlags, gridConfig.GetString("Region_" + rdata.RegionID.ToString(), String.Empty));
217 rdata.Data["flags"] = newFlags.ToString();
218 }
219 }
220
221 int flags = Convert.ToInt32(rdata.Data["flags"]);
222 flags |= (int)OpenSim.Data.RegionFlags.RegionOnline;
223 rdata.Data["flags"] = flags.ToString();
224
113 try 225 try
114 { 226 {
227 rdata.Data["last_seen"] = Util.UnixTimeSinceEpoch();
115 m_Database.Store(rdata); 228 m_Database.Store(rdata);
116 } 229 }
117 catch (Exception e) 230 catch (Exception e)
@@ -128,6 +241,30 @@ namespace OpenSim.Services.GridService
128 public bool DeregisterRegion(UUID regionID) 241 public bool DeregisterRegion(UUID regionID)
129 { 242 {
130 m_log.DebugFormat("[GRID SERVICE]: Region {0} deregistered", regionID); 243 m_log.DebugFormat("[GRID SERVICE]: Region {0} deregistered", regionID);
244 RegionData region = m_Database.Get(regionID, UUID.Zero);
245 if (region == null)
246 return false;
247
248 int flags = Convert.ToInt32(region.Data["flags"]);
249
250 if (!m_DeleteOnUnregister || (flags & (int)OpenSim.Data.RegionFlags.Persistent) != 0)
251 {
252 flags &= ~(int)OpenSim.Data.RegionFlags.RegionOnline;
253 region.Data["flags"] = flags.ToString();
254 region.Data["last_seen"] = Util.UnixTimeSinceEpoch();
255 try
256 {
257 m_Database.Store(region);
258 }
259 catch (Exception e)
260 {
261 m_log.DebugFormat("[GRID SERVICE]: Database exception: {0}", e);
262 }
263
264 return true;
265
266 }
267
131 return m_Database.Delete(regionID); 268 return m_Database.Delete(regionID);
132 } 269 }
133 270
@@ -194,6 +331,13 @@ namespace OpenSim.Services.GridService
194 } 331 }
195 } 332 }
196 333
334 if (m_AllowHypergridMapSearch && rdatas == null || (rdatas != null && rdatas.Count == 0) && name.Contains("."))
335 {
336 GridRegion r = m_HypergridLinker.LinkRegion(scopeID, name);
337 if (r != null)
338 rinfos.Add(r);
339 }
340
197 return rinfos; 341 return rinfos;
198 } 342 }
199 343
@@ -216,7 +360,7 @@ namespace OpenSim.Services.GridService
216 360
217 #region Data structure conversions 361 #region Data structure conversions
218 362
219 protected RegionData RegionInfo2RegionData(GridRegion rinfo) 363 public RegionData RegionInfo2RegionData(GridRegion rinfo)
220 { 364 {
221 RegionData rdata = new RegionData(); 365 RegionData rdata = new RegionData();
222 rdata.posX = (int)rinfo.RegionLocX; 366 rdata.posX = (int)rinfo.RegionLocX;
@@ -229,7 +373,7 @@ namespace OpenSim.Services.GridService
229 return rdata; 373 return rdata;
230 } 374 }
231 375
232 protected GridRegion RegionData2RegionInfo(RegionData rdata) 376 public GridRegion RegionData2RegionInfo(RegionData rdata)
233 { 377 {
234 GridRegion rinfo = new GridRegion(rdata.Data); 378 GridRegion rinfo = new GridRegion(rdata.Data);
235 rinfo.RegionLocX = rdata.posX; 379 rinfo.RegionLocX = rdata.posX;
@@ -243,5 +387,142 @@ namespace OpenSim.Services.GridService
243 387
244 #endregion 388 #endregion
245 389
390 public List<GridRegion> GetDefaultRegions(UUID scopeID)
391 {
392 List<GridRegion> ret = new List<GridRegion>();
393
394 List<RegionData> regions = m_Database.GetDefaultRegions(scopeID);
395
396 foreach (RegionData r in regions)
397 {
398 if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
399 ret.Add(RegionData2RegionInfo(r));
400 }
401
402 return ret;
403 }
404
405 public List<GridRegion> GetFallbackRegions(UUID scopeID, int x, int y)
406 {
407 List<GridRegion> ret = new List<GridRegion>();
408
409 List<RegionData> regions = m_Database.GetFallbackRegions(scopeID, x, y);
410
411 foreach (RegionData r in regions)
412 {
413 if ((Convert.ToInt32(r.Data["flags"]) & (int)OpenSim.Data.RegionFlags.RegionOnline) != 0)
414 ret.Add(RegionData2RegionInfo(r));
415 }
416
417 m_log.DebugFormat("[GRID SERVICE]: Fallback returned {0} regions", ret.Count);
418 return ret;
419 }
420
421 public int GetRegionFlags(UUID scopeID, UUID regionID)
422 {
423 RegionData region = m_Database.Get(regionID, scopeID);
424
425 if (region != null)
426 {
427 int flags = Convert.ToInt32(region.Data["flags"]);
428 //m_log.DebugFormat("[GRID SERVICE]: Request for flags of {0}: {1}", regionID, flags);
429 return flags;
430 }
431 else
432 return -1;
433 }
434
435 private void HandleShowRegion(string module, string[] cmd)
436 {
437 if (cmd.Length != 3)
438 {
439 MainConsole.Instance.Output("Syntax: show region <region name>");
440 return;
441 }
442 List<RegionData> regions = m_Database.Get(cmd[2], UUID.Zero);
443 if (regions == null || regions.Count < 1)
444 {
445 MainConsole.Instance.Output("Region not found");
446 return;
447 }
448
449 MainConsole.Instance.Output("Region Name Region UUID");
450 MainConsole.Instance.Output("Location URI");
451 MainConsole.Instance.Output("Owner ID Flags");
452 MainConsole.Instance.Output("-------------------------------------------------------------------------------");
453 foreach (RegionData r in regions)
454 {
455 OpenSim.Data.RegionFlags flags = (OpenSim.Data.RegionFlags)Convert.ToInt32(r.Data["flags"]);
456 MainConsole.Instance.Output(String.Format("{0,-20} {1}\n{2,-20} {3}\n{4,-39} {5}\n\n",
457 r.RegionName, r.RegionID,
458 String.Format("{0},{1}", r.posX, r.posY), "http://" + r.Data["serverIP"].ToString() + ":" + r.Data["serverPort"].ToString(),
459 r.Data["owner_uuid"].ToString(), flags.ToString()));
460 }
461 return;
462 }
463
464 private int ParseFlags(int prev, string flags)
465 {
466 OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)prev;
467
468 string[] parts = flags.Split(new char[] {',', ' '}, StringSplitOptions.RemoveEmptyEntries);
469
470 foreach (string p in parts)
471 {
472 int val;
473
474 try
475 {
476 if (p.StartsWith("+"))
477 {
478 val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
479 f |= (OpenSim.Data.RegionFlags)val;
480 }
481 else if (p.StartsWith("-"))
482 {
483 val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p.Substring(1));
484 f &= ~(OpenSim.Data.RegionFlags)val;
485 }
486 else
487 {
488 val = (int)Enum.Parse(typeof(OpenSim.Data.RegionFlags), p);
489 f |= (OpenSim.Data.RegionFlags)val;
490 }
491 }
492 catch (Exception e)
493 {
494 MainConsole.Instance.Output("Error in flag specification: " + p);
495 }
496 }
497
498 return (int)f;
499 }
500
501 private void HandleSetFlags(string module, string[] cmd)
502 {
503 if (cmd.Length < 5)
504 {
505 MainConsole.Instance.Output("Syntax: set region flags <region name> <flags>");
506 return;
507 }
508
509 List<RegionData> regions = m_Database.Get(cmd[3], UUID.Zero);
510 if (regions == null || regions.Count < 1)
511 {
512 MainConsole.Instance.Output("Region not found");
513 return;
514 }
515
516 foreach (RegionData r in regions)
517 {
518 int flags = Convert.ToInt32(r.Data["flags"]);
519 flags = ParseFlags(flags, cmd[4]);
520 r.Data["flags"] = flags.ToString();
521 OpenSim.Data.RegionFlags f = (OpenSim.Data.RegionFlags)flags;
522
523 MainConsole.Instance.Output(String.Format("Set region {0} to {1}", r.RegionName, f));
524 m_Database.Store(r);
525 }
526 }
246 } 527 }
247} 528}
diff --git a/OpenSim/Services/GridService/HypergridLinker.cs b/OpenSim/Services/GridService/HypergridLinker.cs
new file mode 100644
index 0000000..de5df9d
--- /dev/null
+++ b/OpenSim/Services/GridService/HypergridLinker.cs
@@ -0,0 +1,634 @@
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
28using System;
29using System.Collections.Generic;
30using System.Net;
31using System.Reflection;
32using System.Xml;
33
34using Nini.Config;
35using log4net;
36using OpenSim.Framework;
37using OpenSim.Framework.Console;
38using OpenSim.Data;
39using OpenSim.Server.Base;
40using OpenSim.Services.Interfaces;
41using OpenSim.Services.Connectors.Hypergrid;
42using GridRegion = OpenSim.Services.Interfaces.GridRegion;
43using OpenMetaverse;
44
45namespace OpenSim.Services.GridService
46{
47 public class HypergridLinker
48 {
49 private static readonly ILog m_log =
50 LogManager.GetLogger(
51 MethodBase.GetCurrentMethod().DeclaringType);
52
53 private static UUID m_HGMapImage = new UUID("00000000-0000-1111-9999-000000000013");
54
55 private static uint m_autoMappingX = 0;
56 private static uint m_autoMappingY = 0;
57 private static bool m_enableAutoMapping = false;
58
59 protected IRegionData m_Database;
60 protected GridService m_GridService;
61 protected IAssetService m_AssetService;
62 protected GatekeeperServiceConnector m_GatekeeperConnector;
63
64 protected UUID m_ScopeID = UUID.Zero;
65
66 // Hyperlink regions are hyperlinks on the map
67 public readonly Dictionary<UUID, GridRegion> m_HyperlinkRegions = new Dictionary<UUID, GridRegion>();
68 protected Dictionary<UUID, ulong> m_HyperlinkHandles = new Dictionary<UUID, ulong>();
69
70 protected GridRegion m_DefaultRegion;
71 protected GridRegion DefaultRegion
72 {
73 get
74 {
75 if (m_DefaultRegion == null)
76 {
77 List<GridRegion> defs = m_GridService.GetDefaultRegions(m_ScopeID);
78 if (defs != null && defs.Count > 0)
79 m_DefaultRegion = defs[0];
80 else
81 {
82 // Best guess, may be totally off
83 m_DefaultRegion = new GridRegion(1000, 1000);
84 m_log.WarnFormat("[HYPERGRID LINKER]: This grid does not have a default region. Assuming default coordinates at 1000, 1000.");
85 }
86 }
87 return m_DefaultRegion;
88 }
89 }
90
91 public HypergridLinker(IConfigSource config, GridService gridService, IRegionData db)
92 {
93 m_log.DebugFormat("[HYPERGRID LINKER]: Starting...");
94
95 m_Database = db;
96 m_GridService = gridService;
97
98 IConfig gridConfig = config.Configs["GridService"];
99 if (gridConfig != null)
100 {
101 string assetService = gridConfig.GetString("AssetService", string.Empty);
102
103 Object[] args = new Object[] { config };
104
105 if (assetService != string.Empty)
106 m_AssetService = ServerUtils.LoadPlugin<IAssetService>(assetService, args);
107
108 string scope = gridConfig.GetString("ScopeID", string.Empty);
109 if (scope != string.Empty)
110 UUID.TryParse(scope, out m_ScopeID);
111
112 m_GatekeeperConnector = new GatekeeperServiceConnector(m_AssetService);
113
114 m_log.DebugFormat("[HYPERGRID LINKER]: Loaded all services...");
115 }
116
117 if (MainConsole.Instance != null)
118 {
119 MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-region",
120 "link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>] <cr>",
121 "Link a hypergrid region", RunCommand);
122 MainConsole.Instance.Commands.AddCommand("hypergrid", false, "unlink-region",
123 "unlink-region <local name> or <HostName>:<HttpPort> <cr>",
124 "Unlink a hypergrid region", RunCommand);
125 MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-mapping", "link-mapping [<x> <y>] <cr>",
126 "Set local coordinate to map HG regions to", RunCommand);
127 MainConsole.Instance.Commands.AddCommand("hypergrid", false, "show hyperlinks", "show hyperlinks <cr>",
128 "List the HG regions", HandleShow);
129 }
130 }
131
132
133 #region Link Region
134
135 public GridRegion LinkRegion(UUID scopeID, string regionDescriptor)
136 {
137 string reason = string.Empty;
138 int xloc = random.Next(0, Int16.MaxValue) * (int)Constants.RegionSize;
139 return TryLinkRegionToCoords(scopeID, regionDescriptor, xloc, 0, out reason);
140 }
141
142 private static Random random = new Random();
143
144 // From the command line link-region
145 public GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, out string reason)
146 {
147 reason = string.Empty;
148 string host = "127.0.0.1";
149 string portstr;
150 string regionName = "";
151 uint port = 9000;
152 string[] parts = mapName.Split(new char[] { ':' });
153 if (parts.Length >= 1)
154 {
155 host = parts[0];
156 }
157 if (parts.Length >= 2)
158 {
159 portstr = parts[1];
160 //m_log.Debug("-- port = " + portstr);
161 if (!UInt32.TryParse(portstr, out port))
162 regionName = parts[1];
163 }
164 // always take the last one
165 if (parts.Length >= 3)
166 {
167 regionName = parts[2];
168 }
169
170 // Sanity check.
171 IPAddress ipaddr = null;
172 try
173 {
174 ipaddr = Util.GetHostFromDNS(host);
175 }
176 catch
177 {
178 reason = "Malformed hostname";
179 return null;
180 }
181
182 GridRegion regInfo;
183 bool success = TryCreateLink(scopeID, xloc, yloc, regionName, port, host, out regInfo, out reason);
184 if (success)
185 {
186 regInfo.RegionName = mapName;
187 return regInfo;
188 }
189
190 return null;
191 }
192
193
194 // From the command line and the 2 above
195 public bool TryCreateLink(UUID scopeID, int xloc, int yloc,
196 string externalRegionName, uint externalPort, string externalHostName, out GridRegion regInfo, out string reason)
197 {
198 m_log.DebugFormat("[HYPERGRID LINKER]: Link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc);
199
200 reason = string.Empty;
201 regInfo = new GridRegion();
202 regInfo.RegionName = externalRegionName;
203 regInfo.HttpPort = externalPort;
204 regInfo.ExternalHostName = externalHostName;
205 regInfo.RegionLocX = xloc;
206 regInfo.RegionLocY = yloc;
207 regInfo.ScopeID = scopeID;
208
209 try
210 {
211 regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
212 }
213 catch (Exception e)
214 {
215 m_log.Warn("[HYPERGRID LINKER]: Wrong format for link-region: " + e.Message);
216 reason = "Internal error";
217 return false;
218 }
219
220 // Finally, link it
221 ulong handle = 0;
222 UUID regionID = UUID.Zero;
223 string externalName = string.Empty;
224 string imageURL = string.Empty;
225 if (!m_GatekeeperConnector.LinkRegion(regInfo, out regionID, out handle, out externalName, out imageURL, out reason))
226 return false;
227
228 if (regionID != UUID.Zero)
229 {
230 GridRegion r = m_GridService.GetRegionByUUID(scopeID, regionID);
231 if (r != null)
232 {
233 m_log.DebugFormat("[HYPERGRID LINKER]: Region already exists in coordinates {0} {1}", r.RegionLocX / Constants.RegionSize, r.RegionLocY / Constants.RegionSize);
234 regInfo = r;
235 return true;
236 }
237
238 regInfo.RegionID = regionID;
239 Uri uri = null;
240 try
241 {
242 uri = new Uri(externalName);
243 regInfo.ExternalHostName = uri.Host;
244 regInfo.HttpPort = (uint)uri.Port;
245 }
246 catch
247 {
248 m_log.WarnFormat("[HYPERGRID LINKER]: Remote Gatekeeper at {0} provided malformed ExternalName {1}", regInfo.ExternalHostName, externalName);
249 }
250 regInfo.RegionName = regInfo.ExternalHostName + ":" + regInfo.HttpPort + ":" + regInfo.RegionName;
251 // Try get the map image
252 //regInfo.TerrainImage = m_GatekeeperConnector.GetMapImage(regionID, imageURL);
253 // I need a texture that works for this... the one I tried doesn't seem to be working
254 regInfo.TerrainImage = m_HGMapImage;
255
256 AddHyperlinkRegion(regInfo, handle);
257 m_log.Info("[HYPERGRID LINKER]: Successfully linked to region_uuid " + regInfo.RegionID);
258
259 }
260 else
261 {
262 m_log.Warn("[HYPERGRID LINKER]: Unable to link region");
263 reason = "Remote region could not be found";
264 return false;
265 }
266
267 uint x, y;
268 if (!Check4096(handle, out x, out y))
269 {
270 RemoveHyperlinkRegion(regInfo.RegionID);
271 reason = "Region is too far (" + x + ", " + y + ")";
272 m_log.Info("[HYPERGRID LINKER]: Unable to link, region is too far (" + x + ", " + y + ")");
273 return false;
274 }
275
276 m_log.Debug("[HYPERGRID LINKER]: link region succeeded");
277 return true;
278 }
279
280 public bool TryUnlinkRegion(string mapName)
281 {
282 GridRegion regInfo = null;
283 if (mapName.Contains(":"))
284 {
285 string host = "127.0.0.1";
286 //string portstr;
287 //string regionName = "";
288 uint port = 9000;
289 string[] parts = mapName.Split(new char[] { ':' });
290 if (parts.Length >= 1)
291 {
292 host = parts[0];
293 }
294
295 foreach (GridRegion r in m_HyperlinkRegions.Values)
296 if (host.Equals(r.ExternalHostName) && (port == r.HttpPort))
297 regInfo = r;
298 }
299 else
300 {
301 foreach (GridRegion r in m_HyperlinkRegions.Values)
302 if (r.RegionName.Equals(mapName))
303 regInfo = r;
304 }
305 if (regInfo != null)
306 {
307 RemoveHyperlinkRegion(regInfo.RegionID);
308 return true;
309 }
310 else
311 {
312 m_log.InfoFormat("[HYPERGRID LINKER]: Region {0} not found", mapName);
313 return false;
314 }
315 }
316
317 /// <summary>
318 /// Cope with this viewer limitation.
319 /// </summary>
320 /// <param name="regInfo"></param>
321 /// <returns></returns>
322 public bool Check4096(ulong realHandle, out uint x, out uint y)
323 {
324 GridRegion defRegion = DefaultRegion;
325
326 uint ux = 0, uy = 0;
327 Utils.LongToUInts(realHandle, out ux, out uy);
328 x = ux / Constants.RegionSize;
329 y = uy / Constants.RegionSize;
330
331 if ((Math.Abs((int)defRegion.RegionLocX - ux) >= 4096 * Constants.RegionSize) ||
332 (Math.Abs((int)defRegion.RegionLocY - uy) >= 4096 * Constants.RegionSize))
333 {
334 return false;
335 }
336 return true;
337 }
338
339 private void AddHyperlinkRegion(GridRegion regionInfo, ulong regionHandle)
340 {
341 //m_HyperlinkRegions[regionInfo.RegionID] = regionInfo;
342 //m_HyperlinkHandles[regionInfo.RegionID] = regionHandle;
343
344 RegionData rdata = m_GridService.RegionInfo2RegionData(regionInfo);
345 int flags = (int)OpenSim.Data.RegionFlags.Hyperlink + (int)OpenSim.Data.RegionFlags.NoDirectLogin + (int)OpenSim.Data.RegionFlags.RegionOnline;
346 rdata.Data["flags"] = flags.ToString();
347
348 m_Database.Store(rdata);
349
350 }
351
352 private void RemoveHyperlinkRegion(UUID regionID)
353 {
354 //// Try the hyperlink collection
355 //if (m_HyperlinkRegions.ContainsKey(regionID))
356 //{
357 // m_HyperlinkRegions.Remove(regionID);
358 // m_HyperlinkHandles.Remove(regionID);
359 //}
360 m_Database.Delete(regionID);
361 }
362
363 #endregion
364
365
366 #region Console Commands
367
368 public void HandleShow(string module, string[] cmd)
369 {
370 MainConsole.Instance.Output("Not Implemented Yet");
371 //if (cmd.Length != 2)
372 //{
373 // MainConsole.Instance.Output("Syntax: show hyperlinks");
374 // return;
375 //}
376 //List<GridRegion> regions = new List<GridRegion>(m_HypergridService.m_HyperlinkRegions.Values);
377 //if (regions == null || regions.Count < 1)
378 //{
379 // MainConsole.Instance.Output("No hyperlinks");
380 // return;
381 //}
382
383 //MainConsole.Instance.Output("Region Name Region UUID");
384 //MainConsole.Instance.Output("Location URI");
385 //MainConsole.Instance.Output("Owner ID ");
386 //MainConsole.Instance.Output("-------------------------------------------------------------------------------");
387 //foreach (GridRegion r in regions)
388 //{
389 // MainConsole.Instance.Output(String.Format("{0,-20} {1}\n{2,-20} {3}\n{4,-39} \n\n",
390 // r.RegionName, r.RegionID,
391 // String.Format("{0},{1}", r.RegionLocX, r.RegionLocY), "http://" + r.ExternalHostName + ":" + r.HttpPort.ToString(),
392 // r.EstateOwner.ToString()));
393 //}
394 //return;
395 }
396 public void RunCommand(string module, string[] cmdparams)
397 {
398 List<string> args = new List<string>(cmdparams);
399 if (args.Count < 1)
400 return;
401
402 string command = args[0];
403 args.RemoveAt(0);
404
405 cmdparams = args.ToArray();
406
407 RunHGCommand(command, cmdparams);
408
409 }
410
411 private void RunHGCommand(string command, string[] cmdparams)
412 {
413 if (command.Equals("link-mapping"))
414 {
415 if (cmdparams.Length == 2)
416 {
417 try
418 {
419 m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
420 m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
421 m_enableAutoMapping = true;
422 }
423 catch (Exception)
424 {
425 m_autoMappingX = 0;
426 m_autoMappingY = 0;
427 m_enableAutoMapping = false;
428 }
429 }
430 }
431 else if (command.Equals("link-region"))
432 {
433 if (cmdparams.Length < 3)
434 {
435 if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
436 {
437 LoadXmlLinkFile(cmdparams);
438 }
439 else
440 {
441 LinkRegionCmdUsage();
442 }
443 return;
444 }
445
446 if (cmdparams[2].Contains(":"))
447 {
448 // New format
449 int xloc, yloc;
450 string mapName;
451 try
452 {
453 xloc = Convert.ToInt32(cmdparams[0]);
454 yloc = Convert.ToInt32(cmdparams[1]);
455 mapName = cmdparams[2];
456 if (cmdparams.Length > 3)
457 for (int i = 3; i < cmdparams.Length; i++)
458 mapName += " " + cmdparams[i];
459
460 //m_log.Info(">> MapName: " + mapName);
461 }
462 catch (Exception e)
463 {
464 MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
465 LinkRegionCmdUsage();
466 return;
467 }
468
469 // Convert cell coordinates given by the user to meters
470 xloc = xloc * (int)Constants.RegionSize;
471 yloc = yloc * (int)Constants.RegionSize;
472 string reason = string.Empty;
473 if (TryLinkRegionToCoords(UUID.Zero, mapName, xloc, yloc, out reason) == null)
474 MainConsole.Instance.Output("Failed to link region: " + reason);
475 else
476 MainConsole.Instance.Output("Hyperlink established");
477 }
478 else
479 {
480 // old format
481 GridRegion regInfo;
482 int xloc, yloc;
483 uint externalPort;
484 string externalHostName;
485 try
486 {
487 xloc = Convert.ToInt32(cmdparams[0]);
488 yloc = Convert.ToInt32(cmdparams[1]);
489 externalPort = Convert.ToUInt32(cmdparams[3]);
490 externalHostName = cmdparams[2];
491 //internalPort = Convert.ToUInt32(cmdparams[4]);
492 //remotingPort = Convert.ToUInt32(cmdparams[5]);
493 }
494 catch (Exception e)
495 {
496 MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
497 LinkRegionCmdUsage();
498 return;
499 }
500
501 // Convert cell coordinates given by the user to meters
502 xloc = xloc * (int)Constants.RegionSize;
503 yloc = yloc * (int)Constants.RegionSize;
504 string reason = string.Empty;
505 if (TryCreateLink(UUID.Zero, xloc, yloc, "", externalPort, externalHostName, out regInfo, out reason))
506 {
507 if (cmdparams.Length >= 5)
508 {
509 regInfo.RegionName = "";
510 for (int i = 4; i < cmdparams.Length; i++)
511 regInfo.RegionName += cmdparams[i] + " ";
512 }
513 }
514 }
515 return;
516 }
517 else if (command.Equals("unlink-region"))
518 {
519 if (cmdparams.Length < 1)
520 {
521 UnlinkRegionCmdUsage();
522 return;
523 }
524 if (TryUnlinkRegion(cmdparams[0]))
525 MainConsole.Instance.Output("Successfully unlinked " + cmdparams[0]);
526 else
527 MainConsole.Instance.Output("Unable to unlink " + cmdparams[0] + ", region not found.");
528 }
529 }
530
531 private void LoadXmlLinkFile(string[] cmdparams)
532 {
533 //use http://www.hgurl.com/hypergrid.xml for test
534 try
535 {
536 XmlReader r = XmlReader.Create(cmdparams[0]);
537 XmlConfigSource cs = new XmlConfigSource(r);
538 string[] excludeSections = null;
539
540 if (cmdparams.Length == 2)
541 {
542 if (cmdparams[1].ToLower().StartsWith("excludelist:"))
543 {
544 string excludeString = cmdparams[1].ToLower();
545 excludeString = excludeString.Remove(0, 12);
546 char[] splitter = { ';' };
547
548 excludeSections = excludeString.Split(splitter);
549 }
550 }
551
552 for (int i = 0; i < cs.Configs.Count; i++)
553 {
554 bool skip = false;
555 if ((excludeSections != null) && (excludeSections.Length > 0))
556 {
557 for (int n = 0; n < excludeSections.Length; n++)
558 {
559 if (excludeSections[n] == cs.Configs[i].Name.ToLower())
560 {
561 skip = true;
562 break;
563 }
564 }
565 }
566 if (!skip)
567 {
568 ReadLinkFromConfig(cs.Configs[i]);
569 }
570 }
571 }
572 catch (Exception e)
573 {
574 m_log.Error(e.ToString());
575 }
576 }
577
578
579 private void ReadLinkFromConfig(IConfig config)
580 {
581 GridRegion regInfo;
582 int xloc, yloc;
583 uint externalPort;
584 string externalHostName;
585 uint realXLoc, realYLoc;
586
587 xloc = Convert.ToInt32(config.GetString("xloc", "0"));
588 yloc = Convert.ToInt32(config.GetString("yloc", "0"));
589 externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
590 externalHostName = config.GetString("externalHostName", "");
591 realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
592 realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
593
594 if (m_enableAutoMapping)
595 {
596 xloc = (int)((xloc % 100) + m_autoMappingX);
597 yloc = (int)((yloc % 100) + m_autoMappingY);
598 }
599
600 if (((realXLoc == 0) && (realYLoc == 0)) ||
601 (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
602 ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
603 {
604 xloc = xloc * (int)Constants.RegionSize;
605 yloc = yloc * (int)Constants.RegionSize;
606 string reason = string.Empty;
607 if (TryCreateLink(UUID.Zero, xloc, yloc, "", externalPort,
608 externalHostName, out regInfo, out reason))
609 {
610 regInfo.RegionName = config.GetString("localName", "");
611 }
612 else
613 MainConsole.Instance.Output("Unable to link " + externalHostName + ": " + reason);
614 }
615 }
616
617
618 private void LinkRegionCmdUsage()
619 {
620 MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
621 MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
622 MainConsole.Instance.Output("Usage: link-region <URI_of_xml> [<exclude>]");
623 }
624
625 private void UnlinkRegionCmdUsage()
626 {
627 MainConsole.Instance.Output("Usage: unlink-region <HostName>:<HttpPort>");
628 MainConsole.Instance.Output("Usage: unlink-region <LocalName>");
629 }
630
631 #endregion
632
633 }
634}