Main.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 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. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using log4net.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Communications.Cache;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Statistics;
  40. namespace OpenSim.Grid.UserServer
  41. {
  42. /// <summary>
  43. /// </summary>
  44. public class OpenUser_Main : BaseOpenSimServer, conscmd_callback
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private UserConfig Cfg;
  48. public UserManager m_userManager;
  49. public UserLoginService m_loginService;
  50. public MessageServersConnector m_messagesService;
  51. private LLUUID m_lastCreatedUser = LLUUID.Random();
  52. [STAThread]
  53. public static void Main(string[] args)
  54. {
  55. XmlConfigurator.Configure();
  56. m_log.Info("Launching UserServer...");
  57. OpenUser_Main userserver = new OpenUser_Main();
  58. userserver.Startup();
  59. userserver.Work();
  60. }
  61. private OpenUser_Main()
  62. {
  63. m_console = new ConsoleBase("OpenUser", this);
  64. MainConsole.Instance = m_console;
  65. }
  66. private void Work()
  67. {
  68. m_console.Notice("Enter help for a list of commands\n");
  69. while (true)
  70. {
  71. m_console.Prompt();
  72. }
  73. }
  74. public void Startup()
  75. {
  76. Cfg = new UserConfig("USER SERVER", (Path.Combine(Util.configDir(), "UserServer_Config.xml")));
  77. StatsManager.StartCollectingUserStats();
  78. m_log.Info("[REGION]: Establishing data connection");
  79. m_userManager = new UserManager();
  80. m_userManager._config = Cfg;
  81. m_userManager.AddPlugin(Cfg.DatabaseProvider);
  82. m_loginService = new UserLoginService(
  83. m_userManager, new LibraryRootFolder(), Cfg, Cfg.DefaultStartupMsg);
  84. m_messagesService = new MessageServersConnector();
  85. m_loginService.OnUserLoggedInAtLocation += NotifyMessageServersUserLoggedInToLocation;
  86. m_userManager.OnLogOffUser += NotifyMessageServersUserLoggOff;
  87. m_log.Info("[REGION]: Starting HTTP process");
  88. m_httpServer = new BaseHttpServer(Cfg.HttpPort);
  89. AddHttpHandlers();
  90. m_httpServer.Start();
  91. m_log.Info("[SERVER]: Userserver 0.5 - Startup complete");
  92. }
  93. protected void AddHttpHandlers()
  94. {
  95. m_httpServer.AddXmlRPCHandler("login_to_simulator", m_loginService.XmlRpcLoginMethod);
  96. m_httpServer.AddHTTPHandler("login", m_loginService.ProcessHTMLLogin);
  97. m_httpServer.SetLLSDHandler(m_loginService.LLSDLoginMethod);
  98. m_httpServer.AddXmlRPCHandler("get_user_by_name", m_userManager.XmlRPCGetUserMethodName);
  99. m_httpServer.AddXmlRPCHandler("get_user_by_uuid", m_userManager.XmlRPCGetUserMethodUUID);
  100. m_httpServer.AddXmlRPCHandler("get_avatar_picker_avatar", m_userManager.XmlRPCGetAvatarPickerAvatar);
  101. m_httpServer.AddXmlRPCHandler("add_new_user_friend", m_userManager.XmlRpcResponseXmlRPCAddUserFriend);
  102. m_httpServer.AddXmlRPCHandler("remove_user_friend", m_userManager.XmlRpcResponseXmlRPCRemoveUserFriend);
  103. m_httpServer.AddXmlRPCHandler("update_user_friend_perms", m_userManager.XmlRpcResponseXmlRPCUpdateUserFriendPerms);
  104. m_httpServer.AddXmlRPCHandler("get_user_friend_list", m_userManager.XmlRpcResponseXmlRPCGetUserFriendList);
  105. m_httpServer.AddXmlRPCHandler("logout_of_simulator", m_userManager.XmlRPCLogOffUserMethodUUID);
  106. // Message Server ---> User Server
  107. m_httpServer.AddXmlRPCHandler("register_messageserver", m_messagesService.XmlRPCRegisterMessageServer);
  108. m_httpServer.AddXmlRPCHandler("agent_change_region", m_messagesService.XmlRPCUserMovedtoRegion);
  109. m_httpServer.AddXmlRPCHandler("deregister_messageserver", m_messagesService.XmlRPCDeRegisterMessageServer);
  110. m_httpServer.AddStreamHandler(
  111. new RestStreamHandler("DELETE", "/usersessions/", m_userManager.RestDeleteUserSessionMethod));
  112. m_httpServer.AddXmlRPCHandler("update_user_profile", m_userManager.XmlRpcResponseXmlRPCUpdateUserProfile);
  113. }
  114. public void do_create(string what)
  115. {
  116. switch (what)
  117. {
  118. case "user":
  119. string tempfirstname;
  120. string templastname;
  121. string tempMD5Passwd;
  122. uint regX = 1000;
  123. uint regY = 1000;
  124. tempfirstname = m_console.CmdPrompt("First name");
  125. templastname = m_console.CmdPrompt("Last name");
  126. //tempMD5Passwd = m_console.PasswdPrompt("Password");
  127. tempMD5Passwd = m_console.CmdPrompt("Password");
  128. regX = Convert.ToUInt32(m_console.CmdPrompt("Start Region X"));
  129. regY = Convert.ToUInt32(m_console.CmdPrompt("Start Region Y"));
  130. if (null != m_userManager.GetUserProfile(tempfirstname, templastname))
  131. {
  132. m_log.ErrorFormat(
  133. "[USERS]: A user with the name {0} {1} already exists!", tempfirstname, templastname);
  134. break;
  135. }
  136. tempMD5Passwd = Util.Md5Hash(Util.Md5Hash(tempMD5Passwd) + ":" + String.Empty);
  137. LLUUID userID = new LLUUID();
  138. try
  139. {
  140. userID = m_userManager.AddUserProfile(tempfirstname, templastname, tempMD5Passwd, regX, regY);
  141. }
  142. catch (Exception ex)
  143. {
  144. m_log.ErrorFormat("[USERS]: Error creating user: {0}", ex.ToString());
  145. }
  146. try
  147. {
  148. bool created
  149. = SynchronousRestObjectPoster.BeginPostObject<Guid, bool>(
  150. "POST", m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  151. if (!created)
  152. {
  153. throw new Exception(
  154. String.Format(
  155. "The inventory creation request for user {0} did not succeed."
  156. + " Please contact your inventory service provider for more information.",
  157. userID));
  158. }
  159. }
  160. catch (WebException e)
  161. {
  162. m_log.ErrorFormat(
  163. "[USERS]: Could not contact the inventory service at {0} to create an inventory for {1}",
  164. m_userManager._config.InventoryUrl + "CreateInventory/", userID.UUID);
  165. }
  166. catch (Exception e)
  167. {
  168. m_log.ErrorFormat("[USERS]: Error creating inventory for user: {0}", e);
  169. }
  170. m_lastCreatedUser = userID;
  171. break;
  172. }
  173. }
  174. public override void RunCmd(string cmd, string[] cmdparams)
  175. {
  176. base.RunCmd(cmd, cmdparams);
  177. switch (cmd)
  178. {
  179. case "help":
  180. m_console.Notice("create user - create a new user");
  181. m_console.Notice("stats - statistical information for this server");
  182. m_console.Notice("shutdown - shutdown the grid (USE CAUTION!)");
  183. break;
  184. case "create":
  185. do_create(cmdparams[0]);
  186. break;
  187. case "shutdown":
  188. m_loginService.OnUserLoggedInAtLocation -= NotifyMessageServersUserLoggedInToLocation;
  189. m_console.Close();
  190. Environment.Exit(0);
  191. break;
  192. case "stats":
  193. m_console.Notice(StatsManager.UserStats.Report());
  194. break;
  195. case "test-inventory":
  196. // RestObjectPosterResponse<List<InventoryFolderBase>> requester = new RestObjectPosterResponse<List<InventoryFolderBase>>();
  197. // requester.ReturnResponseVal = TestResponse;
  198. // requester.BeginPostObject<LLUUID>(m_userManager._config.InventoryUrl + "RootFolders/", m_lastCreatedUser);
  199. SynchronousRestObjectPoster.BeginPostObject<LLUUID, List<InventoryFolderBase>>("POST",
  200. m_userManager.
  201. _config.
  202. InventoryUrl +
  203. "RootFolders/",
  204. m_lastCreatedUser);
  205. break;
  206. }
  207. }
  208. public void TestResponse(List<InventoryFolderBase> resp)
  209. {
  210. m_console.Notice("response got");
  211. }
  212. public void NotifyMessageServersUserLoggOff(LLUUID agentID)
  213. {
  214. m_messagesService.TellMessageServersAboutUserLogoff(agentID);
  215. }
  216. public void NotifyMessageServersUserLoggedInToLocation(LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  217. ulong regionhandle, float positionX, float positionY,
  218. float positionZ, string firstname, string lastname)
  219. {
  220. m_messagesService.TellMessageServersAboutUser( agentID, sessionID, RegionID, regionhandle, positionX,
  221. positionY, positionZ, firstname, lastname);
  222. }
  223. }
  224. }