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