OpenSimMain.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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. */
  28. using System;
  29. using System.IO;
  30. using libsecondlife;
  31. using OpenSim.Assets;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Data;
  36. using OpenSim.Framework.Interfaces;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Types;
  39. using OpenSim.GenericConfig;
  40. using OpenSim.Physics.Manager;
  41. using OpenSim.Region.Caches;
  42. using OpenSim.Region.ClientStack;
  43. using OpenSim.Region.Communications.Local;
  44. using OpenSim.Region.Communications.OGS1;
  45. using OpenSim.Region.Environment.Scenes;
  46. using OpenSim.Region.Environment;
  47. using System.Text;
  48. namespace OpenSim
  49. {
  50. public class OpenSimMain : RegionApplicationBase, conscmd_callback
  51. {
  52. protected CommunicationsManager commsManager;
  53. // private CheckSumServer checkServer;
  54. private bool m_silent;
  55. private string m_logFilename = "region-console-" + Guid.NewGuid().ToString() + ".log";
  56. public OpenSimMain(bool sandBoxMode, bool startLoginServer, string physicsEngine, bool useConfigFile, bool silent, string configFile)
  57. {
  58. this.configFileSetup = useConfigFile;
  59. m_sandbox = sandBoxMode;
  60. m_loginserver = startLoginServer;
  61. m_physicsEngine = physicsEngine;
  62. m_config = configFile;
  63. m_silent = silent;
  64. }
  65. /// <summary>
  66. /// Performs initialisation of the world, such as loading configuration from disk.
  67. /// </summary>
  68. public override void StartUp()
  69. {
  70. this.serversData = new NetworkServersInfo();
  71. this.localConfig = new XmlConfig(m_config);
  72. this.localConfig.LoadData();
  73. if (this.configFileSetup)
  74. {
  75. this.SetupFromConfigFile(this.localConfig);
  76. }
  77. m_log = new LogBase(m_logFilename, "Region", this, m_silent);
  78. MainLog.Instance = m_log;
  79. m_log.Verbose("Main.cs:Startup() - Loading configuration");
  80. this.serversData.InitConfig(this.m_sandbox, this.localConfig);
  81. this.localConfig.Close();//for now we can close it as no other classes read from it , but this should change
  82. ScenePresence.LoadTextureFile("avatar-texture.dat");
  83. ClientView.TerrainManager = new TerrainManager(new SecondLife());
  84. this.SetupHttpListener();
  85. if (m_sandbox)
  86. {
  87. this.SetupLocalGridServers();
  88. // this.checkServer = new CheckSumServer(12036);
  89. // this.checkServer.ServerListener();
  90. }
  91. else
  92. {
  93. this.SetupRemoteGridServers();
  94. }
  95. startuptime = DateTime.Now;
  96. this.physManager = new PhysicsManager();
  97. this.physManager.LoadPlugins();
  98. this.SetupScene();
  99. m_log.Verbose("Main.cs:Startup() - Initialising HTTP server");
  100. //Start http server
  101. m_log.Verbose("Main.cs:Startup() - Starting HTTP server");
  102. httpServer.Start();
  103. // Start UDP servers
  104. for (int i = 0; i < m_udpServer.Count; i++)
  105. {
  106. this.m_udpServer[i].ServerListener();
  107. }
  108. }
  109. # region Setup methods
  110. protected override void SetupLocalGridServers()
  111. {
  112. try
  113. {
  114. AssetCache = new AssetCache("OpenSim.Region.GridInterfaces.Local.dll", this.serversData.AssetURL, this.serversData.AssetSendKey);
  115. InventoryCache = new InventoryCache();
  116. this.commsManager = new CommunicationsLocal(this.serversData, httpServer);
  117. }
  118. catch (Exception e)
  119. {
  120. m_log.Error(e.Message + "\nSorry, could not setup local cache");
  121. Environment.Exit(1);
  122. }
  123. }
  124. protected override void SetupRemoteGridServers()
  125. {
  126. try
  127. {
  128. AssetCache = new AssetCache("OpenSim.Region.GridInterfaces.Local.dll", this.serversData.AssetURL, this.serversData.AssetSendKey);
  129. InventoryCache = new InventoryCache();
  130. this.commsManager = new CommunicationsOGS1(this.serversData, httpServer);
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.Error(e.Message + "\nSorry, could not setup remote cache");
  135. Environment.Exit(1);
  136. }
  137. }
  138. protected override void SetupScene()
  139. {
  140. IGenericConfig regionConfig;
  141. Scene scene;
  142. UDPServer udpServer;
  143. RegionInfo regionDat = new RegionInfo();
  144. AuthenticateSessionsBase authenBase;
  145. string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Regions");
  146. string[] configFiles = Directory.GetFiles(path, "*.xml");
  147. if (configFiles.Length == 0)
  148. {
  149. string path2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Regions");
  150. string path3 = Path.Combine(path2, "default.xml");
  151. Console.WriteLine("Creating default region config file");
  152. //TODO create default region
  153. IGenericConfig defaultConfig = new XmlConfig(path3);
  154. defaultConfig.LoadData();
  155. defaultConfig.Commit();
  156. defaultConfig.Close();
  157. defaultConfig = null;
  158. configFiles = Directory.GetFiles(path, "*.xml");
  159. }
  160. for (int i = 0; i < configFiles.Length; i++)
  161. {
  162. regionDat = new RegionInfo();
  163. if (m_sandbox)
  164. {
  165. AuthenticateSessionsBase authen = new AuthenticateSessionsBase(); // new AuthenticateSessionsLocal();
  166. this.AuthenticateSessionsHandler.Add(authen);
  167. authenBase = authen;
  168. }
  169. else
  170. {
  171. AuthenticateSessionsBase authen = new AuthenticateSessionsBase(); //new AuthenticateSessionsRemote();
  172. this.AuthenticateSessionsHandler.Add(authen);
  173. authenBase = authen;
  174. }
  175. Console.WriteLine("Loading region config file");
  176. regionConfig = new XmlConfig(configFiles[i]);
  177. regionConfig.LoadData();
  178. regionDat.InitConfig(this.m_sandbox, regionConfig);
  179. regionConfig.Close();
  180. udpServer = new UDPServer(regionDat.InternalEndPoint.Port, this.AssetCache, this.InventoryCache, this.m_log, authenBase);
  181. m_udpServer.Add(udpServer);
  182. this.regionData.Add(regionDat);
  183. StorageManager tmpStoreManager = new StorageManager("OpenSim.DataStore.NullStorage.dll", regionDat.DataStore, regionDat.RegionName);
  184. scene = new Scene( regionDat, authenBase, commsManager, this.AssetCache, tmpStoreManager, httpServer);
  185. this.m_localWorld.Add(scene);
  186. udpServer.LocalWorld = scene;
  187. scene.LoadStorageDLL("OpenSim.Region.Storage.LocalStorageDb4o.dll"); //all these dll names shouldn't be hard coded.
  188. scene.LoadWorldMap();
  189. m_log.Verbose("Main.cs:Startup() - Starting up messaging system");
  190. scene.PhysScene = this.physManager.GetPhysicsScene(this.m_physicsEngine);
  191. scene.PhysScene.SetTerrain(scene.Terrain.getHeights1D());
  192. scene.LoadPrimsFromStorage();
  193. //Master Avatar Setup
  194. UserProfileData masterAvatar = commsManager.UserServer.SetupMasterUser(scene.RegionInfo.MasterAvatarFirstName, scene.RegionInfo.MasterAvatarLastName, scene.RegionInfo.MasterAvatarSandboxPassword);
  195. if (masterAvatar != null)
  196. {
  197. m_log.Notice("Parcels - Found master avatar [" + masterAvatar.UUID.ToStringHyphenated() + "]");
  198. scene.RegionInfo.MasterAvatarAssignedUUID = masterAvatar.UUID;
  199. scene.localStorage.LoadParcels((ILocalStorageParcelReceiver)scene.ParcelManager);
  200. }
  201. else
  202. {
  203. m_log.Notice("Parcels - No master avatar found, using null.");
  204. scene.RegionInfo.MasterAvatarAssignedUUID = libsecondlife.LLUUID.Zero;
  205. scene.localStorage.LoadParcels((ILocalStorageParcelReceiver)scene.ParcelManager);
  206. }
  207. scene.performParcelPrimCountUpdate();
  208. scene.StartTimer();
  209. }
  210. }
  211. private class SimStatusHandler : IStreamHandler
  212. {
  213. public byte[] Handle(string path, Stream request)
  214. {
  215. return Encoding.UTF8.GetBytes("OK");
  216. }
  217. public string ContentType
  218. {
  219. get { return "text/plain"; }
  220. }
  221. public string HttpMethod
  222. {
  223. get { return "GET"; }
  224. }
  225. public string Path
  226. {
  227. get { return "/simstatus/"; }
  228. }
  229. }
  230. protected override void SetupHttpListener()
  231. {
  232. httpServer = new BaseHttpServer(this.serversData.HttpListenerPort); //regionData[0].IPListenPort);
  233. if (!this.m_sandbox)
  234. {
  235. httpServer.AddStreamHandler( new SimStatusHandler() );
  236. }
  237. }
  238. protected override void ConnectToRemoteGridServer()
  239. {
  240. }
  241. #endregion
  242. private void SetupFromConfigFile(IGenericConfig configData)
  243. {
  244. // Log filename
  245. string attri = "";
  246. attri = configData.GetAttribute("LogFilename");
  247. if (String.IsNullOrEmpty(attri))
  248. {
  249. }
  250. else
  251. {
  252. m_logFilename = attri;
  253. }
  254. // SandBoxMode
  255. attri = "";
  256. attri = configData.GetAttribute("SandBox");
  257. if ((attri == "") || ((attri != "false") && (attri != "true")))
  258. {
  259. this.m_sandbox = false;
  260. configData.SetAttribute("SandBox", "false");
  261. }
  262. else
  263. {
  264. this.m_sandbox = Convert.ToBoolean(attri);
  265. }
  266. // LoginServer
  267. attri = "";
  268. attri = configData.GetAttribute("LoginServer");
  269. if ((attri == "") || ((attri != "false") && (attri != "true")))
  270. {
  271. this.m_loginserver = false;
  272. configData.SetAttribute("LoginServer", "false");
  273. }
  274. else
  275. {
  276. this.m_loginserver = Convert.ToBoolean(attri);
  277. }
  278. // Sandbox User accounts
  279. attri = "";
  280. attri = configData.GetAttribute("UserAccount");
  281. if ((attri == "") || ((attri != "false") && (attri != "true")))
  282. {
  283. this.user_accounts = false;
  284. configData.SetAttribute("UserAccounts", "false");
  285. }
  286. else if (attri == "true")
  287. {
  288. this.user_accounts = Convert.ToBoolean(attri);
  289. }
  290. // Grid mode hack to use local asset server
  291. attri = "";
  292. attri = configData.GetAttribute("LocalAssets");
  293. if ((attri == "") || ((attri != "false") && (attri != "true")))
  294. {
  295. this.gridLocalAsset = false;
  296. configData.SetAttribute("LocalAssets", "false");
  297. }
  298. else if (attri == "true")
  299. {
  300. this.gridLocalAsset = Convert.ToBoolean(attri);
  301. }
  302. attri = "";
  303. attri = configData.GetAttribute("PhysicsEngine");
  304. switch (attri)
  305. {
  306. default:
  307. m_log.Warn("Main.cs: SetupFromConfig() - Invalid value for PhysicsEngine attribute, terminating");
  308. Environment.Exit(1);
  309. break;
  310. case "":
  311. this.m_physicsEngine = "basicphysics";
  312. configData.SetAttribute("PhysicsEngine", "basicphysics");
  313. ScenePresence.PhysicsEngineFlying = false;
  314. break;
  315. case "basicphysics":
  316. this.m_physicsEngine = "basicphysics";
  317. configData.SetAttribute("PhysicsEngine", "basicphysics");
  318. ScenePresence.PhysicsEngineFlying = false;
  319. break;
  320. case "RealPhysX":
  321. this.m_physicsEngine = "RealPhysX";
  322. ScenePresence.PhysicsEngineFlying = true;
  323. break;
  324. case "OpenDynamicsEngine":
  325. this.m_physicsEngine = "OpenDynamicsEngine";
  326. ScenePresence.PhysicsEngineFlying = true;
  327. break;
  328. case "BulletXEngine":
  329. this.m_physicsEngine = "BulletXEngine";
  330. ScenePresence.PhysicsEngineFlying = true;
  331. break;
  332. }
  333. configData.Commit();
  334. }
  335. /// <summary>
  336. /// Performs any last-minute sanity checking and shuts down the region server
  337. /// </summary>
  338. public virtual void Shutdown()
  339. {
  340. m_log.Verbose("Main.cs:Shutdown() - Closing all threads");
  341. m_log.Verbose("Main.cs:Shutdown() - Killing listener thread");
  342. m_log.Verbose("Main.cs:Shutdown() - Killing clients");
  343. // IMPLEMENT THIS
  344. m_log.Verbose("Main.cs:Shutdown() - Closing console and terminating");
  345. for (int i = 0; i < m_localWorld.Count; i++)
  346. {
  347. ((Scene)m_localWorld[i]).Close();
  348. }
  349. m_log.Close();
  350. Environment.Exit(0);
  351. }
  352. #region Console Commands
  353. /// <summary>
  354. /// Runs commands issued by the server console from the operator
  355. /// </summary>
  356. /// <param name="command">The first argument of the parameter (the command)</param>
  357. /// <param name="cmdparams">Additional arguments passed to the command</param>
  358. public void RunCmd(string command, string[] cmdparams)
  359. {
  360. switch (command)
  361. {
  362. case "help":
  363. m_log.Error("show users - show info about connected users");
  364. m_log.Error("quit - disconnect all clients and shutdown");
  365. break;
  366. case "show":
  367. if (cmdparams.Length > 0)
  368. {
  369. Show(cmdparams[0]);
  370. }
  371. break;
  372. case "terrain":
  373. string result = "";
  374. for (int i = 0; i < m_localWorld.Count; i++)
  375. {
  376. if (!((Scene)m_localWorld[i]).Terrain.RunTerrainCmd(cmdparams, ref result, m_localWorld[i].RegionInfo.RegionName))
  377. {
  378. m_log.Error(result);
  379. }
  380. }
  381. break;
  382. case "script":
  383. for (int i = 0; i < m_localWorld.Count; i++)
  384. {
  385. ((Scene)m_localWorld[i]).SendCommandToScripts(cmdparams);
  386. }
  387. break;
  388. case "quit":
  389. case "shutdown":
  390. Shutdown();
  391. break;
  392. default:
  393. m_log.Error("Unknown command");
  394. break;
  395. }
  396. }
  397. /// <summary>
  398. /// Outputs to the console information about the region
  399. /// </summary>
  400. /// <param name="ShowWhat">What information to display (valid arguments are "uptime", "users")</param>
  401. public void Show(string ShowWhat)
  402. {
  403. switch (ShowWhat)
  404. {
  405. case "uptime":
  406. m_log.Error("OpenSim has been running since " + startuptime.ToString());
  407. m_log.Error("That is " + (DateTime.Now - startuptime).ToString());
  408. break;
  409. case "users":
  410. ScenePresence TempAv;
  411. m_log.Error(String.Format("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16}{5,-16}{6,-16}", "Firstname", "Lastname", "Agent ID", "Session ID", "Circuit", "IP", "World"));
  412. for (int i = 0; i < m_localWorld.Count; i++)
  413. {
  414. foreach (libsecondlife.LLUUID UUID in ((Scene)m_localWorld[i]).Entities.Keys)
  415. {
  416. if (((Scene)m_localWorld[i]).Entities[UUID].ToString() == "OpenSim.world.Avatar")
  417. {
  418. TempAv = (ScenePresence)((Scene)m_localWorld[i]).Entities[UUID];
  419. m_log.Error(String.Format("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}{6,-16}", TempAv.firstname, TempAv.lastname, UUID, TempAv.ControllingClient.AgentId, "Unknown", "Unknown"), ((Scene)m_localWorld[i]).RegionInfo.RegionName);
  420. }
  421. }
  422. }
  423. break;
  424. }
  425. }
  426. #endregion
  427. }
  428. }