aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Framework')
-rw-r--r--OpenSim/Region/Framework/Scenes/Hypergrid/HGHyperlink.cs192
1 files changed, 192 insertions, 0 deletions
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 @@
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 OpenSim 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.Reflection;
30using System.Net;
31using OpenSim.Framework;
32using OpenSim.Framework.Communications;
33using OpenMetaverse;
34using log4net;
35using Nini.Config;
36
37namespace OpenSim.Region.Framework.Scenes.Hypergrid
38{
39 public class HGHyperlink
40 {
41 private static readonly ILog m_log =
42 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43 private static Random random = new Random();
44
45 public static RegionInfo TryLinkRegionToCoords(Scene m_scene, IClientAPI client, string mapName, uint xloc, uint yloc)
46 {
47 string host = "127.0.0.1";
48 string portstr;
49 string regionName = "";
50 uint port = 9000;
51 string[] parts = mapName.Split(new char[] { ':' });
52 if (parts.Length >= 1)
53 {
54 host = parts[0];
55 }
56 if (parts.Length >= 2)
57 {
58 portstr = parts[1];
59 if (!UInt32.TryParse(portstr, out port))
60 regionName = parts[1];
61 }
62 // always take the last one
63 if (parts.Length >= 3)
64 {
65 regionName = parts[2];
66 }
67
68 // Sanity check. Don't ever link to this sim.
69 IPAddress ipaddr = null;
70 try
71 {
72 ipaddr = Util.GetHostFromDNS(host);
73 }
74 catch { }
75
76 if ((ipaddr != null) &&
77 !((m_scene.RegionInfo.ExternalEndPoint.Address.Equals(ipaddr)) && (m_scene.RegionInfo.HttpPort == port)))
78 {
79 RegionInfo regInfo;
80 bool success = TryCreateLink(m_scene, client, xloc, yloc, regionName, port, host, out regInfo);
81 if (success)
82 {
83 regInfo.RegionName = mapName;
84 return regInfo;
85 }
86 }
87
88 return null;
89 }
90
91 public static RegionInfo TryLinkRegion(Scene m_scene, IClientAPI client, string mapName)
92 {
93 uint xloc = (uint)(random.Next(0, Int16.MaxValue));
94 return TryLinkRegionToCoords(m_scene, client, mapName, xloc, 0);
95 }
96
97 public static bool TryCreateLink(Scene m_scene, IClientAPI client, uint xloc, uint yloc,
98 string externalRegionName, uint externalPort, string externalHostName, out RegionInfo regInfo)
99 {
100 m_log.DebugFormat("[HGrid]: Link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc);
101
102 regInfo = new RegionInfo();
103 regInfo.RegionName = externalRegionName;
104 regInfo.HttpPort = externalPort;
105 regInfo.ExternalHostName = externalHostName;
106 regInfo.RegionLocX = xloc;
107 regInfo.RegionLocY = yloc;
108
109 try
110 {
111 regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
112 }
113 catch (Exception e)
114 {
115 m_log.Warn("[HGrid]: Wrong format for link-region: " + e.Message);
116 return false;
117 }
118 //regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString();
119
120 // Finally, link it
121 try
122 {
123 m_scene.CommsManager.GridService.RegisterRegion(regInfo);
124 }
125 catch (Exception e)
126 {
127 m_log.Warn("[HGrid]: Unable to link region: " + e.Message);
128 return false;
129 }
130
131 uint x, y;
132 if (!Check4096(m_scene, regInfo, out x, out y))
133 {
134 m_scene.CommsManager.GridService.DeregisterRegion(regInfo);
135 if (client != null)
136 client.SendAlertMessage("Region is too far (" + x + ", " + y + ")");
137 m_log.Info("[HGrid]: Unable to link, region is too far (" + x + ", " + y + ")");
138 return false;
139 }
140
141 if (!CheckCoords(m_scene.RegionInfo.RegionLocX, m_scene.RegionInfo.RegionLocY, x, y))
142 {
143 m_scene.CommsManager.GridService.DeregisterRegion(regInfo);
144 if (client != null)
145 client.SendAlertMessage("Region has incompatible coordinates (" + x + ", " + y + ")");
146 m_log.Info("[HGrid]: Unable to link, region has incompatible coordinates (" + x + ", " + y + ")");
147 return false;
148 }
149
150 m_log.Debug("[HGrid]: link region succeeded");
151 return true;
152 }
153
154 /// <summary>
155 /// Cope with this viewer limitation.
156 /// </summary>
157 /// <param name="regInfo"></param>
158 /// <returns></returns>
159 public static bool Check4096(Scene m_scene, RegionInfo regInfo, out uint x, out uint y)
160 {
161 ulong realHandle;
162 if (UInt64.TryParse(regInfo.regionSecret, out realHandle))
163 {
164 Utils.LongToUInts(realHandle, out x, out y);
165 x = x / Constants.RegionSize;
166 y = y / Constants.RegionSize;
167
168 if ((Math.Abs((int)m_scene.RegionInfo.RegionLocX - (int)x) >= 4096) ||
169 (Math.Abs((int)m_scene.RegionInfo.RegionLocY - (int)y) >= 4096))
170 {
171 return false;
172 }
173 return true;
174 }
175 else
176 {
177 m_scene.CommsManager.GridService.RegisterRegion(regInfo);
178 m_log.Debug("[HGrid]: Gnomes. Region deregistered.");
179 x = y = 0;
180 return false;
181 }
182 }
183
184 public static bool CheckCoords(uint thisx, uint thisy, uint x, uint y)
185 {
186 if ((thisx == x) && (thisy == y))
187 return false;
188 return true;
189 }
190
191 }
192}