HypergridLinker.cs 31 KB

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