2020-01-20 21:55:12 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using Aurora.Proto.PartyV2 ;
namespace Aurora.Services.Server.EventManager
{
public class EventManager
{
#region Fields
private Dictionary < string , List < EventType > > _subscriptionList ;
private Dictionary < string , EventAction > _actionList ;
#endregion Fields
public EventManager ( )
{
_subscriptionList = new Dictionary < string , List < EventType > > ( ) ;
_actionList = new Dictionary < string , EventAction > ( ) ;
}
#region Private Methods
#endregion Private Methods
#region Public Methods
/// <summary>
/// Get the list of event type subscriptions for a given sessionIdentifier id.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public List < EventType > GetSubscriptionList ( string sessionIdentifier )
{
List < EventType > eventList = new List < EventType > ( ) ;
if ( _subscriptionList . ContainsKey ( sessionIdentifier ) )
{
_subscriptionList . TryGetValue ( sessionIdentifier , out eventList ) ;
}
return eventList ;
}
/// <summary>
/// Get the number of event subscriptions for a given sessionIdentifier
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public int GetSubscriptionCount ( string sessionIdentifier )
{
List < EventType > eventList = new List < EventType > ( ) ;
if ( _subscriptionList . ContainsKey ( sessionIdentifier ) )
{
_subscriptionList . TryGetValue ( sessionIdentifier , out eventList ) ;
}
return eventList . Count ( ) ;
}
/// <summary>
/// Add a new subscription
/// </summary>
/// <param name="sessionIdentifier"></param>
/// <param name="type"></param>
public bool AddSubscription ( string sessionIdentifier , EventType type )
{
bool success = false ;
lock ( _subscriptionList )
{
if ( ! _subscriptionList . ContainsKey ( sessionIdentifier ) )
{
//Add sessionIdentifier to subscription list
List < EventType > eventList = new List < EventType > ( ) ;
eventList . Add ( type ) ;
_subscriptionList . Add ( sessionIdentifier , eventList ) ;
success = true ;
}
else
{
_subscriptionList . TryGetValue ( sessionIdentifier , out List < EventType > eventList ) ;
if ( eventList ! = null )
{
eventList . Add ( type ) ;
success = true ;
}
}
}
return success ;
}
/// <summary>
/// Add a list of subscriptions. This unsubscribes from unused events.
/// </summary>
/// <param name="sessionIdentifier">The browser sessionIdentifier id.</param>
/// <param name="types">The list of event types to subscribe to.</param>
public void AddSubscriptionList ( string sessionIdentifier , List < EventType > types )
{
RemoveAllSubscriptions ( sessionIdentifier ) ;
foreach ( EventType e in types )
{
AddSubscription ( sessionIdentifier , e ) ;
}
}
/// <summary>
/// Unsubscribe from a given event type.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <param name="type">Event Type to be removed</param>
public void RemoveSubscription ( string sessionIdentifier , EventType type )
{
lock ( _subscriptionList )
{
if ( _subscriptionList . ContainsKey ( sessionIdentifier ) )
{
List < EventType > eventTypeList ;
_subscriptionList . TryGetValue ( sessionIdentifier , out eventTypeList ) ;
if ( eventTypeList ! = null & & eventTypeList . Contains ( type ) )
{
eventTypeList . Remove ( type ) ;
//base.LogInformation(string.Format("Subscription removed for event type {0} subscription on sessionIdentifier {1}", type.ToString(), sessionIdentifier));
}
}
}
}
public void RemoveSubscriptionList ( string sessionIdentifier , List < EventType > types )
{
foreach ( EventType e in types )
{
RemoveSubscription ( sessionIdentifier , e ) ;
}
}
/// <summary>
/// Remove all subscriptons for a given sessionIdentifier.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
public void RemoveAllSubscriptions ( string sessionIdentifier )
{
if ( _subscriptionList . ContainsKey ( sessionIdentifier ) )
{
_subscriptionList . Remove ( sessionIdentifier ) ;
}
}
public void AddEventHandler ( Action < BaseEvent > action , Action cancel , string sessionIdentifierId )
{
lock ( _actionList )
{
_actionList . Add ( sessionIdentifierId , new EventAction ( action , cancel ) ) ;
}
}
public void RemoveEventHandler ( string sessionIdentifierId )
{
_actionList . Remove ( sessionIdentifierId ) ;
}
public void CancelEventStream ( string sessionIdentifierId )
{
_actionList . TryGetValue ( sessionIdentifierId , out EventAction value ) ;
2020-01-21 04:22:58 +00:00
if ( value ! = null )
{
value . Cancel ( ) ;
}
2020-01-20 21:55:12 +00:00
RemoveEventHandler ( sessionIdentifierId ) ;
}
public void FireEvent ( BaseEvent bEvent )
{
Dictionary < string , EventAction > actionsCopy = new Dictionary < string , EventAction > ( ) ;
//Copy actions list
lock ( _actionList )
{
foreach ( KeyValuePair < string , EventAction > pair in _actionList )
{
actionsCopy . Add ( pair . Key , pair . Value ) ;
}
}
lock ( _subscriptionList )
{
foreach ( KeyValuePair < string , List < EventType > > pair in _subscriptionList )
{
Task . Delay ( 1000 ) ;
//If action list contains an action for id, invoke
if ( actionsCopy . ContainsKey ( pair . Key ) )
{
actionsCopy . TryGetValue ( pair . Key , out EventAction action ) ;
Task executionTask = new Task ( ( ) = > action . Callback ( bEvent ) ) ;
//Execute task with exception handler
executionTask . ContinueWith ( ( Task task ) = >
{
var exception = executionTask . Exception ;
Console . WriteLine ( string . Format ( "SERVER --- Exception occurred firing event" ) ) ;
this . _actionList . Remove ( pair . Key ) ;
} ,
TaskContinuationOptions . OnlyOnFaulted ) ;
executionTask . Start ( ) ;
}
}
}
}
#endregion Public Methods
}
}