private void ClientEventHandler(ClientEvent e) { if (logs != null) { logs_mutex.WaitOne(); while (logs.Count >= LogsCacheMax) logs.RemoveAt(0); logs.Add(new Log { Timestamp = e.Timestamp, Type = e.Type, Message = e.String }); logs_mutex.ReleaseMutex(); } switch (e.Type) { case ClientEventType.UserLogout: UserLogout(); break; case ClientEventType.DeviceCreation: if (e.Device != null) DeviceCreatedHandler(e.Device); break; case ClientEventType.DeviceUpdate: if (e.Device != null) DeviceUpdatedHandler(e.Device); break; case ClientEventType.DeviceRemoval: if (e.Device != null) DeviceRemovedHandler(e.Device.Guid); else if (e.Guid != null && e.Guid != Guid.Empty) DeviceRemovedHandler(e.Guid); break; case ClientEventType.UserCreation: if (e.User!=null) UserCreatedHandler(e.User); break; case ClientEventType.UserUpdate: if (e.User != null) UserUpdatedHandler(e.User); break; case ClientEventType.UserRemoval: if (e.User != null) UserRemovedHandler(e.User.ID); else if (e.ID > 0) UserRemovedHandler(e.ID); break; case ClientEventType.RuleCreation: if (e.Rule!=null) RuleCreatedHandler(e.Rule); break; case ClientEventType.RuleUpdate: if (e.Rule!=null) RuleUpdatedHandler(e.Rule); break; case ClientEventType.RuleRemoval: if (e.Rule != null) RuleRemovedHandler(e.Rule.ID); else if (e.ID > 0) RuleRemovedHandler(e.ID); break; case ClientEventType.SubscriptionUpdate: if (e.Subscription != null) SubscriptionUpdatedHandler(e.Subscription); break; case ClientEventType.SubscriptionNotification: if (e.Guid != null && e.Guid != Guid.Empty && e.Data != null) SubscriptionNotifiedHandler(e.Guid, e.Data); break; } }
/// <summary> /// Dequeue events /// </summary> /// <returns></returns> public ClientEvent[] ClientEventDequeue(int timeout) { if (queue.Count <= 0 && timeout > 0) if (!queue_wait.WaitOne(timeout, true)) return new ClientEvent[] { }; queue_mutex.WaitOne(); if (queue.Count <= 0) { queue_mutex.ReleaseMutex(); return new ClientEvent[] { }; } ClientEvent[] e = new ClientEvent[queue.Count]; for (int i = 0; i < e.Length; i++) e[i] = queue.Dequeue(); queue_mutex.ReleaseMutex(); return e; }
/// <summary> /// Push an event into the queue /// </summary> /// <param name="e"></param> public void ClientEventEnqueue(ClientEvent e) { if (e == null) throw new ArgumentNullException(); queue_mutex.WaitOne(); int max = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Reactivity.Server.Clients.ClientSession.ClientEventQueueLength"]); while (queue.Count >= max) queue.Dequeue(); queue.Enqueue(e); queue_mutex.ReleaseMutex(); queue_wait.Set(); }
/// <summary> /// Notify all clients about an event /// This does permission control /// </summary> /// <param name="e"></param> public static void Notify(ClientEvent e) { if (e == null) return; mutex.WaitOne(); foreach (ClientSession session in sessions.Values) { // Client that is if (!session.UserIsLoggedIn) continue; // Take care of some system notification if (e.Type == ClientEventType.UserUpdate && e.User.ID == session.User.ID) { // update user to reflect information change session.User.Name = e.User.Name; session.User.Description = e.User.Description; session.User.Permission = e.User.Permission; // notified the logged in client session.ClientEventEnqueue(new ClientEvent { Type = ClientEventType.UserUpdate, User = session.User, Timestamp = DateTime.Now }); } else if (e.Type == ClientEventType.UserRemoval && e.ID == session.User.ID) { // if this user is removed, then log him out right away session.UserLogout(); } // if you are admin, everything is fine if(session.UserHasAdminPermission) { session.ClientEventEnqueue(e); continue; } // if you are just a user, denied access to some sensitive information switch (e.Type) { case ClientEventType.DeviceCreation: case ClientEventType.DeviceRemoval: case ClientEventType.DeviceUpdate: if (e.Device!=null) { Device temp = (Device)e.Device.Clone(); temp.Configuration = ""; e.Device = temp; } session.ClientEventEnqueue(e); continue; } } mutex.ReleaseMutex(); }