HypergridLinker.cs 29 KB

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