HypergridLinker.cs 30 KB

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