Main.cs 9.0 KB

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