-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathrpc_server.cpp
More file actions
130 lines (111 loc) · 3.98 KB
/
Copy pathrpc_server.cpp
File metadata and controls
130 lines (111 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Tutorial 6: Remote Procedure Call, server
//
// Consumes fib(n) requests from the `rpc_queue` queue and publishes each result
// back to the queue named in the request's reply-to property, echoing the
// correlation id so the client can match the reply to its request.
#include <rmqa_consumer.h>
#include <rmqa_producer.h>
#include <rmqa_rabbitcontext.h>
#include <rmqa_topology.h>
#include <rmqa_vhost.h>
#include <rmqp_messageguard.h>
#include <rmqp_producer.h>
#include <rmqt_confirmresponse.h>
#include <rmqt_consumerconfig.h>
#include <rmqt_fieldvalue.h>
#include <rmqt_message.h>
#include <rmqt_plaincredentials.h>
#include <rmqt_properties.h>
#include <rmqt_result.h>
#include <rmqt_simpleendpoint.h>
#include <bslmt_semaphore.h>
#include <bsl_memory.h>
#include <bsl_string.h>
#include <bsl_vector.h>
#include <iostream>
#include <string>
using namespace BloombergLP;
namespace {
long long fib(int n)
{
long long a = 0;
long long b = 1;
for (int i = 0; i < n; ++i) {
long long next = a + b;
a = b;
b = next;
}
return a;
}
} // namespace
int main()
{
rmqa::RabbitContext rabbit;
bsl::shared_ptr<rmqa::VHost> vhost = rabbit.createVHostConnection(
"tutorial-six rpc server",
bsl::make_shared<rmqt::SimpleEndpoint>("localhost", "/"),
bsl::make_shared<rmqt::PlainCredentials>("guest", "guest"));
rmqa::Topology topology;
rmqt::FieldTable quorum;
quorum["x-queue-type"] = rmqt::FieldValue(bsl::string("quorum"));
rmqt::QueueHandle rpcQueue = topology.addQueue(
"rpc_queue", rmqt::AutoDelete::OFF, rmqt::Durable::ON, quorum);
// Replies are published through the default exchange, routed by the
// reply-to queue name each client provides.
rmqt::Result<rmqa::Producer> publisherResult = vhost->createProducer(
topology, topology.defaultExchange(), /* maxOutstandingConfirms */ 10);
if (!publisherResult) {
std::cerr << "Failed to create publisher: " << publisherResult.error()
<< "\n";
return 1;
}
auto publisher = publisherResult.value();
rmqt::Result<rmqa::Consumer> consumerResult = vhost->createConsumer(
topology,
rpcQueue,
[publisher](rmqp::MessageGuard& guard) {
const rmqt::Message& request = guard.message();
const rmqt::Properties& props = request.properties();
if (props.replyTo.isNull()) {
guard.ack();
return;
}
std::string body(reinterpret_cast<const char*>(request.payload()),
request.payloadSize());
int n = 0;
try {
n = std::stoi(body);
}
catch (const std::exception&) {
// An unhandled exception in this callback would terminate the
// server, so reject requests that aren't an integer.
std::cerr << " [!] Ignoring unparseable request" << std::endl;
guard.nack(/* requeue */ false);
return;
}
std::cout << " [.] fib(" << n << ")" << std::endl;
const std::string answer = std::to_string(fib(n));
rmqt::Message reply(bsl::make_shared<bsl::vector<uint8_t> >(
answer.begin(), answer.end()));
reply.properties().correlationId = props.correlationId;
publisher->send(
reply,
props.replyTo.value(),
[](const rmqt::Message&,
const bsl::string&,
const rmqt::ConfirmResponse&) {});
guard.ack();
},
rmqt::ConsumerConfig()
.setConsumerTag("tutorial-six rpc server")
.setPrefetchCount(1));
if (!consumerResult) {
std::cerr << "Failed to create consumer: " << consumerResult.error()
<< "\n";
return 1;
}
std::cout << " [x] Awaiting RPC requests" << std::endl;
bslmt::Semaphore stop;
stop.wait();
return 0;
}