简体   繁体   中英

Consumer Publisher model for Socket communication

I am implementing publisher consumer model for UDP socket communication. In that application the consumers will create the UDP socket while subscribing the events.So each consumer will have different IP address and port for data sending. And process of subscribing to events is dynamic means we can add subscribers run time. In my application the publisher invokes the event when after some interval of time. Suppose in my application currently I have 5 subscribers so after invoking the method by publisher, this data should be sent to 5 different subscribers using their IP and Port. As I am having single method of sending data in subscribers, I am not getting how to send data to different IP and port simultaneously? How to maintain the states of event handlers? Please help me.

Here I am adding the sample code.

public class Program
    {        
        public static EventHandler<ListEventArgs> DataSending;

        static void Main(string[] args)
        {

            Class1.CreateNewEvent();          
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            Class1.CreateNewEvent();
            OnDataSending("Hello2");
            Console.ReadLine();          

        }

        static void OnDataSending(object data)
        {
            if (DataSending != null)
            {
                List<string> ss = new List<string>();
                ss.Add(IPADDRESS);
                ss.Add(PORT);               
                DataSending(data,new ListEventArgs(ss));
            }
        }
    }




public static class Class1
    {      

         public static void CreateNewEvent()
         {
             Program.DataSending += new EventHandler<ListEventArgs>(newEvent);
         }

         static void newEvent(object o, ListEventArgs e)
         {
            //Code for socket creation and sending data
         }
    }

There may be better ways of doing this. But, this is what I've done based on my understanding. This should work as prototype for your requirements.

 public class Program
 {
    static List<Subscriber> GetSubscribers()
    {
        return
            new List<Subscriber>
            {
                new Subscriber{IP ="1.2.3.4",Port="1521"},
                new Subscriber{IP="2.2.2.2",Port="8080"},
                new Subscriber{IP="4.4.4.4",Port="1250"},
                new Subscriber{IP="6.6.6.6",Port="4123"}
            };

    }

    static void Main(string[] args)
    {

        Publisher p = new Publisher();
        List<Subscriber> subs = GetSubscribers();
        p.PublishEvent += new Publisher.PublishData(subs[0].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[1].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[2].PrintMessage);
        p.PublishEvent += new Publisher.PublishData(subs[3].PrintMessage);
        p.OnPublishData();
        Console.ReadKey();
    }

}

class Publisher
{

    public delegate void PublishData();
    public event PublishData PublishEvent;
    public void OnPublishData()
    {           
        if (PublishEvent != null)
            PublishEvent();
    }
}


class Subscriber
{
    public string IP { get; set; }
    public string Port { get; set; }

    public void PrintMessage()
    {
        Console.WriteLine("Data has arrived for IP " + IP + " Port " + Port);
    }
}

I think a better approach.

public class Program
{
    static List<Subscriber> GetSubscribers()
    {
        return
            new List<Subscriber>
            {
                new Subscriber{IP ="1.2.3.4",Port="1521"},
                new Subscriber{IP="2.2.2.2",Port="8080"},
                new Subscriber{IP="4.4.4.4",Port="1250"},
                new Subscriber{IP="6.6.6.6",Port="4123"}
            };

    }

    static void Main(string[] args)
    {

        PublishDataEventArgs e = new PublishDataEventArgs();
        e.Message = "Hello from Subscriber";

        Publisher p = new Publisher();
        List<Subscriber> subs = GetSubscribers();
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[0].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[1].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[2].PrintMessage);
        p.PublishData += new EventHandler<PublishDataEventArgs>(subs[3].PrintMessage);
        p.OnPublishData(e);
        Console.ReadKey();
    }

}

class Publisher
{  
    public void OnPublishData(PublishDataEventArgs p)
    {
        EventHandler<PublishDataEventArgs> _PublishData = PublishData;
        if (_PublishData != null)
            _PublishData(this, p);
    }


    public event EventHandler<PublishDataEventArgs> PublishData;
}

class PublishDataEventArgs:EventArgs
{
    public string Message{get;set;}
}


class Subscriber
{
    public string IP { get; set; }
    public string Port { get; set; }

    public void PrintMessage(object sender,PublishDataEventArgs e)
    {
        Console.WriteLine("Data has arrived for IP " + IP + " Port " + Port + " message " + e.Message );
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM