AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SessionUsersTracker.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using HoloToolkit.Unity;
7 
8 namespace HoloToolkit.Sharing
9 {
14  public class SessionUsersTracker : IDisposable
15  {
19  public event Action<User> UserJoined;
20 
24  public event Action<User> UserLeft;
25 
29  private readonly ServerSessionsTracker serverSessionsTracker;
30 
34  public List<User> CurrentUsers { get; private set; }
35 
37  {
38  CurrentUsers = new List<User>();
39 
40  serverSessionsTracker = sessionsTracker;
41  serverSessionsTracker.CurrentUserJoined += OnCurrentUserJoinedSession;
42  serverSessionsTracker.CurrentUserLeft += OnCurrentUserLeftSession;
43 
44  serverSessionsTracker.UserJoined += OnUserJoinedSession;
45  serverSessionsTracker.UserLeft += OnUserLeftSession;
46  }
47 
53  public User GetUserById(int userId)
54  {
55  for (int u = 0; u < CurrentUsers.Count; u++)
56  {
57  User user = CurrentUsers[u];
58  if (user.GetID() == userId)
59  {
60  return user;
61  }
62  }
63  return null;
64  }
65 
66  #region IDisposable
67 
68  public void Dispose()
69  {
70  Dispose(true);
71  GC.SuppressFinalize(this);
72  }
73 
74  protected virtual void Dispose(bool disposing)
75  {
76  if (disposing)
77  {
78  serverSessionsTracker.CurrentUserJoined -= OnCurrentUserJoinedSession;
79  serverSessionsTracker.CurrentUserLeft -= OnCurrentUserLeftSession;
80 
81  serverSessionsTracker.UserJoined -= OnUserJoinedSession;
82  serverSessionsTracker.UserLeft -= OnUserLeftSession;
83  }
84  }
85 
86  #endregion
87 
88  private void OnCurrentUserJoinedSession(Session joinedSession)
89  {
90  //Debug.LogFormat("Joining session {0}.", joinedSession.GetName());
91 
92  // If joining a new session, any user in the previous session (if any) have left
93  ClearCurrentSession();
94 
95  // Send a join event for every user currently in the session we joined
96  for (int i = 0; i < joinedSession.GetUserCount(); i++)
97  {
98  User user = joinedSession.GetUser(i);
99  CurrentUsers.Add(user);
100  UserJoined.RaiseEvent(user);
101  }
102  }
103 
104  private void OnCurrentUserLeftSession(Session leftSession)
105  {
106  //Debug.Log("Left current session.");
107 
108  // If we leave a session, notify that every user has left the current session of this app
109  ClearCurrentSession();
110  }
111 
112  private void OnUserJoinedSession(Session session, User user)
113  {
114  if (!session.IsJoined())
115  {
116  return;
117  }
118 
119  if (!CurrentUsers.Contains(user))
120  {
121  // Remove any old users with the same ID
122  for (int i = CurrentUsers.Count - 1; i >= 0; i--)
123  {
124  if (CurrentUsers[i].GetID() == user.GetID())
125  {
126  CurrentUsers.RemoveAt(i);
127  }
128  }
129 
130  CurrentUsers.Add(user);
131  UserJoined.RaiseEvent(user);
132  // Debug.LogFormat("User {0} joined current session.", user.GetName());
133  }
134  }
135 
136  private void OnUserLeftSession(Session session, User user)
137  {
138  if (!session.IsJoined())
139  {
140  return;
141  }
142 
143  for (int i = CurrentUsers.Count - 1; i >= 0; i--)
144  {
145  if (CurrentUsers[i].GetID() == user.GetID())
146  {
147  CurrentUsers.RemoveAt(i);
148  UserLeft.RaiseEvent(user);
149  // Debug.LogFormat("User {0} left current session.", user.GetName());
150  }
151  }
152  }
153 
159  private void ClearCurrentSession()
160  {
161  for (int i = 0; i < CurrentUsers.Count; i++)
162  {
163  UserLeft.RaiseEvent(CurrentUsers[i]);
164  }
165 
166  CurrentUsers.Clear();
167  }
168  }
169 }
virtual User GetUser(int i)
Definition: Session.cs:75
Action< User > UserJoined
UserJoined event notifies when a user joins the current session.
Keeps track of the users in the current session. Instance is created by Sharing Stage when a connecti...
The ServerSessionsTracker manages the list of sessions on the server and the users in these sessions...
virtual int GetID()
Definition: User.cs:49
User GetUserById(int userId)
Finds and returns an object representing a user who has the supplied id number. Returns null if the u...
virtual int GetUserCount()
Definition: Session.cs:70
virtual void Dispose(bool disposing)
virtual bool IsJoined()
Definition: Session.cs:56
SessionUsersTracker(ServerSessionsTracker sessionsTracker)
Action< User > UserLeft
UserLeft event notifies when a user leaves the current session.