forked from Redth/PushSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushBroker.cs
90 lines (76 loc) · 3.3 KB
/
PushBroker.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PushSharp.Core;
namespace PushSharp
{
public class PushBroker : IDisposable
{
public event ChannelCreatedDelegate OnChannelCreated;
public event ChannelDestroyedDelegate OnChannelDestroyed;
public event NotificationSentDelegate OnNotificationSent;
public event NotificationFailedDelegate OnNotificationFailed;
public event NotificationRequeueDelegate OnNotificationRequeue;
public event ChannelExceptionDelegate OnChannelException;
public event ServiceExceptionDelegate OnServiceException;
public event DeviceSubscriptionExpiredDelegate OnDeviceSubscriptionExpired;
public event DeviceSubscriptionChangedDelegate OnDeviceSubscriptionChanged;
private Dictionary<Type, List<IPushService>> registeredServices;
public PushBroker()
{
registeredServices = new Dictionary<Type, List<IPushService>>();
}
public void RegisterService<TPushNotification>(IPushService pushService) where TPushNotification : Notification
{
var pushNotificationType = typeof (TPushNotification);
if (registeredServices.ContainsKey(pushNotificationType))
registeredServices[pushNotificationType].Add(pushService);
else
registeredServices.Add(pushNotificationType, new List<IPushService>() { pushService });
pushService.OnChannelCreated += OnChannelCreated;
pushService.OnChannelDestroyed += OnChannelDestroyed;
pushService.OnChannelException += OnChannelException;
pushService.OnDeviceSubscriptionExpired += OnDeviceSubscriptionExpired;
pushService.OnNotificationFailed += OnNotificationFailed;
pushService.OnNotificationSent += OnNotificationSent;
pushService.OnNotificationRequeue += OnNotificationRequeue;
pushService.OnServiceException += OnServiceException;
pushService.OnDeviceSubscriptionChanged += OnDeviceSubscriptionChanged;
}
public void QueueNotification<TPushNotification>(TPushNotification notification) where TPushNotification : Notification
{
var pushNotificationType = typeof (TPushNotification);
if (registeredServices.ContainsKey(pushNotificationType))
registeredServices[pushNotificationType].ForEach(pushService => pushService.QueueNotification(notification));
else
throw new IndexOutOfRangeException("There are no Registered Services that handle this type of Notification");
}
public IEnumerable<IPushService> GetRegistrations<TNotification>()
{
var type = typeof(TNotification);
if (registeredServices != null && registeredServices.ContainsKey(type))
return registeredServices[type];
return null;
}
public void StopAllServices(bool waitForQueuesToFinish = true)
{
registeredServices.Values.AsParallel().ForAll(svc => svc.ForEach(svcOn => {
svcOn.Stop(waitForQueuesToFinish);
svcOn.OnChannelCreated -= OnChannelCreated;
svcOn.OnChannelDestroyed -= OnChannelDestroyed;
svcOn.OnChannelException -= OnChannelException;
svcOn.OnDeviceSubscriptionExpired -= OnDeviceSubscriptionExpired;
svcOn.OnNotificationFailed -= OnNotificationFailed;
svcOn.OnNotificationSent -= OnNotificationSent;
svcOn.OnServiceException -= OnServiceException;
svcOn.OnDeviceSubscriptionChanged -= OnDeviceSubscriptionChanged;
}));
}
void IDisposable.Dispose()
{
StopAllServices(false);
}
}
}