Repository navigation
Expand file tree
/
Copy pathEventManager.cs
More file actions
171 lines (154 loc) · 5.98 KB
/
Copy pathEventManager.cs
File metadata and controls
171 lines (154 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using IBM.Watson.DeveloperCloud.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
namespace IBM.Watson.DeveloperCloud.Utilities
{
/// <summary>
/// Singleton class for sending and receiving events.
/// </summary>
public class EventManager
{
#region Public Properties
/// <summary>
/// Returns the singleton event manager instance.
/// </summary>
public static EventManager Instance { get { return Singleton<EventManager>.Instance; } }
#endregion
#region Public Types
/// <summary>
/// The delegate for an event receiver.
/// </summary>
/// <param name="args">The arguments passed into SendEvent().</param>
public delegate void OnReceiveEvent(object[] args);
#endregion
#region Public Functions
/// <summary>
/// Register an event receiver with this EventManager.
/// </summary>
/// <param name="eventName">The name of the event.</param>
/// <param name="callback">The event receiver function.</param>
public void RegisterEventReceiver(string eventName, OnReceiveEvent callback)
{
if (!m_EventMap.ContainsKey(eventName))
m_EventMap.Add(eventName, new List<OnReceiveEvent>() { callback });
else
m_EventMap[eventName].Add(callback);
}
/// <summary>
/// Unregisters all event receivers.
/// </summary>
public void UnregisterAllEventReceivers()
{
m_EventMap.Clear();
}
/// <summary>
/// Unregister all event receivers for a given event.
/// </summary>
/// <param name="eventName">Name of the event to unregister.</param>
public void UnregisterEventReceivers(string eventName)
{
m_EventMap.Remove(eventName);
}
/// <summary>
/// Unregister a specific receiver.
/// </summary>
/// <param name="eventName">Name of the event.</param>
/// <param name="callback">The event handler.</param>
public void UnregisterEventReceiver(string eventName, OnReceiveEvent callback)
{
if (m_EventMap.ContainsKey(eventName))
m_EventMap[eventName].Remove(callback);
}
/// <summary>
/// Send an event to all registered receivers.
/// </summary>
/// <param name="eventName">The name of the event to send.</param>
/// <param name="args">Arguments to send to the event receiver.</param>
/// <returns>Returns true if a event receiver was found for the event.</returns>
public bool SendEvent(string eventName, params object[] args)
{
if (string.IsNullOrEmpty(eventName))
throw new ArgumentNullException(eventName);
List<OnReceiveEvent> receivers = null;
if (m_EventMap.TryGetValue(eventName, out receivers))
{
for (int i = 0; i < receivers.Count; ++i)
{
if (receivers[i] == null)
{
Log.Warning("EventManager", "Removing invalid event receiver.");
receivers.RemoveAt(i--);
continue;
}
try
{
receivers[i](args);
}
catch (Exception ex)
{
Log.Error("EventManager", "Event Receiver Exception: {0}", ex.ToString());
}
}
return true;
}
return false;
}
/// <summary>
/// Queues an event to be sent, returns immediately.
/// </summary>
/// <param name="eventName">The name of the event to send.</param>
/// <param name="args">Arguments to send to the event receiver.</param>
public void SendEventAsync(string eventName, params object[] args)
{
m_AsyncEvents.Enqueue(new AsyncEvent() { m_EventName = eventName, m_Args = args });
if (m_ProcesserCount == 0)
Runnable.Run(ProcessAsyncEvents());
}
#endregion
#region Private Data
private Dictionary<Type, Dictionary<object, string>> m_EventTypeName = new Dictionary<Type, Dictionary<object, string>>();
private Dictionary<string, List<OnReceiveEvent>> m_EventMap = new Dictionary<string, List<OnReceiveEvent>>();
private class AsyncEvent
{
public string m_EventName;
public object[] m_Args;
}
private Queue<AsyncEvent> m_AsyncEvents = new Queue<AsyncEvent>();
private int m_ProcesserCount = 0;
private IEnumerator ProcessAsyncEvents()
{
m_ProcesserCount += 1;
yield return null;
while (m_AsyncEvents.Count > 0)
{
AsyncEvent send = m_AsyncEvents.Dequeue();
SendEvent(send.m_EventName, send.m_Args);
}
m_ProcesserCount -= 1;
}
#endregion
private void InitializeEventTypeNames(Type enumType)
{
m_EventTypeName[enumType] = new Dictionary<object, string>();
foreach (var en in Enum.GetNames(enumType))
m_EventTypeName[enumType][Enum.Parse(enumType, en)] = en;
}
}
}