HGOpenSimNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.Generic;
  29. using System.Reflection;
  30. using System.Xml;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Region.Communications.Hypergrid;
  39. using OpenSim.Region.Communications.Local;
  40. using OpenSim.Region.Framework;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Region.Framework.Scenes.Hypergrid;
  43. namespace OpenSim
  44. {
  45. public class HGOpenSimNode : OpenSim
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. public IHyperlink HGServices = null;
  49. private uint m_autoMappingX = 0;
  50. private uint m_autoMappingY = 0;
  51. private bool m_enableAutoMapping = false;
  52. public HGOpenSimNode(IConfigSource configSource) : base(configSource)
  53. {
  54. }
  55. /// <summary>
  56. /// Performs initialisation of the scene, such as loading configuration from disk.
  57. /// </summary>
  58. protected override void StartupSpecific()
  59. {
  60. m_log.Info("====================================================================");
  61. m_log.Info("=================== STARTING HYPERGRID NODE ========================");
  62. m_log.Info("====================================================================");
  63. base.StartupSpecific();
  64. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-mapping", "link-mapping [<x> <y>] <cr>", "Set local coordinate to map HG regions to", RunCommand);
  65. MainConsole.Instance.Commands.AddCommand("hypergrid", false, "link-region", "link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>] <cr>", "Link a hypergrid region", RunCommand);
  66. }
  67. protected override Scene CreateScene(RegionInfo regionInfo, StorageManager storageManager,
  68. AgentCircuitManager circuitManager)
  69. {
  70. HGSceneCommunicationService sceneGridService = new HGSceneCommunicationService(m_commsManager, HGServices);
  71. return
  72. new HGScene(
  73. regionInfo, circuitManager, m_commsManager, sceneGridService, storageManager,
  74. m_moduleLoader, m_configSettings.DumpAssetsToFile, m_configSettings.PhysicalPrim,
  75. m_configSettings.See_into_region_from_neighbor, m_config.Source, m_version);
  76. }
  77. new void RunCommand(string module, string[] cp)
  78. {
  79. List<string> cmdparams = new List<string>(cp);
  80. if (cmdparams.Count < 1)
  81. return;
  82. string command = cmdparams[0];
  83. cmdparams.RemoveAt(0);
  84. if (command.Equals("link-mapping"))
  85. {
  86. if (cmdparams.Count == 2)
  87. {
  88. try
  89. {
  90. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  91. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  92. m_enableAutoMapping = true;
  93. }
  94. catch (Exception)
  95. {
  96. m_autoMappingX = 0;
  97. m_autoMappingY = 0;
  98. m_enableAutoMapping = false;
  99. }
  100. }
  101. }
  102. else if (command.Equals("link-region"))
  103. {
  104. if (cmdparams.Count < 3)
  105. {
  106. if ((cmdparams.Count == 1) || (cmdparams.Count == 2))
  107. {
  108. LoadXmlLinkFile(cmdparams);
  109. }
  110. else
  111. {
  112. LinkRegionCmdUsage();
  113. }
  114. return;
  115. }
  116. if (cmdparams[2].Contains(":"))
  117. { // New format
  118. uint xloc, yloc;
  119. string mapName;
  120. try
  121. {
  122. xloc = Convert.ToUInt32(cmdparams[0]);
  123. yloc = Convert.ToUInt32(cmdparams[1]);
  124. mapName = cmdparams[2];
  125. if (cmdparams.Count > 3)
  126. for (int i = 3; i < cmdparams.Count; i++)
  127. mapName += " " + cmdparams[i];
  128. m_log.Info(">> MapName: " + mapName);
  129. //internalPort = Convert.ToUInt32(cmdparams[4]);
  130. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  135. LinkRegionCmdUsage();
  136. return;
  137. }
  138. HGHyperlink.TryLinkRegionToCoords(m_sceneManager.CurrentOrFirstScene, null, mapName, xloc, yloc);
  139. }
  140. else
  141. { // old format
  142. RegionInfo regInfo;
  143. uint xloc, yloc;
  144. uint externalPort;
  145. string externalHostName;
  146. try
  147. {
  148. xloc = Convert.ToUInt32(cmdparams[0]);
  149. yloc = Convert.ToUInt32(cmdparams[1]);
  150. externalPort = Convert.ToUInt32(cmdparams[3]);
  151. externalHostName = cmdparams[2];
  152. //internalPort = Convert.ToUInt32(cmdparams[4]);
  153. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  154. }
  155. catch (Exception e)
  156. {
  157. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  158. LinkRegionCmdUsage();
  159. return;
  160. }
  161. //if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo))
  162. if (HGHyperlink.TryCreateLink(m_sceneManager.CurrentOrFirstScene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo))
  163. {
  164. if (cmdparams.Count >= 5)
  165. {
  166. regInfo.RegionName = "";
  167. for (int i = 4; i < cmdparams.Count; i++)
  168. regInfo.RegionName += cmdparams[i] + " ";
  169. }
  170. }
  171. }
  172. return;
  173. }
  174. }
  175. private void LoadXmlLinkFile(List<string> cmdparams)
  176. {
  177. //use http://www.hgurl.com/hypergrid.xml for test
  178. try
  179. {
  180. XmlReader r = XmlReader.Create(cmdparams[0]);
  181. XmlConfigSource cs = new XmlConfigSource(r);
  182. string[] excludeSections = null;
  183. if (cmdparams.Count == 2)
  184. {
  185. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  186. {
  187. string excludeString = cmdparams[1].ToLower();
  188. excludeString = excludeString.Remove(0, 12);
  189. char[] splitter = { ';' };
  190. excludeSections = excludeString.Split(splitter);
  191. }
  192. }
  193. for (int i = 0; i < cs.Configs.Count; i++)
  194. {
  195. bool skip = false;
  196. if ((excludeSections != null) && (excludeSections.Length > 0))
  197. {
  198. for (int n = 0; n < excludeSections.Length; n++)
  199. {
  200. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  201. {
  202. skip = true;
  203. break;
  204. }
  205. }
  206. }
  207. if (!skip)
  208. {
  209. ReadLinkFromConfig(cs.Configs[i]);
  210. }
  211. }
  212. }
  213. catch (Exception e)
  214. {
  215. m_log.Error(e.ToString());
  216. }
  217. }
  218. private void ReadLinkFromConfig(IConfig config)
  219. {
  220. RegionInfo regInfo;
  221. uint xloc, yloc;
  222. uint externalPort;
  223. string externalHostName;
  224. uint realXLoc, realYLoc;
  225. xloc = Convert.ToUInt32(config.GetString("xloc", "0"));
  226. yloc = Convert.ToUInt32(config.GetString("yloc", "0"));
  227. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  228. externalHostName = config.GetString("externalHostName", "");
  229. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  230. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  231. if (m_enableAutoMapping)
  232. {
  233. xloc = (uint)((xloc % 100) + m_autoMappingX);
  234. yloc = (uint)((yloc % 100) + m_autoMappingY);
  235. }
  236. if (((realXLoc == 0) && (realYLoc == 0)) || (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) && ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  237. {
  238. if (HGHyperlink.TryCreateLink(m_sceneManager.CurrentOrFirstScene, null, xloc, yloc, "", externalPort, externalHostName, out regInfo))
  239. {
  240. regInfo.RegionName = config.GetString("localName", "");
  241. }
  242. }
  243. }
  244. private void LinkRegionCmdUsage()
  245. {
  246. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName>:<HttpPort>[:<RemoteRegionName>]");
  247. m_log.Info("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  248. m_log.Info("Usage: link-region <URI_of_xml> [<exclude>]");
  249. }
  250. }
  251. }