#include <greeter_service.hpp>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include <samples/greeter_client.usrv.pb.hpp>
#include <samples/greeter_service.usrv.pb.hpp>
namespace samples {
GreeterService::GreeterService(std::string prefix) : prefix_(std::move(prefix)) {}
GreeterService::SayHelloResult GreeterService::SayHello(CallContext& , api::GreetingRequest&& request) {
api::GreetingResponse response;
response.set_greeting(fmt::format("{}, {}!", prefix_, request.name()));
return response;
}
GreeterService::SayHelloResponseStreamResult GreeterService::SayHelloResponseStream(
CallContext& ,
api::GreetingRequest&& request,
SayHelloResponseStreamWriter& writer
) {
std::string message = fmt::format("{}, {}", prefix_, request.name());
api::GreetingResponse response;
constexpr auto kCountSend = 5;
for (auto i = 0; i < kCountSend; ++i) {
message.push_back('!');
response.set_greeting(grpc::string(message));
writer.Write(response);
}
return grpc::Status::OK;
}
GreeterService::SayHelloRequestStreamResult
GreeterService::SayHelloRequestStream(CallContext& , SayHelloRequestStreamReader& reader) {
std::string message{};
api::GreetingRequest request;
while (reader.Read(request)) {
message.append(request.name());
}
api::GreetingResponse response;
response.set_greeting(fmt::format("{}, {}", prefix_, message));
return response;
}
GreeterService::SayHelloStreamsResult
GreeterService::SayHelloStreams(CallContext& , SayHelloStreamsReaderWriter& stream) {
std::string message;
api::GreetingRequest request;
api::GreetingResponse response;
while (stream.Read(request)) {
message.append(request.name());
response.set_greeting(fmt::format("{}, {}", prefix_, message));
stream.Write(response);
}
return grpc::Status::OK;
}
GreeterServiceComponent::GreeterServiceComponent(
)
RegisterService(service_);
}
return yaml_config::MergeSchemas<ugrpc::server::ServiceComponentBase>(R"(
type: object
description: gRPC sample greater service component
additionalProperties: false
properties:
greeting-prefix:
type: string
description: greeting prefix
)");
}
}