From ff0fa1290397d96dbf49477a2668fa62e9be22dc Mon Sep 17 00:00:00 2001 From: diva Date: Sat, 7 Feb 2009 16:10:23 +0000 Subject: Adds support for HG linking to specific regions within an instance. The format is Host:Port:Region. Refactored the linking code from MapSearchModule to HGHyperlink, so that it can be used both by the MapSearchModule and the Console command. --- OpenSim/Region/Application/HGOpenSimNode.cs | 124 ++++++------- .../Communications/Hypergrid/HGGridServices.cs | 6 +- .../Modules/World/WorldMap/MapSearchModule.cs | 123 ++----------- .../Framework/Scenes/Hypergrid/HGHyperlink.cs | 192 +++++++++++++++++++++ 4 files changed, 264 insertions(+), 181 deletions(-) create mode 100644 OpenSim/Region/Framework/Scenes/Hypergrid/HGHyperlink.cs diff --git a/OpenSim/Region/Application/HGOpenSimNode.cs b/OpenSim/Region/Application/HGOpenSimNode.cs index 2de9ddf..57c6fa3 100644 --- a/OpenSim/Region/Application/HGOpenSimNode.cs +++ b/OpenSim/Region/Application/HGOpenSimNode.cs @@ -79,7 +79,7 @@ namespace OpenSim base.StartupSpecific(); MainConsole.Instance.Commands.AddCommand("hypergrid", "link-mapping", "link-mapping [ ] ", "Set local coordinate to map HG regions to", RunCommand); - MainConsole.Instance.Commands.AddCommand("hypergrid", "link-region", "link-region ", "Link a hypergrid region", RunCommand); + MainConsole.Instance.Commands.AddCommand("hypergrid", "link-region", "link-region :[:] ", "Link a hypergrid region", RunCommand); } protected override void InitialiseStandaloneServices(LibraryRootFolder libraryRootFolder) @@ -175,50 +175,71 @@ namespace OpenSim } else if (command.Equals("link-region")) { - // link-region - if (cmdparams.Count < 4) + if (cmdparams.Count < 3) { - if ((cmdparams.Count == 1) || (cmdparams.Count ==2)) + LinkRegionCmdUsage(); + return; + } + + if (cmdparams[2].Contains(":")) + { // New format + uint xloc, yloc; + string mapName; + try { - LoadXmlLinkFile(cmdparams.ToArray()); + xloc = Convert.ToUInt32(cmdparams[0]); + yloc = Convert.ToUInt32(cmdparams[1]); + mapName = cmdparams[2]; + if (cmdparams.Count > 3) + for (int i = 3; i < cmdparams.Count; i++) + mapName += " " + cmdparams[i]; + + Console.WriteLine(">> MapName: " + mapName); + //internalPort = Convert.ToUInt32(cmdparams[4]); + //remotingPort = Convert.ToUInt32(cmdparams[5]); } - else + catch (Exception e) { + m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message); LinkRegionCmdUsage(); + return; } - return; - } - RegionInfo regInfo; - uint xloc, yloc; - uint externalPort; - string externalHostName; - try - { - xloc = Convert.ToUInt32(cmdparams[0]); - yloc = Convert.ToUInt32(cmdparams[1]); - externalPort = Convert.ToUInt32(cmdparams[3]); - externalHostName = cmdparams[2]; - //internalPort = Convert.ToUInt32(cmdparams[4]); - //remotingPort = Convert.ToUInt32(cmdparams[5]); - } - catch (Exception e) - { - m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message); - LinkRegionCmdUsage(); - return; + HGHyperlink.TryLinkRegionToCoords(m_sceneManager.CurrentOrFirstScene, null, mapName, xloc, yloc); } + else + { // old format + RegionInfo regInfo; + uint xloc, yloc; + uint externalPort; + string externalHostName; + try + { + xloc = Convert.ToUInt32(cmdparams[0]); + yloc = Convert.ToUInt32(cmdparams[1]); + externalPort = Convert.ToUInt32(cmdparams[3]); + externalHostName = cmdparams[2]; + //internalPort = Convert.ToUInt32(cmdparams[4]); + //remotingPort = Convert.ToUInt32(cmdparams[5]); + } + catch (Exception e) + { + m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message); + LinkRegionCmdUsage(); + return; + } - if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo)) - { - if (cmdparams.Count >= 5) + //if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo)) + if (HGHyperlink.TryCreateLink(m_sceneManager.CurrentOrFirstScene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo)) { - regInfo.RegionName = ""; - for (int i = 4; i < cmdparams.Count; i++) - regInfo.RegionName += cmdparams[i] + " "; + if (cmdparams.Count >= 5) + { + regInfo.RegionName = ""; + for (int i = 4; i < cmdparams.Count; i++) + regInfo.RegionName += cmdparams[i] + " "; + } } } - return; } } @@ -292,49 +313,16 @@ namespace OpenSim if (((realXLoc == 0) && (realYLoc == 0)) || (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) && ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896)))) { - if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo)) + if (HGHyperlink.TryCreateLink(m_sceneManager.CurrentOrFirstScene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo)) { regInfo.RegionName = config.GetString("localName", ""); } } } - private bool TryCreateLink(uint xloc, uint yloc, uint externalPort, string externalHostName, out RegionInfo regInfo) - { - regInfo = new RegionInfo(); - regInfo.RegionLocX = xloc; - regInfo.RegionLocY = yloc; - regInfo.ExternalHostName = externalHostName; - regInfo.HttpPort = externalPort; - //regInfo.RemotingPort = remotingPort; - try - { - regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0); - } - catch (Exception e) - { - m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message); - LinkRegionCmdUsage(); - return false; - } - regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString(); - - // Finally, link it - try - { - m_sceneManager.CurrentOrFirstScene.CommsManager.GridService.RegisterRegion(regInfo); - } - catch (Exception e) - { - m_log.Warn("[HGrid] Unable to link region: " + e.StackTrace); - return false; - } - - return true; - } - private void LinkRegionCmdUsage() { + Console.WriteLine("Usage: link-region :[:]"); Console.WriteLine("Usage: link-region []"); } } diff --git a/OpenSim/Region/Communications/Hypergrid/HGGridServices.cs b/OpenSim/Region/Communications/Hypergrid/HGGridServices.cs index a39161b..2ca956b 100644 --- a/OpenSim/Region/Communications/Hypergrid/HGGridServices.cs +++ b/OpenSim/Region/Communications/Hypergrid/HGGridServices.cs @@ -329,14 +329,13 @@ namespace OpenSim.Region.Communications.Hypergrid info.RegionSettings.TerrainImageID = ass.Metadata.FullID; ass.Metadata.Type = (int)AssetType.Texture; ass.Metadata.Temporary = false; - //imageData.CopyTo(ass.Data, 0); ass.Data = imageData; m_assetcache.AddAsset(ass); } - catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke + catch // LEGIT: Catching problems caused by OpenJPEG p/invoke { - Console.WriteLine("Failed getting/storing map image: " + e); + Console.WriteLine("[HGrid]: Failed getting/storing map image, because it is probably already in the cache"); } } @@ -463,6 +462,7 @@ namespace OpenSim.Region.Communications.Hypergrid if (hash["region_name"] != null) { info.RegionName = (string)hash["region_name"]; + //Console.WriteLine(">> " + info.RegionName); } if (hash["internal_port"] != null) { diff --git a/OpenSim/Region/Environment/Modules/World/WorldMap/MapSearchModule.cs b/OpenSim/Region/Environment/Modules/World/WorldMap/MapSearchModule.cs index fcac7e1..43ed1e4 100644 --- a/OpenSim/Region/Environment/Modules/World/WorldMap/MapSearchModule.cs +++ b/OpenSim/Region/Environment/Modules/World/WorldMap/MapSearchModule.cs @@ -44,7 +44,7 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); Scene m_scene = null; // only need one for communication with GridService - private Random random; + List m_scenes = new List(); #region IRegionModule Members public void Initialise(Scene scene, IConfigSource source) @@ -52,9 +52,9 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap if (m_scene == null) { m_scene = scene; - random = new Random(); } + m_scenes.Add(scene); scene.EventManager.OnNewClient += OnNewClient; } @@ -65,6 +65,7 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap public void Close() { m_scene = null; + m_scenes.Clear(); } public string Name @@ -110,7 +111,11 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap if (mapName.Contains(".") && mapName.Contains(":")) { // It probably is a domain name. Try to link to it. - TryLinkRegion(remoteClient, mapName, regionInfos); + RegionInfo regInfo; + Scene cScene = GetClientScene(remoteClient); + regInfo = HGHyperlink.TryLinkRegion(cScene, remoteClient, mapName); + if (regInfo != null) + regionInfos.Add(regInfo); } } @@ -154,116 +159,14 @@ namespace OpenSim.Region.Environment.Modules.World.WorldMap return (m_scene.SceneGridService is HGSceneCommunicationService); } - private void TryLinkRegion(IClientAPI client, string mapName, List regionInfos) + private Scene GetClientScene(IClientAPI client) { - string host = "127.0.0.1"; - string portstr; - uint port = 9000; - string[] parts = mapName.Split(new char[] { ':' }); - if (parts.Length >= 1) + foreach (Scene s in m_scenes) { - host = parts[0]; + if (client.Scene.RegionInfo.RegionHandle == s.RegionInfo.RegionHandle) + return s; } - if (parts.Length >= 2) - { - portstr = parts[1]; - UInt32.TryParse(portstr, out port); - } - - // Sanity check. Don't ever link to this sim. - IPAddress ipaddr = null; - try - { - ipaddr = Util.GetHostFromDNS(host); - } - catch { } - - if ((ipaddr != null) && - !((m_scene.RegionInfo.ExternalEndPoint.Address.Equals(ipaddr)) && (m_scene.RegionInfo.HttpPort == port))) - { - uint xloc = (uint)(random.Next(0, Int16.MaxValue)); - RegionInfo regInfo; - bool success = TryCreateLink(client, xloc, 0, port, host, out regInfo); - if (success) - { - regInfo.RegionName = mapName; - regionInfos.Add(regInfo); - } - } - } - - private bool TryCreateLink(IClientAPI client, uint xloc, uint yloc, uint externalPort, string externalHostName, out RegionInfo regInfo) - { - m_log.DebugFormat("[HGrid]: Dynamic link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc); - - regInfo = new RegionInfo(); - regInfo.RegionLocX = xloc; - regInfo.RegionLocY = yloc; - regInfo.ExternalHostName = externalHostName; - regInfo.HttpPort = externalPort; - try - { - regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0); - } - catch (Exception e) - { - m_log.Warn("[HGrid] Wrong format for link-region: " + e.Message); - return false; - } - //regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString(); - - // Finally, link it - try - { - m_scene.CommsManager.GridService.RegisterRegion(regInfo); - } - catch (Exception e) - { - m_log.Warn("[HGrid] Unable to dynamically link region: " + e.Message); - return false; - } - - if (!Check4096(client, regInfo)) - { - return false; - } - - m_log.Debug("[HGrid] Dynamic link region succeeded"); - return true; + return m_scene; } - - /// - /// Cope with this viewer limitation. - /// - /// - /// - private bool Check4096(IClientAPI client, RegionInfo regInfo) - { - ulong realHandle; - if (UInt64.TryParse(regInfo.regionSecret, out realHandle)) - { - uint x, y; - Utils.LongToUInts(realHandle, out x, out y); - x = x / Constants.RegionSize; - y = y / Constants.RegionSize; - - if ((Math.Abs((int)m_scene.RegionInfo.RegionLocX - (int)x) >= 4096) || - (Math.Abs((int)m_scene.RegionInfo.RegionLocY - (int)y) >= 4096)) - { - m_scene.CommsManager.GridService.DeregisterRegion(regInfo); - m_log.Debug("[HGrid]: Region deregistered."); - client.SendAlertMessage("Region is too far (" + x + ", " + y + ")"); - return false; - } - return true; - } - else - { - m_scene.CommsManager.GridService.RegisterRegion(regInfo); - m_log.Debug("[HGrid]: Gnomes. Region deregistered."); - return false; - } - } - } } diff --git a/OpenSim/Region/Framework/Scenes/Hypergrid/HGHyperlink.cs b/OpenSim/Region/Framework/Scenes/Hypergrid/HGHyperlink.cs new file mode 100644 index 0000000..6ee28cb --- /dev/null +++ b/OpenSim/Region/Framework/Scenes/Hypergrid/HGHyperlink.cs @@ -0,0 +1,192 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Reflection; +using System.Net; +using OpenSim.Framework; +using OpenSim.Framework.Communications; +using OpenMetaverse; +using log4net; +using Nini.Config; + +namespace OpenSim.Region.Framework.Scenes.Hypergrid +{ + public class HGHyperlink + { + private static readonly ILog m_log = + LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static Random random = new Random(); + + public static RegionInfo TryLinkRegionToCoords(Scene m_scene, IClientAPI client, string mapName, uint xloc, uint yloc) + { + string host = "127.0.0.1"; + string portstr; + string regionName = ""; + uint port = 9000; + string[] parts = mapName.Split(new char[] { ':' }); + if (parts.Length >= 1) + { + host = parts[0]; + } + if (parts.Length >= 2) + { + portstr = parts[1]; + if (!UInt32.TryParse(portstr, out port)) + regionName = parts[1]; + } + // always take the last one + if (parts.Length >= 3) + { + regionName = parts[2]; + } + + // Sanity check. Don't ever link to this sim. + IPAddress ipaddr = null; + try + { + ipaddr = Util.GetHostFromDNS(host); + } + catch { } + + if ((ipaddr != null) && + !((m_scene.RegionInfo.ExternalEndPoint.Address.Equals(ipaddr)) && (m_scene.RegionInfo.HttpPort == port))) + { + RegionInfo regInfo; + bool success = TryCreateLink(m_scene, client, xloc, yloc, regionName, port, host, out regInfo); + if (success) + { + regInfo.RegionName = mapName; + return regInfo; + } + } + + return null; + } + + public static RegionInfo TryLinkRegion(Scene m_scene, IClientAPI client, string mapName) + { + uint xloc = (uint)(random.Next(0, Int16.MaxValue)); + return TryLinkRegionToCoords(m_scene, client, mapName, xloc, 0); + } + + public static bool TryCreateLink(Scene m_scene, IClientAPI client, uint xloc, uint yloc, + string externalRegionName, uint externalPort, string externalHostName, out RegionInfo regInfo) + { + m_log.DebugFormat("[HGrid]: Link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc); + + regInfo = new RegionInfo(); + regInfo.RegionName = externalRegionName; + regInfo.HttpPort = externalPort; + regInfo.ExternalHostName = externalHostName; + regInfo.RegionLocX = xloc; + regInfo.RegionLocY = yloc; + + try + { + regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0); + } + catch (Exception e) + { + m_log.Warn("[HGrid]: Wrong format for link-region: " + e.Message); + return false; + } + //regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString(); + + // Finally, link it + try + { + m_scene.CommsManager.GridService.RegisterRegion(regInfo); + } + catch (Exception e) + { + m_log.Warn("[HGrid]: Unable to link region: " + e.Message); + return false; + } + + uint x, y; + if (!Check4096(m_scene, regInfo, out x, out y)) + { + m_scene.CommsManager.GridService.DeregisterRegion(regInfo); + if (client != null) + client.SendAlertMessage("Region is too far (" + x + ", " + y + ")"); + m_log.Info("[HGrid]: Unable to link, region is too far (" + x + ", " + y + ")"); + return false; + } + + if (!CheckCoords(m_scene.RegionInfo.RegionLocX, m_scene.RegionInfo.RegionLocY, x, y)) + { + m_scene.CommsManager.GridService.DeregisterRegion(regInfo); + if (client != null) + client.SendAlertMessage("Region has incompatible coordinates (" + x + ", " + y + ")"); + m_log.Info("[HGrid]: Unable to link, region has incompatible coordinates (" + x + ", " + y + ")"); + return false; + } + + m_log.Debug("[HGrid]: link region succeeded"); + return true; + } + + /// + /// Cope with this viewer limitation. + /// + /// + /// + public static bool Check4096(Scene m_scene, RegionInfo regInfo, out uint x, out uint y) + { + ulong realHandle; + if (UInt64.TryParse(regInfo.regionSecret, out realHandle)) + { + Utils.LongToUInts(realHandle, out x, out y); + x = x / Constants.RegionSize; + y = y / Constants.RegionSize; + + if ((Math.Abs((int)m_scene.RegionInfo.RegionLocX - (int)x) >= 4096) || + (Math.Abs((int)m_scene.RegionInfo.RegionLocY - (int)y) >= 4096)) + { + return false; + } + return true; + } + else + { + m_scene.CommsManager.GridService.RegisterRegion(regInfo); + m_log.Debug("[HGrid]: Gnomes. Region deregistered."); + x = y = 0; + return false; + } + } + + public static bool CheckCoords(uint thisx, uint thisy, uint x, uint y) + { + if ((thisx == x) && (thisy == y)) + return false; + return true; + } + + } +} -- cgit v1.1