-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from jirkapok/feature/Fixed_Notifications_Tab…
…_Recreation Fixed notification not received in SWQL studio
- Loading branch information
Showing
6 changed files
with
188 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace SwqlStudio.Subscriptions | ||
{ | ||
internal class SubscriptionCallbacks | ||
{ | ||
private readonly string activeSubscriberAddress; | ||
internal string Uri { get; } | ||
internal string Id { get; set; } | ||
|
||
private readonly NotificationDeliveryServiceProxy proxy; | ||
private readonly object itemsLock = new object(); | ||
|
||
internal IEnumerable<SubscriberCallback> Callbacks | ||
{ | ||
get | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
return new List<SubscriberCallback>(this.callbacks); | ||
} | ||
} | ||
} | ||
|
||
public bool Empty | ||
{ | ||
get | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
return !this.callbacks.Any(); | ||
} | ||
} | ||
} | ||
|
||
private readonly List<SubscriberCallback> callbacks = new List<SubscriberCallback>(); | ||
|
||
public SubscriptionCallbacks(string uri, NotificationDeliveryServiceProxy proxy, string activeSubscriberAddress) | ||
{ | ||
this.Uri = uri; | ||
this.Id = uri.Substring(uri.LastIndexOf("=") + 1); | ||
this.proxy = proxy; | ||
this.activeSubscriberAddress = activeSubscriberAddress; | ||
} | ||
|
||
internal void Add(SubscriberCallback callback) | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
if (!this.callbacks.Contains(callback)) | ||
this.callbacks.Add(callback); | ||
} | ||
} | ||
|
||
internal void Remove(SubscriberCallback callback) | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
this.callbacks.Remove(callback); | ||
} | ||
} | ||
|
||
internal void CloseProxy() | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
if (this.proxy != null) | ||
{ | ||
this.proxy.Disconnect(this.activeSubscriberAddress); | ||
this.proxy.Close(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace SwqlStudio.Subscriptions | ||
{ | ||
internal class SubscriptionInfo | ||
{ | ||
private readonly ConnectionInfo connection; | ||
private readonly object itemsLock = new object(); | ||
|
||
private readonly Dictionary<string, SubscriptionCallbacks> subscriptions = | ||
new Dictionary<string, SubscriptionCallbacks>(); | ||
|
||
internal SubscriptionInfo(ConnectionInfo connection) | ||
{ | ||
this.connection = connection; | ||
} | ||
|
||
internal bool HasSubScription(string subscriptionId) | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
return this.subscriptions.Values.Any(s => s.Id == subscriptionId); | ||
} | ||
} | ||
|
||
internal string Register(string query, SubscriberCallback callback, | ||
Func<ConnectionInfo, string, SubscriptionCallbacks> subscribe) | ||
{ | ||
SubscriptionCallbacks subscription; | ||
var normalized = query.ToLower(); | ||
|
||
lock (this.itemsLock) | ||
{ | ||
if (!this.subscriptions.TryGetValue(normalized, out subscription)) | ||
{ | ||
subscription = subscribe(this.connection, query); | ||
this.subscriptions.Add(normalized, subscription); | ||
} | ||
} | ||
|
||
subscription.Add(callback); | ||
return subscription.Uri; | ||
} | ||
|
||
internal void Remove(string subscriptionUri, SubscriberCallback callback) | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
var query = this.subscriptions.Where(kv => kv.Value.Uri == subscriptionUri) | ||
.Select(kv => kv.Key) | ||
.FirstOrDefault(); | ||
|
||
if (String.IsNullOrEmpty(query)) | ||
return; | ||
|
||
var subscription = this.subscriptions[query]; | ||
subscription.Remove(callback); | ||
|
||
if (subscription.Empty) | ||
{ | ||
this.subscriptions.Remove(query); | ||
this.Unsubscribe(subscriptionUri); | ||
subscription.CloseProxy(); | ||
} | ||
} | ||
} | ||
|
||
private void Unsubscribe(string subscriptionUri) | ||
{ | ||
if (this.connection.IsConnected) | ||
this.connection.Proxy.Delete(subscriptionUri); | ||
} | ||
|
||
internal IEnumerable<SubscriberCallback> CallBacks(string subscriptionId) | ||
{ | ||
lock (this.itemsLock) | ||
{ | ||
return this.subscriptions.Values.Where(v => v.Id == subscriptionId) | ||
.SelectMany(kv => kv.Callbacks) | ||
.ToList(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters