aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs')
-rw-r--r--OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs398
1 files changed, 57 insertions, 341 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs
index f5ab454..bafad82 100644
--- a/OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs
+++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/PresenceModule.cs
@@ -24,6 +24,7 @@
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 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. 25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27using System;
27using System.Collections; 28using System.Collections;
28using System.Collections.Generic; 29using System.Collections.Generic;
29using System.Net; 30using System.Net;
@@ -35,408 +36,123 @@ using OpenMetaverse;
35using OpenSim.Framework; 36using OpenSim.Framework;
36using OpenSim.Region.Framework.Interfaces; 37using OpenSim.Region.Framework.Interfaces;
37using OpenSim.Region.Framework.Scenes; 38using OpenSim.Region.Framework.Scenes;
39using OpenSim.Services.Interfaces;
38using GridRegion = OpenSim.Services.Interfaces.GridRegion; 40using GridRegion = OpenSim.Services.Interfaces.GridRegion;
41using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo;
39 42
40namespace OpenSim.Region.CoreModules.Avatar.InstantMessage 43namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
41{ 44{
42 public class PresenceModule : IRegionModule, IPresenceModule 45 public class PresenceModule : ISharedRegionModule, IPresenceModule
43 { 46 {
44 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 47 private static readonly ILog m_log = LogManager.GetLogger(
45 48 MethodBase.GetCurrentMethod().DeclaringType);
46 private bool m_Enabled = false;
47 private bool m_Gridmode = false;
48
49 // some default scene for doing things that aren't connected to a specific scene. Avoids locking.
50 private Scene m_initialScene;
51
52 private List<Scene> m_Scenes = new List<Scene>();
53
54 // we currently are only interested in root-agents. If the root isn't here, we don't know the region the
55 // user is in, so we have to ask the messaging server anyway.
56 private Dictionary<UUID, Scene> m_RootAgents =
57 new Dictionary<UUID, Scene>();
58 49
59 public event PresenceChange OnPresenceChange; 50 public event PresenceChange OnPresenceChange;
60 public event BulkPresenceData OnBulkPresenceData; 51 public event BulkPresenceData OnBulkPresenceData;
61 52
62 public void Initialise(Scene scene, IConfigSource config) 53 protected List<Scene> m_Scenes = new List<Scene>();
63 {
64 lock (m_Scenes)
65 {
66 // This is a shared module; Initialise will be called for every region on this server.
67 // Only check config once for the first region.
68 if (m_Scenes.Count == 0)
69 {
70 IConfig cnf = config.Configs["Messaging"];
71 if (cnf != null && cnf.GetString(
72 "PresenceModule", "PresenceModule") !=
73 "PresenceModule")
74 return;
75
76 cnf = config.Configs["Startup"];
77 if (cnf != null)
78 m_Gridmode = cnf.GetBoolean("gridmode", false);
79 54
80 m_Enabled = true; 55 protected IPresenceService m_PresenceService = null;
81 56
82 m_initialScene = scene; 57 protected IPresenceService PresenceService
83 }
84
85 if (m_Gridmode)
86 NotifyMessageServerOfStartup(scene);
87
88 m_Scenes.Add(scene);
89 }
90
91 scene.RegisterModuleInterface<IPresenceModule>(this);
92
93 scene.EventManager.OnNewClient += OnNewClient;
94 scene.EventManager.OnSetRootAgentScene += OnSetRootAgentScene;
95 scene.EventManager.OnMakeChildAgent += OnMakeChildAgent;
96 }
97
98 public void PostInitialise()
99 {
100 }
101
102 public void Close()
103 { 58 {
104 if (!m_Gridmode || !m_Enabled) 59 get
105 return;
106
107 if (OnPresenceChange != null)
108 { 60 {
109 lock (m_RootAgents) 61 if (m_PresenceService == null)
110 { 62 {
111 // on shutdown, users are kicked, too 63 if (m_Scenes.Count > 0)
112 foreach (KeyValuePair<UUID, Scene> pair in m_RootAgents) 64 m_PresenceService = m_Scenes[0].RequestModuleInterface<IPresenceService>();
113 {
114 OnPresenceChange(new PresenceInfo(pair.Key, UUID.Zero));
115 }
116 } 65 }
117 }
118 66
119 lock (m_Scenes) 67 return m_PresenceService;
120 {
121 foreach (Scene scene in m_Scenes)
122 NotifyMessageServerOfShutdown(scene);
123 } 68 }
124 } 69 }
125 70
126 public string Name 71 public void Initialise(IConfigSource config)
127 {
128 get { return "PresenceModule"; }
129 }
130
131 public bool IsSharedModule
132 { 72 {
133 get { return true; }
134 } 73 }
135 74
136 public void RequestBulkPresenceData(UUID[] users) 75 public void AddRegion(Scene scene)
137 { 76 {
138 if (OnBulkPresenceData != null) 77 m_Scenes.Add(scene);
139 {
140 PresenceInfo[] result = new PresenceInfo[users.Length];
141 if (m_Gridmode)
142 {
143 // first check the local information
144 List<UUID> uuids = new List<UUID>(); // the uuids to check remotely
145 List<int> indices = new List<int>(); // just for performance.
146 lock (m_RootAgents)
147 {
148 for (int i = 0; i < uuids.Count; ++i)
149 {
150 Scene scene;
151 if (m_RootAgents.TryGetValue(users[i], out scene))
152 {
153 result[i] = new PresenceInfo(users[i], scene.RegionInfo.RegionID);
154 }
155 else
156 {
157 uuids.Add(users[i]);
158 indices.Add(i);
159 }
160 }
161 }
162 78
163 // now we have filtered out all the local root agents. The rest we have to request info about 79 scene.EventManager.OnNewClient += OnNewClient;
164 Dictionary<UUID, FriendRegionInfo> infos = m_initialScene.GetFriendRegionInfos(uuids);
165 for (int i = 0; i < uuids.Count; ++i)
166 {
167 FriendRegionInfo info;
168 if (infos.TryGetValue(uuids[i], out info) && info.isOnline)
169 {
170 UUID regionID = info.regionID;
171 if (regionID == UUID.Zero)
172 {
173 // TODO this is the old messaging-server protocol; only the regionHandle is available.
174 // Fetch region-info to get the id
175 uint x = 0, y = 0;
176 Utils.LongToUInts(info.regionHandle, out x, out y);
177 GridRegion regionInfo = m_initialScene.GridService.GetRegionByPosition(m_initialScene.RegionInfo.ScopeID,
178 (int)x, (int)y);
179 regionID = regionInfo.RegionID;
180 }
181 result[indices[i]] = new PresenceInfo(uuids[i], regionID);
182 }
183 else result[indices[i]] = new PresenceInfo(uuids[i], UUID.Zero);
184 }
185 }
186 else
187 {
188 // in standalone mode, we have all the info locally available.
189 lock (m_RootAgents)
190 {
191 for (int i = 0; i < users.Length; ++i)
192 {
193 Scene scene;
194 if (m_RootAgents.TryGetValue(users[i], out scene))
195 {
196 result[i] = new PresenceInfo(users[i], scene.RegionInfo.RegionID);
197 }
198 else
199 {
200 result[i] = new PresenceInfo(users[i], UUID.Zero);
201 }
202 }
203 }
204 }
205 80
206 // tell everyone 81 scene.RegisterModuleInterface<IPresenceModule>(this);
207 OnBulkPresenceData(result);
208 }
209 } 82 }
210 83
211 // new client doesn't mean necessarily that user logged in, it just means it entered one of the 84 public void RegionLoaded(Scene scene)
212 // the regions on this server
213 public void OnNewClient(IClientAPI client)
214 { 85 {
215 client.OnConnectionClosed += OnConnectionClosed;
216 client.OnLogout += OnLogout;
217
218 // KLUDGE: See handler for details.
219 client.OnEconomyDataRequest += OnEconomyDataRequest;
220 } 86 }
221 87
222 // connection closed just means *one* client connection has been closed. It doesn't mean that the 88 public void RemoveRegion(Scene scene)
223 // user has logged off; it might have just TPed away.
224 public void OnConnectionClosed(IClientAPI client)
225 { 89 {
226 // TODO: Have to think what we have to do here... 90 m_Scenes.Remove(scene);
227 // Should we just remove the root from the list (if scene matches)?
228 if (!(client.Scene is Scene))
229 return;
230 Scene scene = (Scene)client.Scene;
231
232 lock (m_RootAgents)
233 {
234 Scene rootScene;
235 if (!(m_RootAgents.TryGetValue(client.AgentId, out rootScene)) || scene != rootScene)
236 return;
237
238 m_RootAgents.Remove(client.AgentId);
239 }
240
241 // Should it have logged off, we'll do the logout part in OnLogout, even if no root is stored
242 // anymore. It logged off, after all...
243 } 91 }
244 92
245 // Triggered when the user logs off. 93 public void PostInitialise()
246 public void OnLogout(IClientAPI client)
247 { 94 {
248 if (!(client.Scene is Scene))
249 return;
250 Scene scene = (Scene)client.Scene;
251
252 // On logout, we really remove the client from rootAgents, even if the scene doesn't match
253 lock (m_RootAgents)
254 {
255 if (m_RootAgents.ContainsKey(client.AgentId)) m_RootAgents.Remove(client.AgentId);
256 }
257
258 // now inform the messaging server and anyone who is interested
259 NotifyMessageServerOfAgentLeaving(client.AgentId, scene.RegionInfo.RegionID, scene.RegionInfo.RegionHandle);
260 if (OnPresenceChange != null) OnPresenceChange(new PresenceInfo(client.AgentId, UUID.Zero));
261 } 95 }
262 96
263 public void OnSetRootAgentScene(UUID agentID, Scene scene) 97 public void Close()
264 { 98 {
265 // OnSetRootAgentScene can be called from several threads at once (with different agentID).
266 // Concurrent access to m_RootAgents is prone to failure on multi-core/-processor systems without
267 // correct locking).
268 lock (m_RootAgents)
269 {
270 Scene rootScene;
271 if (m_RootAgents.TryGetValue(agentID, out rootScene) && scene == rootScene)
272 {
273 return;
274 }
275 m_RootAgents[agentID] = scene;
276 }
277
278 // inform messaging server that agent changed the region
279 Util.FireAndForget(
280 delegate(object o)
281 {
282 NotifyMessageServerOfAgentLocation(agentID, scene.RegionInfo.RegionID, scene.RegionInfo.RegionHandle);
283 }
284 );
285 } 99 }
286 100
287 private void OnEconomyDataRequest(UUID agentID) 101 public string Name
288 { 102 {
289 // KLUDGE: This is the only way I found to get a message (only) after login was completed and the 103 get { return "PresenceModule"; }
290 // client is connected enough to receive UDP packets.
291 // This packet seems to be sent only once, just after connection was established to the first
292 // region after login.
293 // We use it here to trigger a presence update; the old update-on-login was never be heard by
294 // the freshly logged in viewer, as it wasn't connected to the region at that time.
295 // TODO: Feel free to replace this by a better solution if you find one.
296
297 // get the agent. This should work every time, as we just got a packet from it
298 ScenePresence agent = null;
299 lock (m_Scenes)
300 {
301 foreach (Scene scene in m_Scenes)
302 {
303 agent = scene.GetScenePresence(agentID);
304 if (agent != null) break;
305 }
306 }
307
308 // just to be paranoid...
309 if (agent == null)
310 {
311 m_log.ErrorFormat("[PRESENCE]: Got a packet from agent {0} who can't be found anymore!?", agentID);
312 return;
313 }
314
315 // we are a bit premature here, but the next packet will switch this child agent to root.
316 if (OnPresenceChange != null) OnPresenceChange(new PresenceInfo(agentID, agent.Scene.RegionInfo.RegionID));
317 } 104 }
318 105
319 public void OnMakeChildAgent(ScenePresence agent) 106 public Type ReplaceableInterface
320 { 107 {
321 // OnMakeChildAgent can be called from several threads at once (with different agent). 108 get { return null; }
322 // Concurrent access to m_RootAgents is prone to failure on multi-core/-processor systems without
323 // correct locking).
324 lock (m_RootAgents)
325 {
326 Scene rootScene;
327 if (m_RootAgents.TryGetValue(agent.UUID, out rootScene) && agent.Scene == rootScene)
328 {
329 m_RootAgents.Remove(agent.UUID);
330 }
331 }
332 // don't notify the messaging-server; either this agent just had been downgraded and another one will be upgraded
333 // to root momentarily (which will notify the messaging-server), or possibly it will be closed in a moment,
334 // which will update the messaging-server, too.
335 } 109 }
336 110
337 private void NotifyMessageServerOfStartup(Scene scene) 111 public void RequestBulkPresenceData(UUID[] users)
338 { 112 {
339 Hashtable xmlrpcdata = new Hashtable();
340 xmlrpcdata["RegionUUID"] = scene.RegionInfo.RegionID.ToString();
341 ArrayList SendParams = new ArrayList();
342 SendParams.Add(xmlrpcdata);
343 try
344 {
345 XmlRpcRequest UpRequest = new XmlRpcRequest("region_startup", SendParams);
346 XmlRpcResponse resp = UpRequest.Send(scene.CommsManager.NetworkServersInfo.MessagingURL, 5000);
347
348 Hashtable responseData = (Hashtable)resp.Value;
349 if (responseData == null || (!responseData.ContainsKey("success")) || (string)responseData["success"] != "TRUE")
350 {
351 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of region startup for region {0}", scene.RegionInfo.RegionName);
352 }
353 }
354 catch (WebException)
355 {
356 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of region startup for region {0}", scene.RegionInfo.RegionName);
357 }
358 } 113 }
359 114
360 private void NotifyMessageServerOfShutdown(Scene scene) 115 public void OnNewClient(IClientAPI client)
361 { 116 {
362 if (m_Scenes[0].CommsManager.NetworkServersInfo.MessagingURL == string.Empty) 117 client.AddGenericPacketHandler("requestonlinenotification", OnRequestOnlineNotification);
363 return;
364
365 Hashtable xmlrpcdata = new Hashtable();
366 xmlrpcdata["RegionUUID"] = scene.RegionInfo.RegionID.ToString();
367 ArrayList SendParams = new ArrayList();
368 SendParams.Add(xmlrpcdata);
369 try
370 {
371 XmlRpcRequest DownRequest = new XmlRpcRequest("region_shutdown", SendParams);
372 XmlRpcResponse resp = DownRequest.Send(scene.CommsManager.NetworkServersInfo.MessagingURL, 5000);
373
374 Hashtable responseData = (Hashtable)resp.Value;
375 if ((!responseData.ContainsKey("success")) || (string)responseData["success"] != "TRUE")
376 {
377 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of region shutdown for region {0}", scene.RegionInfo.RegionName);
378 }
379 }
380 catch (WebException)
381 {
382 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of region shutdown for region {0}", scene.RegionInfo.RegionName);
383 }
384 } 118 }
385 119
386 private void NotifyMessageServerOfAgentLocation(UUID agentID, UUID region, ulong regionHandle) 120 public void OnRequestOnlineNotification(Object sender, string method, List<String> args)
387 { 121 {
388 if (m_Scenes[0].CommsManager.NetworkServersInfo.MessagingURL == string.Empty) 122 if (!(sender is IClientAPI))
389 return; 123 return;
390 124
391 Hashtable xmlrpcdata = new Hashtable(); 125 IClientAPI client = (IClientAPI)sender;
392 xmlrpcdata["AgentID"] = agentID.ToString(); 126 m_log.DebugFormat("[PRESENCE MODULE]: OnlineNotification requested by {0}", client.Name);
393 xmlrpcdata["RegionUUID"] = region.ToString();
394 xmlrpcdata["RegionHandle"] = regionHandle.ToString();
395 ArrayList SendParams = new ArrayList();
396 SendParams.Add(xmlrpcdata);
397 try
398 {
399 XmlRpcRequest LocationRequest = new XmlRpcRequest("agent_location", SendParams);
400 XmlRpcResponse resp = LocationRequest.Send(m_Scenes[0].CommsManager.NetworkServersInfo.MessagingURL, 5000);
401 127
402 Hashtable responseData = (Hashtable)resp.Value; 128 PresenceInfo[] status = PresenceService.GetAgents(args.ToArray());
403 if ((!responseData.ContainsKey("success")) || (string)responseData["success"] != "TRUE")
404 {
405 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of agent location for {0}", agentID.ToString());
406 }
407 }
408 catch (WebException)
409 {
410 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of agent location for {0}", agentID.ToString());
411 }
412 }
413 129
414 private void NotifyMessageServerOfAgentLeaving(UUID agentID, UUID region, ulong regionHandle) 130 List<UUID> online = new List<UUID>();
415 { 131 List<UUID> offline = new List<UUID>();
416 if (m_Scenes[0].CommsManager.NetworkServersInfo.MessagingURL == string.Empty)
417 return;
418 132
419 Hashtable xmlrpcdata = new Hashtable(); 133 foreach (PresenceInfo pi in status)
420 xmlrpcdata["AgentID"] = agentID.ToString();
421 xmlrpcdata["RegionUUID"] = region.ToString();
422 xmlrpcdata["RegionHandle"] = regionHandle.ToString();
423 ArrayList SendParams = new ArrayList();
424 SendParams.Add(xmlrpcdata);
425 try
426 { 134 {
427 XmlRpcRequest LeavingRequest = new XmlRpcRequest("agent_leaving", SendParams); 135 UUID uuid = new UUID(pi.UserID);
428 XmlRpcResponse resp = LeavingRequest.Send(m_Scenes[0].CommsManager.NetworkServersInfo.MessagingURL, 5000); 136 if (pi.Online)
429
430 Hashtable responseData = (Hashtable)resp.Value;
431 if ((!responseData.ContainsKey("success")) || (string)responseData["success"] != "TRUE")
432 { 137 {
433 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of agent leaving for {0}", agentID.ToString()); 138 if (!online.Contains(uuid))
139 {
140 online.Add(uuid);
141 if (offline.Contains(uuid))
142 offline.Remove(uuid);
143 }
144 }
145 else
146 {
147 if (!online.Contains(uuid) && !offline.Contains(uuid))
148 offline.Add(uuid);
434 } 149 }
435 } 150 }
436 catch (WebException) 151
437 { 152 if (online.Count > 0)
438 m_log.ErrorFormat("[PRESENCE]: Failed to notify message server of agent leaving for {0}", agentID.ToString()); 153 client.SendAgentOnline(online.ToArray());
439 } 154 if (offline.Count > 0)
155 client.SendAgentOffline(offline.ToArray());
440 } 156 }
441 } 157 }
442} 158}