using System; using System.IO; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using System.Threading; using System.Threading.Tasks; using Google.Protobuf.WellKnownTypes; using Google.Protobuf.Reflection; using Aurora.Proto.Events; using Aurora.Models; namespace Aurora.Services.EventManager { public class EventManager : BaseService { #region Fields private Dictionary> _subscriptionList; private Dictionary> _actionList; #endregion Fields public EventManager() { _subscriptionList = new Dictionary>(); _actionList = new Dictionary>(); } #region Private Methods #endregion Private Methods #region Public Methods /// /// Get the list of event type subscriptions for a given session id. /// /// Session Id /// public List GetSubscriptionList(string session) { List eventList = new List(); if (_subscriptionList.ContainsKey(session)) { _subscriptionList.TryGetValue(session, out eventList); } return eventList; } /// /// Get the number of event subscriptions for a given session /// /// Session Id /// public int GetSubscriptionCount(string session) { List eventList = new List(); if (_subscriptionList.ContainsKey(session)) { _subscriptionList.TryGetValue(session, out eventList); } return eventList.Count(); } /// /// Add a new subscription /// /// /// public bool AddSubscription(string session, EventType type) { bool success = false; lock (_subscriptionList) { if (!_subscriptionList.ContainsKey(session)) { //Add session to subscription list List eventList = new List(); eventList.Add(type); _subscriptionList.Add(session, eventList); success = true; } else { _subscriptionList.TryGetValue(session, out List eventList); if (eventList != null) { eventList.Add(type); success = true; } } } return success; } /// /// Add a list of subscriptions. This unsubscribes from unused events. /// /// The browser session id. /// The list of event types to subscribe to. public void AddSubscriptionList(string session, List types) { RemoveAllSubscriptions(session); foreach (EventType e in types) { AddSubscription(session, e); } } /// /// Unsubscribe from a given event type. /// /// Session Id /// Event Type to be removed public void RemoveSubscription(string session, EventType type) { lock (_subscriptionList) { if (_subscriptionList.ContainsKey(session)) { List eventTypeList; _subscriptionList.TryGetValue(session, out eventTypeList); if (eventTypeList != null && eventTypeList.Contains(type)) { eventTypeList.Remove(type); //base.LogInformation(string.Format("Subscription removed for event type {0} subscription on session {1}", type.ToString(), session)); } } } } public void RemoveSubscriptionList(string session, List types) { foreach (EventType e in types) { RemoveSubscription(session, e); } } /// /// Remove all subscriptons for a given session. /// /// Session Id public void RemoveAllSubscriptions(string session) { if (_subscriptionList.ContainsKey(session)) { _subscriptionList.Remove(session); } } public void AddEventHandler(Action action, string sessionId) { lock (_actionList) { _actionList.Add(sessionId, action); } } public void RemoveEventHandler(string sessionId) { _actionList.Remove(sessionId); } public void FireEvent(BaseEvent bEvent) { Dictionary> actionsCopy = new Dictionary>(); //Copy actions list lock (_actionList) { foreach (KeyValuePair> pair in _actionList) { actionsCopy.Add(pair.Key, pair.Value); } } lock (_subscriptionList) { foreach (KeyValuePair> pair in _subscriptionList) { Console.WriteLine("SERVER - Invoking action for client: " + pair.Key); Task.Delay(1000); //If action list contains an action for id, invoke if (actionsCopy.ContainsKey(pair.Key)) { actionsCopy.TryGetValue(pair.Key, out Action action); ParameterizedThreadStart operation = new ParameterizedThreadStart(obj => action((BaseEvent)obj)); Thread executionThread = new Thread(operation, 1024 * 1024); executionThread.Start(bEvent); executionThread.Join(); } } } } #endregion Public Methods } }