aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs')
-rw-r--r--OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs511
1 files changed, 511 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs
new file mode 100644
index 0000000..1b5edf4
--- /dev/null
+++ b/OpenSim/Services/Connectors/SimianGrid/SimianPresenceServiceConnector.cs
@@ -0,0 +1,511 @@
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
28using System;
29using System.Collections.Generic;
30using System.Collections.Specialized;
31using System.Net;
32using System.Reflection;
33using log4net;
34using Mono.Addins;
35using Nini.Config;
36using OpenSim.Framework;
37using OpenSim.Framework.Servers.HttpServer;
38using OpenSim.Region.Framework.Interfaces;
39using OpenSim.Region.Framework.Scenes;
40using OpenSim.Services.Interfaces;
41using OpenSim.Server.Base;
42using OpenMetaverse;
43using OpenMetaverse.StructuredData;
44
45using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo;
46
47namespace OpenSim.Services.Connectors.SimianGrid
48{
49 /// <summary>
50 /// Connects avatar presence information (for tracking current location and
51 /// message routing) to the SimianGrid backend
52 /// </summary>
53 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
54 public class SimianPresenceServiceConnector : IPresenceService, ISharedRegionModule
55 {
56 private static readonly ILog m_log =
57 LogManager.GetLogger(
58 MethodBase.GetCurrentMethod().DeclaringType);
59
60 private string m_serverUrl = String.Empty;
61
62 #region ISharedRegionModule
63
64 public Type ReplaceableInterface { get { return null; } }
65 public void RegionLoaded(Scene scene) { }
66 public void PostInitialise() { }
67 public void Close() { }
68
69 public SimianPresenceServiceConnector() { }
70 public string Name { get { return "SimianPresenceServiceConnector"; } }
71 public void AddRegion(Scene scene)
72 {
73 if (!String.IsNullOrEmpty(m_serverUrl))
74 {
75 scene.RegisterModuleInterface<IPresenceService>(this);
76
77 scene.EventManager.OnMakeRootAgent += MakeRootAgentHandler;
78 scene.EventManager.OnNewClient += NewClientHandler;
79 scene.EventManager.OnSignificantClientMovement += SignificantClientMovementHandler;
80
81 LogoutRegionAgents(scene.RegionInfo.RegionID);
82 }
83 }
84 public void RemoveRegion(Scene scene)
85 {
86 if (!String.IsNullOrEmpty(m_serverUrl))
87 {
88 scene.UnregisterModuleInterface<IPresenceService>(this);
89
90 scene.EventManager.OnMakeRootAgent -= MakeRootAgentHandler;
91 scene.EventManager.OnNewClient -= NewClientHandler;
92 scene.EventManager.OnSignificantClientMovement -= SignificantClientMovementHandler;
93
94 LogoutRegionAgents(scene.RegionInfo.RegionID);
95 }
96 }
97
98 #endregion ISharedRegionModule
99
100 public SimianPresenceServiceConnector(IConfigSource source)
101 {
102 Initialise(source);
103 }
104
105 public void Initialise(IConfigSource source)
106 {
107 if (Simian.IsSimianEnabled(source, "PresenceServices"))
108 {
109 IConfig gridConfig = source.Configs["PresenceService"];
110 if (gridConfig == null)
111 {
112 m_log.Error("[PRESENCE CONNECTOR]: PresenceService missing from OpenSim.ini");
113 throw new Exception("Presence connector init error");
114 }
115
116 string serviceUrl = gridConfig.GetString("PresenceServerURI");
117 if (String.IsNullOrEmpty(serviceUrl))
118 {
119 m_log.Error("[PRESENCE CONNECTOR]: No PresenceServerURI in section PresenceService");
120 throw new Exception("Presence connector init error");
121 }
122
123 m_serverUrl = serviceUrl;
124 }
125 }
126
127 #region IPresenceService
128
129 public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
130 {
131 m_log.ErrorFormat("[PRESENCE CONNECTOR]: Login requested, UserID={0}, SessionID={1}, SecureSessionID={2}",
132 userID, sessionID, secureSessionID);
133
134 NameValueCollection requestArgs = new NameValueCollection
135 {
136 { "RequestMethod", "AddSession" },
137 { "UserID", userID.ToString() }
138 };
139 if (sessionID != UUID.Zero)
140 {
141 requestArgs["SessionID"] = sessionID.ToString();
142 requestArgs["SecureSessionID"] = secureSessionID.ToString();
143 }
144
145 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
146 bool success = response["Success"].AsBoolean();
147
148 if (!success)
149 m_log.Warn("[PRESENCE CONNECTOR]: Failed to login agent " + userID + ": " + response["Message"].AsString());
150
151 return success;
152 }
153
154 public bool LogoutAgent(UUID sessionID, Vector3 position, Vector3 lookAt)
155 {
156 m_log.InfoFormat("[PRESENCE CONNECTOR]: Logout requested for agent with sessionID " + sessionID);
157
158 NameValueCollection requestArgs = new NameValueCollection
159 {
160 { "RequestMethod", "RemoveSession" },
161 { "SessionID", sessionID.ToString() }
162 };
163
164 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
165 bool success = response["Success"].AsBoolean();
166
167 if (!success)
168 m_log.Warn("[PRESENCE CONNECTOR]: Failed to logout agent with sessionID " + sessionID + ": " + response["Message"].AsString());
169
170 return success;
171 }
172
173 public bool LogoutRegionAgents(UUID regionID)
174 {
175 m_log.InfoFormat("[PRESENCE CONNECTOR]: Logout requested for all agents in region " + regionID);
176
177 NameValueCollection requestArgs = new NameValueCollection
178 {
179 { "RequestMethod", "RemoveSessions" },
180 { "SceneID", regionID.ToString() }
181 };
182
183 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
184 bool success = response["Success"].AsBoolean();
185
186 if (!success)
187 m_log.Warn("[PRESENCE CONNECTOR]: Failed to logout agents from region " + regionID + ": " + response["Message"].AsString());
188
189 return success;
190 }
191
192 public bool ReportAgent(UUID sessionID, UUID regionID, Vector3 position, Vector3 lookAt)
193 {
194 //m_log.DebugFormat("[PRESENCE CONNECTOR]: Updating session data for agent with sessionID " + sessionID);
195
196 NameValueCollection requestArgs = new NameValueCollection
197 {
198 { "RequestMethod", "UpdateSession" },
199 { "SessionID", sessionID.ToString() },
200 { "SceneID", regionID.ToString() },
201 { "ScenePosition", position.ToString() },
202 { "SceneLookAt", lookAt.ToString() }
203 };
204
205 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
206 bool success = response["Success"].AsBoolean();
207
208 if (!success)
209 m_log.Warn("[PRESENCE CONNECTOR]: Failed to update agent session " + sessionID + ": " + response["Message"].AsString());
210
211 return success;
212 }
213
214 public PresenceInfo GetAgent(UUID sessionID)
215 {
216 m_log.DebugFormat("[PRESENCE CONNECTOR]: Requesting session data for agent with sessionID " + sessionID);
217
218 NameValueCollection requestArgs = new NameValueCollection
219 {
220 { "RequestMethod", "GetSession" },
221 { "SessionID", sessionID.ToString() }
222 };
223
224 OSDMap sessionResponse = WebUtil.PostToService(m_serverUrl, requestArgs);
225 if (sessionResponse["Success"].AsBoolean())
226 {
227 UUID userID = sessionResponse["UserID"].AsUUID();
228 m_log.DebugFormat("[PRESENCE CONNECTOR]: Requesting user data for " + userID);
229
230 requestArgs = new NameValueCollection
231 {
232 { "RequestMethod", "GetUser" },
233 { "UserID", userID.ToString() }
234 };
235
236 OSDMap userResponse = WebUtil.PostToService(m_serverUrl, requestArgs);
237 if (userResponse["Success"].AsBoolean())
238 return ResponseToPresenceInfo(sessionResponse, userResponse);
239 else
240 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve user data for " + userID + ": " + userResponse["Message"].AsString());
241 }
242 else
243 {
244 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve session " + sessionID + ": " + sessionResponse["Message"].AsString());
245 }
246
247 return null;
248 }
249
250 public PresenceInfo[] GetAgents(string[] userIDs)
251 {
252 List<PresenceInfo> presences = new List<PresenceInfo>(userIDs.Length);
253
254 for (int i = 0; i < userIDs.Length; i++)
255 {
256 UUID userID;
257 if (UUID.TryParse(userIDs[i], out userID) && userID != UUID.Zero)
258 presences.AddRange(GetSessions(userID));
259 }
260
261 return presences.ToArray();
262 }
263
264 public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt)
265 {
266 m_log.DebugFormat("[PRESENCE CONNECTOR]: Setting home location for user " + userID);
267
268 NameValueCollection requestArgs = new NameValueCollection
269 {
270 { "RequestMethod", "AddUserData" },
271 { "UserID", userID.ToString() },
272 { "HomeLocation", SerializeLocation(regionID, position, lookAt) }
273 };
274
275 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
276 bool success = response["Success"].AsBoolean();
277
278 if (!success)
279 m_log.Warn("[PRESENCE CONNECTOR]: Failed to set home location for " + userID + ": " + response["Message"].AsString());
280
281 return success;
282 }
283
284 #endregion IPresenceService
285
286 #region Presence Detection
287
288 private void MakeRootAgentHandler(ScenePresence sp)
289 {
290 m_log.DebugFormat("[PRESENCE DETECTOR]: Detected root presence {0} in {1}", sp.UUID, sp.Scene.RegionInfo.RegionName);
291
292 ReportAgent(sp.ControllingClient.SessionId, sp.Scene.RegionInfo.RegionID, sp.AbsolutePosition, sp.Lookat);
293 SetLastLocation(sp.UUID, sp.Scene.RegionInfo.RegionID, sp.AbsolutePosition, sp.Lookat);
294 }
295
296 private void NewClientHandler(IClientAPI client)
297 {
298 client.OnConnectionClosed += LogoutHandler;
299 }
300
301 private void SignificantClientMovementHandler(IClientAPI client)
302 {
303 ScenePresence sp;
304 if (client.Scene is Scene && ((Scene)client.Scene).TryGetAvatar(client.AgentId, out sp))
305 ReportAgent(sp.ControllingClient.SessionId, sp.Scene.RegionInfo.RegionID, sp.AbsolutePosition, sp.Lookat);
306 }
307
308 private void LogoutHandler(IClientAPI client)
309 {
310 if (client.IsLoggingOut)
311 {
312 client.OnConnectionClosed -= LogoutHandler;
313
314 object obj;
315 if (client.Scene.TryGetAvatar(client.AgentId, out obj) && obj is ScenePresence)
316 {
317 // The avatar is still in the scene, we can get the exact logout position
318 ScenePresence sp = (ScenePresence)obj;
319 SetLastLocation(client.AgentId, client.Scene.RegionInfo.RegionID, sp.AbsolutePosition, sp.Lookat);
320 }
321 else
322 {
323 // The avatar was already removed from the scene, store LastLocation using the most recent session data
324 m_log.Warn("[PRESENCE]: " + client.Name + " has already been removed from the scene, storing approximate LastLocation");
325 SetLastLocation(client.SessionId);
326 }
327
328 LogoutAgent(client.SessionId, Vector3.Zero, Vector3.UnitX);
329 }
330 }
331
332 #endregion Presence Detection
333
334 #region Helpers
335
336 private OSDMap GetUserData(UUID userID)
337 {
338 m_log.DebugFormat("[PRESENCE CONNECTOR]: Requesting user data for " + userID);
339
340 NameValueCollection requestArgs = new NameValueCollection
341 {
342 { "RequestMethod", "GetUser" },
343 { "UserID", userID.ToString() }
344 };
345
346 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
347 if (response["Success"].AsBoolean() && response["User"] is OSDMap)
348 return response;
349 else
350 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve user data for " + userID + ": " + response["Message"].AsString());
351
352 return null;
353 }
354
355 private OSDMap GetSessionData(UUID sessionID)
356 {
357 m_log.DebugFormat("[PRESENCE CONNECTOR]: Requesting session data for session " + sessionID);
358
359 NameValueCollection requestArgs = new NameValueCollection
360 {
361 { "RequestMethod", "GetSession" },
362 { "SessionID", sessionID.ToString() }
363 };
364
365 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
366 if (response["Success"].AsBoolean())
367 return response;
368 else
369 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve session data for session " + sessionID);
370
371 return null;
372 }
373
374 private List<PresenceInfo> GetSessions(UUID userID)
375 {
376 List<PresenceInfo> presences = new List<PresenceInfo>(1);
377
378 OSDMap userResponse = GetUserData(userID);
379 if (userResponse != null)
380 {
381 m_log.DebugFormat("[PRESENCE CONNECTOR]: Requesting sessions for " + userID);
382
383 NameValueCollection requestArgs = new NameValueCollection
384 {
385 { "RequestMethod", "GetSession" },
386 { "UserID", userID.ToString() }
387 };
388
389 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
390 if (response["Success"].AsBoolean())
391 {
392 PresenceInfo presence = ResponseToPresenceInfo(response, userResponse);
393 if (presence != null)
394 presences.Add(presence);
395 }
396 else
397 {
398 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve sessions for " + userID + ": " + response["Message"].AsString());
399 }
400 }
401
402 return presences;
403 }
404
405 /// <summary>
406 /// Fetch the last known avatar location with GetSession and persist it
407 /// as user data with AddUserData
408 /// </summary>
409 private bool SetLastLocation(UUID sessionID)
410 {
411 NameValueCollection requestArgs = new NameValueCollection
412 {
413 { "RequestMethod", "GetSession" },
414 { "SessionID", sessionID.ToString() }
415 };
416
417 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
418 bool success = response["Success"].AsBoolean();
419
420 if (success)
421 {
422 UUID userID = response["UserID"].AsUUID();
423 UUID sceneID = response["SceneID"].AsUUID();
424 Vector3 position = response["ScenePosition"].AsVector3();
425 Vector3 lookAt = response["SceneLookAt"].AsVector3();
426
427 return SetLastLocation(userID, sceneID, position, lookAt);
428 }
429 else
430 {
431 m_log.Warn("[PRESENCE CONNECTOR]: Failed to retrieve presence information for session " + sessionID +
432 " while saving last location: " + response["Message"].AsString());
433 }
434
435 return success;
436 }
437
438 private bool SetLastLocation(UUID userID, UUID sceneID, Vector3 position, Vector3 lookAt)
439 {
440 NameValueCollection requestArgs = new NameValueCollection
441 {
442 { "RequestMethod", "AddUserData" },
443 { "UserID", userID.ToString() },
444 { "LastLocation", SerializeLocation(sceneID, position, lookAt) }
445 };
446
447 OSDMap response = WebUtil.PostToService(m_serverUrl, requestArgs);
448 bool success = response["Success"].AsBoolean();
449
450 if (!success)
451 m_log.Warn("[PRESENCE CONNECTOR]: Failed to set last location for " + userID + ": " + response["Message"].AsString());
452
453 return success;
454 }
455
456 private PresenceInfo ResponseToPresenceInfo(OSDMap sessionResponse, OSDMap userResponse)
457 {
458 if (sessionResponse == null)
459 return null;
460
461 PresenceInfo info = new PresenceInfo();
462
463 info.Online = true;
464 info.UserID = sessionResponse["UserID"].AsUUID().ToString();
465 info.RegionID = sessionResponse["SceneID"].AsUUID();
466 info.Position = sessionResponse["ScenePosition"].AsVector3();
467 info.LookAt = sessionResponse["SceneLookAt"].AsVector3();
468
469 if (userResponse != null && userResponse["User"] is OSDMap)
470 {
471 OSDMap user = (OSDMap)userResponse["User"];
472
473 info.Login = user["LastLoginDate"].AsDate();
474 info.Logout = user["LastLogoutDate"].AsDate();
475 DeserializeLocation(user["HomeLocation"].AsString(), out info.HomeRegionID, out info.HomePosition, out info.HomeLookAt);
476 }
477
478 return info;
479 }
480
481 private string SerializeLocation(UUID regionID, Vector3 position, Vector3 lookAt)
482 {
483 return "{" + String.Format("\"SceneID\":\"{0}\",\"Position\":\"{1}\",\"LookAt\":\"{2}\"", regionID, position, lookAt) + "}";
484 }
485
486 private bool DeserializeLocation(string location, out UUID regionID, out Vector3 position, out Vector3 lookAt)
487 {
488 OSDMap map = null;
489
490 try { map = OSDParser.DeserializeJson(location) as OSDMap; }
491 catch { }
492
493 if (map != null)
494 {
495 regionID = map["SceneID"].AsUUID();
496 if (Vector3.TryParse(map["Position"].AsString(), out position) &&
497 Vector3.TryParse(map["LookAt"].AsString(), out lookAt))
498 {
499 return true;
500 }
501 }
502
503 regionID = UUID.Zero;
504 position = Vector3.Zero;
505 lookAt = Vector3.Zero;
506 return false;
507 }
508
509 #endregion Helpers
510 }
511}