1
0

HypergridLinker.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. // We are now performing this check for each individual teleport in the EntityTransferModule instead. This
  307. // allows us to give better feedback when teleports fail because of the distance reason (which can't be
  308. // done here) and it also hypergrid teleports that are within range (possibly because the source grid
  309. // itself has regions that are very far apart).
  310. // uint x, y;
  311. // if (m_Check4096 && !Check4096(handle, out x, out y))
  312. // {
  313. // //RemoveHyperlinkRegion(regInfo.RegionID);
  314. // reason = "Region is too far (" + x + ", " + y + ")";
  315. // m_log.Info("[HYPERGRID LINKER]: Unable to link, region is too far (" + x + ", " + y + ")");
  316. // //return false;
  317. // }
  318. regInfo.RegionID = regionID;
  319. if (externalName == string.Empty)
  320. regInfo.RegionName = regInfo.ServerURI;
  321. else
  322. regInfo.RegionName = externalName;
  323. m_log.DebugFormat("[HYPERGRID LINKER]: naming linked region {0}, handle {1}", regInfo.RegionName, handle.ToString());
  324. // Get the map image
  325. regInfo.TerrainImage = GetMapImage(regionID, imageURL);
  326. // Store the origin's coordinates somewhere
  327. regInfo.RegionSecret = handle.ToString();
  328. AddHyperlinkRegion(regInfo, handle);
  329. m_log.InfoFormat("[HYPERGRID LINKER]: Successfully linked to region {0} with image {1}", regInfo.RegionName, regInfo.TerrainImage);
  330. return true;
  331. }
  332. public bool TryUnlinkRegion(string mapName)
  333. {
  334. m_log.DebugFormat("[HYPERGRID LINKER]: Request to unlink {0}", mapName);
  335. GridRegion regInfo = null;
  336. List<RegionData> regions = m_Database.Get(mapName, m_ScopeID);
  337. if (regions != null && regions.Count > 0)
  338. {
  339. OpenSim.Data.RegionFlags rflags = (OpenSim.Data.RegionFlags)Convert.ToInt32(regions[0].Data["flags"]);
  340. if ((rflags & OpenSim.Data.RegionFlags.Hyperlink) != 0)
  341. {
  342. regInfo = new GridRegion();
  343. regInfo.RegionID = regions[0].RegionID;
  344. regInfo.ScopeID = m_ScopeID;
  345. }
  346. }
  347. if (regInfo != null)
  348. {
  349. RemoveHyperlinkRegion(regInfo.RegionID);
  350. return true;
  351. }
  352. else
  353. {
  354. m_log.InfoFormat("[HYPERGRID LINKER]: Region {0} not found", mapName);
  355. return false;
  356. }
  357. }
  358. // Not currently used
  359. // /// <summary>
  360. // /// Cope with this viewer limitation.
  361. // /// </summary>
  362. // /// <param name="regInfo"></param>
  363. // /// <returns></returns>
  364. // public bool Check4096(ulong realHandle, out uint x, out uint y)
  365. // {
  366. // uint ux = 0, uy = 0;
  367. // Utils.LongToUInts(realHandle, out ux, out uy);
  368. // x = ux / Constants.RegionSize;
  369. // y = uy / Constants.RegionSize;
  370. //
  371. // const uint limit = (4096 - 1) * Constants.RegionSize;
  372. // uint xmin = ux - limit;
  373. // uint xmax = ux + limit;
  374. // uint ymin = uy - limit;
  375. // uint ymax = uy + limit;
  376. // // World map boundary checks
  377. // if (xmin < 0 || xmin > ux)
  378. // xmin = 0;
  379. // if (xmax > int.MaxValue || xmax < ux)
  380. // xmax = int.MaxValue;
  381. // if (ymin < 0 || ymin > uy)
  382. // ymin = 0;
  383. // if (ymax > int.MaxValue || ymax < uy)
  384. // ymax = int.MaxValue;
  385. //
  386. // // Check for any regions that are within the possible teleport range to the linked region
  387. // List<GridRegion> regions = m_GridService.GetRegionRange(m_ScopeID, (int)xmin, (int)xmax, (int)ymin, (int)ymax);
  388. // if (regions.Count == 0)
  389. // {
  390. // return false;
  391. // }
  392. // else
  393. // {
  394. // // Check for regions which are not linked regions
  395. // List<GridRegion> hyperlinks = m_GridService.GetHyperlinks(m_ScopeID);
  396. // IEnumerable<GridRegion> availableRegions = regions.Except(hyperlinks);
  397. // if (availableRegions.Count() == 0)
  398. // return false;
  399. // }
  400. //
  401. // return true;
  402. // }
  403. private void AddHyperlinkRegion(GridRegion regionInfo, ulong regionHandle)
  404. {
  405. RegionData rdata = m_GridService.RegionInfo2RegionData(regionInfo);
  406. int flags = (int)OpenSim.Data.RegionFlags.Hyperlink + (int)OpenSim.Data.RegionFlags.NoDirectLogin + (int)OpenSim.Data.RegionFlags.RegionOnline;
  407. rdata.Data["flags"] = flags.ToString();
  408. m_Database.Store(rdata);
  409. }
  410. private void RemoveHyperlinkRegion(UUID regionID)
  411. {
  412. m_Database.Delete(regionID);
  413. }
  414. public UUID GetMapImage(UUID regionID, string imageURL)
  415. {
  416. return m_GatekeeperConnector.GetMapImage(regionID, imageURL, m_MapTileDirectory);
  417. }
  418. #endregion
  419. #region Console Commands
  420. public void HandleShow(string module, string[] cmd)
  421. {
  422. if (cmd.Length != 2)
  423. {
  424. MainConsole.Instance.Output("Syntax: show hyperlinks");
  425. return;
  426. }
  427. List<RegionData> regions = m_Database.GetHyperlinks(UUID.Zero);
  428. if (regions == null || regions.Count < 1)
  429. {
  430. MainConsole.Instance.Output("No hyperlinks");
  431. return;
  432. }
  433. MainConsole.Instance.Output("Region Name");
  434. MainConsole.Instance.Output("Location Region UUID");
  435. MainConsole.Instance.Output(new string('-', 72));
  436. foreach (RegionData r in regions)
  437. {
  438. MainConsole.Instance.Output(String.Format("{0}\n{2,-32} {1}\n",
  439. r.RegionName, r.RegionID, String.Format("{0},{1} ({2},{3})", r.posX, r.posY,
  440. r.posX / Constants.RegionSize, r.posY / Constants.RegionSize)));
  441. }
  442. return;
  443. }
  444. public void RunCommand(string module, string[] cmdparams)
  445. {
  446. List<string> args = new List<string>(cmdparams);
  447. if (args.Count < 1)
  448. return;
  449. string command = args[0];
  450. args.RemoveAt(0);
  451. cmdparams = args.ToArray();
  452. RunHGCommand(command, cmdparams);
  453. }
  454. private void RunLinkRegionCommand(string[] cmdparams)
  455. {
  456. int xloc, yloc;
  457. string serverURI;
  458. string remoteName = null;
  459. xloc = Convert.ToInt32(cmdparams[0]) * (int)Constants.RegionSize;
  460. yloc = Convert.ToInt32(cmdparams[1]) * (int)Constants.RegionSize;
  461. serverURI = cmdparams[2];
  462. if (cmdparams.Length > 3)
  463. remoteName = string.Join(" ", cmdparams, 3, cmdparams.Length - 3);
  464. string reason = string.Empty;
  465. GridRegion regInfo;
  466. if (TryCreateLink(UUID.Zero, xloc, yloc, remoteName, 0, null, serverURI, UUID.Zero, out regInfo, out reason))
  467. MainConsole.Instance.Output("Hyperlink established");
  468. else
  469. MainConsole.Instance.Output("Failed to link region: " + reason);
  470. }
  471. private void RunHGCommand(string command, string[] cmdparams)
  472. {
  473. if (command.Equals("link-mapping"))
  474. {
  475. if (cmdparams.Length == 2)
  476. {
  477. try
  478. {
  479. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  480. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  481. m_enableAutoMapping = true;
  482. }
  483. catch (Exception)
  484. {
  485. m_autoMappingX = 0;
  486. m_autoMappingY = 0;
  487. m_enableAutoMapping = false;
  488. }
  489. }
  490. }
  491. else if (command.Equals("link-region"))
  492. {
  493. if (cmdparams.Length < 3)
  494. {
  495. if ((cmdparams.Length == 1) || (cmdparams.Length == 2))
  496. {
  497. LoadXmlLinkFile(cmdparams);
  498. }
  499. else
  500. {
  501. LinkRegionCmdUsage();
  502. }
  503. return;
  504. }
  505. //this should be the prefererred way of setting up hg links now
  506. if (cmdparams[2].StartsWith("http"))
  507. {
  508. RunLinkRegionCommand(cmdparams);
  509. }
  510. else if (cmdparams[2].Contains(":"))
  511. {
  512. // New format
  513. string[] parts = cmdparams[2].Split(':');
  514. if (parts.Length > 2)
  515. {
  516. // Insert remote region name
  517. ArrayList parameters = new ArrayList(cmdparams);
  518. parameters.Insert(3, parts[2]);
  519. cmdparams = (string[])parameters.ToArray(typeof(string));
  520. }
  521. cmdparams[2] = "http://" + parts[0] + ':' + parts[1];
  522. RunLinkRegionCommand(cmdparams);
  523. }
  524. else
  525. {
  526. // old format
  527. GridRegion regInfo;
  528. int xloc, yloc;
  529. uint externalPort;
  530. string externalHostName;
  531. try
  532. {
  533. xloc = Convert.ToInt32(cmdparams[0]);
  534. yloc = Convert.ToInt32(cmdparams[1]);
  535. externalPort = Convert.ToUInt32(cmdparams[3]);
  536. externalHostName = cmdparams[2];
  537. //internalPort = Convert.ToUInt32(cmdparams[4]);
  538. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  539. }
  540. catch (Exception e)
  541. {
  542. MainConsole.Instance.Output("[HGrid] Wrong format for link-region command: " + e.Message);
  543. LinkRegionCmdUsage();
  544. return;
  545. }
  546. // Convert cell coordinates given by the user to meters
  547. xloc = xloc * (int)Constants.RegionSize;
  548. yloc = yloc * (int)Constants.RegionSize;
  549. string reason = string.Empty;
  550. if (TryCreateLink(UUID.Zero, xloc, yloc, string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  551. {
  552. // What is this? The GridRegion instance will be discarded anyway,
  553. // which effectively ignores any local name given with the command.
  554. //if (cmdparams.Length >= 5)
  555. //{
  556. // regInfo.RegionName = "";
  557. // for (int i = 4; i < cmdparams.Length; i++)
  558. // regInfo.RegionName += cmdparams[i] + " ";
  559. //}
  560. }
  561. }
  562. return;
  563. }
  564. else if (command.Equals("unlink-region"))
  565. {
  566. if (cmdparams.Length < 1)
  567. {
  568. UnlinkRegionCmdUsage();
  569. return;
  570. }
  571. string region = string.Join(" ", cmdparams);
  572. if (TryUnlinkRegion(region))
  573. MainConsole.Instance.Output("Successfully unlinked " + region);
  574. else
  575. MainConsole.Instance.Output("Unable to unlink " + region + ", region not found.");
  576. }
  577. }
  578. private void LoadXmlLinkFile(string[] cmdparams)
  579. {
  580. //use http://www.hgurl.com/hypergrid.xml for test
  581. try
  582. {
  583. XmlReader r = XmlReader.Create(cmdparams[0]);
  584. XmlConfigSource cs = new XmlConfigSource(r);
  585. string[] excludeSections = null;
  586. if (cmdparams.Length == 2)
  587. {
  588. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  589. {
  590. string excludeString = cmdparams[1].ToLower();
  591. excludeString = excludeString.Remove(0, 12);
  592. char[] splitter = { ';' };
  593. excludeSections = excludeString.Split(splitter);
  594. }
  595. }
  596. for (int i = 0; i < cs.Configs.Count; i++)
  597. {
  598. bool skip = false;
  599. if ((excludeSections != null) && (excludeSections.Length > 0))
  600. {
  601. for (int n = 0; n < excludeSections.Length; n++)
  602. {
  603. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  604. {
  605. skip = true;
  606. break;
  607. }
  608. }
  609. }
  610. if (!skip)
  611. {
  612. ReadLinkFromConfig(cs.Configs[i]);
  613. }
  614. }
  615. }
  616. catch (Exception e)
  617. {
  618. m_log.Error(e.ToString());
  619. }
  620. }
  621. private void ReadLinkFromConfig(IConfig config)
  622. {
  623. GridRegion regInfo;
  624. int xloc, yloc;
  625. uint externalPort;
  626. string externalHostName;
  627. uint realXLoc, realYLoc;
  628. xloc = Convert.ToInt32(config.GetString("xloc", "0"));
  629. yloc = Convert.ToInt32(config.GetString("yloc", "0"));
  630. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  631. externalHostName = config.GetString("externalHostName", "");
  632. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  633. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  634. if (m_enableAutoMapping)
  635. {
  636. xloc = (int)((xloc % 100) + m_autoMappingX);
  637. yloc = (int)((yloc % 100) + m_autoMappingY);
  638. }
  639. if (((realXLoc == 0) && (realYLoc == 0)) ||
  640. (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) &&
  641. ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  642. {
  643. xloc = xloc * (int)Constants.RegionSize;
  644. yloc = yloc * (int)Constants.RegionSize;
  645. string reason = string.Empty;
  646. if (TryCreateLink(UUID.Zero, xloc, yloc, string.Empty, externalPort, externalHostName, UUID.Zero, out regInfo, out reason))
  647. {
  648. regInfo.RegionName = config.GetString("localName", "");
  649. }
  650. else
  651. MainConsole.Instance.Output("Unable to link " + externalHostName + ": " + reason);
  652. }
  653. }
  654. private void LinkRegionCmdUsage()
  655. {
  656. MainConsole.Instance.Output("Usage: link-region <Xloc> <Yloc> <ServerURI> [<RemoteRegionName>]");
  657. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  658. MainConsole.Instance.Output("Usage (deprecated): link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  659. MainConsole.Instance.Output("Usage: link-region <URI_of_xml> [<exclude>]");
  660. }
  661. private void UnlinkRegionCmdUsage()
  662. {
  663. MainConsole.Instance.Output("Usage: unlink-region <LocalName>");
  664. }
  665. #endregion
  666. }
  667. }