Main.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 Nini.Config;
  33. using log4net.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Grid.Framework;
  39. using OpenSim.Grid.MessagingServer.Modules;
  40. namespace OpenSim.Grid.MessagingServer
  41. {
  42. /// <summary>
  43. /// </summary>
  44. public class OpenMessage_Main : BaseOpenSimServer , IGridServiceCore
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private MessageServerConfig Cfg;
  48. private MessageService msgsvc;
  49. private MessageRegionModule m_regionModule;
  50. private InterMessageUserServerModule m_userServerModule;
  51. private UserDataBaseService m_userDataBaseService;
  52. // private UUID m_lastCreatedUser = UUID.Random();
  53. protected static string m_consoleType = "local";
  54. protected static IConfigSource m_config = null;
  55. protected static string m_configFile = "MessagingServer_Config.xml";
  56. public static void Main(string[] args)
  57. {
  58. ArgvConfigSource argvSource = new ArgvConfigSource(args);
  59. argvSource.AddSwitch("Startup", "console", "c");
  60. argvSource.AddSwitch("Startup", "xmlfile", "x");
  61. IConfig startupConfig = argvSource.Configs["Startup"];
  62. if (startupConfig != null)
  63. {
  64. m_consoleType = startupConfig.GetString("console", "local");
  65. m_configFile = startupConfig.GetString("xmlfile", "MessagingServer_Config.xml");
  66. }
  67. m_config = argvSource;
  68. XmlConfigurator.Configure();
  69. m_log.Info("[SERVER]: Launching MessagingServer...");
  70. OpenMessage_Main messageserver = new OpenMessage_Main();
  71. messageserver.Startup();
  72. messageserver.Work();
  73. }
  74. public OpenMessage_Main()
  75. {
  76. switch (m_consoleType)
  77. {
  78. case "rest":
  79. m_console = new RemoteConsole("Messaging");
  80. break;
  81. case "basic":
  82. m_console = new CommandConsole("Messaging");
  83. break;
  84. default:
  85. m_console = new LocalConsole("Messaging");
  86. break;
  87. }
  88. MainConsole.Instance = m_console;
  89. }
  90. private void Work()
  91. {
  92. m_console.Output("Enter help for a list of commands\n");
  93. while (true)
  94. {
  95. m_console.Prompt();
  96. }
  97. }
  98. private void registerWithUserServer()
  99. {
  100. if (m_userServerModule.registerWithUserServer())
  101. {
  102. if (m_httpServer == null)
  103. {
  104. m_log.Info("[SERVER]: Starting HTTP process");
  105. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  106. if (m_console is RemoteConsole)
  107. {
  108. RemoteConsole c = (RemoteConsole)m_console;
  109. c.SetServer(m_httpServer);
  110. IConfig netConfig = m_config.AddConfig("Network");
  111. netConfig.Set("ConsoleUser", Cfg.ConsoleUser);
  112. netConfig.Set("ConsolePass", Cfg.ConsolePass);
  113. c.ReadConfig(m_config);
  114. }
  115. m_httpServer.AddXmlRPCHandler("login_to_simulator", msgsvc.UserLoggedOn);
  116. m_httpServer.AddXmlRPCHandler("logout_of_simulator", msgsvc.UserLoggedOff);
  117. m_httpServer.AddXmlRPCHandler("get_presence_info_bulk", msgsvc.GetPresenceInfoBulk);
  118. m_httpServer.AddXmlRPCHandler("process_region_shutdown", msgsvc.ProcessRegionShutdown);
  119. m_httpServer.AddXmlRPCHandler("agent_location", msgsvc.AgentLocation);
  120. m_httpServer.AddXmlRPCHandler("agent_leaving", msgsvc.AgentLeaving);
  121. m_httpServer.AddXmlRPCHandler("region_startup", m_regionModule.RegionStartup);
  122. m_httpServer.AddXmlRPCHandler("region_shutdown", m_regionModule.RegionShutdown);
  123. m_httpServer.Start();
  124. }
  125. m_log.Info("[SERVER]: Userserver registration was successful");
  126. }
  127. else
  128. {
  129. m_log.Error("[STARTUP]: Unable to connect to User Server");
  130. }
  131. }
  132. private void deregisterFromUserServer()
  133. {
  134. m_userServerModule.deregisterWithUserServer();
  135. // if (m_httpServer != null)
  136. // {
  137. // try a completely fresh registration, with fresh handlers, too
  138. // m_httpServer.Stop();
  139. // m_httpServer = null;
  140. // }
  141. m_console.Output("[SERVER]: Deregistered from userserver.");
  142. }
  143. protected override void StartupSpecific()
  144. {
  145. Cfg = new MessageServerConfig("MESSAGING SERVER", (Path.Combine(Util.configDir(), m_configFile)));
  146. m_userDataBaseService = new UserDataBaseService();
  147. m_userDataBaseService.AddPlugin(Cfg.DatabaseProvider, Cfg.DatabaseConnect);
  148. //Register the database access service so modules can fetch it
  149. // RegisterInterface<UserDataBaseService>(m_userDataBaseService);
  150. m_userServerModule = new InterMessageUserServerModule(Cfg, this);
  151. m_userServerModule.Initialise();
  152. msgsvc = new MessageService(Cfg, this, m_userDataBaseService);
  153. msgsvc.Initialise();
  154. m_regionModule = new MessageRegionModule(Cfg, this);
  155. m_regionModule.Initialise();
  156. registerWithUserServer();
  157. m_userServerModule.PostInitialise();
  158. msgsvc.PostInitialise();
  159. m_regionModule.PostInitialise();
  160. m_log.Info("[SERVER]: Messageserver 0.5 - Startup complete");
  161. base.StartupSpecific();
  162. m_console.Commands.AddCommand("messageserver", false, "clear cache",
  163. "clear cache",
  164. "Clear presence cache", HandleClearCache);
  165. m_console.Commands.AddCommand("messageserver", false, "register",
  166. "register",
  167. "Re-register with user server(s)", HandleRegister);
  168. }
  169. public void do_create(string what)
  170. {
  171. //switch (what)
  172. //{
  173. // case "user":
  174. // try
  175. // {
  176. // //userID =
  177. // //m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  178. // } catch (Exception ex)
  179. // {
  180. // m_console.Error("[SERVER]: Error creating user: {0}", ex.ToString());
  181. // }
  182. // try
  183. // {
  184. // //RestObjectPoster.BeginPostObject<Guid>(m_userManager._config.InventoryUrl + "CreateInventory/",
  185. // //userID.Guid);
  186. // }
  187. // catch (Exception ex)
  188. // {
  189. // m_console.Error("[SERVER]: Error creating inventory for user: {0}", ex.ToString());
  190. // }
  191. // // m_lastCreatedUser = userID;
  192. // break;
  193. //}
  194. }
  195. private void HandleClearCache(string module, string[] cmd)
  196. {
  197. int entries = m_regionModule.ClearRegionCache();
  198. m_console.Output("Region cache cleared! Cleared " +
  199. entries.ToString() + " entries");
  200. }
  201. private void HandleRegister(string module, string[] cmd)
  202. {
  203. deregisterFromUserServer();
  204. registerWithUserServer();
  205. }
  206. public override void ShutdownSpecific()
  207. {
  208. m_userServerModule.deregisterWithUserServer();
  209. }
  210. #region IUGAIMCore
  211. protected Dictionary<Type, object> m_moduleInterfaces = new Dictionary<Type, object>();
  212. /// <summary>
  213. /// Register an Module interface.
  214. /// </summary>
  215. /// <typeparam name="T"></typeparam>
  216. /// <param name="iface"></param>
  217. public void RegisterInterface<T>(T iface)
  218. {
  219. lock (m_moduleInterfaces)
  220. {
  221. if (!m_moduleInterfaces.ContainsKey(typeof(T)))
  222. {
  223. m_moduleInterfaces.Add(typeof(T), iface);
  224. }
  225. }
  226. }
  227. public bool TryGet<T>(out T iface)
  228. {
  229. if (m_moduleInterfaces.ContainsKey(typeof(T)))
  230. {
  231. iface = (T)m_moduleInterfaces[typeof(T)];
  232. return true;
  233. }
  234. iface = default(T);
  235. return false;
  236. }
  237. public T Get<T>()
  238. {
  239. return (T)m_moduleInterfaces[typeof(T)];
  240. }
  241. public BaseHttpServer GetHttpServer()
  242. {
  243. return m_httpServer;
  244. }
  245. #endregion
  246. }
  247. }