|
using System;
using System.Collections.Generic;
using System.Text;
using SvcHost;
namespace NotepadHandlerNET
{
/// <summary>
/// Class NotificationBase serves as base for all notification classes.
/// </summary>
public class NotificationBase
{
#region NotifAddressTable Helper Class
/// <summary>
/// Class NotifAddressTable contains all subscribers (clients) accessed by their URIs.
/// </summary>
public class NotifAddressTable : Dictionary<string, INotification>
{
}
#endregion
#region Variables
protected static NotifAddressTable nat = null;
protected Exception e = null;
#endregion
#region Static Ctor
/// <summary>
/// Static ctor.
/// </summary>
static NotificationBase()
{
nat = new NotifAddressTable();
}
#endregion
#region Methods
/// <summary>
/// Method returns notification proxy for a given endpoint URI.
/// </summary>
private INotification FindNotificationClient(string endpointAddressUriNotification)
{
INotification notifProxy = null;
if (null != endpointAddressUriNotification)
{
try
{
notifProxy = nat[endpointAddressUriNotification.ToLower()];
}
catch //(Exception e)
{
}
}
return notifProxy;
}
/// <summary>
/// Method returns notification proxy of a given endpoint URI.
/// </summary>
protected void RemoveNotificationClient(INotification notifProxy)
{
lock (typeof(NotifAddressTable))
{
string uriToRemove = null;
foreach (string uri in nat.Keys)
if (nat[uri] == notifProxy)
uriToRemove = uri;
nat.Remove(uriToRemove);
}
}
/// <summary>
/// Wrapper method around notification call.
/// </summary>
protected object Notify(INotification notifProxy, string sender, string eventName, object data)
{
object ret = null;
if (null != notifProxy)
{
try
{
ret = notifProxy.Notify(sender, eventName, data);
}
catch //(Exception e)
{
RemoveNotificationClient(notifProxy);
}
}
return ret;
}
/// <summary>
/// Method provides (either finds or creates) notification proxy. Synchronization is required.
/// </summary>
protected INotification GetNotificationClient(string endpointAddressUriNotification)
{
INotification notifProxy = null;
string uri = endpointAddressUriNotification.ToLower();
lock (typeof(NotifAddressTable))
{
notifProxy = FindNotificationClient(endpointAddressUriNotification);
if (null == notifProxy)
{
try
{
notifProxy = Client.ConnectTo<INotification>(endpointAddressUriNotification);
if (null != notifProxy)
nat.Add(uri, notifProxy);
}
catch (Exception e)
{
this.e = e;
}
}
}
return notifProxy;
}
/// <summary>
/// Method returns a list of all notification proxies. Synchronization is required.
/// </summary>
protected List<INotification> GetAllNotificationSubscribers()
{
List<INotification> listProxy = new List<INotification>();
lock (typeof(NotifAddressTable))
{
foreach (string uri in nat.Keys)
listProxy.Add(FindNotificationClient(uri));
}
return listProxy;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.