GridUserService.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 users online",
  49. "show grid users online",
  50. "Show number of grid users registered as online.",
  51. "This number may not be accurate as a region may crash or not be cleanly shutdown and leave grid users shown as online\n."
  52. + "For this reason, users online for more than 5 days are not currently counted",
  53. HandleShowGridUsersOnline);
  54. }
  55. protected void HandleShowGridUsersOnline(string module, string[] cmdparams)
  56. {
  57. // if (cmdparams.Length != 4)
  58. // {
  59. // MainConsole.Instance.Output("Usage: show grid users online");
  60. // return;
  61. // }
  62. // int onlineCount;
  63. int onlineRecentlyCount = 0;
  64. DateTime now = DateTime.UtcNow;
  65. foreach (GridUserData gu in m_Database.GetAll(""))
  66. {
  67. if (bool.Parse(gu.Data["Online"]))
  68. {
  69. // onlineCount++;
  70. int unixLoginTime = int.Parse(gu.Data["Login"]);
  71. if ((now - Util.ToDateTime(unixLoginTime)).Days < 5)
  72. onlineRecentlyCount++;
  73. }
  74. }
  75. MainConsole.Instance.OutputFormat("Users online: {0}", onlineRecentlyCount);
  76. }
  77. public virtual GridUserInfo GetGridUserInfo(string userID)
  78. {
  79. GridUserData d = null;
  80. if (userID.Length > 36) // it's a UUI
  81. d = m_Database.Get(userID);
  82. else // it's a UUID
  83. {
  84. GridUserData[] ds = m_Database.GetAll(userID);
  85. if (ds == null)
  86. return null;
  87. if (ds.Length > 0)
  88. {
  89. d = ds[0];
  90. foreach (GridUserData dd in ds)
  91. if (dd.UserID.Length > d.UserID.Length) // find the longest
  92. d = dd;
  93. }
  94. }
  95. if (d == null)
  96. return null;
  97. GridUserInfo info = new GridUserInfo();
  98. info.UserID = d.UserID;
  99. info.HomeRegionID = new UUID(d.Data["HomeRegionID"]);
  100. info.HomePosition = Vector3.Parse(d.Data["HomePosition"]);
  101. info.HomeLookAt = Vector3.Parse(d.Data["HomeLookAt"]);
  102. info.LastRegionID = new UUID(d.Data["LastRegionID"]);
  103. info.LastPosition = Vector3.Parse(d.Data["LastPosition"]);
  104. info.LastLookAt = Vector3.Parse(d.Data["LastLookAt"]);
  105. info.Online = bool.Parse(d.Data["Online"]);
  106. info.Login = Util.ToDateTime(Convert.ToInt32(d.Data["Login"]));
  107. info.Logout = Util.ToDateTime(Convert.ToInt32(d.Data["Logout"]));
  108. return info;
  109. }
  110. public virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
  111. {
  112. List<GridUserInfo> ret = new List<GridUserInfo>();
  113. foreach (string id in userIDs)
  114. ret.Add(GetGridUserInfo(id));
  115. return ret.ToArray();
  116. }
  117. public GridUserInfo LoggedIn(string userID)
  118. {
  119. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is online", userID);
  120. GridUserData d = m_Database.Get(userID);
  121. if (d == null)
  122. {
  123. d = new GridUserData();
  124. d.UserID = userID;
  125. }
  126. d.Data["Online"] = true.ToString();
  127. d.Data["Login"] = Util.UnixTimeSinceEpoch().ToString();
  128. m_Database.Store(d);
  129. return GetGridUserInfo(userID);
  130. }
  131. public bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  132. {
  133. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is offline", userID);
  134. GridUserData d = m_Database.Get(userID);
  135. if (d == null)
  136. {
  137. d = new GridUserData();
  138. d.UserID = userID;
  139. }
  140. d.Data["Online"] = false.ToString();
  141. d.Data["Logout"] = Util.UnixTimeSinceEpoch().ToString();
  142. d.Data["LastRegionID"] = regionID.ToString();
  143. d.Data["LastPosition"] = lastPosition.ToString();
  144. d.Data["LastLookAt"] = lastLookAt.ToString();
  145. return m_Database.Store(d);
  146. }
  147. public bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
  148. {
  149. GridUserData d = m_Database.Get(userID);
  150. if (d == null)
  151. {
  152. d = new GridUserData();
  153. d.UserID = userID;
  154. }
  155. d.Data["HomeRegionID"] = homeID.ToString();
  156. d.Data["HomePosition"] = homePosition.ToString();
  157. d.Data["HomeLookAt"] = homeLookAt.ToString();
  158. return m_Database.Store(d);
  159. }
  160. public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  161. {
  162. // m_log.DebugFormat("[GRID USER SERVICE]: SetLastPosition for {0}", userID);
  163. GridUserData d = m_Database.Get(userID);
  164. if (d == null)
  165. {
  166. d = new GridUserData();
  167. d.UserID = userID;
  168. }
  169. d.Data["LastRegionID"] = regionID.ToString();
  170. d.Data["LastPosition"] = lastPosition.ToString();
  171. d.Data["LastLookAt"] = lastLookAt.ToString();
  172. return m_Database.Store(d);
  173. }
  174. }
  175. }