GridUserService.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.Reflection;
  30. using Nini.Config;
  31. using OpenSim.Data;
  32. using OpenSim.Services.Interfaces;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  36. using OpenMetaverse;
  37. using log4net;
  38. namespace OpenSim.Services.UserAccountService
  39. {
  40. public class GridUserService : GridUserServiceBase, IGridUserService
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. public GridUserService(IConfigSource config) : base(config)
  44. {
  45. m_log.Debug("[GRID USER SERVICE]: Starting user grid service");
  46. MainConsole.Instance.Commands.AddCommand(
  47. "Users", false,
  48. "show grid user",
  49. "show grid user <ID>",
  50. "Show grid user entry or entries that match or start with the given ID. This will normally be a UUID.",
  51. "This is for debug purposes to see what data is found for a particular user id.",
  52. HandleShowGridUser);
  53. MainConsole.Instance.Commands.AddCommand(
  54. "Users", false,
  55. "show grid users online",
  56. "show grid users online",
  57. "Show number of grid users registered as online.",
  58. "This number may not be accurate as a region may crash or not be cleanly shutdown and leave grid users shown as online\n."
  59. + "For this reason, users online for more than 5 days are not currently counted",
  60. HandleShowGridUsersOnline);
  61. }
  62. protected void HandleShowGridUser(string module, string[] cmdparams)
  63. {
  64. if (cmdparams.Length != 4)
  65. {
  66. MainConsole.Instance.Output("Usage: show grid user <UUID>");
  67. return;
  68. }
  69. GridUserData[] data = m_Database.GetAll(cmdparams[3]);
  70. foreach (GridUserData gu in data)
  71. {
  72. ConsoleDisplayList cdl = new ConsoleDisplayList();
  73. cdl.AddRow("User ID", gu.UserID);
  74. foreach (KeyValuePair<string,string> kvp in gu.Data)
  75. cdl.AddRow(kvp.Key, kvp.Value);
  76. MainConsole.Instance.Output(cdl.ToString());
  77. }
  78. MainConsole.Instance.OutputFormat("Entries: {0}", data.Length);
  79. }
  80. protected void HandleShowGridUsersOnline(string module, string[] cmdparams)
  81. {
  82. // if (cmdparams.Length != 4)
  83. // {
  84. // MainConsole.Instance.Output("Usage: show grid users online");
  85. // return;
  86. // }
  87. // int onlineCount;
  88. int onlineRecentlyCount = 0;
  89. DateTime now = DateTime.UtcNow;
  90. foreach (GridUserData gu in m_Database.GetAll(""))
  91. {
  92. if (bool.Parse(gu.Data["Online"]))
  93. {
  94. // onlineCount++;
  95. int unixLoginTime = int.Parse(gu.Data["Login"]);
  96. if ((now - Util.ToDateTime(unixLoginTime)).Days < 5)
  97. onlineRecentlyCount++;
  98. }
  99. }
  100. MainConsole.Instance.OutputFormat("Users online: {0}", onlineRecentlyCount);
  101. }
  102. public virtual GridUserInfo GetGridUserInfo(string userID)
  103. {
  104. GridUserData d = null;
  105. if (userID.Length > 36) // it's a UUI
  106. d = m_Database.Get(userID);
  107. else // it's a UUID
  108. {
  109. GridUserData[] ds = m_Database.GetAll(userID);
  110. if (ds == null)
  111. return null;
  112. if (ds.Length > 0)
  113. {
  114. d = ds[0];
  115. foreach (GridUserData dd in ds)
  116. if (dd.UserID.Length > d.UserID.Length) // find the longest
  117. d = dd;
  118. }
  119. }
  120. if (d == null)
  121. return null;
  122. GridUserInfo info = new GridUserInfo();
  123. info.UserID = d.UserID;
  124. info.HomeRegionID = new UUID(d.Data["HomeRegionID"]);
  125. info.HomePosition = Vector3.Parse(d.Data["HomePosition"]);
  126. info.HomeLookAt = Vector3.Parse(d.Data["HomeLookAt"]);
  127. info.LastRegionID = new UUID(d.Data["LastRegionID"]);
  128. info.LastPosition = Vector3.Parse(d.Data["LastPosition"]);
  129. info.LastLookAt = Vector3.Parse(d.Data["LastLookAt"]);
  130. info.Online = bool.Parse(d.Data["Online"]);
  131. info.Login = Util.ToDateTime(Convert.ToInt32(d.Data["Login"]));
  132. info.Logout = Util.ToDateTime(Convert.ToInt32(d.Data["Logout"]));
  133. return info;
  134. }
  135. public virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
  136. {
  137. List<GridUserInfo> ret = new List<GridUserInfo>();
  138. foreach (string id in userIDs)
  139. ret.Add(GetGridUserInfo(id));
  140. return ret.ToArray();
  141. }
  142. public GridUserInfo LoggedIn(string userID)
  143. {
  144. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is online", userID);
  145. GridUserData d = m_Database.Get(userID);
  146. if (d == null)
  147. {
  148. d = new GridUserData();
  149. d.UserID = userID;
  150. }
  151. d.Data["Online"] = true.ToString();
  152. d.Data["Login"] = Util.UnixTimeSinceEpoch().ToString();
  153. m_Database.Store(d);
  154. return GetGridUserInfo(userID);
  155. }
  156. public bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  157. {
  158. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is offline", userID);
  159. GridUserData d = m_Database.Get(userID);
  160. if (d == null)
  161. {
  162. d = new GridUserData();
  163. d.UserID = userID;
  164. }
  165. d.Data["Online"] = false.ToString();
  166. d.Data["Logout"] = Util.UnixTimeSinceEpoch().ToString();
  167. d.Data["LastRegionID"] = regionID.ToString();
  168. d.Data["LastPosition"] = lastPosition.ToString();
  169. d.Data["LastLookAt"] = lastLookAt.ToString();
  170. return m_Database.Store(d);
  171. }
  172. public bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
  173. {
  174. GridUserData d = m_Database.Get(userID);
  175. if (d == null)
  176. {
  177. d = new GridUserData();
  178. d.UserID = userID;
  179. }
  180. d.Data["HomeRegionID"] = homeID.ToString();
  181. d.Data["HomePosition"] = homePosition.ToString();
  182. d.Data["HomeLookAt"] = homeLookAt.ToString();
  183. return m_Database.Store(d);
  184. }
  185. public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  186. {
  187. // m_log.DebugFormat("[GRID USER SERVICE]: SetLastPosition for {0}", userID);
  188. GridUserData d = m_Database.Get(userID);
  189. if (d == null)
  190. {
  191. d = new GridUserData();
  192. d.UserID = userID;
  193. }
  194. d.Data["LastRegionID"] = regionID.ToString();
  195. d.Data["LastPosition"] = lastPosition.ToString();
  196. d.Data["LastLookAt"] = lastLookAt.ToString();
  197. return m_Database.Store(d);
  198. }
  199. }
  200. }