예제 #1
0
        public static void Main(string[] args)
        {
            //Instantiate publishers, subscribers and PubSubService
            IPublisher sportNewsPublisher     = new NewsPublisher();
            IPublisher politicalNewsPublisher = new NewsPublisher();

            Subscriber sportSubscriber    = new NewsSubscriber();
            Subscriber politicsSubscriber = new NewsSubscriber();
            Subscriber newsSubscriber     = new NewsSubscriber();


            //Declare Messages and Publish Messages to PubSubService
            Message sportMsg1    = new Message("Sport", "Sport news 1");
            Message sportMsg2    = new Message("Sport", "sport news 2");
            Message politicsMsg3 = new Message("Politics", "politics 1223");

            //
            PubSubService pubSubService = new PubSubService();

            sportNewsPublisher.publish(sportMsg1, pubSubService);
            sportNewsPublisher.publish(sportMsg2, pubSubService);
            politicalNewsPublisher.publish(politicsMsg3, pubSubService);



            //Declare Subscribers
            sportSubscriber.AddSubscriber("Sport", pubSubService);
            politicsSubscriber.AddSubscriber("Politics", pubSubService);
            newsSubscriber.AddSubscriber("Sport", pubSubService);
            newsSubscriber.AddSubscriber("Politics", pubSubService);

            //Trying unSubscribing a subscriber
            //newsSubscriber.unSubscribe("Sport", pubSubService);

            //Broadcast message to all subscribers. After broadcast, messageQueue will be empty in PubSubService
            pubSubService.Broadcast();

            //Print messages of each subscriber to see which messages they got
            Console.WriteLine("Messages of Sport Subscriber are: ");
            sportSubscriber.printMessages();

            Console.WriteLine("\nMessages of Politics Subscriber are: ");
            politicsSubscriber.printMessages();

            Console.WriteLine("\nMessages of News Subscriber are: ");
            newsSubscriber.printMessages();

            Console.Write("\nPress Any Key:");
            Console.ReadKey();
        }
예제 #2
0
 public void publish(Message Message, PubSubService PubSubService)
 {
     PubSubService.AddMessageToQueue(Message);
 }
예제 #3
0
 //Request specifically for messages related to topic from PubSubService
 public abstract void GetMessagesForSubscriberOfTopic(string Topic, PubSubService pubSubService);
예제 #4
0
 //Unsubscribe subscriber with PubSubService for a topic
 public abstract void UnSubscribe(string Topic, PubSubService PubSubService);
예제 #5
0
 //Add subscriber with PubSubService for a topic
 public abstract void AddSubscriber(string Topic, PubSubService PubSubService);
예제 #6
0
 //Unsubscribe subscriber with PubSubService for a topic
 public override void UnSubscribe(string Topic, PubSubService PubSubService)
 {
     PubSubService.RemoveSubscriber(Topic, this);
 }
예제 #7
0
 //Request specifically for messages related to topic from PubSubService
 public override void GetMessagesForSubscriberOfTopic(string Topic, PubSubService PubSubService)
 {
     PubSubService.GetMessagesForSubscriberOfTopic(Topic, this);
 }
예제 #8
0
 //Add subscriber with PubSubService for a topic
 public override void AddSubscriber(string Topic, PubSubService PubSubService)
 {
     PubSubService.AddSubscriber(Topic, this);
 }