aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/GridService/HypergridLinker.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Services/GridService/HypergridLinker.cs634
1 files changed, 634 insertions, 0 deletions
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}