clockwerk-opensim – Blame information for rev 1

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