HypergridLinker.cs 32 KB

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