1
0

HypergridLinker.cs 30 KB

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