Home | | C Sharp and .NET Framework(CNF) | C# Publish/Subscribe Design Patterns

Chapter: C# and .NET Framework

C# Publish/Subscribe Design Patterns

UDP allows directly sending and receiving messages without establishing any connection. Here, the UDP protocol is used for all the communication between the pub/sub server and the publisher/subscriber.

Publish/Subscribe Design Patterns

 

UDP allows directly sending and receiving messages without establishing any connection. Here, the UDP protocol is used for all the communication between the pub/sub server and the publisher/subscriber. When a sbscriber application sends a message for subscription to the pub/sub server, the pub/sub server keeps the address (IP, port) of the subscriber topic-wise. When an event message of a particular topic is sent to a pub/sub server by the publisher application, the pub/sub server takes the address list for the corresponding topic and sends the event to every address of the list.

 

Implementation

Step 1: Making the filter class

 

The filter class has the following responsibilities and is used by both the Publish Service and the Subscriber Service.

 

Keeps the list of subscriber topic-wise, and

Exposes a method to add a new subscriber,

Exposes a method to remove a subscriber,

Returns a list of subscribers for a topic.

 

Hide Shrink Copy Code

 

class Filter

 

{

static Dictionary<string, List<EndPoint>>

_subscribersList = new Dictionary<string, List<EndPoint>>();

 

static public Dictionary<string, List<EndPoint>> SubscribersList

{

get { return _subscribersList; }

}

 

static public List<EndPoint> GetSubscribers(String topicName)

{

 

 

if (SubscribersList.ContainsKey(topicName))

{

return SubscribersList[topicName];

}

else

return null;

}

 

static public void AddSubscriber(String topicName, EndPoint subscriberEndPoint)

{

if (SubscribersList.ContainsKey(topicName))

{

if (!SubscribersList[topicName].Contains(subscriberEndPoint))

 

{

SubscribersList[topicName].Add(subscriberEndPoint);

}

}

else

{

 

List<EndPoint> newSubscribersList = new List<EndPoint>(); newSubscribersList.Add(subscriberEndPoint); SubscribersList.Add(topicName, newSubscribersList);

}

 

}

 

static public void RemoveSubscriber(String topicName, EndPoint subscriberEndPoint)

{

if (SubscribersList.ContainsKey(topicName))

{

if (SubscribersList[topicName].Contains(subscriberEndPoint))

{

SubscribersList[topicName].Remove(subscriberEndPoint);

}

}

 

}

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
C# and .NET Framework : C# Publish/Subscribe Design Patterns |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.