HypergridLinker.cs 32 KB

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