OpenSimBase.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Framework.Statistics;
  42. using OpenSim.Region.ClientStack;
  43. using OpenSim.Region.Framework;
  44. using OpenSim.Region.Framework.Interfaces;
  45. using OpenSim.Region.Framework.Scenes;
  46. using OpenSim.Region.Physics.Manager;
  47. namespace OpenSim
  48. {
  49. /// <summary>
  50. /// Common OpenSimulator simulator code
  51. /// </summary>
  52. public class OpenSimBase : RegionApplicationBase
  53. {
  54. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  55. // These are the names of the plugin-points extended by this
  56. // class during system startup.
  57. private const string PLUGIN_ASSET_CACHE = "/OpenSim/AssetCache";
  58. private const string PLUGIN_ASSET_SERVER_CLIENT = "/OpenSim/AssetClient";
  59. protected string proxyUrl;
  60. protected int proxyOffset = 0;
  61. public string userStatsURI = String.Empty;
  62. protected bool m_autoCreateClientStack = true;
  63. /// <value>
  64. /// The file used to load and save prim backup xml if no filename has been specified
  65. /// </value>
  66. protected const string DEFAULT_PRIM_BACKUP_FILENAME = "prim-backup.xml";
  67. public ConfigSettings ConfigurationSettings
  68. {
  69. get { return m_configSettings; }
  70. set { m_configSettings = value; }
  71. }
  72. protected ConfigSettings m_configSettings;
  73. protected ConfigurationLoader m_configLoader;
  74. public ConsoleCommand CreateAccount = null;
  75. protected List<IApplicationPlugin> m_plugins = new List<IApplicationPlugin>();
  76. /// <value>
  77. /// The config information passed into the OpenSimulator region server.
  78. /// </value>
  79. public OpenSimConfigSource ConfigSource
  80. {
  81. get { return m_config; }
  82. set { m_config = value; }
  83. }
  84. protected OpenSimConfigSource m_config;
  85. public List<IClientNetworkServer> ClientServers
  86. {
  87. get { return m_clientServers; }
  88. }
  89. protected List<IClientNetworkServer> m_clientServers = new List<IClientNetworkServer>();
  90. public uint HttpServerPort
  91. {
  92. get { return m_httpServerPort; }
  93. }
  94. public ModuleLoader ModuleLoader
  95. {
  96. get { return m_moduleLoader; }
  97. set { m_moduleLoader = value; }
  98. }
  99. protected ModuleLoader m_moduleLoader;
  100. protected IRegistryCore m_applicationRegistry = new RegistryCore();
  101. public IRegistryCore ApplicationRegistry
  102. {
  103. get { return m_applicationRegistry; }
  104. }
  105. /// <summary>
  106. /// Constructor.
  107. /// </summary>
  108. /// <param name="configSource"></param>
  109. public OpenSimBase(IConfigSource configSource) : base()
  110. {
  111. LoadConfigSettings(configSource);
  112. }
  113. protected virtual void LoadConfigSettings(IConfigSource configSource)
  114. {
  115. m_configLoader = new ConfigurationLoader();
  116. m_config = m_configLoader.LoadConfigSettings(configSource, out m_configSettings, out m_networkServersInfo);
  117. ReadExtraConfigSettings();
  118. }
  119. protected virtual void ReadExtraConfigSettings()
  120. {
  121. IConfig networkConfig = m_config.Source.Configs["Network"];
  122. if (networkConfig != null)
  123. {
  124. proxyUrl = networkConfig.GetString("proxy_url", "");
  125. proxyOffset = Int32.Parse(networkConfig.GetString("proxy_offset", "0"));
  126. }
  127. }
  128. protected virtual void LoadPlugins()
  129. {
  130. using (PluginLoader<IApplicationPlugin> loader = new PluginLoader<IApplicationPlugin>(new ApplicationPluginInitialiser(this)))
  131. {
  132. loader.Load("/OpenSim/Startup");
  133. m_plugins = loader.Plugins;
  134. }
  135. }
  136. protected override List<string> GetHelpTopics()
  137. {
  138. List<string> topics = base.GetHelpTopics();
  139. Scene s = SceneManager.CurrentOrFirstScene;
  140. if (s != null && s.GetCommanders() != null)
  141. topics.AddRange(s.GetCommanders().Keys);
  142. return topics;
  143. }
  144. /// <summary>
  145. /// Performs startup specific to the region server, including initialization of the scene
  146. /// such as loading configuration from disk.
  147. /// </summary>
  148. protected override void StartupSpecific()
  149. {
  150. IConfig startupConfig = m_config.Source.Configs["Startup"];
  151. if (startupConfig != null)
  152. {
  153. string pidFile = startupConfig.GetString("PIDFile", String.Empty);
  154. if (pidFile != String.Empty)
  155. CreatePIDFile(pidFile);
  156. userStatsURI = startupConfig.GetString("Stats_URI", String.Empty);
  157. }
  158. base.StartupSpecific();
  159. m_stats = StatsManager.StartCollectingSimExtraStats();
  160. // Create a ModuleLoader instance
  161. m_moduleLoader = new ModuleLoader(m_config.Source);
  162. LoadPlugins();
  163. foreach (IApplicationPlugin plugin in m_plugins)
  164. {
  165. plugin.PostInitialise();
  166. }
  167. AddPluginCommands();
  168. }
  169. protected virtual void AddPluginCommands()
  170. {
  171. // If console exists add plugin commands.
  172. if (m_console != null)
  173. {
  174. List<string> topics = GetHelpTopics();
  175. foreach (string topic in topics)
  176. {
  177. m_console.Commands.AddCommand("plugin", false, "help " + topic,
  178. "help " + topic,
  179. "Get help on plugin command '" + topic + "'",
  180. HandleCommanderHelp);
  181. m_console.Commands.AddCommand("plugin", false, topic,
  182. topic,
  183. "Execute subcommand for plugin '" + topic + "'",
  184. null);
  185. ICommander commander = null;
  186. Scene s = SceneManager.CurrentOrFirstScene;
  187. if (s != null && s.GetCommanders() != null)
  188. {
  189. if (s.GetCommanders().ContainsKey(topic))
  190. commander = s.GetCommanders()[topic];
  191. }
  192. if (commander == null)
  193. continue;
  194. foreach (string command in commander.Commands.Keys)
  195. {
  196. m_console.Commands.AddCommand(topic, false,
  197. topic + " " + command,
  198. topic + " " + commander.Commands[command].ShortHelp(),
  199. String.Empty, HandleCommanderCommand);
  200. }
  201. }
  202. }
  203. }
  204. private void HandleCommanderCommand(string module, string[] cmd)
  205. {
  206. m_sceneManager.SendCommandToPluginModules(cmd);
  207. }
  208. private void HandleCommanderHelp(string module, string[] cmd)
  209. {
  210. // Only safe for the interactive console, since it won't
  211. // let us come here unless both scene and commander exist
  212. //
  213. ICommander moduleCommander = SceneManager.CurrentOrFirstScene.GetCommander(cmd[1]);
  214. if (moduleCommander != null)
  215. m_console.Output(moduleCommander.Help);
  216. }
  217. protected override void Initialize()
  218. {
  219. // Called from base.StartUp()
  220. m_httpServerPort = m_networkServersInfo.HttpListenerPort;
  221. m_sceneManager.OnRestartSim += handleRestartRegion;
  222. }
  223. /// <summary>
  224. /// Execute the region creation process. This includes setting up scene infrastructure.
  225. /// </summary>
  226. /// <param name="regionInfo"></param>
  227. /// <param name="portadd_flag"></param>
  228. /// <returns></returns>
  229. public IClientNetworkServer CreateRegion(RegionInfo regionInfo, bool portadd_flag, out IScene scene)
  230. {
  231. return CreateRegion(regionInfo, portadd_flag, false, out scene);
  232. }
  233. /// <summary>
  234. /// Execute the region creation process. This includes setting up scene infrastructure.
  235. /// </summary>
  236. /// <param name="regionInfo"></param>
  237. /// <returns></returns>
  238. public IClientNetworkServer CreateRegion(RegionInfo regionInfo, out IScene scene)
  239. {
  240. return CreateRegion(regionInfo, false, true, out scene);
  241. }
  242. /// <summary>
  243. /// Execute the region creation process. This includes setting up scene infrastructure.
  244. /// </summary>
  245. /// <param name="regionInfo"></param>
  246. /// <param name="portadd_flag"></param>
  247. /// <param name="do_post_init"></param>
  248. /// <returns></returns>
  249. public IClientNetworkServer CreateRegion(RegionInfo regionInfo, bool portadd_flag, bool do_post_init, out IScene mscene)
  250. {
  251. int port = regionInfo.InternalEndPoint.Port;
  252. // set initial RegionID to originRegionID in RegionInfo. (it needs for loding prims)
  253. // Commented this out because otherwise regions can't register with
  254. // the grid as there is already another region with the same UUID
  255. // at those coordinates. This is required for the load balancer to work.
  256. // --Mike, 2009.02.25
  257. //regionInfo.originRegionID = regionInfo.RegionID;
  258. // set initial ServerURI
  259. regionInfo.ServerURI = "http://" + regionInfo.ExternalHostName + ":" + regionInfo.InternalEndPoint.Port;
  260. regionInfo.HttpPort = m_httpServerPort;
  261. regionInfo.osSecret = m_osSecret;
  262. if ((proxyUrl.Length > 0) && (portadd_flag))
  263. {
  264. // set proxy url to RegionInfo
  265. regionInfo.proxyUrl = proxyUrl;
  266. regionInfo.ProxyOffset = proxyOffset;
  267. Util.XmlRpcCommand(proxyUrl, "AddPort", port, port + proxyOffset, regionInfo.ExternalHostName);
  268. }
  269. IClientNetworkServer clientServer;
  270. Scene scene = SetupScene(regionInfo, proxyOffset, m_config.Source, out clientServer);
  271. m_log.Info("[MODULES]: Loading Region's modules (old style)");
  272. List<IRegionModule> modules = m_moduleLoader.PickupModules(scene, ".");
  273. // This needs to be ahead of the script engine load, so the
  274. // script module can pick up events exposed by a module
  275. m_moduleLoader.InitialiseSharedModules(scene);
  276. // Use this in the future, the line above will be deprecated soon
  277. m_log.Info("[MODULES]: Loading Region's modules (new style)");
  278. IRegionModulesController controller;
  279. if (ApplicationRegistry.TryGet(out controller))
  280. {
  281. controller.AddRegionToModules(scene);
  282. }
  283. else m_log.Error("[MODULES]: The new RegionModulesController is missing...");
  284. scene.SetModuleInterfaces();
  285. // Prims have to be loaded after module configuration since some modules may be invoked during the load
  286. scene.LoadPrimsFromStorage(regionInfo.originRegionID);
  287. // TODO : Try setting resource for region xstats here on scene
  288. MainServer.Instance.AddStreamHandler(new Region.Framework.Scenes.RegionStatsHandler(regionInfo));
  289. try
  290. {
  291. scene.RegisterRegionWithGrid();
  292. }
  293. catch (Exception e)
  294. {
  295. m_log.ErrorFormat(
  296. "[STARTUP]: Registration of region with grid failed, aborting startup due to {0} {1}",
  297. e.Message, e.StackTrace);
  298. // Carrying on now causes a lot of confusion down the
  299. // line - we need to get the user's attention
  300. Environment.Exit(1);
  301. }
  302. scene.loadAllLandObjectsFromStorage(regionInfo.originRegionID);
  303. scene.EventManager.TriggerParcelPrimCountUpdate();
  304. // We need to do this after we've initialized the
  305. // scripting engines.
  306. scene.CreateScriptInstances();
  307. m_sceneManager.Add(scene);
  308. if (m_autoCreateClientStack)
  309. {
  310. m_clientServers.Add(clientServer);
  311. clientServer.Start();
  312. }
  313. if (do_post_init)
  314. {
  315. foreach (IRegionModule module in modules)
  316. {
  317. module.PostInitialise();
  318. }
  319. }
  320. scene.EventManager.OnShutdown += delegate() { ShutdownRegion(scene); };
  321. mscene = scene;
  322. scene.StartTimer();
  323. return clientServer;
  324. }
  325. private void ShutdownRegion(Scene scene)
  326. {
  327. m_log.DebugFormat("[SHUTDOWN]: Shutting down region {0}", scene.RegionInfo.RegionName);
  328. IRegionModulesController controller;
  329. if (ApplicationRegistry.TryGet<IRegionModulesController>(out controller))
  330. {
  331. controller.RemoveRegionFromModules(scene);
  332. }
  333. }
  334. public void RemoveRegion(Scene scene, bool cleanup)
  335. {
  336. // only need to check this if we are not at the
  337. // root level
  338. if ((m_sceneManager.CurrentScene != null) &&
  339. (m_sceneManager.CurrentScene.RegionInfo.RegionID == scene.RegionInfo.RegionID))
  340. {
  341. m_sceneManager.TrySetCurrentScene("..");
  342. }
  343. scene.DeleteAllSceneObjects();
  344. m_sceneManager.CloseScene(scene);
  345. ShutdownClientServer(scene.RegionInfo);
  346. if (!cleanup)
  347. return;
  348. if (!String.IsNullOrEmpty(scene.RegionInfo.RegionFile))
  349. {
  350. if (scene.RegionInfo.RegionFile.ToLower().EndsWith(".xml"))
  351. {
  352. File.Delete(scene.RegionInfo.RegionFile);
  353. m_log.InfoFormat("[OPENSIM]: deleting region file \"{0}\"", scene.RegionInfo.RegionFile);
  354. }
  355. if (scene.RegionInfo.RegionFile.ToLower().EndsWith(".ini"))
  356. {
  357. try
  358. {
  359. IniConfigSource source = new IniConfigSource(scene.RegionInfo.RegionFile);
  360. if (source.Configs[scene.RegionInfo.RegionName] != null)
  361. {
  362. source.Configs.Remove(scene.RegionInfo.RegionName);
  363. if (source.Configs.Count == 0)
  364. {
  365. File.Delete(scene.RegionInfo.RegionFile);
  366. }
  367. else
  368. {
  369. source.Save(scene.RegionInfo.RegionFile);
  370. }
  371. }
  372. }
  373. catch (Exception)
  374. {
  375. }
  376. }
  377. }
  378. }
  379. public void RemoveRegion(string name, bool cleanUp)
  380. {
  381. Scene target;
  382. if (m_sceneManager.TryGetScene(name, out target))
  383. RemoveRegion(target, cleanUp);
  384. }
  385. /// <summary>
  386. /// Remove a region from the simulator without deleting it permanently.
  387. /// </summary>
  388. /// <param name="scene"></param>
  389. /// <returns></returns>
  390. public void CloseRegion(Scene scene)
  391. {
  392. // only need to check this if we are not at the
  393. // root level
  394. if ((m_sceneManager.CurrentScene != null) &&
  395. (m_sceneManager.CurrentScene.RegionInfo.RegionID == scene.RegionInfo.RegionID))
  396. {
  397. m_sceneManager.TrySetCurrentScene("..");
  398. }
  399. m_sceneManager.CloseScene(scene);
  400. ShutdownClientServer(scene.RegionInfo);
  401. }
  402. /// <summary>
  403. /// Remove a region from the simulator without deleting it permanently.
  404. /// </summary>
  405. /// <param name="name"></param>
  406. /// <returns></returns>
  407. public void CloseRegion(string name)
  408. {
  409. Scene target;
  410. if (m_sceneManager.TryGetScene(name, out target))
  411. CloseRegion(target);
  412. }
  413. /// <summary>
  414. /// Create a scene and its initial base structures.
  415. /// </summary>
  416. /// <param name="regionInfo"></param>
  417. /// <param name="clientServer"> </param>
  418. /// <returns></returns>
  419. protected Scene SetupScene(RegionInfo regionInfo, out IClientNetworkServer clientServer)
  420. {
  421. return SetupScene(regionInfo, 0, null, out clientServer);
  422. }
  423. /// <summary>
  424. /// Create a scene and its initial base structures.
  425. /// </summary>
  426. /// <param name="regionInfo"></param>
  427. /// <param name="proxyOffset"></param>
  428. /// <param name="configSource"></param>
  429. /// <param name="clientServer"> </param>
  430. /// <returns></returns>
  431. protected Scene SetupScene(
  432. RegionInfo regionInfo, int proxyOffset, IConfigSource configSource, out IClientNetworkServer clientServer)
  433. {
  434. AgentCircuitManager circuitManager = new AgentCircuitManager();
  435. IPAddress listenIP = regionInfo.InternalEndPoint.Address;
  436. //if (!IPAddress.TryParse(regionInfo.InternalEndPoint, out listenIP))
  437. // listenIP = IPAddress.Parse("0.0.0.0");
  438. uint port = (uint) regionInfo.InternalEndPoint.Port;
  439. if (m_autoCreateClientStack)
  440. {
  441. clientServer
  442. = m_clientStackManager.CreateServer(
  443. listenIP, ref port, proxyOffset, regionInfo.m_allow_alternate_ports, configSource,
  444. circuitManager);
  445. }
  446. else
  447. {
  448. clientServer = null;
  449. }
  450. regionInfo.InternalEndPoint.Port = (int) port;
  451. Scene scene = CreateScene(regionInfo, m_storageManager, circuitManager);
  452. if (m_autoCreateClientStack)
  453. {
  454. clientServer.AddScene(scene);
  455. }
  456. scene.LoadWorldMap();
  457. scene.PhysicsScene = GetPhysicsScene(scene.RegionInfo.RegionName);
  458. scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
  459. scene.PhysicsScene.SetWaterLevel((float) regionInfo.RegionSettings.WaterHeight);
  460. return scene;
  461. }
  462. protected override StorageManager CreateStorageManager()
  463. {
  464. return
  465. CreateStorageManager(m_configSettings.StorageConnectionString, m_configSettings.EstateConnectionString);
  466. }
  467. protected StorageManager CreateStorageManager(string connectionstring, string estateconnectionstring)
  468. {
  469. return new StorageManager(m_configSettings.StorageDll, connectionstring, estateconnectionstring);
  470. }
  471. protected override ClientStackManager CreateClientStackManager()
  472. {
  473. return new ClientStackManager(m_configSettings.ClientstackDll);
  474. }
  475. protected override Scene CreateScene(RegionInfo regionInfo, StorageManager storageManager,
  476. AgentCircuitManager circuitManager)
  477. {
  478. SceneCommunicationService sceneGridService = new SceneCommunicationService();
  479. return new Scene(
  480. regionInfo, circuitManager, sceneGridService,
  481. storageManager, m_moduleLoader, false, m_configSettings.PhysicalPrim,
  482. m_configSettings.See_into_region_from_neighbor, m_config.Source, m_version);
  483. }
  484. protected void ShutdownClientServer(RegionInfo whichRegion)
  485. {
  486. // Close and remove the clientserver for a region
  487. bool foundClientServer = false;
  488. int clientServerElement = 0;
  489. Location location = new Location(whichRegion.RegionHandle);
  490. for (int i = 0; i < m_clientServers.Count; i++)
  491. {
  492. if (m_clientServers[i].HandlesRegion(location))
  493. {
  494. clientServerElement = i;
  495. foundClientServer = true;
  496. break;
  497. }
  498. }
  499. if (foundClientServer)
  500. {
  501. m_clientServers[clientServerElement].NetworkStop();
  502. m_clientServers.RemoveAt(clientServerElement);
  503. }
  504. }
  505. public void handleRestartRegion(RegionInfo whichRegion)
  506. {
  507. m_log.Info("[OPENSIM]: Got restart signal from SceneManager");
  508. ShutdownClientServer(whichRegion);
  509. IScene scene;
  510. CreateRegion(whichRegion, true, out scene);
  511. }
  512. # region Setup methods
  513. protected override PhysicsScene GetPhysicsScene(string osSceneIdentifier)
  514. {
  515. return GetPhysicsScene(
  516. m_configSettings.PhysicsEngine, m_configSettings.MeshEngineName, m_config.Source, osSceneIdentifier);
  517. }
  518. /// <summary>
  519. /// Handler to supply the current status of this sim
  520. /// </summary>
  521. /// Currently this is always OK if the simulator is still listening for connections on its HTTP service
  522. public class SimStatusHandler : IStreamedRequestHandler
  523. {
  524. public byte[] Handle(string path, Stream request,
  525. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  526. {
  527. return Util.UTF8.GetBytes("OK");
  528. }
  529. public string ContentType
  530. {
  531. get { return "text/plain"; }
  532. }
  533. public string HttpMethod
  534. {
  535. get { return "GET"; }
  536. }
  537. public string Path
  538. {
  539. get { return "/simstatus/"; }
  540. }
  541. }
  542. /// <summary>
  543. /// Handler to supply the current extended status of this sim
  544. /// Sends the statistical data in a json serialization
  545. /// </summary>
  546. public class XSimStatusHandler : IStreamedRequestHandler
  547. {
  548. OpenSimBase m_opensim;
  549. string osXStatsURI = String.Empty;
  550. public XSimStatusHandler(OpenSimBase sim)
  551. {
  552. m_opensim = sim;
  553. osXStatsURI = Util.SHA1Hash(sim.osSecret);
  554. }
  555. public byte[] Handle(string path, Stream request,
  556. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  557. {
  558. return Util.UTF8.GetBytes(m_opensim.StatReport(httpRequest));
  559. }
  560. public string ContentType
  561. {
  562. get { return "text/plain"; }
  563. }
  564. public string HttpMethod
  565. {
  566. get { return "GET"; }
  567. }
  568. public string Path
  569. {
  570. // This is for the OpenSimulator instance and is the osSecret hashed
  571. get { return "/" + osXStatsURI + "/"; }
  572. }
  573. }
  574. /// <summary>
  575. /// Handler to supply the current extended status of this sim to a user configured URI
  576. /// Sends the statistical data in a json serialization
  577. /// If the request contains a key, "callback" the response will be wrappend in the
  578. /// associated value for jsonp used with ajax/javascript
  579. /// </summary>
  580. public class UXSimStatusHandler : IStreamedRequestHandler
  581. {
  582. OpenSimBase m_opensim;
  583. string osUXStatsURI = String.Empty;
  584. public UXSimStatusHandler(OpenSimBase sim)
  585. {
  586. m_opensim = sim;
  587. osUXStatsURI = sim.userStatsURI;
  588. }
  589. public byte[] Handle(string path, Stream request,
  590. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  591. {
  592. return Util.UTF8.GetBytes(m_opensim.StatReport(httpRequest));
  593. }
  594. public string ContentType
  595. {
  596. get { return "text/plain"; }
  597. }
  598. public string HttpMethod
  599. {
  600. get { return "GET"; }
  601. }
  602. public string Path
  603. {
  604. // This is for the OpenSimulator instance and is the user provided URI
  605. get { return "/" + osUXStatsURI + "/"; }
  606. }
  607. }
  608. #endregion
  609. /// <summary>
  610. /// Performs any last-minute sanity checking and shuts down the region server
  611. /// </summary>
  612. public override void ShutdownSpecific()
  613. {
  614. if (proxyUrl.Length > 0)
  615. {
  616. Util.XmlRpcCommand(proxyUrl, "Stop");
  617. }
  618. m_log.Info("[SHUTDOWN]: Closing all threads");
  619. m_log.Info("[SHUTDOWN]: Killing listener thread");
  620. m_log.Info("[SHUTDOWN]: Killing clients");
  621. // TODO: implement this
  622. m_log.Info("[SHUTDOWN]: Closing console and terminating");
  623. try
  624. {
  625. m_sceneManager.Close();
  626. }
  627. catch (Exception e)
  628. {
  629. m_log.ErrorFormat("[SHUTDOWN]: Ignoring failure during shutdown - {0}", e);
  630. }
  631. }
  632. /// <summary>
  633. /// Get the start time and up time of Region server
  634. /// </summary>
  635. /// <param name="starttime">The first out parameter describing when the Region server started</param>
  636. /// <param name="uptime">The second out parameter describing how long the Region server has run</param>
  637. public void GetRunTime(out string starttime, out string uptime)
  638. {
  639. starttime = m_startuptime.ToString();
  640. uptime = (DateTime.Now - m_startuptime).ToString();
  641. }
  642. /// <summary>
  643. /// Get the number of the avatars in the Region server
  644. /// </summary>
  645. /// <param name="usernum">The first out parameter describing the number of all the avatars in the Region server</param>
  646. public void GetAvatarNumber(out int usernum)
  647. {
  648. usernum = m_sceneManager.GetCurrentSceneAvatars().Count;
  649. }
  650. /// <summary>
  651. /// Get the number of regions
  652. /// </summary>
  653. /// <param name="regionnum">The first out parameter describing the number of regions</param>
  654. public void GetRegionNumber(out int regionnum)
  655. {
  656. regionnum = m_sceneManager.Scenes.Count;
  657. }
  658. /// <summary>
  659. /// Load the estate information for the provided RegionInfo object.
  660. /// </summary>
  661. /// <param name="regInfo">
  662. /// A <see cref="RegionInfo"/>
  663. /// </param>
  664. public void PopulateRegionEstateInfo(RegionInfo regInfo)
  665. {
  666. if (m_storageManager.EstateDataStore != null)
  667. {
  668. regInfo.EstateSettings = m_storageManager.EstateDataStore.LoadEstateSettings(regInfo.RegionID, false);
  669. }
  670. if (regInfo.EstateSettings.EstateID == 0) // No record at all
  671. {
  672. MainConsole.Instance.Output("Your region is not part of an estate.");
  673. while (true)
  674. {
  675. string response = MainConsole.Instance.CmdPrompt("Do you wish to join an existing estate?", "no", new List<string>() {"yes", "no"});
  676. if (response == "no")
  677. {
  678. // Create a new estate
  679. regInfo.EstateSettings = m_storageManager.EstateDataStore.LoadEstateSettings(regInfo.RegionID, true);
  680. regInfo.EstateSettings.EstateName = MainConsole.Instance.CmdPrompt("New estate name", regInfo.EstateSettings.EstateName);
  681. //regInfo.EstateSettings.Save();
  682. break;
  683. }
  684. else
  685. {
  686. response = MainConsole.Instance.CmdPrompt("Estate name to join", "None");
  687. if (response == "None")
  688. continue;
  689. List<int> estateIDs = m_storageManager.EstateDataStore.GetEstates(response);
  690. if (estateIDs.Count < 1)
  691. {
  692. MainConsole.Instance.Output("The name you have entered matches no known estate. Please try again");
  693. continue;
  694. }
  695. int estateID = estateIDs[0];
  696. regInfo.EstateSettings = m_storageManager.EstateDataStore.LoadEstateSettings(estateID);
  697. if (m_storageManager.EstateDataStore.LinkRegion(regInfo.RegionID, estateID))
  698. break;
  699. MainConsole.Instance.Output("Joining the estate failed. Please try again.");
  700. }
  701. }
  702. }
  703. }
  704. }
  705. public class OpenSimConfigSource
  706. {
  707. public IConfigSource Source;
  708. public void Save(string path)
  709. {
  710. if (Source is IniConfigSource)
  711. {
  712. IniConfigSource iniCon = (IniConfigSource) Source;
  713. iniCon.Save(path);
  714. }
  715. else if (Source is XmlConfigSource)
  716. {
  717. XmlConfigSource xmlCon = (XmlConfigSource) Source;
  718. xmlCon.Save(path);
  719. }
  720. }
  721. }
  722. }