GridUserService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. private static bool m_Initialized;
  44. public GridUserService(IConfigSource config) : base(config)
  45. {
  46. m_log.Debug("[GRID USER SERVICE]: Starting user grid service");
  47. if (!m_Initialized)
  48. {
  49. m_Initialized = true;
  50. MainConsole.Instance.Commands.AddCommand(
  51. "Users", false,
  52. "show grid user",
  53. "show grid user <ID>",
  54. "Show grid user entry or entries that match or start with the given ID. This will normally be a UUID.",
  55. "This is for debug purposes to see what data is found for a particular user id.",
  56. HandleShowGridUser);
  57. MainConsole.Instance.Commands.AddCommand(
  58. "Users", false,
  59. "show grid users online",
  60. "show grid users online",
  61. "Show number of grid users registered as online.",
  62. "This number may not be accurate as a region may crash or not be cleanly shutdown and leave grid users shown as online\n."
  63. + "For this reason, users online for more than 5 days are not currently counted",
  64. HandleShowGridUsersOnline);
  65. }
  66. }
  67. protected void HandleShowGridUser(string module, string[] cmdparams)
  68. {
  69. if (cmdparams.Length != 4)
  70. {
  71. MainConsole.Instance.Output("Usage: show grid user <UUID>");
  72. return;
  73. }
  74. GridUserData[] data = m_Database.GetAll(cmdparams[3]);
  75. foreach (GridUserData gu in data)
  76. {
  77. ConsoleDisplayList cdl = new ConsoleDisplayList();
  78. cdl.AddRow("User ID", gu.UserID);
  79. foreach (KeyValuePair<string,string> kvp in gu.Data)
  80. cdl.AddRow(kvp.Key, kvp.Value);
  81. MainConsole.Instance.Output(cdl.ToString());
  82. }
  83. MainConsole.Instance.Output("Entries: {0}", data.Length);
  84. }
  85. protected void HandleShowGridUsersOnline(string module, string[] cmdparams)
  86. {
  87. // if (cmdparams.Length != 4)
  88. // {
  89. // MainConsole.Instance.Output("Usage: show grid users online");
  90. // return;
  91. // }
  92. // int onlineCount;
  93. int onlineRecentlyCount = 0;
  94. DateTime now = DateTime.UtcNow;
  95. foreach (GridUserData gu in m_Database.GetAll(""))
  96. {
  97. if (bool.Parse(gu.Data["Online"]))
  98. {
  99. // onlineCount++;
  100. int unixLoginTime = int.Parse(gu.Data["Login"]);
  101. if ((now - Util.ToDateTime(unixLoginTime)).Days < 5)
  102. onlineRecentlyCount++;
  103. }
  104. }
  105. MainConsole.Instance.Output("Users online: {0}", onlineRecentlyCount);
  106. }
  107. private static ExpiringCacheOS<string, GridUserData> cache = new ExpiringCacheOS<string, GridUserData>(00000);
  108. private GridUserData GetGridUserData(string userID)
  109. {
  110. if (userID.Length > 36)
  111. userID = userID.Substring(0, 36);
  112. if (cache.TryGetValue(userID, out GridUserData d))
  113. return d;
  114. GridUserData[] ds = m_Database.GetAll(userID);
  115. if (ds == null || ds.Length == 0)
  116. {
  117. cache.Add(userID, null, 300000);
  118. return null;
  119. }
  120. d = ds[0];
  121. if (ds.Length > 1)
  122. {
  123. // try find most recent record
  124. try
  125. {
  126. int tsta = int.Parse(d.Data["Login"]);
  127. int tstb = int.Parse(d.Data["Logout"]);
  128. int cur = tstb > tsta? tstb : tsta;
  129. for (int i = 1; i < ds.Length; ++i)
  130. {
  131. GridUserData dd = ds[i];
  132. tsta = int.Parse(dd.Data["Login"]);
  133. tstb = int.Parse(dd.Data["Logout"]);
  134. if(tsta > tstb)
  135. tstb = tsta;
  136. if (tstb > cur)
  137. {
  138. cur = tstb;
  139. d = dd;
  140. }
  141. }
  142. }
  143. catch { }
  144. }
  145. cache.Add(userID, d, 300000);
  146. return d;
  147. }
  148. private GridUserInfo ToInfo(GridUserData d)
  149. {
  150. GridUserInfo info = new GridUserInfo();
  151. info.UserID = d.UserID;
  152. Dictionary<string, string> kvp = d.Data;
  153. string tmpstr;
  154. if (kvp.TryGetValue("HomeRegionID", out tmpstr))
  155. info.HomeRegionID = new UUID(tmpstr);
  156. else
  157. info.HomeRegionID = UUID.Zero;
  158. if (kvp.TryGetValue("HomePosition", out tmpstr))
  159. info.HomePosition = Vector3.Parse(tmpstr);
  160. else
  161. info.HomePosition = Vector3.Zero;
  162. if (kvp.TryGetValue("HomeLookAt", out tmpstr))
  163. info.HomeLookAt = Vector3.Parse(tmpstr);
  164. else
  165. info.HomeLookAt = Vector3.Zero;
  166. if (kvp.TryGetValue("LastRegionID", out tmpstr))
  167. info.LastRegionID = new UUID(tmpstr);
  168. else
  169. info.LastRegionID = UUID.Zero;
  170. if (kvp.TryGetValue("LastPosition", out tmpstr))
  171. info.LastPosition = Vector3.Parse(tmpstr);
  172. else
  173. info.LastPosition = Vector3.Zero;
  174. if (kvp.TryGetValue("LastLookAt", out tmpstr))
  175. info.LastLookAt = Vector3.Parse(tmpstr);
  176. else
  177. info.LastLookAt = Vector3.Zero;
  178. if (kvp.TryGetValue("Online", out tmpstr))
  179. info.Online = bool.Parse(tmpstr);
  180. else
  181. info.Online = false;
  182. if (kvp.TryGetValue("Login", out tmpstr))
  183. info.Login = Util.ToDateTime(Convert.ToInt32(tmpstr));
  184. else
  185. info.Login = Util.UnixEpoch;
  186. if (kvp.TryGetValue("Logout", out tmpstr))
  187. info.Logout = Util.ToDateTime(Convert.ToInt32(tmpstr));
  188. else
  189. info.Logout = Util.UnixEpoch;
  190. return info;
  191. }
  192. public virtual GridUserInfo GetGridUserInfo(string userID)
  193. {
  194. GridUserData d = GetGridUserData(userID);
  195. if (d == null)
  196. return null;
  197. return ToInfo(d);
  198. }
  199. public virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
  200. {
  201. List<GridUserInfo> ret = new List<GridUserInfo>();
  202. foreach (string id in userIDs)
  203. ret.Add(GetGridUserInfo(id));
  204. return ret.ToArray();
  205. }
  206. public GridUserInfo LoggedIn(string userID)
  207. {
  208. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is online", userID);
  209. GridUserData d = GetGridUserData(userID);
  210. if (d == null)
  211. {
  212. d = new GridUserData();
  213. d.UserID = userID;
  214. }
  215. d.Data["Online"] = true.ToString();
  216. d.Data["Login"] = Util.UnixTimeSinceEpoch().ToString();
  217. m_Database.Store(d);
  218. if (userID.Length >= 36)
  219. cache.Add(userID.Substring(0, 36), d, 300000);
  220. return ToInfo(d);
  221. }
  222. public bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  223. {
  224. m_log.DebugFormat("[GRID USER SERVICE]: User {0} is offline", userID);
  225. GridUserData d = GetGridUserData(userID);
  226. if (d == null)
  227. {
  228. d = new GridUserData();
  229. d.UserID = userID;
  230. }
  231. d.Data["Online"] = false.ToString();
  232. d.Data["Logout"] = Util.UnixTimeSinceEpoch().ToString();
  233. d.Data["LastRegionID"] = regionID.ToString();
  234. d.Data["LastPosition"] = lastPosition.ToString();
  235. d.Data["LastLookAt"] = lastLookAt.ToString();
  236. bool ret = m_Database.Store(d);
  237. if (ret && userID.Length >= 36)
  238. cache.Add(userID.Substring(0, 36), d, 300000);
  239. return ret;
  240. }
  241. public bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
  242. {
  243. GridUserData d = GetGridUserData(userID);
  244. if (d == null)
  245. {
  246. d = new GridUserData();
  247. d.UserID = userID;
  248. }
  249. d.Data["HomeRegionID"] = homeID.ToString();
  250. d.Data["HomePosition"] = homePosition.ToString();
  251. d.Data["HomeLookAt"] = homeLookAt.ToString();
  252. bool ret = m_Database.Store(d);
  253. if (ret && userID.Length >= 36)
  254. cache.Add(userID.Substring(0, 36), d, 300000);
  255. return ret;
  256. }
  257. public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
  258. {
  259. // m_log.DebugFormat("[GRID USER SERVICE]: SetLastPosition for {0}", userID);
  260. GridUserData d = GetGridUserData(userID);
  261. if (d == null)
  262. {
  263. d = new GridUserData();
  264. d.UserID = userID;
  265. }
  266. d.Data["LastRegionID"] = regionID.ToString();
  267. d.Data["LastPosition"] = lastPosition.ToString();
  268. d.Data["LastLookAt"] = lastLookAt.ToString();
  269. bool ret = m_Database.Store(d);
  270. if (ret && userID.Length >= 36)
  271. cache.Add(userID.Substring(0, 36), d, 300000);
  272. return ret;
  273. }
  274. }
  275. }