OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
GridUserService.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using Nini.Config;
32 using OpenSim.Data;
33 using OpenSim.Services.Interfaces;
34 using OpenSim.Framework;
35 using OpenSim.Framework.Console;
37 
38 using OpenMetaverse;
39 using log4net;
40 
41 namespace OpenSim.Services.UserAccountService
42 {
44  {
45  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46  private static bool m_Initialized;
47 
48  public GridUserService(IConfigSource config) : base(config)
49  {
50  m_log.Debug("[GRID USER SERVICE]: Starting user grid service");
51 
52  if (!m_Initialized)
53  {
54  m_Initialized = true;
55 
56  MainConsole.Instance.Commands.AddCommand(
57  "Users", false,
58  "show grid user",
59  "show grid user <ID>",
60  "Show grid user entry or entries that match or start with the given ID. This will normally be a UUID.",
61  "This is for debug purposes to see what data is found for a particular user id.",
62  HandleShowGridUser);
63 
64  MainConsole.Instance.Commands.AddCommand(
65  "Users", false,
66  "show grid users online",
67  "show grid users online",
68  "Show number of grid users registered as online.",
69  "This number may not be accurate as a region may crash or not be cleanly shutdown and leave grid users shown as online\n."
70  + "For this reason, users online for more than 5 days are not currently counted",
71  HandleShowGridUsersOnline);
72  }
73  }
74 
75  protected void HandleShowGridUser(string module, string[] cmdparams)
76  {
77  if (cmdparams.Length != 4)
78  {
79  MainConsole.Instance.Output("Usage: show grid user <UUID>");
80  return;
81  }
82 
83  GridUserData[] data = m_Database.GetAll(cmdparams[3]);
84 
85  foreach (GridUserData gu in data)
86  {
88 
89  cdl.AddRow("User ID", gu.UserID);
90 
91  foreach (KeyValuePair<string,string> kvp in gu.Data)
92  cdl.AddRow(kvp.Key, kvp.Value);
93 
94  MainConsole.Instance.Output(cdl.ToString());
95  }
96 
97  MainConsole.Instance.OutputFormat("Entries: {0}", data.Length);
98  }
99 
100  protected void HandleShowGridUsersOnline(string module, string[] cmdparams)
101  {
102 // if (cmdparams.Length != 4)
103 // {
104 // MainConsole.Instance.Output("Usage: show grid users online");
105 // return;
106 // }
107 
108 // int onlineCount;
109  int onlineRecentlyCount = 0;
110 
111  DateTime now = DateTime.UtcNow;
112 
113  foreach (GridUserData gu in m_Database.GetAll(""))
114  {
115  if (bool.Parse(gu.Data["Online"]))
116  {
117 // onlineCount++;
118 
119  int unixLoginTime = int.Parse(gu.Data["Login"]);
120 
121  if ((now - Util.ToDateTime(unixLoginTime)).Days < 5)
122  onlineRecentlyCount++;
123  }
124  }
125 
126  MainConsole.Instance.OutputFormat("Users online: {0}", onlineRecentlyCount);
127  }
128 
129  private GridUserData GetGridUserData(string userID)
130  {
131  GridUserData d = null;
132  if (userID.Length > 36) // it's a UUI
133  {
134  d = m_Database.Get(userID);
135  }
136  else // it's a UUID
137  {
138  GridUserData[] ds = m_Database.GetAll(userID);
139  if (ds == null)
140  return null;
141 
142  if (ds.Length > 0)
143  {
144  d = ds[0];
145  foreach (GridUserData dd in ds)
146  if (dd.UserID.Length > d.UserID.Length) // find the longest
147  d = dd;
148  }
149  }
150 
151  return d;
152  }
153 
154  public virtual GridUserInfo GetGridUserInfo(string userID)
155  {
156  GridUserData d = GetGridUserData(userID);
157 
158  if (d == null)
159  return null;
160 
161  GridUserInfo info = new GridUserInfo();
162  info.UserID = d.UserID;
163  info.HomeRegionID = new UUID(d.Data["HomeRegionID"]);
164  info.HomePosition = Vector3.Parse(d.Data["HomePosition"]);
165  info.HomeLookAt = Vector3.Parse(d.Data["HomeLookAt"]);
166 
167  info.LastRegionID = new UUID(d.Data["LastRegionID"]);
168  info.LastPosition = Vector3.Parse(d.Data["LastPosition"]);
169  info.LastLookAt = Vector3.Parse(d.Data["LastLookAt"]);
170 
171  info.Online = bool.Parse(d.Data["Online"]);
172  info.Login = Util.ToDateTime(Convert.ToInt32(d.Data["Login"]));
173  info.Logout = Util.ToDateTime(Convert.ToInt32(d.Data["Logout"]));
174 
175  return info;
176  }
177 
178  public virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
179  {
180  List<GridUserInfo> ret = new List<GridUserInfo>();
181 
182  foreach (string id in userIDs)
183  ret.Add(GetGridUserInfo(id));
184 
185  return ret.ToArray();
186  }
187 
188  public GridUserInfo LoggedIn(string userID)
189  {
190  m_log.DebugFormat("[GRID USER SERVICE]: User {0} is online", userID);
191 
192  GridUserData d = GetGridUserData(userID);
193 
194  if (d == null)
195  {
196  d = new GridUserData();
197  d.UserID = userID;
198  }
199 
200  d.Data["Online"] = true.ToString();
201  d.Data["Login"] = Util.UnixTimeSinceEpoch().ToString();
202 
203  m_Database.Store(d);
204 
205  return GetGridUserInfo(userID);
206  }
207 
208  public bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
209  {
210  m_log.DebugFormat("[GRID USER SERVICE]: User {0} is offline", userID);
211 
212  GridUserData d = GetGridUserData(userID);
213 
214  if (d == null)
215  {
216  d = new GridUserData();
217  d.UserID = userID;
218  }
219 
220  d.Data["Online"] = false.ToString();
221  d.Data["Logout"] = Util.UnixTimeSinceEpoch().ToString();
222  d.Data["LastRegionID"] = regionID.ToString();
223  d.Data["LastPosition"] = lastPosition.ToString();
224  d.Data["LastLookAt"] = lastLookAt.ToString();
225 
226  return m_Database.Store(d);
227  }
228 
229  public bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
230  {
231  GridUserData d = GetGridUserData(userID);
232 
233  if (d == null)
234  {
235  d = new GridUserData();
236  d.UserID = userID;
237  }
238 
239  d.Data["HomeRegionID"] = homeID.ToString();
240  d.Data["HomePosition"] = homePosition.ToString();
241  d.Data["HomeLookAt"] = homeLookAt.ToString();
242 
243  return m_Database.Store(d);
244  }
245 
246  public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
247  {
248 // m_log.DebugFormat("[GRID USER SERVICE]: SetLastPosition for {0}", userID);
249 
250  GridUserData d = GetGridUserData(userID);
251 
252  if (d == null)
253  {
254  d = new GridUserData();
255  d.UserID = userID;
256  }
257 
258  d.Data["LastRegionID"] = regionID.ToString();
259  d.Data["LastPosition"] = lastPosition.ToString();
260  d.Data["LastLookAt"] = lastLookAt.ToString();
261 
262  return m_Database.Store(d);
263  }
264  }
265 }
Used to generated a formatted table for the console.
virtual GridUserInfo[] GetGridUserInfo(string[] userIDs)
Dictionary< string, string > Data
void HandleShowGridUser(string module, string[] cmdparams)
bool LoggedOut(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
Informs the grid that a user is logged out and to remove any session data for them ...
Records user information specific to a grid but which is not part of a user's account.
OpenSim.Services.Interfaces.GridRegion GridRegion
virtual GridUserInfo GetGridUserInfo(string userID)
bool SetHome(string userID, UUID homeID, Vector3 homePosition, Vector3 homeLookAt)
bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 lastPosition, Vector3 lastLookAt)
Stores the last known user position at the grid level
void HandleShowGridUsersOnline(string module, string[] cmdparams)