diff options
author | Tatiana Borisova <tatiana.borisova@qt.io> | 2023-01-13 16:31:42 +0100 |
---|---|---|
committer | Tatiana Borisova <tatiana.borisova@qt.io> | 2023-02-22 15:48:21 +0100 |
commit | 6ae02a93124b358efa45c386b40c483717c5e6b7 (patch) | |
tree | ddb4f88eb9ca6c919a33082dd66ad5e85100e7ea /examples/grpc/magic8ball/grpc_server_example | |
parent | 68cc8297e9d5627715d98169e4c03a3bf3072150 (diff) |
The example of Qt gRPC-Client part with qml based UI
Task-number: QTBUG-109598
Change-Id: I670f779fb9a85d02ad69a54dc6adaa50f52a7a71
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
(cherry picked from commit 681d104f0d03f97681536d3691d170d7c06aab2b)
Diffstat (limited to 'examples/grpc/magic8ball/grpc_server_example')
4 files changed, 191 insertions, 0 deletions
diff --git a/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt b/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt new file mode 100644 index 0000000..aa7f917 --- /dev/null +++ b/examples/grpc/magic8ball/grpc_server_example/CMakeLists.txt @@ -0,0 +1,82 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +# Qt6::Grpc module is not used directly in this project. But this allows to find Qt6::Grpc's +# dependencies without setting extra cmake module paths. +find_package(Qt6 COMPONENTS Grpc) +find_package(WrapgRPCPlugin) +find_package(WrapgRPC) + +if(NOT TARGET WrapgRPC::WrapgRPCPlugin OR NOT TARGET WrapProtoc::WrapProtoc + OR NOT TARGET WrapgRPC::WrapLibgRPC) + message(WARNING "Dependencies of QtGrpc test server not found. Skipping.") + return() +endif() + +set(proto_files "${CMAKE_CURRENT_LIST_DIR}/../proto/exampleservice.proto") +set(out_dir ${CMAKE_CURRENT_BINARY_DIR}) + +set(generated_files + "${out_dir}/exampleservice.pb.h" "${out_dir}/exampleservice.pb.cc" + "${out_dir}/exampleservice.grpc.pb.h" "${out_dir}/exampleservice.grpc.pb.cc") + +add_custom_command( + OUTPUT ${generated_files} + COMMAND + $<TARGET_FILE:WrapProtoc::WrapProtoc> + ARGS + --grpc_out "${out_dir}" + --cpp_out "${out_dir}" + -I "${CMAKE_CURRENT_LIST_DIR}/../proto/" + --plugin=protoc-gen-grpc=$<TARGET_FILE:WrapgRPC::WrapgRPCPlugin> + "${proto_files}" + WORKING_DIRECTORY ${out_dir} + DEPENDS "${proto_files}" + COMMENT "Generating gRPC ${target} sources..." + COMMAND_EXPAND_LISTS + VERBATIM +) + +set_source_files_properties(${generated_files} PROPERTIES GENERATED TRUE) +add_library(ServerRunner_grpc_gen STATIC ${generated_files}) +target_include_directories(ServerRunner_grpc_gen + PRIVATE + ${out_dir} + WrapgRPC_INCLUDE_PATH +) + +target_link_libraries(ServerRunner_grpc_gen + PRIVATE + WrapProtobuf::WrapLibProtobuf + WrapgRPC::WrapLibgRPC +) + +add_library(MagicServerRunner + STATIC + serverrunner.cpp + serverrunner.h +) + +target_include_directories(MagicServerRunner PRIVATE ${out_dir}) + +target_link_libraries(MagicServerRunner + PRIVATE + ServerRunner_grpc_gen + WrapgRPC::WrapLibgRPC + Qt6::Core +) + +qt_add_executable(SimpleGrpcServer + main.cpp +) + +target_link_libraries(SimpleGrpcServer PRIVATE + Qt6::Core + MagicServerRunner +) + +install(TARGETS SimpleGrpcServer + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) diff --git a/examples/grpc/magic8ball/grpc_server_example/main.cpp b/examples/grpc/magic8ball/grpc_server_example/main.cpp new file mode 100644 index 0000000..3cd5cd3 --- /dev/null +++ b/examples/grpc/magic8ball/grpc_server_example/main.cpp @@ -0,0 +1,15 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "serverrunner.h" + +#include <QCoreApplication> +#include <memory> + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + auto server = std::make_unique<ExampleServer>(); + server->run(); + return a.exec(); +} diff --git a/examples/grpc/magic8ball/grpc_server_example/serverrunner.cpp b/examples/grpc/magic8ball/grpc_server_example/serverrunner.cpp new file mode 100644 index 0000000..0588c83 --- /dev/null +++ b/examples/grpc/magic8ball/grpc_server_example/serverrunner.cpp @@ -0,0 +1,81 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include "serverrunner.h" +#include "exampleservice.grpc.pb.h" + +#include <QThread> +#include <QDebug> +#include <QRandomGenerator> + +#include <grpc++/grpc++.h> +#include <memory> +#include <array> +#include <random> + +namespace { + +using grpc::Server; +using grpc::ServerBuilder; +using grpc::ServerContext; +using grpc::ServerWriter; +using grpc::Status; +using qtgrpc::examples::AnswerRequest; +using qtgrpc::examples::AnswerResponse; +using qtgrpc::examples::ExampleService; + +static const std::array<std::string_view, 10> answers = {"Yes", + "Yep", + "Most\nlikely", + "It is\ncertain", + "No", + "Nope", + "Try later", + "Are you\nsure?", + "Maybe", + "Very\ndoubtful"}; + +// Generates random index value. +static int generateRandomIndex() +{ + static std::uniform_int_distribution<int> dist(0, answers.size() - 1); + return dist(*QRandomGenerator::global()); +} + +// Logic and data behind the server's behavior. +class ExampleServiceServiceImpl final : public qtgrpc::examples::ExampleService::Service +{ + grpc::Status answerMethod(grpc::ServerContext *, const AnswerRequest *request, + AnswerResponse *response) override; +}; +} + +Status ExampleServiceServiceImpl::answerMethod(grpc::ServerContext *, + const AnswerRequest *request, + AnswerResponse *response) +{ + if (request->message() == "sleep") + QThread::msleep(2000); + + response->set_message(std::string(answers[generateRandomIndex()])); + return Status(); +} + +void ExampleServer::run() +{ + std::string serverUri("127.0.0.1:50051"); + ExampleServiceServiceImpl service; + + grpc::ServerBuilder builder; + builder.AddListeningPort(serverUri, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + + std::unique_ptr<grpc::Server> server(builder.BuildAndStart()); + if (!server) { + qDebug() << "Creating grpc::Server failed."; + return; + } + + qDebug() << "Server listening on " << serverUri; + server->Wait(); +} diff --git a/examples/grpc/magic8ball/grpc_server_example/serverrunner.h b/examples/grpc/magic8ball/grpc_server_example/serverrunner.h new file mode 100644 index 0000000..50c00c8 --- /dev/null +++ b/examples/grpc/magic8ball/grpc_server_example/serverrunner.h @@ -0,0 +1,13 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#ifndef SERVER_RUNNER_H +#define SERVER_RUNNER_H + +class ExampleServer +{ +public: + void run(); +}; + +#endif // SERVER_RUNNER_H |