Exemplo n.º 1
0
        /// <summary>
        /// This is a simple demonstration of zeroMQ sockets using netMQ and protocol-buffer  
        /// This code demonstrates two things
        /// 
        /// 1. Use of zeroMQ REQUEST-RESPONSE socket using NetMQ library
        /// 2. Serialization and Deserialization using protocol-buf library
        /// 
        /// NOTE: Request-Response pattern is strictly sequential
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            const string serverAddress = "tcp://localhost:5556";
            const string serverIdentifier = "Response Server";

            using (var context = NetMQContext.Create())
            using (var server = context.CreateResponseSocket())
            using (var client = context.CreateRequestSocket())
            {
                // Bind the server to a local TCP address
                // Server is the stable infrastructure and that should BIND
                server.Bind(serverAddress);

                // Connect the client to the server. Client is dynamic so it CONNECTs to the address
                client.Connect(serverAddress);

                {
                    var clientMsg = new Message();
                    clientMsg.initMessage("Request Client1", serverIdentifier, "hello", "getting introduced");
                    Console.WriteLine("Client sending message: {0}", clientMsg.ToString());

                    // Send a message from the client socket
                    client.SendFrame(clientMsg.Serialize());
                }

                // Receive the message from the server socket
                {
                    byte[] rawmsg = server.ReceiveFrameBytes();
                    if (rawmsg != null && rawmsg.Length > 0)
                    {
                        var receivedMsg = Message.Deserialize(rawmsg);
                        Console.WriteLine("Server Received Message From Client: {0}", receivedMsg.ToString());

                        var serverMsg = new Message();
                        serverMsg.initMessage(serverIdentifier, receivedMsg.Source, "hello " + receivedMsg.Source, "Server has a new connection!");

                        Console.WriteLine("Server sending message: {0}", serverMsg.ToString());
                        // Send a response back from the server
                        server.SendFrame(serverMsg.Serialize());

                    }

                }

                // Now client should receive the message
                {
                    byte[] rawmsg = client.ReceiveFrameBytes();
                    if (rawmsg != null && rawmsg.Length > 0)
                    {

                        var receivedMsg = Message.Deserialize(rawmsg);
                        Console.WriteLine("Message From Server: {0}", receivedMsg.ToString());
                    }
                }

                Console.WriteLine("Press any key to exit ...");
                Console.ReadKey();
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            const string publisherAddress = "tcp://localhost:12345";
            const string publisherIdentifier = "Response Server";

            using (var context = NetMQContext.Create())
            using (var publisherSocket = context.CreatePublisherSocket())
            {

                Console.WriteLine("Publisher socket binding...");
                publisherSocket.Options.SendHighWatermark = 1000;
                publisherSocket.Bind(publisherAddress);

                Random rnd = new Random();
                long msgCount = 0;

                while(true)
                {
                    // Give a chance for subscribers to join before firing off messages
                    Thread.Sleep(rnd.Next(10000));

                    // Fire off 100 messages to all registered subscribers
                    for (var i = 0; i < 100; i++)
                    {
                        // Randomly generate 5 types of messages to replicate 5 types of subscribers
                        var randomizedTopic = (rnd.Next() % 5) + 1;
                        var subscriberType = String.Format("Subscribers Type:{0}", randomizedTopic);
                        var messageType = String.Format("MessageType:{0}", randomizedTopic);

                        var publisherMsg = new Message();
                        publisherMsg.initMessage(publisherIdentifier, subscriberType, messageType, "Welcome " + subscriberType);
                        Console.WriteLine("Publisher sending message: {0}", publisherMsg.ToString());

                        publisherSocket.SendMoreFrame(messageType).SendFrame(publisherMsg.Serialize());

                    }

                    msgCount += 100;
                    Console.WriteLine("\n Received {0} messages", msgCount);

                    Console.WriteLine("\n --- Press Ctrl + C to exit ... --- \n");
                }

            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// This is a simple demonstration of zeroMQ sockets using netMQ and protocol-buffer  
        /// This code demonstrates two things
        /// 
        /// 1. Use of zeroMQ REQUEST-RESPONSE socket using NetMQ library
        /// 2. Serialization and Deserialization using protocol-buf library
        /// 
        /// NOTE: Request-Response pattern is strictly sequential
        /// RequestSocket and ResponseSocket are synchronous, blocking, and throw exceptions if you try to read messages in the wrong order
        /// 
        /// http://netmq.readthedocs.org/en/latest/request-response/
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            const string serverAddress = "tcp://localhost:5556";
            const string serverIdentifier = "Response Server";

            using (var context = NetMQContext.Create())
            using (var server = context.CreateResponseSocket())
            using (var client = context.CreateRequestSocket())
            {
                // Bind the server to a local TCP address
                // Server is the stable infrastructure and that should BIND
                server.Bind(serverAddress);

                // Connect the client to the server. Client is dynamic so it CONNECTs to the address
                client.Connect(serverAddress);

                {
                    var clientMsg = new Message();
                    clientMsg.initMessage("Request Client1", serverIdentifier, "hello", "getting introduced");
                    Console.WriteLine("Client sending message: {0}", clientMsg.ToString());

                    // Send a message from the client socket
                    // Note: ZeroMQ using a wire-level protocol called ZMTP. It is agnostic about the datatypes and expects byte data.
                    // Processes can use any format for communication. In our specific demo, we use Google's protocol-buffer for serialization & deserialization
                    client.SendFrame(clientMsg.Serialize());
                }

                // Receive the message from the server socket
                {
                    byte[] rawmsg = server.ReceiveFrameBytes();
                    if (rawmsg != null && rawmsg.Length > 0)
                    {
                        var receivedMsg = Message.Deserialize(rawmsg);
                        Console.WriteLine("Server Received Message From Client: {0}", receivedMsg.ToString());

                        var serverMsg = new Message();
                        serverMsg.initMessage(serverIdentifier, receivedMsg.Source, "hello " + receivedMsg.Source, "Server has a new connection!");

                        Console.WriteLine("Server sending message: {0}", serverMsg.ToString());
                        // Send a response back from the server
                        server.SendFrame(serverMsg.Serialize());

                    }

                }

                // Now client should receive the message
                {
                    byte[] rawmsg = client.ReceiveFrameBytes();
                    if (rawmsg != null && rawmsg.Length > 0)
                    {

                        var receivedMsg = Message.Deserialize(rawmsg);
                        Console.WriteLine("Message From Server: {0}", receivedMsg.ToString());
                    }
                }

                Console.WriteLine("Press any key to exit ...");
                Console.ReadKey();
            }
        }