Beispiel #1
0
        public RpcClient()
        {
            TestUtilitiesClass.WriteLine("RpcClient constructor started");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            connection     = factory.CreateConnection();
            channel        = connection.CreateModel();
            replyQueueName = channel.QueueDeclare().QueueName;
            consumer       = new EventingBasicConsumer(channel);
            TestUtilitiesClass.WriteLine("RpcClient replyQueueName: " + replyQueueName);
            props = channel.CreateBasicProperties();
            string correlationId = Guid.NewGuid().ToString();

            TestUtilitiesClass.WriteLine("RpcClient correlationId: " + correlationId);
            props.CorrelationId = correlationId;
            props.ReplyTo       = replyQueueName;

            consumer.Received += (model, ea) =>
            {
                TestUtilitiesClass.WriteLine("RpcClient Received");
                byte[] body     = ea.Body;
                string response = Encoding.UTF8.GetString(body);
                if (ea.BasicProperties.CorrelationId == correlationId)
                {
                    respQueue.Add(response);
                }
            };
        }
Beispiel #2
0
        public static void Main2()
        {
            TestUtilitiesClass.WriteLine("RpcServer Main2 started.");
            ConnectionFactory factory = TestUtilitiesClass.GetConnectionFactory();

            IConnection connection = factory.CreateConnection();
            IModel      channel    = connection.CreateModel();

            TestUtilitiesClass.WriteLine("RpcServer Main2 QueueDeclare: rpc_queue");

            channel.QueueDeclare(queue: "rpc_queue",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.BasicQos(0, 1, false);

            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                TestUtilitiesClass.WriteLine("RpcServer Main2 Received message");
                string response = null;

                byte[]           body       = ea.Body;
                IBasicProperties props      = ea.BasicProperties;
                IBasicProperties replyProps = channel.CreateBasicProperties();
                replyProps.CorrelationId = props.CorrelationId;
                TestUtilitiesClass.WriteLine("RpcServer Main2 CorrelationId: " + props.CorrelationId);

                try
                {
                    string message = Encoding.UTF8.GetString(body);
                    int    n       = int.Parse(message);
                    Console.WriteLine("RpcServer Main2 fib({0})", message);
                    response = fib(n).ToString();
                }
                catch (Exception e)
                {
                    TestUtilitiesClass.WriteLine("RpcServer Main2 Exception " + e.Message);
                    response = "";
                }
                finally
                {
                    TestUtilitiesClass.WriteLine("RpcServer Main2 finally");
                    byte[] responseBytes = Encoding.UTF8.GetBytes(response);
                    TestUtilitiesClass.WriteLine("RpcServer Main2 finally BasicPublish routingKey: " + props.ReplyTo);
                    channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
                                         basicProperties: replyProps, body: responseBytes);
                    channel.BasicAck(deliveryTag: ea.DeliveryTag,
                                     multiple: false);
                }
            };
            TestUtilitiesClass.WriteLine("RpcServer Main2 BasicConsume queue: rpc_queue");
            channel.BasicConsume(queue: "rpc_queue",
                                 autoAck: false,
                                 consumer: consumer);
            Console.WriteLine("RpcServer Main2 Awaiting RPC requests");
        }
Beispiel #3
0
 static void Main(string[] args)
 {
     TestUtilitiesClass.WriteLine("Program.Main Started.");
     RunClient();
     RunServer();
     TestUtilitiesClass.WriteLine("main finished.");
     Console.ReadLine();
 }
Beispiel #4
0
        public static void Main2()
        {
            RpcClient rpcClient = new RpcClient();

            TestUtilitiesClass.WriteLine("Rpc Main2 Requesting fib(30)");
            string response = rpcClient.Call("5");

            Console.WriteLine("Rpc Main2 Got '{0}'", response);
            rpcClient.Close();
        }
Beispiel #5
0
        ///

        /// Assumes only valid positive integer input.
        /// Don't expect this one to work for big numbers, and it's
        /// probably the slowest recursive implementation possible.
        ///
        private static int fib(int n)
        {
            TestUtilitiesClass.WriteLine("RPCServer fib is called: " + n);
            TestUtilitiesClass.Sleep(0.5);
            if (n == 0 || n == 1)
            {
                return(n);
            }

            return(fib(n - 1) + fib(n - 2));
        }
Beispiel #6
0
 public string Call(string message)
 {
     TestUtilitiesClass.WriteLine("RpcClient Call message: " + message);
     byte[] messageBytes = Encoding.UTF8.GetBytes(message);
     TestUtilitiesClass.WriteLine("RpcClient BasicPublish routingKey: rpc_queue");
     channel.BasicPublish(
         exchange: "",
         routingKey: "rpc_queue",
         basicProperties: props,
         body: messageBytes);
     TestUtilitiesClass.WriteLine("RpcClient BasicConsume queue: " + replyQueueName);
     channel.BasicConsume(
         consumer: consumer,
         queue: replyQueueName,
         autoAck: true);
     // https://docs.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview
     return(respQueue.Take());
 }
Beispiel #7
0
 public void Close()
 {
     TestUtilitiesClass.WriteLine("RpcClient Close");
     connection.Close();
 }