-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataProvider.cpp
40 lines (33 loc) · 1.84 KB
/
dataProvider.cpp
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
#include "dataProvider.hpp"
#include "ole/v1/dataProvider.grpc.pb.h"
#include <agrpc/asioGrpc.hpp>
#include <boost/asio/bind_executor.hpp>
#include <grpcpp/server_builder.h>
#include <string_view>
void run_data_provider(std::string_view host)
{
namespace asio = boost::asio;
grpc::ServerBuilder builder;
std::unique_ptr<grpc::Server> server;
ole::v1::DataProvider::AsyncService service;
agrpc::GrpcContext grpc_context{builder.AddCompletionQueue()};
builder.AddListeningPort(std::string{host}, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
server = builder.BuildAndStart();
agrpc::repeatedly_request(&ole::v1::DataProvider::AsyncService::RequestGet, service,
asio::bind_executor(grpc_context,
[&](auto&, auto& reader_writer) -> asio::awaitable<void>
{
ole::v1::Request request;
ole::v1::Data data;
bool write_ok{true};
while (write_ok && co_await agrpc::read(reader_writer, request))
{
data.set_id(request.id());
data.set_data(request.input());
write_ok = co_await agrpc::write(reader_writer, data);
}
co_await agrpc::finish(reader_writer, grpc::Status::OK);
}));
grpc_context.run();
}