HypergridLinker.cs 30 KB

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