HGOpenSimNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.Net;
  32. using System.Reflection;
  33. using System.Xml;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Statistics;
  40. using OpenSim.Region.ClientStack;
  41. using OpenSim.Framework.Communications;
  42. using OpenSim.Framework.Communications.Cache;
  43. using OpenSim.Region.Communications.Local;
  44. using OpenSim.Region.Communications.Hypergrid;
  45. using OpenSim.Region.Environment;
  46. using OpenSim.Region.Environment.Interfaces;
  47. using OpenSim.Region.Environment.Scenes;
  48. using OpenSim.Region.Environment.Scenes.Hypergrid;
  49. using Timer = System.Timers.Timer;
  50. namespace OpenSim
  51. {
  52. public class HGOpenSimNode : OpenSim
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. private IHyperlink HGServices = null;
  56. private uint m_autoMappingX = 0;
  57. private uint m_autoMappingY = 0;
  58. private bool m_enableAutoMapping = false;
  59. public HGOpenSimNode(IConfigSource configSource) : base(configSource)
  60. {
  61. }
  62. /// <summary>
  63. /// Performs initialisation of the scene, such as loading configuration from disk.
  64. /// </summary>
  65. protected override void StartupSpecific()
  66. {
  67. m_log.Info("====================================================================");
  68. m_log.Info("=================== STARTING HYPERGRID NODE ========================");
  69. m_log.Info("====================================================================");
  70. base.StartupSpecific();
  71. }
  72. protected override void InitialiseStandaloneServices(LibraryRootFolder libraryRootFolder)
  73. {
  74. // Standalone mode
  75. HGInventoryService inventoryService = new HGInventoryService(m_networkServersInfo.InventoryURL, null, false);
  76. inventoryService.AddPlugin(m_configSettings.StandaloneInventoryPlugin, m_configSettings.StandaloneInventorySource);
  77. LocalUserServices userService =
  78. new LocalUserServices(
  79. m_networkServersInfo.DefaultHomeLocX, m_networkServersInfo.DefaultHomeLocY, inventoryService);
  80. userService.AddPlugin(m_configSettings.StandaloneUserPlugin, m_configSettings.StandaloneUserSource);
  81. //LocalBackEndServices backendService = new LocalBackEndServices();
  82. HGGridServicesStandalone gridService = new HGGridServicesStandalone(m_networkServersInfo, m_httpServer, m_assetCache, m_sceneManager);
  83. LocalLoginService loginService =
  84. new LocalLoginService(
  85. userService, m_configSettings.StandaloneWelcomeMessage, inventoryService, gridService.LocalBackend, m_networkServersInfo,
  86. m_configSettings.StandaloneAuthenticate, libraryRootFolder);
  87. m_commsManager = new HGCommunicationsStandalone(m_networkServersInfo, m_httpServer, m_assetCache,
  88. userService, userService, inventoryService, gridService, gridService, userService, libraryRootFolder, m_configSettings.DumpAssetsToFile);
  89. inventoryService.UserProfileCache = m_commsManager.UserProfileCacheService;
  90. HGServices = gridService;
  91. // set up XMLRPC handler for client's initial login request message
  92. m_httpServer.AddXmlRPCHandler("login_to_simulator", loginService.XmlRpcLoginMethod);
  93. // provides the web form login
  94. m_httpServer.AddHTTPHandler("login", loginService.ProcessHTMLLogin);
  95. // Provides the LLSD login
  96. m_httpServer.SetDefaultLLSDHandler(loginService.LLSDLoginMethod);
  97. // provide grid info
  98. // m_gridInfoService = new GridInfoService(m_config.Source.Configs["Startup"].GetString("inifile", Path.Combine(Util.configDir(), "OpenSim.ini")));
  99. m_gridInfoService = new GridInfoService(m_config.Source);
  100. m_httpServer.AddXmlRPCHandler("get_grid_info", m_gridInfoService.XmlRpcGridInfoMethod);
  101. m_httpServer.AddStreamHandler(new RestStreamHandler("GET", "/get_grid_info", m_gridInfoService.RestGetGridInfoMethod));
  102. }
  103. protected override void InitialiseGridServices(LibraryRootFolder libraryRootFolder)
  104. {
  105. m_commsManager = new HGCommunicationsGridMode(m_networkServersInfo, m_httpServer, m_assetCache, m_sceneManager, libraryRootFolder);
  106. HGServices = ((HGCommunicationsGridMode)m_commsManager).HGServices;
  107. m_httpServer.AddStreamHandler(new SimStatusHandler());
  108. }
  109. protected override Scene CreateScene(RegionInfo regionInfo, StorageManager storageManager,
  110. AgentCircuitManager circuitManager)
  111. {
  112. HGSceneCommunicationService sceneGridService = new HGSceneCommunicationService(m_commsManager, HGServices);
  113. return
  114. new HGScene(
  115. regionInfo, circuitManager, m_commsManager, sceneGridService, m_assetCache, storageManager,
  116. m_moduleLoader, m_configSettings.DumpAssetsToFile, m_configSettings.PhysicalPrim,
  117. m_configSettings.See_into_region_from_neighbor, m_config.Source, m_version);
  118. }
  119. public override void RunCmd(string command, string[] cmdparams)
  120. {
  121. if (command.Equals("link-mapping"))
  122. {
  123. if (cmdparams.Length == 2)
  124. {
  125. try
  126. {
  127. m_autoMappingX = Convert.ToUInt32(cmdparams[0]);
  128. m_autoMappingY = Convert.ToUInt32(cmdparams[1]);
  129. m_enableAutoMapping = true;
  130. }
  131. catch (Exception)
  132. {
  133. m_autoMappingX = 0;
  134. m_autoMappingY = 0;
  135. m_enableAutoMapping = false;
  136. }
  137. }
  138. }
  139. else if (command.Equals("link-region"))
  140. {
  141. // link-region <Xloc> <Yloc> <HostName> <HttpPort> <LocalName>
  142. if (cmdparams.Length < 4)
  143. {
  144. if ((cmdparams.Length == 1) || (cmdparams.Length ==2))
  145. {
  146. LoadXmlLinkFile(cmdparams);
  147. }
  148. else
  149. {
  150. LinkRegionCmdUsage();
  151. }
  152. return;
  153. }
  154. RegionInfo regInfo;
  155. uint xloc, yloc;
  156. uint externalPort;
  157. string externalHostName;
  158. try
  159. {
  160. xloc = Convert.ToUInt32(cmdparams[0]);
  161. yloc = Convert.ToUInt32(cmdparams[1]);
  162. externalPort = Convert.ToUInt32(cmdparams[3]);
  163. externalHostName = cmdparams[2];
  164. //internalPort = Convert.ToUInt32(cmdparams[4]);
  165. //remotingPort = Convert.ToUInt32(cmdparams[5]);
  166. }
  167. catch (Exception e)
  168. {
  169. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  170. LinkRegionCmdUsage();
  171. return;
  172. }
  173. if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo))
  174. {
  175. if (cmdparams.Length >= 5)
  176. {
  177. regInfo.RegionName = "";
  178. for (int i = 4; i < cmdparams.Length; i++)
  179. regInfo.RegionName += cmdparams[i] + " ";
  180. }
  181. }
  182. return;
  183. }
  184. base.RunCmd(command, cmdparams);
  185. }
  186. private void LoadXmlLinkFile(string[] cmdparams)
  187. {
  188. try
  189. {
  190. XmlReader r = XmlReader.Create(cmdparams[0]);
  191. XmlConfigSource cs = new XmlConfigSource(r);
  192. string[] excludeSections = null;
  193. if (cmdparams.Length == 2)
  194. {
  195. if (cmdparams[1].ToLower().StartsWith("excludelist:"))
  196. {
  197. string excludeString = cmdparams[1].ToLower();
  198. excludeString = excludeString.Remove(0, 12);
  199. char[] splitter = { ';' };
  200. excludeSections = excludeString.Split(splitter);
  201. }
  202. }
  203. for (int i = 0; i < cs.Configs.Count; i++)
  204. {
  205. bool skip = false;
  206. if ((excludeSections != null) && (excludeSections.Length > 0))
  207. {
  208. for (int n = 0; n < excludeSections.Length; n++)
  209. {
  210. if (excludeSections[n] == cs.Configs[i].Name.ToLower())
  211. {
  212. skip = true;
  213. break;
  214. }
  215. }
  216. }
  217. if (!skip)
  218. {
  219. ReadLinkFromConfig(cs.Configs[i]);
  220. }
  221. }
  222. }
  223. catch (Exception e)
  224. {
  225. Console.WriteLine(e.ToString());
  226. }
  227. }
  228. private void ReadLinkFromConfig(IConfig config)
  229. {
  230. RegionInfo regInfo;
  231. uint xloc, yloc;
  232. uint externalPort;
  233. string externalHostName;
  234. uint realXLoc, realYLoc;
  235. xloc = Convert.ToUInt32(config.GetString("xloc", "0"));
  236. yloc = Convert.ToUInt32(config.GetString("yloc", "0"));
  237. externalPort = Convert.ToUInt32(config.GetString("externalPort", "0"));
  238. externalHostName = config.GetString("externalHostName", "");
  239. realXLoc = Convert.ToUInt32(config.GetString("real-xloc", "0"));
  240. realYLoc = Convert.ToUInt32(config.GetString("real-yloc", "0"));
  241. if (m_enableAutoMapping)
  242. {
  243. xloc = (uint)((xloc % 100) + m_autoMappingX);
  244. yloc = (uint)((yloc % 100) + m_autoMappingY);
  245. }
  246. if (((realXLoc == 0) && (realYLoc == 0)) || (((realXLoc - xloc < 3896) || (xloc - realXLoc < 3896)) && ((realYLoc - yloc < 3896) || (yloc - realYLoc < 3896))))
  247. {
  248. if (TryCreateLink(xloc, yloc, externalPort, externalHostName, out regInfo))
  249. {
  250. regInfo.RegionName = config.GetString("localName", "");
  251. }
  252. }
  253. }
  254. private bool TryCreateLink(uint xloc, uint yloc, uint externalPort, string externalHostName, out RegionInfo regInfo)
  255. {
  256. regInfo = new RegionInfo();
  257. regInfo.RegionLocX = xloc;
  258. regInfo.RegionLocY = yloc;
  259. regInfo.ExternalHostName = externalHostName;
  260. regInfo.HttpPort = externalPort;
  261. //regInfo.RemotingPort = remotingPort;
  262. try
  263. {
  264. regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
  265. }
  266. catch (Exception e)
  267. {
  268. m_log.Warn("[HGrid] Wrong format for link-region command: " + e.Message);
  269. LinkRegionCmdUsage();
  270. return false;
  271. }
  272. regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString();
  273. // Finally, link it
  274. try
  275. {
  276. m_sceneManager.CurrentOrFirstScene.CommsManager.GridService.RegisterRegion(regInfo);
  277. }
  278. catch (Exception e)
  279. {
  280. m_log.Warn("[HGrid] Unable to link region: " + e.StackTrace);
  281. return false;
  282. }
  283. return true;
  284. }
  285. private void LinkRegionCmdUsage()
  286. {
  287. Console.WriteLine("Usage: link-region <Xloc> <Yloc> <HostName> <HttpPort> [<LocalName>]");
  288. }
  289. }
  290. }