HypergridLinker.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Linq;
  32. using System.Net;
  33. using System.Reflection;
  34. using System.Xml;
  35. using Nini.Config;
  36. using log4net;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Data;
  40. using OpenSim.Server.Base;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Services.Connectors.Hypergrid;
  43. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  44. using OpenMetaverse;
  45. namespace OpenSim.Services.GridService
  46. {
  47. public class HypergridLinker
  48. {
  49. private static readonly ILog m_log =
  50. LogManager.GetLogger(
  51. MethodBase.GetCurrentMethod().DeclaringType);
  52. private static uint m_autoMappingX = 0;
  53. private static uint m_autoMappingY = 0;
  54. private static bool m_enableAutoMapping = false;
  55. protected IRegionData m_Database;
  56. protected GridService m_GridService;
  57. protected IAssetService m_AssetService;
  58. protected GatekeeperServiceConnector m_GatekeeperConnector;
  59. protected UUID m_ScopeID = UUID.Zero;
  60. // protected bool m_Check4096 = true;
  61. protected string m_MapTileDirectory = string.Empty;
  62. protected string m_ThisGatekeeper = string.Empty;
  63. protected Uri m_ThisGatekeeperURI = null;
  64. // Hyperlink regions are hyperlinks on the map
  65. public readonly Dictionary<UUID, GridRegion> m_HyperlinkRegions = new Dictionary<UUID, GridRegion>();
  66. protected Dictionary<UUID, ulong> m_HyperlinkHandles = new Dictionary<UUID, ulong>();
  67. protected GridRegion m_DefaultRegion;
  68. protected GridRegion DefaultRegion
  69. {
  70. get
  71. {
  72. if (m_DefaultRegion == null)
  73. {
  74. List<GridRegion> defs = m_GridService.GetDefaultHypergridRegions(m_ScopeID);
  75. if (defs != null && defs.Count > 0)
  76. m_DefaultRegion = defs[0];
  77. else
  78. {
  79. // Get any region
  80. defs = m_GridService.GetRegionsByName(m_ScopeID, "", 1);
  81. if (defs != null && defs.Count > 0)
  82. m_DefaultRegion = defs[0];
  83. else
  84. {
  85. // This shouldn't happen
  86. m_DefaultRegion = new GridRegion(1000, 1000);
  87. m_log.Error("[HYPERGRID LINKER]: Something is wrong with this grid. It has no regions?");
  88. }
  89. }
  90. }
  91. return m_DefaultRegion;
  92. }
  93. }
  94. public HypergridLinker(IConfigSource config, GridService gridService, IRegionData db)
  95. {
  96. IConfig gridConfig = config.Configs["GridService"];
  97. if (gridConfig == null)
  98. return;
  99. if (!gridConfig.GetBoolean("HypergridLinker", false))
  100. return;
  101. m_Database = db;
  102. m_GridService = gridService;
  103. m_log.DebugFormat("[HYPERGRID LINKER]: Starting with db {0}", db.GetType());
  104. string assetService = gridConfig.GetString("AssetService", string.Empty);
  105. Object[] args = new Object[] { config };
  106. if (assetService != string.Empty)
  107. m_AssetService = ServerUtils.LoadPlugin<IAssetService>(assetService, args);
  108. string scope = gridConfig.GetString("ScopeID", string.Empty);
  109. if (scope != string.Empty)
  110. UUID.TryParse(scope, out m_ScopeID);
  111. // m_Check4096 = gridConfig.GetBoolean("Check4096", true);
  112. m_MapTileDirectory = gridConfig.GetString("MapTileDirectory", "maptiles");
  113. m_ThisGatekeeper = Util.GetConfigVarFromSections<string>(config, "GatekeeperURI",
  114. new string[] { "Startup", "Hypergrid", "GridService" }, String.Empty);
  115. // Legacy. Remove soon!
  116. m_ThisGatekeeper = gridConfig.GetString("Gatekeeper", m_ThisGatekeeper);
  117. try
  118. {
  119. m_ThisGatekeeperURI = new Uri(m_ThisGatekeeper);
  120. }
  121. catch
  122. {
  123. m_log.WarnFormat("[HYPERGRID LINKER]: Malformed URL in [GridService], variable Gatekeeper = {0}", m_ThisGatekeeper);
  124. }
  125. m_GatekeeperConnector = new GatekeeperServiceConnector(m_AssetService);
  126. m_log.Debug("[HYPERGRID LINKER]: Loaded all services...");
  127. if (!string.IsNullOrEmpty(m_MapTileDirectory))
  128. {
  129. try
  130. {
  131. Directory.CreateDirectory(m_MapTileDirectory);
  132. }
  133. catch (Exception e)
  134. {
  135. m_log.WarnFormat("[HYPERGRID LINKER]: Could not create map tile storage directory {0}: {1}", m_MapTileDirectory, e);
  136. m_MapTileDirectory = string.Empty;
  137. }
  138. }
  139. if (MainConsole.Instance != null)
  140. {
  141. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-region",
  142. "link-region <Xloc> <Yloc> <ServerURI> [<RemoteRegionName>]",
  143. "Link a HyperGrid Region. Examples for <ServerURI>: http://grid.net:8002/ or http://example.org/path/foo.php", RunCommand);
  144. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-region",
  145. "link-region <Xloc> <Yloc> <RegionIP> <RegionPort> [<RemoteRegionName>]",
  146. "Link a hypergrid region (deprecated)", RunCommand);
  147. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "unlink-region",
  148. "unlink-region <local name>",
  149. "Unlink a hypergrid region", RunCommand);
  150. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-mapping", "link-mapping [<x> <y>]",
  151. "Set local coordinate to map HG regions to", RunCommand);
  152. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "show hyperlinks", "show hyperlinks",
  153. "List the HG regions", HandleShow);
  154. }
  155. }
  156. #region Link Region
  157. // from map search
  158. public GridRegion LinkRegion(UUID scopeID, string regionDescriptor)
  159. {
  160. string reason = string.Empty;
  161. int xloc = random.Next(0, Int16.MaxValue) * (int)Constants.RegionSize;
  162. return TryLinkRegionToCoords(scopeID, regionDescriptor, xloc, 0, out reason);
  163. }
  164. private static Random random = new Random();
  165. // From the command line link-region (obsolete) and the map
  166. public GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, out string reason)
  167. {
  168. return TryLinkRegionToCoords(scopeID, mapName, xloc, yloc, UUID.Zero, out reason);
  169. }
  170. public GridRegion TryLinkRegionToCoords(UUID scopeID, string mapName, int xloc, int yloc, UUID ownerID, out string reason)
  171. {
  172. reason = string.Empty;
  173. GridRegion regInfo = null;
  174. if (!mapName.StartsWith("http"))
  175. {
  176. string host = "127.0.0.1";
  177. string portstr;
  178. string regionName = "";
  179. uint port = 0;
  180. string[] parts = mapName.Split(new char[] { ':' });
  181. if (parts.Length >= 1)
  182. {
  183. host = parts[0];
  184. }
  185. if (parts.Length >= 2)
  186. {
  187. portstr = parts[1];
  188. //m_log.Debug("-- port = " + portstr);
  189. if (!UInt32.TryParse(portstr, out port))
  190. regionName = parts[1];
  191. }
  192. // always take the last one
  193. if (parts.Length >= 3)
  194. {
  195. regionName = parts[2];
  196. }
  197. bool success = TryCreateLink(scopeID, xloc, yloc, regionName, port, host, ownerID, out regInfo, out reason);
  198. if (success)
  199. {
  200. regInfo.RegionName = mapName;
  201. return regInfo;
  202. }
  203. }
  204. else
  205. {
  206. string[] parts = mapName.Split(new char[] {' '});
  207. string regionName = String.Empty;
  208. if (parts.Length > 1)
  209. {
  210. regionName = mapName.Substring(parts[0].Length + 1);
  211. regionName = regionName.Trim(new char[] {'"'});
  212. }
  213. if (TryCreateLink(scopeID, xloc, yloc, regionName, 0, null, parts[0], ownerID, out regInfo, out reason))
  214. {
  215. regInfo.RegionName = mapName;
  216. return regInfo;
  217. }
  218. }
  219. return null;
  220. }
  221. public bool TryCreateLink(UUID scopeID, int xloc, int yloc, string remoteRegionName, uint externalPort, string externalHostName, UUID ownerID, out GridRegion regInfo, out string reason)
  222. {
  223. return TryCreateLink(scopeID, xloc, yloc, remoteRegionName, externalPort, externalHostName, null, ownerID, out regInfo, out reason);
  224. }
  225. public bool TryCreateLink(UUID scopeID, int xloc, int yloc, string remoteRegionName, uint externalPort, string externalHostName, string serverURI, UUID ownerID, out GridRegion regInfo, out string reason)
  226. {
  227. m_log.DebugFormat("[HYPERGRID LINKER]: Link to {0} {1}, in {2}-{3}",
  228. ((serverURI == null) ? (externalHostName + ":" + externalPort) : serverURI),
  229. remoteRegionName, xloc / Constants.RegionSize, yloc / Constants.RegionSize);
  230. reason = string.Empty;
  231. Uri uri = null;
  232. regInfo = new GridRegion();
  233. if ( externalPort > 0)
  234. regInfo.HttpPort = externalPort;
  235. else
  236. regInfo.HttpPort = 0;
  237. if ( externalHostName != null)
  238. regInfo.ExternalHostName = externalHostName;
  239. else
  240. regInfo.ExternalHostName = "0.0.0.0";
  241. if ( serverURI != null)
  242. {
  243. regInfo.ServerURI = serverURI;
  244. try
  245. {
  246. uri = new Uri(serverURI);
  247. regInfo.ExternalHostName = uri.Host;
  248. regInfo.HttpPort = (uint)uri.Port;
  249. }
  250. catch {}
  251. }
  252. if ( remoteRegionName != string.Empty )
  253. regInfo.RegionName = remoteRegionName;
  254. regInfo.RegionLocX = xloc;
  255. regInfo.RegionLocY = yloc;
  256. regInfo.ScopeID = scopeID;
  257. regInfo.EstateOwner = ownerID;
  258. // Make sure we're not hyperlinking to regions on this grid!
  259. if (m_ThisGatekeeperURI != null)
  260. {
  261. if (regInfo.ExternalHostName == m_ThisGatekeeperURI.Host && regInfo.HttpPort == m_ThisGatekeeperURI.Port)
  262. {
  263. reason = "Cannot hyperlink to regions on the same grid";
  264. return false;
  265. }
  266. }
  267. else
  268. m_log.WarnFormat("[HYPERGRID LINKER]: Please set this grid's Gatekeeper's address in [GridService]!");
  269. // Check for free coordinates
  270. GridRegion region = m_GridService.GetRegionByPosition(regInfo.ScopeID, regInfo.RegionLocX, regInfo.RegionLocY);
  271. if (region != null)
  272. {
  273. m_log.WarnFormat("[HYPERGRID LINKER]: Coordinates {0}-{1} are already occupied by region {2} with uuid {3}",
  274. regInfo.RegionLocX / Constants.RegionSize, regInfo.RegionLocY / Constants.RegionSize,
  275. region.RegionName, region.RegionID);
  276. reason = "Coordinates are already in use";
  277. return false;
  278. }
  279. try
  280. {
  281. regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.Warn("[HYPERGRID LINKER]: Wrong format for link-region: " + e.Message);
  286. reason = "Internal error";
  287. return false;
  288. }
  289. // Finally, link it
  290. ulong handle = 0;
  291. UUID regionID = UUID.Zero;
  292. string externalName = string.Empty;
  293. string imageURL = string.Empty;
  294. if (!m_GatekeeperConnector.LinkRegion(regInfo, out regionID, out handle, out externalName, out imageURL, out reason))
  295. return false;
  296. if (regionID == UUID.Zero)
  297. {
  298. m_log.Warn("[HYPERGRID LINKER]: Unable to link region");
  299. reason = "Remote region could not be found";
  300. return false;
  301. }
  302. region = m_GridService.GetRegionByUUID(scopeID, regionID);
  303. if (region != null)
  304. {
  305. m_log.DebugFormat("[HYPERGRID LINKER]: Region already exists in coordinates {0} {1}",
  306. region.RegionLocX / Constants.RegionSize, region.RegionLocY / Constants.RegionSize);
  307. regInfo = region;
  308. return true;
  309. }
  310. // We are now performing this check for each individual teleport in the EntityTransferModule instead. This
  311. // allows us to give better feedback when teleports fail because of the distance reason (which can't be
  312. // done here) and it also hypergrid teleports that are within range (possibly because the source grid
  313. // itself has regions that are very far apart).
  314. // uint x, y;
  315. // if (m_Check4096 && !Check4096(handle, out x, out y))
  316. // {
  317. // //RemoveHyperlinkRegion(regInfo.RegionID);
  318. // reason = "Region is too far (" + x + ", " + y + ")";
  319. // m_log.Info("[HYPERGRID LINKER]: Unable to link, region is too far (" + x + ", " + y + ")");
  320. // //return false;
  321. // }
  322. regInfo.RegionID = regionID;
  323. if (externalName == string.Empty)
  324. regInfo.RegionName = regInfo.ServerURI;
  325. else
  326. regInfo.RegionName = externalName;
  327. m_log.DebugFormat("[HYPERGRID LINKER]: naming linked region {0}, handle {1}", regInfo.RegionName, handle.ToString());
  328. // Get the map image
  329. regInfo.TerrainImage = GetMapImage(regionID, imageURL);
  330. // Store the origin's coordinates somewhere
  331. regInfo.RegionSecret = handle.ToString();
  332. AddHyperlinkRegion(regInfo, handle);
  333. m_log.InfoFormat("[HYPERGRID LINKER]: Successfully linked to region {0} with image {1}", regInfo.RegionName, regInfo.TerrainImage);
  334. return true;
  335. }
  336. public bool TryUnlinkRegion(string mapName)
  337. {
  338. m_log.DebugFormat("[HYPERGRID LINKER]: Request to unlink {0}", mapName);
  339. GridRegion regInfo = null;
  340. List<RegionData> regions = m_Database.Get(Util.EscapeForLike(mapName), m_ScopeID);
  341. if (regions != null && regions.Count > 0)
  342. {
  343. OpenSim.Framework.RegionFlags rflags = (OpenSim.Framework.RegionFlags)Convert.ToInt32(regions[0].Data["flags"]);
  344. if ((rflags & OpenSim.Framework.RegionFlags.Hyperlink) != 0)
  345. {
  346. regInfo = new GridRegion();
  347. regInfo.RegionID = regions[0].RegionID;
  348. regInfo.ScopeID = m_ScopeID;
  349. }
  350. }
  351. if (regInfo != null)
  352. {
  353. RemoveHyperlinkRegion(regInfo.RegionID);
  354. return true;
  355. }
  356. else
  357. {
  358. m_log.InfoFormat("[HYPERGRID LINKER]: Region {0} not found", mapName);
  359. return false;
  360. }
  361. }
  362. // Not currently used
  363. // /// <summary>
  364. // /// Cope with this viewer limitation.
  365. // /// </summary>
  366. // /// <param name="regInfo"></param>
  367. // /// <returns></returns>
  368. // public bool Check4096(ulong realHandle, out uint x, out uint y)
  369. // {
  370. // uint ux = 0, uy = 0;
  371. // Utils.LongToUInts(realHandle, out ux, out uy);
  372. // x = ux / Constants.RegionSize;
  373. // y = uy / Constants.RegionSize;
  374. //
  375. // const uint limit = (4096 - 1) * Constants.RegionSize;
  376. // uint xmin = ux - limit;
  377. // uint xmax = ux + limit;
  378. // uint ymin = uy - limit;
  379. // uint ymax = uy + limit;
  380. // // World map boundary checks
  381. // if (xmin < 0 || xmin > ux)
  382. // xmin = 0;
  383. // if (xmax > int.MaxValue || xmax < ux)
  384. // xmax = int.MaxValue;
  385. // if (ymin < 0 || ymin > uy)
  386. // ymin = 0;
  387. // if (ymax > int.MaxValue || ymax < uy)
  388. // ymax = int.MaxValue;
  389. //
  390. // // Check for any regions that are within the possible teleport range to the linked region
  391. // List<GridRegion> regions = m_GridService.GetRegionRange(m_ScopeID, (int)xmin, (int)xmax, (int)ymin, (int)ymax);
  392. // if (regions.Count == 0)
  393. // {
  394. // return false;
  395. // }
  396. // else
  397. // {
  398. // // Check for regions which are not linked regions
  399. // List<GridRegion> hyperlinks = m_GridService.GetHyperlinks(m_ScopeID);
  400. // IEnumerable<GridRegion> availableRegions = regions.Except(hyperlinks);
  401. // if (availableRegions.Count() == 0)
  402. // return false;
  403. // }
  404. //
  405. // return true;
  406. // }
  407. private void AddHyperlinkRegion(GridRegion regionInfo, ulong regionHandle)
  408. {
  409. RegionData rdata = m_GridService.RegionInfo2RegionData(regionInfo);
  410. int flags = (int)OpenSim.Framework.RegionFlags.Hyperlink + (int)OpenSim.Framework.RegionFlags.NoDirectLogin + (int)OpenSim.Framework.RegionFlags.RegionOnline;
  411. rdata.Data["flags"] = flags.ToString();
  412. m_Database.Store(rdata);
  413. }
  414. private void RemoveHyperlinkRegion(UUID regionID)
  415. {
  416. m_Database.Delete(regionID);
  417. }
  418. public UUID GetMapImage(UUID regionID, string imageURL)
  419. {
  420. return m_GatekeeperConnector.GetMapImage(regionID, imageURL, m_MapTileDirectory);
  421. }
  422. #endregion
  423. #region Console Commands
  424. public void HandleShow(string module, string[] cmd)
  425. {
  426. if (cmd.Length != 2)
  427. {
  428. MainConsole.Instance.Output("Syntax: show hyperlinks");
  429. return;
  430. }
  431. List<RegionData> regions = m_Database.GetHyperlinks(UUID.Zero);
  432. if (regions == null || regions.Count < 1)
  433. {
  434. MainConsole.Instance.Output("No hyperlinks");
  435. return;
  436. }
  437. MainConsole.Instance.Output("Region Name");
  438. MainConsole.Instance.Output("Location Region UUID");
  439. MainConsole.Instance.Output(new string('-', 72));
  440. foreach (RegionData r in regions)
  441. {
  442. MainConsole.Instance.Output(String.Format("{0}\n{2,-32} {1}\n",
  443. r.RegionName, r.RegionID, String.Format("{0},{1} ({2},{3})", r.posX, r.posY,
  444. r.posX / Constants.RegionSize, r.posY / Constants.RegionSize)));
  445. }
  446. return;
  447. }
  448. public void RunCommand(string module, string[] cmdparams)
  449. {
  450. List<string> args = new List<string>(cmdparams);
  451. if (args.Count < 1)
  452. return;
  453. string command = args[0];
  454. args.RemoveAt(0);
  455. cmdparams = args.ToArray();
  456. RunHGCommand(command, cmdparams);
  457. }
  458. private void RunLinkRegionCommand(string[] cmdparams)
  459. {
  460. int xloc, yloc;
  461. string serverURI;
  462. string remoteName = null;
  463. xloc = Convert.ToInt32(cmdparams[0]) * (int)Constants.RegionSize;
  464. yloc = Convert.ToInt32(cmdparams[1]) * (int)Constants.RegionSize;
  465. serverURI = cmdparams[2];
  466. if (cmdparams.Length > 3)
  467. remoteName = string.Join(" ", cmdparams, 3, cmdparams.Length - 3);
  468. string reason = string.Empty;
  469. GridRegion regInfo;
  470. if (TryCreateLink(UUID.Zero, xloc, yloc, remoteName, 0, null, serverURI, UUID.Zero, out regInfo, out reason))
  471. MainConsole.Instance.Output("Hyperlink established");
  472. else
  473. MainConsole.Instance.Output("Failed to link region: " + reason);
  474. }
  475. private void RunHGCommand(string command, string[] cmdparams)
  476. {
  477. if (command.Equals("link-mapping"))
  478. {
  479. if (cmdparams.Length == 2)
  480. {
  481. try
  482. {
  483. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  484. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  485. m_enableAutoMapping = true;
  486. }
  487. catch (Exception)
  488. {
  489. m_autoMappingX = 0;
  490. m_autoMappingY = 0;
  491. m_enableAutoMapping = false;
  492. }
  493. }
  494. }
  495. else if (command.Equals("link-region"))
  496. {
  497. if (cmdparams.Length < 3)
  498. {
  499. if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
  500. {
  501. LoadXmlLinkFile(cmdparams);
  502. }
  503. else
  504. {
  505. LinkRegionCmdUsage();
  506. }
  507. return;
  508. }
  509. //this should be the prefererred way of setting up hg links now
  510. if (cmdparams[2].StartsWith("http"))
  511. {
  512. RunLinkRegionCommand(cmdparams);
  513. }
  514. else if (cmdparams[2].Contains(":"))
  515. {
  516. // New format
  517. string[] parts = cmdparams[2].Split(':');
  518. if (parts.Length > 2)
  519. {
  520. // Insert remote region name
  521. ArrayList parameters = new ArrayList(cmdparams);
  522. parameters.Insert(3, parts[2]);
  523. cmdparams = (string[])parameters.ToArray(typeof(string));
  524. }
  525. cmdparams[2] = "http://" + parts[0] + ':' + parts[1];
  526. RunLinkRegionCommand(cmdparams);
  527. }
  528. else
  529. {
  530. // old format
  531. GridRegion regInfo;
  532. int xloc, yloc;
  533. uint externalPort;
  534. string externalHostName;
  535. try
  536. {
  537. xloc = Convert.ToInt32(cmdparams[0]);
  538. yloc = Convert.ToInt32(cmdparams[1]);
  539. externalPort = Convert.ToUInt32(cmdparams[3]);
  540. externalHostName = cmdparams[2];
  541. //internalPort = Convert.ToUInt32(cmdparams[4]);
  542. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  543. }
  544. catch (Exception e)
  545. {
  546. MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
  547. LinkRegionCmdUsage();
  548. return;
  549. }
  550. // Convert cell coordinates given by the user to meters
  551. xloc = xloc * (int)Constants.RegionSize;
  552. yloc = yloc * (int)Constants.RegionSize;
  553. string reason = string.Empty;
  554. if (TryCreateLink(UUID.Zero, xloc, yloc, string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  555. {
  556. // What is this? The GridRegion instance will be discarded anyway,
  557. // which effectively ignores any local name given with the command.
  558. //if (cmdparams.Length >= 5)
  559. //{
  560. // regInfo.RegionName = "";
  561. // for (int i = 4; i < cmdparams.Length; i++)
  562. // regInfo.RegionName += cmdparams[i] + " ";
  563. //}
  564. }
  565. }
  566. return;
  567. }
  568. else if (command.Equals("unlink-region"))
  569. {
  570. if (cmdparams.Length < 1)
  571. {
  572. UnlinkRegionCmdUsage();
  573. return;
  574. }
  575. string region = string.Join(" ", cmdparams);
  576. if (TryUnlinkRegion(region))
  577. MainConsole.Instance.Output("Successfully unlinked " + region);
  578. else
  579. MainConsole.Instance.Output("Unable to unlink " + region + ", region not found.");
  580. }
  581. }
  582. private void LoadXmlLinkFile(string[] cmdparams)
  583. {
  584. //use http://www.hgurl.com/hypergrid.xml for test
  585. try
  586. {
  587. XmlReader r = XmlReader.Create(cmdparams[0]);
  588. XmlConfigSource cs = new XmlConfigSource(r);
  589. string[] excludeSections = null;
  590. if (cmdparams.Length == 2)
  591. {
  592. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  593. {
  594. string excludeString = cmdparams[1].ToLower();
  595. excludeString = excludeString.Remove(0, 12);
  596. char[] splitter = { ';' };
  597. excludeSections = excludeString.Split(splitter);
  598. }
  599. }
  600. for (int i = 0; i < cs.Configs.Count; i++)
  601. {
  602. bool skip = false;
  603. if ((excludeSections != null) && (excludeSections.Length > 0))
  604. {
  605. for (int n = 0; n < excludeSections.Length; n++)
  606. {
  607. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  608. {
  609. skip = true;
  610. break;
  611. }
  612. }
  613. }
  614. if (!skip)
  615. {
  616. ReadLinkFromConfig(cs.Configs[i]);
  617. }
  618. }
  619. }
  620. catch (Exception e)
  621. {
  622. m_log.Error(e.ToString());
  623. }
  624. }
  625. private void ReadLinkFromConfig(IConfig config)
  626. {
  627. GridRegion regInfo;
  628. int xloc, yloc;
  629. uint externalPort;
  630. string externalHostName;
  631. uint realXLoc, realYLoc;
  632. xloc = Convert.ToInt32(config.GetString("xloc", "0"));
  633. yloc = Convert.ToInt32(config.GetString("yloc", "0"));
  634. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  635. externalHostName = config.GetString("externalHostName", "");
  636. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  637. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  638. if (m_enableAutoMapping)
  639. {
  640. xloc = (int)((xloc % 100) + m_autoMappingX);
  641. yloc = (int)((yloc % 100) + m_autoMappingY);
  642. }
  643. if (((realXLoc == 0) && (realYLoc == 0)) ||
  644. (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
  645. ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  646. {
  647. xloc = xloc * (int)Constants.RegionSize;
  648. yloc = yloc * (int)Constants.RegionSize;
  649. string reason = string.Empty;
  650. if (TryCreateLink(UUID.Zero, xloc, yloc, string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  651. {
  652. regInfo.RegionName = config.GetString("localName", "");
  653. }
  654. else
  655. MainConsole.Instance.Output("Unable to link " + externalHostName + ": " + reason);
  656. }
  657. }
  658. private void LinkRegionCmdUsage()
  659. {
  660. MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <ServerURI> [<RemoteRegionName>]");
  661. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  662. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  663. MainConsole.Instance.Output("Usage: link-region <URI_of_xml> [<exclude>]");
  664. }
  665. private void UnlinkRegionCmdUsage()
  666. {
  667. MainConsole.Instance.Output("Usage: unlink-region <LocalName>");
  668. }
  669. #endregion
  670. }
  671. }