From 836adead5ad7d6ffdd4e1db870d55f6b57dddcb8 Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Thu, 5 Sep 2024 00:42:37 +0800 Subject: [PATCH] add gateway initializer --- cpp/ppc-framework/protocol/GrpcConfig.h | 7 ++ .../ppc-tools/src/config/PPCConfig.h | 2 + cpp/wedpr-main/CMakeLists.txt | 2 +- cpp/wedpr-main/gateway/CMakeLists.txt | 5 +- cpp/wedpr-main/gateway/GatewayInitializer.cpp | 83 +++++++++++++++++++ cpp/wedpr-main/gateway/GatewayInitializer.h | 49 +++++++++++ cpp/wedpr-main/gateway/main.cpp | 34 ++------ cpp/wedpr-protocol/grpc/server/GrpcServer.h | 8 +- 8 files changed, 153 insertions(+), 37 deletions(-) create mode 100644 cpp/wedpr-main/gateway/GatewayInitializer.cpp create mode 100644 cpp/wedpr-main/gateway/GatewayInitializer.h diff --git a/cpp/ppc-framework/protocol/GrpcConfig.h b/cpp/ppc-framework/protocol/GrpcConfig.h index f4b78814..76b2ac17 100644 --- a/cpp/ppc-framework/protocol/GrpcConfig.h +++ b/cpp/ppc-framework/protocol/GrpcConfig.h @@ -18,11 +18,18 @@ * @date 2024-09-02 */ #pragma once +#include "ppc-framework/protocol/EndPoint.h" #include #include namespace ppc::protocol { +struct GrpcServerConfig +{ + ppc::protocol::EndPoint endPoint; + + std::string listenEndPoint() const { return endPoint.listenEndPoint(); } +}; class GrpcConfig { public: diff --git a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h index 2e695a89..f5b70e75 100644 --- a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h +++ b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h @@ -23,6 +23,7 @@ #include "NetworkConfig.h" #include "ParamChecker.h" #include "ppc-framework/front/FrontConfig.h" +#include "ppc-framework/protocol/GrpcConfig.h" #include "ppc-framework/storage/CacheStorage.h" #include #include @@ -99,6 +100,7 @@ struct GatewayConfig bool disableCache; NetworkConfig networkConfig; + ppc::protocol::GrpcServerConfig grpcConfig; ppc::storage::CacheStorageConfig cacheStorageConfig; std::string nodeFileName; std::string nodePath; diff --git a/cpp/wedpr-main/CMakeLists.txt b/cpp/wedpr-main/CMakeLists.txt index ba80ab5f..44e9a218 100644 --- a/cpp/wedpr-main/CMakeLists.txt +++ b/cpp/wedpr-main/CMakeLists.txt @@ -3,7 +3,7 @@ project(ppc-main VERSION ${VERSION}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_subdirectory(air-node) add_subdirectory(pro-node) -#TODO: ppc-gateway +add_subdirectory(gateway) if (BUILD_CEM) add_subdirectory(cem-node) diff --git a/cpp/wedpr-main/gateway/CMakeLists.txt b/cpp/wedpr-main/gateway/CMakeLists.txt index 8406c395..062d9c66 100644 --- a/cpp/wedpr-main/gateway/CMakeLists.txt +++ b/cpp/wedpr-main/gateway/CMakeLists.txt @@ -1,12 +1,11 @@ cmake_minimum_required(VERSION 3.14) -project(ppctars-Gateway) +project(wedpr-gateway) include_directories(${CMAKE_SOURCE_DIR}) aux_source_directory(./ SRC_LIST) -aux_source_directory(../../ppc-tars-service/GatewayService SRC_LIST) add_executable(${GATEWAY_BINARY_NAME} ${SRC_LIST}) -target_link_libraries(${GATEWAY_BINARY_NAME} ${GATEWAY_TARGET} ${STORAGE_TARGET} ${PROTOCOL_TARGET} ${HELPER_TARGET}) \ No newline at end of file +target_link_libraries(${GATEWAY_BINARY_NAME} ${GATEWAY_TARGET} ${STORAGE_TARGET} ${SERVICE_CLIENT_TARGET} ${SERVICE_SERVER_TARGET} ${HELPER_TARGET}) \ No newline at end of file diff --git a/cpp/wedpr-main/gateway/GatewayInitializer.cpp b/cpp/wedpr-main/gateway/GatewayInitializer.cpp new file mode 100644 index 00000000..ca1c25c7 --- /dev/null +++ b/cpp/wedpr-main/gateway/GatewayInitializer.cpp @@ -0,0 +1,83 @@ +/** + * Copyright (C) 2022 WeDPR. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file GatewayInitializer.cpp + * @author: yujiechen + * @date 2022-11-14 + */ +#include "GatewayInitializer.h" +#include "grpc/server/GatewayServer.h" +#include "grpc/server/GrpcServer.h" +#include "ppc-gateway/GatewayFactory.h" +#include "ppc-tools/src/config/PPCConfig.h" +#include "protobuf/NodeInfoImpl.h" +#include "wedpr-protocol/grpc/client/RemoteFrontBuilder.h" +#include "wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h" + +using namespace ppc::tools; +using namespace ppc::protocol; +using namespace bcos; +using namespace ppc::gateway; +using namespace ppc::front; + +void GatewayInitializer::init(std::string const& _configPath) +{ + // init the log + boost::property_tree::ptree pt; + boost::property_tree::read_ini(_configPath, pt); + + m_logInitializer = std::make_shared(); + m_logInitializer->initLog(pt); + INIT_LOG(INFO) << LOG_DESC("initLog success"); + + INIT_LOG(INFO) << LOG_DESC("initGateway: ") << _configPath; + auto config = std::make_shared(); + + config->loadGatewayConfig(ppc::protocol::NodeArch::PRO, nullptr, _configPath); + auto threadPool = std::make_shared( + "gateway", config->gatewayConfig().networkConfig.threadPoolSize); + + GatewayFactory gatewayFactory(config); + m_gateway = gatewayFactory.build(std::make_shared(config->grpcConfig())); + + m_server = std::make_shared(config->gatewayConfig().grpcConfig); + // register the gateway service + auto gatewayService = std::make_shared(m_gateway, + std::make_shared(), std::make_shared()); + m_server->registerService(gatewayService); +} + +void GatewayInitializer::start() +{ + if (m_gateway) + { + m_gateway->start(); + } + if (m_server) + { + m_server->start(); + } +} +void GatewayInitializer::stop() +{ + if (m_server) + { + m_server->stop(); + } + if (m_gateway) + { + m_gateway->start(); + } +} \ No newline at end of file diff --git a/cpp/wedpr-main/gateway/GatewayInitializer.h b/cpp/wedpr-main/gateway/GatewayInitializer.h new file mode 100644 index 00000000..7cd15a01 --- /dev/null +++ b/cpp/wedpr-main/gateway/GatewayInitializer.h @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2022 WeDPR. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file GatewayInitializer.h + * @author: yujiechen + * @date 2022-11-14 + */ +#pragma once +#include "ppc-framework/gateway/IGateway.h" +#include +#include +#include + +#define INIT_LOG(LEVEL) BCOS_LOG(LEVEL) << LOG_BADGE("GATEWAYInit") +namespace ppc::protocol +{ +class GrpcServer; +} +namespace ppc::gateway +{ +class GatewayInitializer +{ +public: + using Ptr = std::shared_ptr; + GatewayInitializer() = default; + virtual ~GatewayInitializer() { stop(); } + + virtual void init(std::string const& _configPath); + virtual void start(); + virtual void stop(); + +protected: + bcos::BoostLogInitializer::Ptr m_logInitializer; + ppc::gateway::IGateway::Ptr m_gateway; + std::shared_ptr m_server; +}; +} // namespace ppc::gateway \ No newline at end of file diff --git a/cpp/wedpr-main/gateway/main.cpp b/cpp/wedpr-main/gateway/main.cpp index 0212cf82..3f309bbd 100644 --- a/cpp/wedpr-main/gateway/main.cpp +++ b/cpp/wedpr-main/gateway/main.cpp @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2022 WeDPR. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -15,32 +15,14 @@ * * @file main.cpp * @author: yujiechen - * @date 2022-11-25 + * @date 2022-11-14 */ -#include "GatewayServiceApp.h" -#include "libhelper/CommandHelper.h" +#include "GatewayInitializer.h" +#include "wedpr-main/common/NodeStarter.h" -using namespace ppctars; - -int main(int argc, char* argv[]) +using namespace ppc::node; +int main(int argc, const char* argv[]) { - try - { - ppc::initAppCommandLine(argc, argv); - GatewayServiceApp app; - app.main(argc, argv); - app.waitForShutdown(); - - return 0; - } - catch (std::exception& e) - { - cerr << "ppc-gateway-service std::exception:" << boost::diagnostic_information(e) - << std::endl; - } - catch (...) - { - cerr << "ppc-gateway-service unknown exception." << std::endl; - } - return -1; + auto initializer = std::make_shared(); + startProgram(argc, argv, "ppc-gateway-service", initializer); } \ No newline at end of file diff --git a/cpp/wedpr-protocol/grpc/server/GrpcServer.h b/cpp/wedpr-protocol/grpc/server/GrpcServer.h index f5ac678e..71bb6156 100644 --- a/cpp/wedpr-protocol/grpc/server/GrpcServer.h +++ b/cpp/wedpr-protocol/grpc/server/GrpcServer.h @@ -18,7 +18,7 @@ * @date 2024-09-03 */ #pragma once -#include "ppc-framework/protocol/EndPoint.h" +#include "ppc-framework/protocol/GrpcConfig.h" #include #include #include @@ -26,12 +26,6 @@ namespace ppc::protocol { // refer to: https://grpc.io/docs/languages/cpp/callback/ -struct GrpcServerConfig -{ - ppc::protocol::EndPoint endPoint; - - std::string listenEndPoint() const { return endPoint.listenEndPoint(); } -}; class GrpcServer { public: