aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/GridService/HypergridLinker.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Services/GridService/HypergridLinker.cs635
1 files changed, 635 insertions, 0 deletions
diff --git a/OpenSim/Services/GridService/HypergridLinker.cs b/OpenSim/Services/GridService/HypergridLinker.cs
new file mode 100644
index 0000000..58746d0
--- /dev/null
+++ b/OpenSim/Services/GridService/HypergridLinker.cs
@@ -0,0 +1,635 @@
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 Util.GetHostFromDNS(host);
176 }
177 catch
178 {
179 reason = "Malformed hostname";
180 return null;
181 }
182
183 GridRegion regInfo;
184 bool success = TryCreateLink(scopeID, xloc, yloc, regionName, port, host, out regInfo, out reason);
185 if (success)
186 {
187 regInfo.RegionName = mapName;
188 return regInfo;
189 }
190
191 return null;
192 }
193
194
195 // From the command line and the 2 above
196 public bool TryCreateLink(UUID scopeID, int xloc, int yloc,
197 string externalRegionName, uint externalPort, string externalHostName, out GridRegion regInfo, out string reason)
198 {
199 m_log.DebugFormat("[HYPERGRID LINKER]: Link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc);
200
201 reason = string.Empty;
202 regInfo = new GridRegion();
203 regInfo.RegionName = externalRegionName;
204 regInfo.HttpPort = externalPort;
205 regInfo.ExternalHostName = externalHostName;
206 regInfo.RegionLocX = xloc;
207 regInfo.RegionLocY = yloc;
208 regInfo.ScopeID = scopeID;
209
210 try
211 {
212 regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
213 }
214 catch (Exception e)
215 {
216 m_log.Warn("[HYPERGRID LINKER]: Wrong format for link-region: " + e.Message);
217 reason = "Internal error";
218 return false;
219 }
220
221 // Finally, link it
222 ulong handle = 0;
223 UUID regionID = UUID.Zero;
224 string externalName = string.Empty;
225 string imageURL = string.Empty;
226 if (!m_GatekeeperConnector.LinkRegion(regInfo, out regionID, out handle, out externalName, out imageURL, out reason))
227 return false;
228
229 if (regionID != UUID.Zero)
230 {
231 GridRegion r = m_GridService.GetRegionByUUID(scopeID, regionID);
232 if (r != null)
233 {
234 m_log.DebugFormat("[HYPERGRID LINKER]: Region already exists in coordinates {0} {1}", r.RegionLocX / Constants.RegionSize, r.RegionLocY / Constants.RegionSize);
235 regInfo = r;
236 return true;
237 }
238
239 regInfo.RegionID = regionID;
240 Uri uri = null;
241 try
242 {
243 uri = new Uri(externalName);
244 regInfo.ExternalHostName = uri.Host;
245 regInfo.HttpPort = (uint)uri.Port;
246 }
247 catch
248 {
249 m_log.WarnFormat("[HYPERGRID LINKER]: Remote Gatekeeper at {0} provided malformed ExternalName {1}", regInfo.ExternalHostName, externalName);
250 }
251 regInfo.RegionName = regInfo.ExternalHostName + ":" + regInfo.HttpPort + ":" + regInfo.RegionName;
252 // Try get the map image
253 //regInfo.TerrainImage = m_GatekeeperConnector.GetMapImage(regionID, imageURL);
254 // I need a texture that works for this... the one I tried doesn't seem to be working
255 regInfo.TerrainImage = m_HGMapImage;
256
257 AddHyperlinkRegion(regInfo, handle);
258 m_log.Info("[HYPERGRID LINKER]: Successfully linked to region_uuid " + regInfo.RegionID);
259
260 }
261 else
262 {
263 m_log.Warn("[HYPERGRID LINKER]: Unable to link region");
264 reason = "Remote region could not be found";
265 return false;
266 }
267
268 uint x, y;
269 if (!Check4096(handle, out x, out y))
270 {
271 RemoveHyperlinkRegion(regInfo.RegionID);
272 reason = "Region is too far (" + x + ", " + y + ")";
273 m_log.Info("[HYPERGRID LINKER]: Unable to link, region is too far (" + x + ", " + y + ")");
274 return false;
275 }
276
277 m_log.Debug("[HYPERGRID LINKER]: link region succeeded");
278 return true;
279 }
280
281 public bool TryUnlinkRegion(string mapName)
282 {
283 GridRegion regInfo = null;
284 if (mapName.Contains(":"))
285 {
286 string host = "127.0.0.1";
287 //string portstr;
288 //string regionName = "";
289 uint port = 9000;
290 string[] parts = mapName.Split(new char[] { ':' });
291 if (parts.Length >= 1)
292 {
293 host = parts[0];
294 }
295
296 foreach (GridRegion r in m_HyperlinkRegions.Values)
297 if (host.Equals(r.ExternalHostName) && (port == r.HttpPort))
298 regInfo = r;
299 }
300 else
301 {
302 foreach (GridRegion r in m_HyperlinkRegions.Values)
303 if (r.RegionName.Equals(mapName))
304 regInfo = r;
305 }
306 if (regInfo != null)
307 {
308 RemoveHyperlinkRegion(regInfo.RegionID);
309 return true;
310 }
311 else
312 {
313 m_log.InfoFormat("[HYPERGRID LINKER]: Region {0} not found", mapName);
314 return false;
315 }
316 }
317
318 /// <summary>
319 /// Cope with this viewer limitation.
320 /// </summary>
321 /// <param name="regInfo"></param>
322 /// <returns></returns>
323 public bool Check4096(ulong realHandle, out uint x, out uint y)
324 {
325 GridRegion defRegion = DefaultRegion;
326
327 uint ux = 0, uy = 0;
328 Utils.LongToUInts(realHandle, out ux, out uy);
329 x = ux / Constants.RegionSize;
330 y = uy / Constants.RegionSize;
331
332 if ((Math.Abs((int)defRegion.RegionLocX - ux) >= 4096 * Constants.RegionSize) ||
333 (Math.Abs((int)defRegion.RegionLocY - uy) >= 4096 * Constants.RegionSize))
334 {
335 return false;
336 }
337 return true;
338 }
339
340 private void AddHyperlinkRegion(GridRegion regionInfo, ulong regionHandle)
341 {
342 //m_HyperlinkRegions[regionInfo.RegionID] = regionInfo;
343 //m_HyperlinkHandles[regionInfo.RegionID] = regionHandle;
344
345 RegionData rdata = m_GridService.RegionInfo2RegionData(regionInfo);
346 int flags = (int)OpenSim.Data.RegionFlags.Hyperlink + (int)OpenSim.Data.RegionFlags.NoDirectLogin + (int)OpenSim.Data.RegionFlags.RegionOnline;
347 rdata.Data["flags"] = flags.ToString();
348
349 m_Database.Store(rdata);
350
351 }
352
353 private void RemoveHyperlinkRegion(UUID regionID)
354 {
355 //// Try the hyperlink collection
356 //if (m_HyperlinkRegions.ContainsKey(regionID))
357 //{
358 // m_HyperlinkRegions.Remove(regionID);
359 // m_HyperlinkHandles.Remove(regionID);
360 //}
361 m_Database.Delete(regionID);
362 }
363
364 #endregion
365
366
367 #region Console Commands
368
369 public void HandleShow(string module, string[] cmd)
370 {
371 MainConsole.Instance.Output("Not Implemented Yet");
372 //if (cmd.Length != 2)
373 //{
374 // MainConsole.Instance.Output("Syntax: show hyperlinks");
375 // return;
376 //}
377 //List<GridRegion> regions = new List<GridRegion>(m_HypergridService.m_HyperlinkRegions.Values);
378 //if (regions == null || regions.Count < 1)
379 //{
380 // MainConsole.Instance.Output("No hyperlinks");
381 // return;
382 //}
383
384 //MainConsole.Instance.Output("Region Name Region UUID");
385 //MainConsole.Instance.Output("Location URI");
386 //MainConsole.Instance.Output("Owner ID ");
387 //MainConsole.Instance.Output("-------------------------------------------------------------------------------");
388 //foreach (GridRegion r in regions)
389 //{
390 // MainConsole.Instance.Output(String.Format("{0,-20} {1}\n{2,-20} {3}\n{4,-39} \n\n",
391 // r.RegionName, r.RegionID,
392 // String.Format("{0},{1}", r.RegionLocX, r.RegionLocY), "http://" + r.ExternalHostName + ":" + r.HttpPort.ToString(),
393 // r.EstateOwner.ToString()));
394 //}
395 //return;
396 }
397 public void RunCommand(string module, string[] cmdparams)
398 {
399 List<string> args = new List<string>(cmdparams);
400 if (args.Count < 1)
401 return;
402
403 string command = args[0];
404 args.RemoveAt(0);
405
406 cmdparams = args.ToArray();
407
408 RunHGCommand(command, cmdparams);
409
410 }
411
412 private void RunHGCommand(string command, string[] cmdparams)
413 {
414 if (command.Equals("link-mapping"))
415 {
416 if (cmdparams.Length == 2)
417 {
418 try
419 {
420 m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
421 m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
422 m_enableAutoMapping = true;
423 }
424 catch (Exception)
425 {
426 m_autoMappingX = 0;
427 m_autoMappingY = 0;
428 m_enableAutoMapping = false;
429 }
430 }
431 }
432 else if (command.Equals("link-region"))
433 {
434 if (cmdparams.Length < 3)
435 {
436 if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
437 {
438 LoadXmlLinkFile(cmdparams);
439 }
440 else
441 {
442 LinkRegionCmdUsage();
443 }
444 return;
445 }
446
447 if (cmdparams[2].Contains(":"))
448 {
449 // New format
450 int xloc, yloc;
451 string mapName;
452 try
453 {
454 xloc = Convert.ToInt32(cmdparams[0]);
455 yloc = Convert.ToInt32(cmdparams[1]);
456 mapName = cmdparams[2];
457 if (cmdparams.Length > 3)
458 for (int i = 3; i < cmdparams.Length; i++)
459 mapName += " " + cmdparams[i];
460
461 //m_log.Info(">> MapName: " + mapName);
462 }
463 catch (Exception e)
464 {
465 MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
466 LinkRegionCmdUsage();
467 return;
468 }
469
470 // Convert cell coordinates given by the user to meters
471 xloc = xloc * (int)Constants.RegionSize;
472 yloc = yloc * (int)Constants.RegionSize;
473 string reason = string.Empty;
474 if (TryLinkRegionToCoords(UUID.Zero, mapName, xloc, yloc, out reason) == null)
475 MainConsole.Instance.Output("Failed to link region: " + reason);
476 else
477 MainConsole.Instance.Output("Hyperlink established");
478 }
479 else
480 {
481 // old format
482 GridRegion regInfo;
483 int xloc, yloc;
484 uint externalPort;
485 string externalHostName;
486 try
487 {
488 xloc = Convert.ToInt32(cmdparams[0]);
489 yloc = Convert.ToInt32(cmdparams[1]);
490 externalPort = Convert.ToUInt32(cmdparams[3]);
491 externalHostName = cmdparams[2];
492 //internalPort = Convert.ToUInt32(cmdparams[4]);
493 //remotingPort = Convert.ToUInt32(cmdparams[5]);
494 }
495 catch (Exception e)
496 {
497 MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
498 LinkRegionCmdUsage();
499 return;
500 }
501
502 // Convert cell coordinates given by the user to meters
503 xloc = xloc * (int)Constants.RegionSize;
504 yloc = yloc * (int)Constants.RegionSize;
505 string reason = string.Empty;
506 if (TryCreateLink(UUID.Zero, xloc, yloc, "", externalPort, externalHostName, out regInfo, out reason))
507 {
508 if (cmdparams.Length >= 5)
509 {
510 regInfo.RegionName = "";
511 for (int i = 4; i < cmdparams.Length; i++)
512 regInfo.RegionName += cmdparams[i] + " ";
513 }
514 }
515 }
516 return;
517 }
518 else if (command.Equals("unlink-region"))
519 {
520 if (cmdparams.Length < 1)
521 {
522 UnlinkRegionCmdUsage();
523 return;
524 }
525 if (TryUnlinkRegion(cmdparams[0]))
526 MainConsole.Instance.Output("Successfully unlinked " + cmdparams[0]);
527 else
528 MainConsole.Instance.Output("Unable to unlink " + cmdparams[0] + ", region not found.");
529 }
530 }
531
532 private void LoadXmlLinkFile(string[] cmdparams)
533 {
534 //use http://www.hgurl.com/hypergrid.xml for test
535 try
536 {
537 XmlReader r = XmlReader.Create(cmdparams[0]);
538 XmlConfigSource cs = new XmlConfigSource(r);
539 string[] excludeSections = null;
540
541 if (cmdparams.Length == 2)
542 {
543 if (cmdparams[1].ToLower().StartsWith("excludelist:"))
544 {
545 string excludeString = cmdparams[1].ToLower();
546 excludeString = excludeString.Remove(0, 12);
547 char[] splitter = { ';' };
548
549 excludeSections = excludeString.Split(splitter);
550 }
551 }
552
553 for (int i = 0; i < cs.Configs.Count; i++)
554 {
555 bool skip = false;
556 if ((excludeSections != null) && (excludeSections.Length > 0))
557 {
558 for (int n = 0; n < excludeSections.Length; n++)
559 {
560 if (excludeSections[n] == cs.Configs[i].Name.ToLower())
561 {
562 skip = true;
563 break;
564 }
565 }
566 }
567 if (!skip)
568 {
569 ReadLinkFromConfig(cs.Configs[i]);
570 }
571 }
572 }
573 catch (Exception e)
574 {
575 m_log.Error(e.ToString());
576 }
577 }
578
579
580 private void ReadLinkFromConfig(IConfig config)
581 {
582 GridRegion regInfo;
583 int xloc, yloc;
584 uint externalPort;
585 string externalHostName;
586 uint realXLoc, realYLoc;
587
588 xloc = Convert.ToInt32(config.GetString("xloc", "0"));
589 yloc = Convert.ToInt32(config.GetString("yloc", "0"));
590 externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
591 externalHostName = config.GetString("externalHostName", "");
592 realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
593 realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
594
595 if (m_enableAutoMapping)
596 {
597 xloc = (int)((xloc % 100) + m_autoMappingX);
598 yloc = (int)((yloc % 100) + m_autoMappingY);
599 }
600
601 if (((realXLoc == 0) && (realYLoc == 0)) ||
602 (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
603 ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
604 {
605 xloc = xloc * (int)Constants.RegionSize;
606 yloc = yloc * (int)Constants.RegionSize;
607 string reason = string.Empty;
608 if (TryCreateLink(UUID.Zero, xloc, yloc, "", externalPort,
609 externalHostName, out regInfo, out reason))
610 {
611 regInfo.RegionName = config.GetString("localName", "");
612 }
613 else
614 MainConsole.Instance.Output("Unable to link " + externalHostName + ": " + reason);
615 }
616 }
617
618
619 private void LinkRegionCmdUsage()
620 {
621 MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
622 MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
623 MainConsole.Instance.Output("Usage: link-region <URI_of_xml> [<exclude>]");
624 }
625
626 private void UnlinkRegionCmdUsage()
627 {
628 MainConsole.Instance.Output("Usage: unlink-region <HostName>:<HttpPort>");
629 MainConsole.Instance.Output("Usage: unlink-region <LocalName>");
630 }
631
632 #endregion
633
634 }
635}