Reformatted and cleaned up
This commit is contained in:
parent
1e524f6fb0
commit
8d16f8dba6
@ -1,49 +1,61 @@
|
|||||||
using System;
|
using System;
|
||||||
using RabbitMQ.Client;
|
using RabbitMQ.Client;
|
||||||
using RabbitMQ.Client.Events;
|
using RabbitMQ.Client.Events;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
class RPCServer {
|
class RPCServer
|
||||||
public static void Main() {
|
{
|
||||||
ConnectionFactory factory = new ConnectionFactory();
|
public static void Main()
|
||||||
factory.HostName = "localhost";
|
{
|
||||||
using (IConnection connection = factory.CreateConnection())
|
var factory = new ConnectionFactory() { HostName = "localhost" };
|
||||||
using (IModel channel = connection.CreateModel()) {
|
using (var connection = factory.CreateConnection())
|
||||||
channel.QueueDeclare("rpc_queue", false, false, false, null);
|
{
|
||||||
channel.BasicQos(0, 1, false);
|
using (var channel = connection.CreateModel())
|
||||||
QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
|
{
|
||||||
channel.BasicConsume("rpc_queue", false, consumer);
|
channel.QueueDeclare("rpc_queue", false, false, false, null);
|
||||||
Console.WriteLine(" [x] Awaiting RPC requests");
|
channel.BasicQos(0, 1, false);
|
||||||
|
var consumer = new QueueingBasicConsumer(channel);
|
||||||
|
channel.BasicConsume("rpc_queue", false, consumer);
|
||||||
|
Console.WriteLine(" [x] Awaiting RPC requests");
|
||||||
|
|
||||||
while(true) {
|
while (true)
|
||||||
string response = null;
|
{
|
||||||
BasicDeliverEventArgs ea =
|
string response = null;
|
||||||
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
|
var ea =
|
||||||
|
(BasicDeliverEventArgs)consumer.Queue.Dequeue();
|
||||||
|
|
||||||
byte[] body = ea.Body;
|
var body = ea.Body;
|
||||||
IBasicProperties props = ea.BasicProperties;
|
var props = ea.BasicProperties;
|
||||||
IBasicProperties replyProps = channel.CreateBasicProperties();
|
var replyProps = channel.CreateBasicProperties();
|
||||||
replyProps.CorrelationId = props.CorrelationId;
|
replyProps.CorrelationId = props.CorrelationId;
|
||||||
|
|
||||||
try {
|
try
|
||||||
string message = System.Text.Encoding.UTF8.GetString(body);
|
{
|
||||||
int n = int.Parse(message);
|
var message = Encoding.UTF8.GetString(body);
|
||||||
Console.WriteLine(" [.] fib({0})", message);
|
int n = int.Parse(message);
|
||||||
response = fib(n).ToString();
|
Console.WriteLine(" [.] fib({0})", message);
|
||||||
} catch (Exception e) {
|
response = fib(n).ToString();
|
||||||
Console.WriteLine(" [.] " + e);
|
}
|
||||||
response = "";
|
catch (Exception e)
|
||||||
} finally {
|
{
|
||||||
byte[] responseBytes =
|
Console.WriteLine(" [.] " + e.Message);
|
||||||
System.Text.Encoding.UTF8.GetBytes(response);
|
response = "";
|
||||||
channel.BasicPublish("", props.ReplyTo, replyProps,
|
}
|
||||||
responseBytes);
|
finally
|
||||||
channel.BasicAck(ea.DeliveryTag, false);
|
{
|
||||||
|
var responseBytes =
|
||||||
|
Encoding.UTF8.GetBytes(response);
|
||||||
|
channel.BasicPublish("", props.ReplyTo, replyProps,
|
||||||
|
responseBytes);
|
||||||
|
channel.BasicAck(ea.DeliveryTag, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int fib(int n) {
|
private static int fib(int n)
|
||||||
|
{
|
||||||
if (n == 0 || n == 1) return n;
|
if (n == 0 || n == 1) return n;
|
||||||
return fib(n - 1) + fib(n - 2);
|
return fib(n - 1) + fib(n - 2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user