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