Example #1
0
        static void Main(string[] args)
        {
            // Create the NetMQ context
            using (var context = NetMQContext.Create())
            {
                // Setup a default message factory
                var messageFactory = new MessageFactory();

                // Setup a message handler and register two request handlers
                var messageHandler =
                    RequestDispatcher.Create()
                        .Register<EchoRequest, EchoResponse>(new EchoRequestHandler())
                        .Register<QuadraticRequest, QuadraticResponse>(new QuadraticRequestHandler());

                // Create the server and client
                Server server = new Server(new NetMQReceiverManager(context, "tcp://127.0.0.1:5556"), messageFactory, messageHandler, "ExampleServer");
                Client client = new Client(new NetMQSenderManager(context, "tcp://127.0.0.1:5556"), messageFactory);

                // Run the server in a task with a cancellation token to cancel the task later
                var cancellationTokenSource = new CancellationTokenSource();
                Task.Run(() => server.Run(cancellationTokenSource.Token));


                // Make some requests from the server
                var echo1 = new EchoRequest() { Name = "Jon", Message = "My first message!" };
                var echo2 = client.Send<EchoResponse, EchoRequest>(echo1);
                Display(echo1);
                Display(echo2);

                var echo3 = new EchoRequest() { Name = "Steve", Message = "Hi, I am steve" };
                var echo4 = client.Send<EchoResponse, EchoRequest>(echo3);
                Display(echo3);
                Display(echo4);

                var quad1 = new QuadraticRequest() { A = 1, B = 2, C = 3 };
                var quad2 = client.Send<QuadraticResponse, QuadraticRequest>(quad1);
                Display(quad1);
                Display(quad2);

                var quad3 = new QuadraticRequest() { A = -1, B = 5, C = 4 };
                var quad4 = client.Send<QuadraticResponse, QuadraticRequest>(quad3);
                Display(quad3);
                Display(quad4);


                // Stop the server
                cancellationTokenSource.Cancel();
            }
        }
Example #2
0
 private static void Display(QuadraticRequest request)
 {
     Console.WriteLine("Request roots for: {0}x^2 + {1}x + {2}", request.A, request.B, request.C);
 }