Exemplo n.º 1
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            {
                var receiverManager = new NetMQReceiverManager(context, "tcp://127.0.0.1:5557");
                var messageFactory = new MessageFactory();
                var messageHandler = RequestDispatcher.Create()
                                                      .Register<App2ComplementRequest, App2ComplementResponse>(request =>
                                                      {
                                                          Console.WriteLine("Received a {0} urgency request for a complement", request.Urgency.ToString());

                                                          if (request.Urgency == Urgency.Low)
                                                              return new App2ComplementResponse("You're hair looks nice today");
                                                          else if (request.Urgency == Urgency.Medium)
                                                              return new App2ComplementResponse("Hey, you're pretty excellent");
                                                          else
                                                              return new App2ComplementResponse("You are probably a better programmer than me");
                                                      });

                IServer server = new Server(receiverManager, messageFactory, messageHandler, "ExampleServer");

                var cancellationTokenSource = new CancellationTokenSource();

                Console.WriteLine("Starting App2 server...");

                server.Start(cancellationTokenSource.Token);

                Console.WriteLine("Server started, press enter to shutdown");

                Console.ReadLine();

                Console.WriteLine("Stopping server");
                cancellationTokenSource.Cancel();
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            {
                var receiverManager = new NetMQReceiverManager(context, "tcp://127.0.0.1:5556");
                var messageFactory = new MessageFactory();
                var messageHandler = RequestDispatcher.Create()
                                                      .Register<App1RandomNumberRequest, App1RandomNumberResponse>(request => {
                                                          Console.WriteLine("Received request for random number between {0} and {1}", request.MinBound, request.MaxBound);
                                                          var random = new Random();
                                                          return new App1RandomNumberResponse(random.Next(request.MinBound, request.MaxBound));
                                                      });

                IServer server = new Server(receiverManager, messageFactory, messageHandler, "ExampleServer");

                var cancellationTokenSource = new CancellationTokenSource();

                Console.WriteLine("Starting server...");

                server.Start(cancellationTokenSource.Token);

                Console.WriteLine("Server started, press enter to shutdown");

                Console.ReadLine();

                Console.WriteLine("Stopping server");
                cancellationTokenSource.Cancel();
            }
        }
Exemplo n.º 3
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 a request handler
                var messageHandler = 
                    RequestDispatcher.Create()
                        .Register<RandomNumberRequest, RandomNumberResponse>(new RandomNumberRequestHandler());

                // 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));

                // Send 10 requests to the server and display results 
                for (int i = 0; i < 10; i++)
                {
                    var request = new RandomNumberRequest() { Min = i, Max = 10 * i * i };
                    var response = client.Send<RandomNumberResponse, RandomNumberRequest>(request);
                    Console.WriteLine("Request random number betweem {0} to {1}", request.Min, request.Max);
                    Console.WriteLine("Response: {0}\n", response.RandomNumber);
                }

                // Stop the server
                cancellationTokenSource.Cancel();
            }
        }
Exemplo n.º 4
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();
            }
        }