Main.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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.Reflection;
  31. using log4net;
  32. using log4net.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Data;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications;
  37. using OpenSim.Framework.Communications.Cache;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Framework.Statistics;
  42. using OpenSim.Grid.Communications.OGS1;
  43. using OpenSim.Grid.Framework;
  44. using OpenSim.Grid.UserServer.Modules;
  45. using Nini.Config;
  46. namespace OpenSim.Grid.UserServer
  47. {
  48. /// <summary>
  49. /// Grid user server main class
  50. /// </summary>
  51. public class OpenUser_Main : BaseOpenSimServer, IGridServiceCore
  52. {
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. protected UserConfig Cfg;
  55. protected UserDataBaseService m_userDataBaseService;
  56. public UserManager m_userManager;
  57. protected UserServerAvatarAppearanceModule m_avatarAppearanceModule;
  58. protected UserServerFriendsModule m_friendsModule;
  59. public UserLoginService m_loginService;
  60. public UserLoginAuthService m_loginAuthService;
  61. public MessageServersConnector m_messagesService;
  62. protected GridInfoServiceModule m_gridInfoService;
  63. protected UserServerCommandModule m_consoleCommandModule;
  64. protected UserServerEventDispatchModule m_eventDispatcher;
  65. protected AvatarCreationModule m_appearanceModule;
  66. protected static string m_consoleType = "local";
  67. protected static IConfigSource m_config = null;
  68. protected static string m_configFile = "UserServer_Config.xml";
  69. public static void Main(string[] args)
  70. {
  71. ArgvConfigSource argvSource = new ArgvConfigSource(args);
  72. argvSource.AddSwitch("Startup", "console", "c");
  73. argvSource.AddSwitch("Startup", "xmlfile", "x");
  74. IConfig startupConfig = argvSource.Configs["Startup"];
  75. if (startupConfig != null)
  76. {
  77. m_consoleType = startupConfig.GetString("console", "local");
  78. m_configFile = startupConfig.GetString("xmlfile", "UserServer_Config.xml");
  79. }
  80. m_config = argvSource;
  81. XmlConfigurator.Configure();
  82. m_log.Info("Launching UserServer...");
  83. OpenUser_Main userserver = new OpenUser_Main();
  84. userserver.Startup();
  85. userserver.Work();
  86. }
  87. public OpenUser_Main()
  88. {
  89. switch (m_consoleType)
  90. {
  91. case "rest":
  92. m_console = new RemoteConsole("User");
  93. break;
  94. case "basic":
  95. m_console = new CommandConsole("User");
  96. break;
  97. default:
  98. m_console = new LocalConsole("User");
  99. break;
  100. }
  101. MainConsole.Instance = m_console;
  102. }
  103. public void Work()
  104. {
  105. m_console.Output("Enter help for a list of commands\n");
  106. while (true)
  107. {
  108. m_console.Prompt();
  109. }
  110. }
  111. protected override void StartupSpecific()
  112. {
  113. IInterServiceInventoryServices inventoryService = StartupCoreComponents();
  114. m_stats = StatsManager.StartCollectingUserStats();
  115. //setup services/modules
  116. StartupUserServerModules();
  117. StartOtherComponents(inventoryService);
  118. //PostInitialise the modules
  119. PostInitialiseModules();
  120. //register http handlers and start http server
  121. m_log.Info("[STARTUP]: Starting HTTP process");
  122. RegisterHttpHandlers();
  123. m_httpServer.Start();
  124. base.StartupSpecific();
  125. }
  126. protected virtual IInterServiceInventoryServices StartupCoreComponents()
  127. {
  128. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), m_configFile)));
  129. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  130. if (m_console is RemoteConsole)
  131. {
  132. RemoteConsole c = (RemoteConsole)m_console;
  133. c.SetServer(m_httpServer);
  134. IConfig netConfig = m_config.AddConfig("Network");
  135. netConfig.Set("ConsoleUser", Cfg.ConsoleUser);
  136. netConfig.Set("ConsolePass", Cfg.ConsolePass);
  137. c.ReadConfig(m_config);
  138. }
  139. RegisterInterface<CommandConsole>(m_console);
  140. RegisterInterface<UserConfig>(Cfg);
  141. //Should be in modules?
  142. IInterServiceInventoryServices inventoryService = new OGS1InterServiceInventoryService(Cfg.InventoryUrl);
  143. // IRegionProfileRouter regionProfileService = new RegionProfileServiceProxy();
  144. RegisterInterface<IInterServiceInventoryServices>(inventoryService);
  145. // RegisterInterface<IRegionProfileRouter>(regionProfileService);
  146. return inventoryService;
  147. }
  148. /// <summary>
  149. /// Start up the user manager
  150. /// </summary>
  151. /// <param name="inventoryService"></param>
  152. protected virtual void StartupUserServerModules()
  153. {
  154. m_log.Info("[STARTUP]: Establishing data connection");
  155. //we only need core components so we can request them from here
  156. IInterServiceInventoryServices inventoryService;
  157. TryGet<IInterServiceInventoryServices>(out inventoryService);
  158. CommunicationsManager commsManager = new UserServerCommsManager(inventoryService);
  159. //setup database access service, for now this has to be created before the other modules.
  160. m_userDataBaseService = new UserDataBaseService(commsManager);
  161. m_userDataBaseService.Initialise(this);
  162. //TODO: change these modules so they fetch the databaseService class in the PostInitialise method
  163. m_userManager = new UserManager(m_userDataBaseService);
  164. m_userManager.Initialise(this);
  165. m_avatarAppearanceModule = new UserServerAvatarAppearanceModule(m_userDataBaseService);
  166. m_avatarAppearanceModule.Initialise(this);
  167. m_friendsModule = new UserServerFriendsModule(m_userDataBaseService);
  168. m_friendsModule.Initialise(this);
  169. m_consoleCommandModule = new UserServerCommandModule();
  170. m_consoleCommandModule.Initialise(this);
  171. m_messagesService = new MessageServersConnector();
  172. m_messagesService.Initialise(this);
  173. m_gridInfoService = new GridInfoServiceModule();
  174. m_gridInfoService.Initialise(this);
  175. }
  176. protected virtual void StartOtherComponents(IInterServiceInventoryServices inventoryService)
  177. {
  178. m_appearanceModule = new AvatarCreationModule(m_userDataBaseService, Cfg, inventoryService);
  179. m_appearanceModule.Initialise(this);
  180. StartupLoginService(inventoryService);
  181. //
  182. // Get the minimum defaultLevel to access to the grid
  183. //
  184. m_loginService.setloginlevel((int)Cfg.DefaultUserLevel);
  185. RegisterInterface<UserLoginService>(m_loginService); //TODO: should be done in the login service
  186. m_eventDispatcher = new UserServerEventDispatchModule(m_userManager, m_messagesService, m_loginService);
  187. m_eventDispatcher.Initialise(this);
  188. }
  189. /// <summary>
  190. /// Start up the login service
  191. /// </summary>
  192. /// <param name="inventoryService"></param>
  193. protected virtual void StartupLoginService(IInterServiceInventoryServices inventoryService)
  194. {
  195. m_loginService = new UserLoginService(
  196. m_userDataBaseService, inventoryService, new LibraryRootFolder(Cfg.LibraryXmlfile), Cfg, Cfg.DefaultStartupMsg, new RegionProfileServiceProxy());
  197. if (Cfg.EnableHGLogin)
  198. m_loginAuthService = new UserLoginAuthService(m_userDataBaseService, inventoryService, new LibraryRootFolder(Cfg.LibraryXmlfile),
  199. Cfg, Cfg.DefaultStartupMsg, new RegionProfileServiceProxy());
  200. }
  201. protected virtual void PostInitialiseModules()
  202. {
  203. m_consoleCommandModule.PostInitialise(); //it will register its Console command handlers in here
  204. m_userDataBaseService.PostInitialise();
  205. m_messagesService.PostInitialise();
  206. m_eventDispatcher.PostInitialise(); //it will register event handlers in here
  207. m_gridInfoService.PostInitialise();
  208. m_userManager.PostInitialise();
  209. m_avatarAppearanceModule.PostInitialise();
  210. m_friendsModule.PostInitialise();
  211. }
  212. protected virtual void RegisterHttpHandlers()
  213. {
  214. m_loginService.RegisterHandlers(m_httpServer, Cfg.EnableLLSDLogin, true);
  215. if (m_loginAuthService != null)
  216. m_loginAuthService.RegisterHandlers(m_httpServer);
  217. m_userManager.RegisterHandlers(m_httpServer);
  218. m_friendsModule.RegisterHandlers(m_httpServer);
  219. m_avatarAppearanceModule.RegisterHandlers(m_httpServer);
  220. m_messagesService.RegisterHandlers(m_httpServer);
  221. m_gridInfoService.RegisterHandlers(m_httpServer);
  222. }
  223. public override void ShutdownSpecific()
  224. {
  225. m_eventDispatcher.Close();
  226. }
  227. #region IUGAIMCore
  228. protected Dictionary<Type, object> m_moduleInterfaces = new Dictionary<Type, object>();
  229. /// <summary>
  230. /// Register an Module interface.
  231. /// </summary>
  232. /// <typeparam name="T"></typeparam>
  233. /// <param name="iface"></param>
  234. public void RegisterInterface<T>(T iface)
  235. {
  236. lock (m_moduleInterfaces)
  237. {
  238. if (!m_moduleInterfaces.ContainsKey(typeof(T)))
  239. {
  240. m_moduleInterfaces.Add(typeof(T), iface);
  241. }
  242. }
  243. }
  244. public bool TryGet<T>(out T iface)
  245. {
  246. if (m_moduleInterfaces.ContainsKey(typeof(T)))
  247. {
  248. iface = (T)m_moduleInterfaces[typeof(T)];
  249. return true;
  250. }
  251. iface = default(T);
  252. return false;
  253. }
  254. public T Get<T>()
  255. {
  256. return (T)m_moduleInterfaces[typeof(T)];
  257. }
  258. public BaseHttpServer GetHttpServer()
  259. {
  260. return m_httpServer;
  261. }
  262. #endregion
  263. public void TestResponse(List<InventoryFolderBase> resp)
  264. {
  265. m_console.Output("response got");
  266. }
  267. }
  268. }