|
1 | 1 | using System; |
2 | 2 | using RabbitMQ.Client; |
3 | 3 | using RabbitMQ.Client.Events; |
| 4 | +using System.Text; |
4 | 5 |
|
5 | | -class RPCServer { |
6 | | - public static void Main() { |
7 | | - ConnectionFactory factory = new ConnectionFactory(); |
8 | | - factory.HostName = "localhost"; |
9 | | - using (IConnection connection = factory.CreateConnection()) |
10 | | - using (IModel channel = connection.CreateModel()) { |
11 | | - channel.QueueDeclare("rpc_queue", false, false, false, null); |
12 | | - channel.BasicQos(0, 1, false); |
13 | | - QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel); |
14 | | - channel.BasicConsume("rpc_queue", false, consumer); |
15 | | - Console.WriteLine(" [x] Awaiting RPC requests"); |
| 6 | +class RPCServer |
| 7 | +{ |
| 8 | + public static void Main() |
| 9 | + { |
| 10 | + var factory = new ConnectionFactory() { HostName = "localhost" }; |
| 11 | + using (var connection = factory.CreateConnection()) |
| 12 | + { |
| 13 | + using (var channel = connection.CreateModel()) |
| 14 | + { |
| 15 | + channel.QueueDeclare("rpc_queue", false, false, false, null); |
| 16 | + channel.BasicQos(0, 1, false); |
| 17 | + var consumer = new QueueingBasicConsumer(channel); |
| 18 | + channel.BasicConsume("rpc_queue", false, consumer); |
| 19 | + Console.WriteLine(" [x] Awaiting RPC requests"); |
16 | 20 |
|
17 | | - while(true) { |
18 | | - string response = null; |
19 | | - BasicDeliverEventArgs ea = |
20 | | - (BasicDeliverEventArgs)consumer.Queue.Dequeue(); |
| 21 | + while (true) |
| 22 | + { |
| 23 | + string response = null; |
| 24 | + var ea = |
| 25 | + (BasicDeliverEventArgs)consumer.Queue.Dequeue(); |
21 | 26 |
|
22 | | - byte[] body = ea.Body; |
23 | | - IBasicProperties props = ea.BasicProperties; |
24 | | - IBasicProperties replyProps = channel.CreateBasicProperties(); |
25 | | - replyProps.CorrelationId = props.CorrelationId; |
| 27 | + var body = ea.Body; |
| 28 | + var props = ea.BasicProperties; |
| 29 | + var replyProps = channel.CreateBasicProperties(); |
| 30 | + replyProps.CorrelationId = props.CorrelationId; |
26 | 31 |
|
27 | | - try { |
28 | | - string message = System.Text.Encoding.UTF8.GetString(body); |
29 | | - int n = int.Parse(message); |
30 | | - Console.WriteLine(" [.] fib({0})", message); |
31 | | - response = fib(n).ToString(); |
32 | | - } catch (Exception e) { |
33 | | - Console.WriteLine(" [.] " + e); |
34 | | - response = ""; |
35 | | - } finally { |
36 | | - byte[] responseBytes = |
37 | | - System.Text.Encoding.UTF8.GetBytes(response); |
38 | | - channel.BasicPublish("", props.ReplyTo, replyProps, |
39 | | - responseBytes); |
40 | | - channel.BasicAck(ea.DeliveryTag, false); |
| 32 | + try |
| 33 | + { |
| 34 | + var message = Encoding.UTF8.GetString(body); |
| 35 | + int n = int.Parse(message); |
| 36 | + Console.WriteLine(" [.] fib({0})", message); |
| 37 | + response = fib(n).ToString(); |
| 38 | + } |
| 39 | + catch (Exception e) |
| 40 | + { |
| 41 | + Console.WriteLine(" [.] " + e.Message); |
| 42 | + response = ""; |
| 43 | + } |
| 44 | + finally |
| 45 | + { |
| 46 | + var responseBytes = |
| 47 | + Encoding.UTF8.GetBytes(response); |
| 48 | + channel.BasicPublish("", props.ReplyTo, replyProps, |
| 49 | + responseBytes); |
| 50 | + channel.BasicAck(ea.DeliveryTag, false); |
| 51 | + } |
41 | 52 | } |
42 | 53 | } |
43 | 54 | } |
44 | 55 | } |
45 | 56 |
|
46 | | - private static int fib(int n) { |
| 57 | + private static int fib(int n) |
| 58 | + { |
47 | 59 | if (n == 0 || n == 1) return n; |
48 | 60 | return fib(n - 1) + fib(n - 2); |
49 | 61 | } |
|
0 commit comments