AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ServerSessionsTracker.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 ServerSessionsTracker : IDisposable
15  {
16  private bool disposed;
17  private SessionManager sessionManager;
18  private SessionManagerAdapter sessionManagerAdapter;
19 
20  public event Action<bool, string> SessionCreated;
21  public event Action<Session> SessionAdded;
22  public event Action<Session> SessionClosed;
23 
24  public event Action<Session, User> UserChanged;
25  public event Action<Session, User> UserJoined;
26  public event Action<Session, User> UserLeft;
27  public event Action<Session> CurrentUserLeft;
28  public event Action<Session> CurrentUserJoined;
29 
30  public event Action ServerConnected;
31  public event Action ServerDisconnected;
32 
36  public List<Session> Sessions { get; private set; }
37 
41  public bool IsServerConnected { get; private set; }
42 
44  {
45  sessionManager = sessionMgr;
46  Sessions = new List<Session>();
47 
48  if (sessionManager != null)
49  {
50  sessionManagerAdapter = new SessionManagerAdapter();
51  sessionManagerAdapter.ServerConnectedEvent += OnServerConnected;
52  sessionManagerAdapter.ServerDisconnectedEvent += OnServerDisconnected;
53  sessionManagerAdapter.SessionClosedEvent += OnSessionClosed;
54  sessionManagerAdapter.SessionAddedEvent += OnSessionAdded;
55  sessionManagerAdapter.CreateSucceededEvent += OnSessionCreatedSuccess;
56  sessionManagerAdapter.CreateFailedEvent += OnSessionCreatedFail;
57  sessionManagerAdapter.UserChangedEvent += OnUserChanged;
58  sessionManagerAdapter.UserJoinedSessionEvent += OnUserJoined;
59  sessionManagerAdapter.UserLeftSessionEvent += OnUserLeft;
60  sessionManager.AddListener(sessionManagerAdapter);
61  }
62  }
63 
69  {
70  return sessionManager.GetCurrentSession();
71  }
72 
78  public bool JoinSession(Session session)
79  {
80  // TODO Should prevent joining multiple sessions at the same time
81  if (session != null)
82  {
83  return session.IsJoined() || session.Join();
84  }
85 
86  return false;
87  }
88 
92  public void LeaveCurrentSession()
93  {
94  using (Session currentSession = GetCurrentSession())
95  {
96  if (currentSession != null)
97  {
98  currentSession.Leave();
99  }
100  }
101  }
102 
108  public bool CreateSession(string sessionName)
109  {
110  if (disposed)
111  {
112  return false;
113  }
114 
115  return sessionManager.CreateSession(sessionName);
116  }
117 
118  private void OnSessionCreatedFail(XString reason)
119  {
120  using (reason)
121  {
122  SessionCreated.RaiseEvent(false, reason.GetString());
123  }
124  }
125 
126  private void OnSessionCreatedSuccess(Session session)
127  {
128  using (session)
129  {
130  SessionCreated.RaiseEvent(true, session.GetName().GetString());
131  }
132  }
133 
134  private void OnUserChanged(Session session, User user)
135  {
136  UserChanged.RaiseEvent(session, user);
137  }
138 
139  private void OnUserLeft(Session session, User user)
140  {
141  UserLeft.RaiseEvent(session, user);
142 
143  if (IsLocalUser(user))
144  {
145  CurrentUserLeft.RaiseEvent(session);
146  }
147  }
148 
149  private void OnUserJoined(Session session, User newUser)
150  {
151  UserJoined.RaiseEvent(session, newUser);
152 
153  if (IsLocalUser(newUser))
154  {
155  CurrentUserJoined.RaiseEvent(session);
156  }
157  }
158 
159  private void OnSessionAdded(Session newSession)
160  {
161  Sessions.Add(newSession);
162  SessionAdded.RaiseEvent(newSession);
163  }
164 
165  private void OnSessionClosed(Session session)
166  {
167  for (int i = 0; i < Sessions.Count; i++)
168  {
169  if (Sessions[i].GetName().ToString().Equals(session.GetName().ToString()))
170  {
171  SessionClosed.RaiseEvent(Sessions[i]);
172  Sessions.Remove(Sessions[i]);
173  }
174  }
175  }
176 
177  private void OnServerDisconnected()
178  {
179  IsServerConnected = false;
180 
181  // Remove all sessions
182  for (int i = 0; i < Sessions.Count; i++)
183  {
184  Sessions[i].Dispose();
185  }
186 
187  Sessions.Clear();
188  ServerDisconnected.RaiseEvent();
189  }
190 
191  private void OnServerConnected()
192  {
193  IsServerConnected = true;
194  ServerConnected.RaiseEvent();
195  }
196 
197  private bool IsLocalUser(User user)
198  {
199  int changedUserId = user.GetID();
200  using (User localUser = sessionManager.GetCurrentUser())
201  {
202  int localUserId = localUser.GetID();
203  return localUserId == changedUserId;
204  }
205  }
206 
207  #region IDisposable
208 
209  public void Dispose()
210  {
211  Dispose(true);
212  GC.SuppressFinalize(this);
213  }
214 
215  protected virtual void Dispose(bool disposing)
216  {
217  if (disposing)
218  {
219  OnServerDisconnected();
220 
221  if (sessionManagerAdapter != null)
222  {
223  sessionManagerAdapter.ServerConnectedEvent -= OnServerConnected;
224  sessionManagerAdapter.ServerDisconnectedEvent -= OnServerDisconnected;
225  sessionManagerAdapter.SessionClosedEvent -= OnSessionClosed;
226  sessionManagerAdapter.SessionAddedEvent -= OnSessionAdded;
227  sessionManagerAdapter.CreateSucceededEvent -= OnSessionCreatedSuccess;
228  sessionManagerAdapter.CreateFailedEvent -= OnSessionCreatedFail;
229  sessionManagerAdapter.UserChangedEvent -= OnUserChanged;
230  sessionManagerAdapter.UserJoinedSessionEvent -= OnUserJoined;
231  sessionManagerAdapter.UserLeftSessionEvent -= OnUserLeft;
232  sessionManagerAdapter.Dispose();
233  sessionManagerAdapter = null;
234  }
235 
236  if (sessionManager != null)
237  {
238  sessionManager.Dispose();
239  sessionManager = null;
240  }
241  }
242 
243  disposed = true;
244  }
245 
246  #endregion
247  }
248 }
virtual void AddListener(SessionManagerListener newListener)
The ServerSessionsTracker manages the list of sessions on the server and the users in these sessions...
Session GetCurrentSession()
Retrieves the current session, if any.
virtual int GetID()
Definition: User.cs:49
System.Action< Session, User > UserChangedEvent
virtual bool CreateSession(XString sessionName)
System.Action< Session, User > UserLeftSessionEvent
bool CreateSession(string sessionName)
Creates a new session on the server.
ServerSessionsTracker(SessionManager sessionMgr)
virtual XString GetName()
Definition: Session.cs:86
virtual bool IsJoined()
Definition: Session.cs:56
void LeaveCurrentSession()
Leave the current session.
virtual bool Join()
Definition: Session.cs:61
bool JoinSession(Session session)
Join the specified session.
System.Action< Session, User > UserJoinedSessionEvent
Allows users of the SessionManager to register to receive event callbacks without having their classe...
override string ToString()
Definition: XString.cs:43
virtual void Leave()
Definition: Session.cs:66