forked from dotnet/orleans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObserverSubscriptionManager.cs
116 lines (108 loc) · 4.2 KB
/
ObserverSubscriptionManager.cs
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
using System;
using System.Collections.Generic;
using Orleans.Runtime;
namespace Orleans
{
/// <summary>
/// The ObserverSubscriptionManager class is a helper class for grains that support observers.
/// It provides methods for tracking subscribing observers and for sending notifications.
/// </summary>
/// <typeparam name="T">The observer interface type to be managed.</typeparam>
[Serializable]
public class ObserverSubscriptionManager<T>
where T : IGrainObserver
{
/// <summary>
/// Number of subscribers currently registered
/// </summary>
public int Count
{
get { return observers.Count; }
}
/// <summary>
/// The set of currently-subscribed observers.
/// This is implemented as a HashSet of IGrainObserver so that if the same observer subscribes multiple times,
/// it will still only get invoked once per notification.
/// </summary>
private readonly HashSet<T> observers;
/// <summary>
/// Constructs an empty subscription manager.
/// </summary>
public ObserverSubscriptionManager()
{
observers = new HashSet<T>();
}
/// <summary>
/// Records a new subscribing observer.
/// </summary>
/// <param name="observer">The new subscriber.</param>
/// <returns>A promise that resolves when the subscriber is added.
/// <para>This promise will be broken if the observer is already a subscriber.
/// In this case, the existing subscription is unaffected.</para></returns>
public void Subscribe(T observer)
{
if (!observers.Add(observer))
throw new OrleansException(String.Format("Cannot subscribe already subscribed observer {0}.", observer));
}
/// <summary>
/// Determines if the SubscriptionManager has the input observer
/// </summary>
/// <param name="observer">True if the the observer is already subscribed, otherwise False.</param>
/// <returns>True is the SubscriptionManager has the input observer.</returns>
public bool IsSubscribed(T observer)
{
return observers.Contains(observer);
}
/// <summary>
/// Removes a (former) subscriber.
/// </summary>
/// <param name="observer">The unsubscribing observer.</param>
/// <returns>A promise that resolves when the subscriber is removed.
/// This promise will be broken if the observer is not a subscriber.</returns>
public void Unsubscribe(T observer)
{
if (!observers.Remove(observer))
throw new OrleansException(String.Format("Observer {0} is not subscribed.", observer));
}
/// <summary>
/// Removes all subscriptions.
/// </summary>
public void Clear()
{
observers.Clear();
}
/// <summary>
/// Sends a notification to all subscribers.
/// </summary>
/// <param name="notification">An action that sends the notification by invoking the proper method on the provided subscriber.
/// This action is called once for each current subscriber.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public void Notify(Action<T> notification)
{
List<T> failed = null;
foreach (var observer in observers)
{
try
{
notification(observer);
}
catch (Exception)
{
if (failed == null)
{
failed = new List<T>();
}
failed.Add(observer);
}
}
if (failed != null)
{
foreach (var key in failed)
{
observers.Remove(key);
}
throw new OrleansException(String.Format("Failed to notify the following observers: {0}", Utils.EnumerableToString(failed)));
}
}
}
}