From b091192f78afb9080d86297406ca26980c2cd737 Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Wed, 4 Sep 2024 15:09:21 +0800 Subject: [PATCH] add sdk to wedpr-transport --- cpp/cmake/TargetSettings.cmake | 3 + cpp/libinitializer/Initializer.cpp | 2 - cpp/ppc-framework/front/FrontConfig.h | 63 ++++---- cpp/ppc-framework/front/IFrontBuilder.h | 41 +++++ cpp/ppc-framework/protocol/EndPoint.h | 59 +++++++ cpp/ppc-framework/protocol/INodeInfo.h | 7 +- cpp/ppc-framework/protocol/RouteType.h | 4 +- cpp/ppc-main/gateway/GatewayServiceApp.cpp | 133 ---------------- cpp/ppc-main/gateway/GatewayServiceApp.h | 54 ------- cpp/ppc-main/pro-node/ProNodeServiceApp.cpp | 147 ------------------ cpp/ppc-main/pro-node/ProNodeServiceApp.h | 52 ------- .../ppc-tools/src/config/PPCConfig.cpp | 81 ---------- .../ppc-tools/src/config/PPCConfig.h | 47 ------ cpp/wedpr-protocol/grpc/client/FrontClient.h | 4 +- .../grpc/client/GatewayClient.h | 4 +- cpp/wedpr-protocol/grpc/client/GrpcClient.h | 5 +- cpp/wedpr-protocol/grpc/server/GrpcServer.cpp | 6 +- cpp/wedpr-protocol/grpc/server/GrpcServer.h | 6 +- cpp/wedpr-protocol/protobuf/NodeInfoImpl.h | 13 +- .../ppc-front/ppc-front/FrontFactory.cpp | 37 +++++ .../ppc-front/ppc-front/FrontFactory.h | 42 +++++ .../ppc-gateway/CMakeLists.txt | 6 +- cpp/wedpr-transport/sdk/CMakeLists.txt | 8 + cpp/wedpr-transport/sdk/ProTransportImpl.cpp | 43 +++++ cpp/wedpr-transport/sdk/ProTransportImpl.h | 48 ++++++ cpp/wedpr-transport/sdk/Transport.h | 38 +++++ cpp/wedpr-transport/sdk/TransportBuilder.h | 57 +++++++ cpp/wedpr-transport/sdk/TransportImpl.h | 46 ++++++ 28 files changed, 477 insertions(+), 579 deletions(-) create mode 100644 cpp/ppc-framework/front/IFrontBuilder.h create mode 100644 cpp/ppc-framework/protocol/EndPoint.h delete mode 100644 cpp/ppc-main/gateway/GatewayServiceApp.cpp delete mode 100644 cpp/ppc-main/gateway/GatewayServiceApp.h delete mode 100644 cpp/ppc-main/pro-node/ProNodeServiceApp.cpp delete mode 100644 cpp/ppc-main/pro-node/ProNodeServiceApp.h create mode 100644 cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.cpp create mode 100644 cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.h create mode 100644 cpp/wedpr-transport/sdk/CMakeLists.txt create mode 100644 cpp/wedpr-transport/sdk/ProTransportImpl.cpp create mode 100644 cpp/wedpr-transport/sdk/ProTransportImpl.h create mode 100644 cpp/wedpr-transport/sdk/Transport.h create mode 100644 cpp/wedpr-transport/sdk/TransportBuilder.h create mode 100644 cpp/wedpr-transport/sdk/TransportImpl.h diff --git a/cpp/cmake/TargetSettings.cmake b/cpp/cmake/TargetSettings.cmake index ffe57295..46735456 100644 --- a/cpp/cmake/TargetSettings.cmake +++ b/cpp/cmake/TargetSettings.cmake @@ -133,6 +133,9 @@ endif() set(BOOST_UNIT_TEST Boost::unit_test_framework) +# ==== the transport sdk=== +set(WEDPR_TRANSPORT_SDK_TARGET wedpr-transport-sdk) + # ==== the swig wrapper ===== set(WEDPR_PYTHON_TOOLKIT "wedpr_python_toolkit") set(WEDPR_PYTHON_TOOLKIT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TOOLKIT}) \ No newline at end of file diff --git a/cpp/libinitializer/Initializer.cpp b/cpp/libinitializer/Initializer.cpp index 99465a3a..cb5d68f7 100644 --- a/cpp/libinitializer/Initializer.cpp +++ b/cpp/libinitializer/Initializer.cpp @@ -89,8 +89,6 @@ void Initializer::init(ppc::protocol::NodeArch _arch) } init->fetchAgencyListPeriodically(); }); - // load the tars config - m_config->loadTarsConfig(m_configPath); } INIT_LOG(INFO) << LOG_DESC("init the frontService success") << LOG_KV("agency", m_config->agencyID()) << LOG_KV("arch", _arch); diff --git a/cpp/ppc-framework/front/FrontConfig.h b/cpp/ppc-framework/front/FrontConfig.h index e9775c79..6cee462a 100644 --- a/cpp/ppc-framework/front/FrontConfig.h +++ b/cpp/ppc-framework/front/FrontConfig.h @@ -19,65 +19,58 @@ */ #pragma once +#include "ppc-framework/protocol/EndPoint.h" #include #include #include namespace ppc::front { -/** - * @brief the gateway endpoint - * - */ -class GatewayEndPoint -{ -public: - GatewayEndPoint() = default; - GatewayEndPoint(std::string const& host, uint16_t port) : m_host(std::move(host)), m_port(port) - {} - virtual ~GatewayEndPoint() = default; - - virtual std::string const& host() const { return m_host; } - uint16_t port() const { return m_port; } - - void setHost(std::string host) { m_host = std::move(host); } - void setPort(uint16_t port) { m_port = port; } - -private: - // the host - std::string m_host; - // the port - uint16_t m_port; -}; - // Note: swig explosed interface class FrontConfig { public: using Ptr = std::shared_ptr; - FrontConfig(int threadPoolSize, std::string agencyID) - : m_threadPoolSize(threadPoolSize), m_agencyID(std::move(agencyID)) + FrontConfig(int threadPoolSize, std::string nodeID) + : m_threadPoolSize(threadPoolSize), m_nodeID(std::move(nodeID)) {} virtual ~FrontConfig() = default; virtual int threadPoolSize() const { return m_threadPoolSize; } - virtual std::string const agencyID() const { return m_agencyID; } - virtual std::vector const& gatewayInfo() const { return m_gatewayInfo; } - virtual void setGatewayInfo(std::vector gatewayInfo) + virtual std::string const& nodeID() const { return m_nodeID; } + virtual std::vector const& gatewayInfo() const + { + return m_gatewayInfo; + } + virtual void setGatewayInfo(std::vector gatewayInfo) { m_gatewayInfo = std::move(gatewayInfo); } - virtual void appendGatewayInfo(GatewayEndPoint&& endpoint) + virtual void appendGatewayInfo(ppc::protocol::EndPoint&& endpoint) { // TODO:check the endpoint m_gatewayInfo.push_back(endpoint); } + ppc::protocol::EndPoint const& selfEndPoint() const { return m_selfEndPoint; } + void setSelfEndPoint(ppc::protocol::EndPoint const& endPoint) { m_selfEndPoint = endPoint; } + + // TODO here + std::string gatewayEndPoints() { return ""; } + + std::string const& loadBalancePolicy() const { return m_loadBanlancePolicy; } + void setLoadBalancePolicy(std::string const& loadBanlancePolicy) + { + m_loadBanlancePolicy = loadBanlancePolicy; + } + private: + std::string m_loadBanlancePolicy = "round_robin"; + ppc::protocol::EndPoint m_selfEndPoint; int m_threadPoolSize; - std::string m_agencyID; - std::vector m_gatewayInfo; + std::string m_nodeID; + std::vector m_gatewayInfo; }; class FrontConfigBuilder @@ -87,9 +80,9 @@ class FrontConfigBuilder FrontConfigBuilder() = default; virtual ~FrontConfigBuilder() = default; - FrontConfig::Ptr build(int threadPoolSize, std::string agencyID) + FrontConfig::Ptr build(int threadPoolSize, std::string nodeID) { - return std::make_shared(threadPoolSize, agencyID); + return std::make_shared(threadPoolSize, nodeID); } }; } // namespace ppc::front \ No newline at end of file diff --git a/cpp/ppc-framework/front/IFrontBuilder.h b/cpp/ppc-framework/front/IFrontBuilder.h new file mode 100644 index 00000000..50423645 --- /dev/null +++ b/cpp/ppc-framework/front/IFrontBuilder.h @@ -0,0 +1,41 @@ +/** + * Copyright (C) 2023 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 IFront.h + * @author: yujiechen + * @date 2024-08-22 + */ +#pragma once +#include "IFront.h" + +namespace ppc::front +{ +class IFrontBuilder +{ +public: + using Ptr = std::shared_ptr; + IFrontBuilder() = default; + virtual ~IFrontBuilder() = default; + + /** + * @brief create the Front using specified config + * + * @param config the config used to build the Front + * @return IFront::Ptr he created Front + */ + virtual IFront::Ptr build(ppc::front::FrontConfig::Ptr config) const = 0; + virtual IFrontClient::Ptr buildClient(std::string endPoint) const = 0; +}; +} // namespace ppc::front \ No newline at end of file diff --git a/cpp/ppc-framework/protocol/EndPoint.h b/cpp/ppc-framework/protocol/EndPoint.h new file mode 100644 index 00000000..099b7de5 --- /dev/null +++ b/cpp/ppc-framework/protocol/EndPoint.h @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2023 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 EndPoint.h + * @author: yujiechen + * @date 2024-08-22 + */ + +#pragma once +#include +#include +#include + +namespace ppc::protocol +{ +/** + * @brief the endpoint + * + */ +class EndPoint +{ +public: + EndPoint() = default; + EndPoint(std::string const& host, uint16_t port) : m_host(std::move(host)), m_port(port) {} + virtual ~EndPoint() = default; + + virtual std::string const& host() const { return m_host; } + uint16_t port() const { return m_port; } + + void setHost(std::string host) { m_host = std::move(host); } + void setPort(uint16_t port) { m_port = port; } + + std::string entryPoint() const { return m_host + ":" + std::to_string(m_port); } + + std::string listenEndPoint() const { return m_listenIp + ":" + std::to_string(m_port); } + + std::string const& listenIp() const { return m_listenIp; } + +private: + // the listenIp + std::string m_listenIp = "0.0.0.0"; + // the host + std::string m_host; + // the port + uint16_t m_port; +}; +} // namespace ppc::protocol \ No newline at end of file diff --git a/cpp/ppc-framework/protocol/INodeInfo.h b/cpp/ppc-framework/protocol/INodeInfo.h index 76f460b2..680f0df9 100644 --- a/cpp/ppc-framework/protocol/INodeInfo.h +++ b/cpp/ppc-framework/protocol/INodeInfo.h @@ -62,14 +62,11 @@ class INodeInfoFactory { public: using Ptr = std::shared_ptr; - INodeInfoFactory(bcos::bytes nodeID) : m_nodeID(std::move(nodeID)) {} + INodeInfoFactory() = default; virtual ~INodeInfoFactory() = default; virtual INodeInfo::Ptr build() = 0; - virtual INodeInfo::Ptr build(std::string const& endPoint) = 0; - -protected: - bcos::bytes m_nodeID; + virtual INodeInfo::Ptr build(bcos::bytesConstRef nodeID, std::string const& endPoint) = 0; }; inline std::string printNodeInfo(INodeInfo::Ptr const& nodeInfo) diff --git a/cpp/ppc-framework/protocol/RouteType.h b/cpp/ppc-framework/protocol/RouteType.h index 1e3a715e..cb7feb96 100644 --- a/cpp/ppc-framework/protocol/RouteType.h +++ b/cpp/ppc-framework/protocol/RouteType.h @@ -19,8 +19,8 @@ */ #pragma once -#include #include +#include namespace ppc::protocol { @@ -51,4 +51,4 @@ inline std::ostream& operator<<(std::ostream& _out, RouteType const& _type) } return _out; } -} // namespace ppc::front \ No newline at end of file +} // namespace ppc::protocol \ No newline at end of file diff --git a/cpp/ppc-main/gateway/GatewayServiceApp.cpp b/cpp/ppc-main/gateway/GatewayServiceApp.cpp deleted file mode 100644 index 70dbf15f..00000000 --- a/cpp/ppc-main/gateway/GatewayServiceApp.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/** - * 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 GatewayServiceApp.cpp - * @author: shawnhe - * @date 2022-10-21 - */ -#include "GatewayServiceApp.h" -#include "ppc-storage/src/CacheStorageFactoryImpl.h" -#include "ppc-tars-protocol/Common.h" -#include "ppc-tars-service/GatewayService/GatewayInitializer.h" -#include "ppc-tars-service/GatewayService/GatewayServiceServer.h" -#include "ppc-tools/src/config/NetworkConfig.h" - -using namespace ppctars; -using namespace ppc; -using namespace ppc::tools; -using namespace ppc::gateway; -using namespace ppc::storage; -using namespace ppc::protocol; - -using namespace bcos; -using namespace bcos::boostssl; -using namespace bcos::boostssl::ws; - -void GatewayServiceApp::initialize() -{ - try - { - m_configPath = tars::ServerConfig::BasePath + "/config.ini"; - addConfig("config.ini"); - - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService") << LOG_KV("config", m_configPath); - initService(m_configPath); - - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService success") << LOG_KV("config", m_configPath); - - GatewayServiceParam param; - param.gateway = m_gatewayInitializer->gateway(); - param.ppcMsgFactory = m_gatewayInitializer->messageFactory(); - addServantWithParams( - getProxyDesc(GATEWAY_SERVANT_NAME), param); - } - catch (std::exception const& e) - { - std::cout << "init GatewayServiceApp failed, error: " << boost::diagnostic_information(e) - << std::endl; - throw e; - } -} - -void GatewayServiceApp::initService(std::string const& _configPath) -{ - boost::property_tree::ptree pt; - boost::property_tree::read_ini(_configPath, pt); - auto ppcConfig = std::make_shared(); - // init the log - m_logInitializer = std::make_shared(); - m_logInitializer->initLog(pt); - - // load the gatewayConfig - auto config = std::make_shared(); - config->loadGatewayConfig( - ppc::protocol::NodeArch::PRO, tars::ServerConfig::BasePath.c_str(), pt); - // add the config - auto const& networkConfig = config->gatewayConfig().networkConfig; - - if (!networkConfig.disableSsl) - { - GATEWAYAPP_LOG(INFO) << LOG_DESC("addConfig") << LOG_KV("enableSM", networkConfig.enableSM); - if (!networkConfig.enableSM) - { - addConfig(std::string(NetworkConfig::CA_CERT_NAME)); - addConfig(std::string(NetworkConfig::SSL_CERT_NAME)); - addConfig(std::string(NetworkConfig::SSL_KEY_NAME)); - } - else - { - addConfig(std::string(NetworkConfig::SM_CA_CERT_NAME)); - addConfig(std::string(NetworkConfig::SM_SSL_CERT_NAME)); - addConfig(std::string(NetworkConfig::SM_SSL_KEY_NAME)); - addConfig(std::string(NetworkConfig::SM_SSL_EN_KEY_NAME)); - addConfig(std::string(NetworkConfig::SM_SSL_EN_CERT_NAME)); - } - GATEWAYAPP_LOG(INFO) << LOG_DESC("addConfig success") - << LOG_KV("enableSM", networkConfig.enableSM); - } - - // redis cache - storage::CacheStorage::Ptr cache; - if (!config->gatewayConfig().disableCache) - { - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService: buildRedisStorage") - << config->gatewayConfig().cacheStorageConfig.desc(); - auto cacheStorageFactory = std::make_shared(); - cache = cacheStorageFactory->createCacheStorage(config->gatewayConfig().cacheStorageConfig); - try - { - cache->exists("check_cache"); - } - catch (std::exception& e) - { - BOOST_THROW_EXCEPTION( - InvalidConfig() << errinfo_comment( - "init cache error:" + std::string(boost::diagnostic_information(e)))); - } - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService: buildRedisStorage success"); - } - - // message factory - auto messageFactory = std::make_shared(); - // global thread pool - auto threadPoolSize = config->gatewayConfig().networkConfig.threadPoolSize; - auto threadPool = std::make_shared(GATEWAY_THREAD_POOL_MODULE, threadPoolSize); - - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService: build and start gateway"); - m_gatewayInitializer = std::make_shared( - NodeArch::PRO, config, cache, messageFactory, threadPool); - m_gatewayInitializer->start(); - GATEWAYAPP_LOG(INFO) << LOG_DESC("initService: build and start gateway success"); -} \ No newline at end of file diff --git a/cpp/ppc-main/gateway/GatewayServiceApp.h b/cpp/ppc-main/gateway/GatewayServiceApp.h deleted file mode 100644 index 7c462f2e..00000000 --- a/cpp/ppc-main/gateway/GatewayServiceApp.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 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 GatewayServiceApp.h - * @author: shawnhe - * @date 2022-10-21 - */ -#pragma once -#include "ppc-framework/protocol/Protocol.h" -#include "ppc-tars-protocol/Common.h" -#include "ppc-tars-service/GatewayService/GatewayInitializer.h" -#include -#include - -#define GATEWAYAPP_LOG(LEVEL) BCOS_LOG(LEVEL) << "[GatewayServiceApp]" - -namespace ppctars -{ -class GatewayServiceApp : public tars::Application -{ -public: - GatewayServiceApp() {} - ~GatewayServiceApp() override{}; - - void destroyApp() override - { - if (m_gatewayInitializer) - { - m_gatewayInitializer->stop(); - } - } - void initialize() override; - -protected: - virtual void initService(std::string const& _configPath); - -private: - std::string m_configPath; - bcos::BoostLogInitializer::Ptr m_logInitializer; - ppctars::GatewayInitializer::Ptr m_gatewayInitializer; -}; -} // namespace ppctars \ No newline at end of file diff --git a/cpp/ppc-main/pro-node/ProNodeServiceApp.cpp b/cpp/ppc-main/pro-node/ProNodeServiceApp.cpp deleted file mode 100644 index 4199d163..00000000 --- a/cpp/ppc-main/pro-node/ProNodeServiceApp.cpp +++ /dev/null @@ -1,147 +0,0 @@ -/** - * 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 ProNodeServiceApp.cpp - * @author: yujiechen - * @date 2022-11-25 - */ -#include "ProNodeServiceApp.h" -#include "GatewayService.h" -#include "ppc-rpc/src/RpcFactory.h" -#include "ppc-rpc/src/RpcMemory.h" -#include "ppc-tars-protocol/Common.h" -#include "ppc-tars-protocol/client/GatewayServiceClient.h" -#include "ppc-tars-service/FrontService/FrontServiceServer.h" - -using namespace ppctars; -using namespace ppc::initializer; -using namespace ppc::rpc; -using namespace ppc::tools; -using namespace ppc::protocol; - -void ProNodeServiceApp::destroyApp() -{ - NodeService_LOG(INFO) << LOG_DESC("destroyApp"); - if (m_rpc) - { - m_rpc->stop(); - } - if (m_nodeInitializer) - { - m_nodeInitializer->stop(); - } - NodeService_LOG(INFO) << LOG_DESC("destroyApp success"); -} - -void ProNodeServiceApp::initialize() -{ - try - { - m_configPath = tars::ServerConfig::BasePath + "/config.ini"; - addConfig("config.ini"); - - NodeService_LOG(INFO) << LOG_DESC("initService") << LOG_KV("config", m_configPath); - initService(m_configPath); - NodeService_LOG(INFO) << LOG_DESC("initService success") << LOG_KV("config", m_configPath); - } - catch (std::exception const& e) - { - // since the tars will not print the detailed information when exceptioned, we print here - std::cout << "init NodeServiceApp failed, error: " << boost::diagnostic_information(e) - << std::endl; - throw e; - } -} - -void ProNodeServiceApp::initService(std::string const& _configPath) -{ - // init the log - boost::property_tree::ptree pt; - boost::property_tree::read_ini(_configPath, pt); - auto ppcConfig = std::make_shared(); - ppcConfig->loadSelfTarsEndpoint(pt); - // init the log - m_logInitializer = std::make_shared(); - m_logInitializer->initLog(pt); - - // init the nodeInitializer - addConfig(std::string(NODE_PEM_NAME)); - NodeService_LOG(INFO) << LOG_DESC("initService: init the node"); - m_nodeInitializer = std::make_shared(_configPath); - auto privateKeyPath = tars::ServerConfig::BasePath + "/" + std::string(NODE_PEM_NAME); - NodeService_LOG(INFO) << LOG_DESC("generate the node private key path: ") << privateKeyPath; - m_nodeInitializer->config()->setPrivateKeyPath(privateKeyPath); - - m_nodeInitializer->init(ppc::protocol::NodeArch::PRO); - NodeService_LOG(INFO) << LOG_DESC("initService: init the node success"); - - auto config = m_nodeInitializer->config(); - - // init the gateway - auto gatewayServiceName = config->gatewayServiceName(); - auto endPoints = config->getServiceEndPointsByName(gatewayServiceName); - NodeService_LOG(INFO) << LOG_DESC("initGateway") << LOG_KV("serviceName", gatewayServiceName) - << LOG_KV("endPointSize", endPoints.size()); - auto gatewayPrx = - createServantProxy(true, gatewayServiceName, toTarsEndPoints(endPoints)); - auto gateway = std::make_shared( - gatewayServiceName, gatewayPrx, config->holdingMessageMinutes()); - NodeService_LOG(INFO) << LOG_DESC("initGateway success"); - - // addservant for the front-service - auto frontInitializer = m_nodeInitializer->frontInitializer(); - FrontServiceParam frontParam{frontInitializer}; - addServantWithParams( - getProxyDesc(ppc::protocol::FRONT_SERVANT_NAME), frontParam); - // get the endpoint for front-service-object - auto ret = - getEndPointDescByAdapter(this, ppc::protocol::FRONT_SERVANT_NAME, ppcConfig->endpoint()); - if (!ret.first) - { - throw std::runtime_error("get load endpoint for front-service-object information failed"); - } - - std::string selfEndPoint = ret.second; - NodeService_LOG(INFO) << LOG_DESC("get local-endpoint for the front-service-object success") - << LOG_KV("endPoint", selfEndPoint); - frontInitializer->front()->setSelfEndPoint(selfEndPoint); - // set the gateway into front - m_nodeInitializer->frontInitializer()->front()->setGatewayInterface(gateway); - - // init the rpc - NodeService_LOG(INFO) << LOG_DESC("init the rpc"); - // load the rpc config - // not specify the certPath in air-mode - config->loadRpcConfig(nullptr, pt); - // init RpcStatusInterface - RpcStatusInterface::Ptr rpcStatusInterface = std::make_shared(gateway); - m_nodeInitializer->frontInitializer()->setRpcStatus(rpcStatusInterface); - auto rpcFactory = std::make_shared(config->agencyID()); - m_rpc = rpcFactory->buildRpc(config); - m_rpc->setRpcStorage(rpcStatusInterface); - m_rpc->setBsEcdhPSI(m_nodeInitializer->bsEcdhPsi()); - - m_nodeInitializer->registerRpcHandler(m_rpc); - NodeService_LOG(INFO) << LOG_DESC("init the rpc success"); - - // start the node - NodeService_LOG(INFO) << LOG_DESC("start the node"); - m_nodeInitializer->start(); - NodeService_LOG(INFO) << LOG_DESC("start the node success"); - - NodeService_LOG(INFO) << LOG_DESC("start the rpc"); - m_rpc->start(); - NodeService_LOG(INFO) << LOG_DESC("start the rpc success"); -} \ No newline at end of file diff --git a/cpp/ppc-main/pro-node/ProNodeServiceApp.h b/cpp/ppc-main/pro-node/ProNodeServiceApp.h deleted file mode 100644 index 13883c62..00000000 --- a/cpp/ppc-main/pro-node/ProNodeServiceApp.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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 ProNodeServiceApp.h - * @author: yujiechen - * @date 2022-11-25 - */ -#pragma once -#include "libinitializer/Initializer.h" -#include -#include - -#define NodeService_LOG(LEVEL) BCOS_LOG(LEVEL) << "[NodeServiceApp]" - -namespace ppc::rpc -{ -class Rpc; -} -namespace ppctars -{ -class ProNodeServiceApp : public tars::Application -{ -public: - ProNodeServiceApp() {} - ~ProNodeServiceApp() override{}; - - void destroyApp() override; - void initialize() override; - -protected: - virtual void initService(std::string const& _configPath); - -private: - std::string m_configPath; - bcos::BoostLogInitializer::Ptr m_logInitializer; - ppc::initializer::Initializer::Ptr m_nodeInitializer; - // TODO: rpc support pro-mode - std::shared_ptr m_rpc; -}; -} // namespace ppctars \ No newline at end of file diff --git a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.cpp b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.cpp index ace6cee6..f644bd6d 100644 --- a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.cpp +++ b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.cpp @@ -527,84 +527,3 @@ void PPCConfig::loadMPCConfig(boost::property_tree::ptree const& _pt) m_mpcConfig.readPerBatchLines = _pt.get("mpc.read_per_batch_lines", 100000); loadHDFSConfig(_pt); } - -//////// the tars config -void PPCConfig::loadTarsConfig(boost::property_tree::ptree const& _pt) -{ - PPCConfig_LOG(INFO) << LOG_DESC("loadTarsConfig"); - // load the service name - std::string gatewayServiceConfigSection = "tars_gateway"; - m_gatewayServiceName = - getServiceName(_pt, gatewayServiceConfigSection + ".name", GATEWAY_SERVANT_NAME); - // load the holding_msg_minutes - m_holdingMessageMinutes = - loadHoldingMessageMinutes(_pt, gatewayServiceConfigSection + ".holding_msg_minutes"); - // config the gateway information - loadServiceTarsConfig(_pt, m_gatewayServiceName, gatewayServiceConfigSection); - - PPCConfig_LOG(INFO) << LOG_DESC("loadTarsConfig success") - << LOG_KV("gatewayService", m_gatewayServiceName) - << LOG_KV("endPoints", m_serviceToEndPoints.at(m_gatewayServiceName).size()) - << LOG_KV("holdingMessageMinutes", m_holdingMessageMinutes); -} - -void PPCConfig::loadServiceTarsConfig(boost::property_tree::ptree const& _pt, - std::string const& _serviceName, std::string const& _sectionName) -{ - PPCConfig_LOG(INFO) << LOG_DESC("loadServiceTarsConfig for ") << _sectionName - << LOG_KV("service", _serviceName); - if (!_pt.get_child_optional(_sectionName)) - { - BOOST_THROW_EXCEPTION(InvalidConfig() << errinfo_comment("Must config " + _serviceName)); - } - for (auto const& it : _pt.get_child(_sectionName)) - { - if (it.first.find("proxy.") != 0) - { - continue; - } - std::string endpoint = it.second.data(); - PPCConfig_LOG(INFO) << LOG_BADGE("loadServiceTarsConfig") - << LOG_KV("serviceName", _serviceName) << LOG_KV("endpoint", endpoint); - if (!checkEndpoint(endpoint)) - { - BOOST_THROW_EXCEPTION( - InvalidConfig() << errinfo_comment("Invalid endpoint: " + endpoint)); - } - m_serviceToEndPoints[_serviceName].emplace_back(std::move(endpoint)); - } -} - -std::string PPCConfig::getServiceName(boost::property_tree::ptree const& _pt, - std::string const& _configSection, std::string const& _objName) -{ - auto serviceName = _pt.get(_configSection, ""); - checkService(_configSection, serviceName); - return getPrxDesc(serviceName, _objName); -} - -void PPCConfig::checkService(std::string const& _serviceType, std::string const& _serviceName) -{ - if (_serviceName.empty()) - { - BOOST_THROW_EXCEPTION( - InvalidConfig() << errinfo_comment("Must set service name for " + _serviceType + "!")); - } - std::vector serviceNameList; - boost::split(serviceNameList, _serviceName, boost::is_any_of(".")); - std::string errorMsg = - "Must set service name in format of application_name.server_name with only include letters " - "and numbers for " + - _serviceType + ", invalid config now is:" + _serviceName; - if (serviceNameList.size() != 2) - { - BOOST_THROW_EXCEPTION(InvalidConfig() << errinfo_comment(errorMsg)); - } - for (const auto& serviceName : serviceNameList) - { - if (!isalNumStr(serviceName)) - { - BOOST_THROW_EXCEPTION(InvalidConfig() << errinfo_comment(errorMsg)); - } - } -} diff --git a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h index 3bd29cfa..76aac8b0 100644 --- a/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h +++ b/cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h @@ -178,13 +178,6 @@ class PPCConfig loadGatewayConfig(_arch, _certPath, iniConfig); } - virtual void loadTarsConfig(std::string const& _configPath) - { - boost::property_tree::ptree iniConfig; - boost::property_tree::read_ini(_configPath, iniConfig); - loadTarsConfig(iniConfig); - } - virtual void loadRpcConfig(const char* _certPath, boost::property_tree::ptree const& _pt) { // rpc default disable-ssl @@ -192,11 +185,6 @@ class PPCConfig m_rpcConfig, _certPath, _pt, "rpc", NetworkConfig::DefaultRpcListenPort, true); } - virtual void loadSelfTarsEndpoint(boost::property_tree::ptree const& _pt) - { - m_endpoint = _pt.get("agency.endpoint", ""); - } - virtual void loadGatewayConfig(ppc::protocol::NodeArch _arch, const char* _certPath, boost::property_tree::ptree const& _pt); @@ -208,8 +196,6 @@ class PPCConfig virtual void loadMPCConfig(boost::property_tree::ptree const& _pt); - virtual void loadTarsConfig(boost::property_tree::ptree const& _pt); - NetworkConfig const& rpcConfig() const { return m_rpcConfig; } // the gateway-config GatewayConfig const& gatewayConfig() const { return m_gatewayConfig; } @@ -229,19 +215,6 @@ class PPCConfig uint32_t const& taskTimeoutMinutes() const { return m_taskTimeoutMinutes; } uint32_t const& threadPoolSize() const { return m_threadPoolSize; } - std::string const& gatewayServiceName() const { return m_gatewayServiceName; } - std::vector getServiceEndPointsByName(std::string const& _service) - { - // Note: not ensure thread-safe, since only use when init - std::vector endPoints; - auto it = m_serviceToEndPoints.find(_service); - if (it != m_serviceToEndPoints.end()) - { - return it->second; - } - return endPoints; - } - EcdhPSIParam const& ecdhPSIConfig() const { return m_ecdhPSIConfig; } EcdhPSIParam& mutableEcdhPSIConfig() { return m_ecdhPSIConfig; } @@ -297,22 +270,6 @@ class PPCConfig void initRedisConfigForGateway( ppc::storage::CacheStorageConfig& _redisConfig, const boost::property_tree::ptree& _pt); - - // load the tars-config for the given service, e.g: - /* - [tars_gateway] - name = agencyAGateway - proxy.0 = "192.168.0.1:3000" - proxy.0 = "192.168.0.2:3002" - proxy.0 = "192.168.0.3:3003" - */ - virtual void loadServiceTarsConfig(boost::property_tree::ptree const& _pt, - std::string const& _serviceName, std::string const& _sectionName); - - std::string getServiceName(boost::property_tree::ptree const& _pt, - std::string const& _configSection, std::string const& _objName); - void checkService(std::string const& _serviceType, std::string const& _serviceName); - int64_t getDataBatchSize(std::string const& _section, int64_t _dataBatchSize); int loadHoldingMessageMinutes( @@ -352,10 +309,6 @@ class PPCConfig bcos::bytes m_privateKey; std::string m_privateKeyPath; - // the tars config - std::map> m_serviceToEndPoints; - std::string m_gatewayServiceName; - bool m_disableRA2018 = false; std::string m_endpoint; diff --git a/cpp/wedpr-protocol/grpc/client/FrontClient.h b/cpp/wedpr-protocol/grpc/client/FrontClient.h index 4f40b4bb..29e1a21a 100644 --- a/cpp/wedpr-protocol/grpc/client/FrontClient.h +++ b/cpp/wedpr-protocol/grpc/client/FrontClient.h @@ -27,7 +27,9 @@ class FrontClient : public ppc::front::IFrontClient, public GrpcClient { public: using Ptr = std::shared_ptr; - FrontClient(std::shared_ptr channel) : GrpcClient(std::move(channel)) {} + FrontClient(grpc::ChannelArguments const& channelConfig, std::string const& endPoints) + : GrpcClient(channelConfig, endPoints) + {} ~FrontClient() override = default; void onReceiveMessage( diff --git a/cpp/wedpr-protocol/grpc/client/GatewayClient.h b/cpp/wedpr-protocol/grpc/client/GatewayClient.h index a6635ccc..9a75a52b 100644 --- a/cpp/wedpr-protocol/grpc/client/GatewayClient.h +++ b/cpp/wedpr-protocol/grpc/client/GatewayClient.h @@ -27,8 +27,8 @@ class GatewayClient : public ppc::gateway::IGateway, public GrpcClient { public: using Ptr = std::shared_ptr; - GatewayClient(std::shared_ptr channel) - : GrpcClient(std::move(channel)), m_stub(ppc::proto::Gateway::NewStub(m_channel)) + GatewayClient(grpc::ChannelArguments const& channelConfig, std::string const& endPoints) + : GrpcClient(channelConfig, endPoints), m_stub(ppc::proto::Gateway::NewStub(m_channel)) {} ~GatewayClient() override = default; diff --git a/cpp/wedpr-protocol/grpc/client/GrpcClient.h b/cpp/wedpr-protocol/grpc/client/GrpcClient.h index 028718a9..86e44fb5 100644 --- a/cpp/wedpr-protocol/grpc/client/GrpcClient.h +++ b/cpp/wedpr-protocol/grpc/client/GrpcClient.h @@ -28,7 +28,10 @@ class GrpcClient { public: using Ptr = std::shared_ptr; - GrpcClient(std::shared_ptr channel) : m_channel(std::move(channel)) {} + GrpcClient(grpc::ChannelArguments const& channelConfig, std::string const& endPoints) + : m_channel( + grpc::CreateCustomChannel(endPoints, grpc::InsecureChannelCredentials(), channelConfig)) + {} virtual ~GrpcClient() = default; diff --git a/cpp/wedpr-protocol/grpc/server/GrpcServer.cpp b/cpp/wedpr-protocol/grpc/server/GrpcServer.cpp index 9d612dfa..8ad659b9 100644 --- a/cpp/wedpr-protocol/grpc/server/GrpcServer.cpp +++ b/cpp/wedpr-protocol/grpc/server/GrpcServer.cpp @@ -29,14 +29,14 @@ void GrpcServer::start() if (m_running) { GRPC_SERVER_LOG(INFO) << LOG_DESC("GrpcServer has already been started!") - << LOG_KV("endPoint", m_config.endPoint()); + << LOG_KV("listenEndPoint", m_config.listenEndPoint()); return; } m_running = true; grpc::reflection::InitProtoReflectionServerBuilderPlugin(); grpc::ServerBuilder builder; // without authentication - builder.AddListeningPort(m_config.endPoint(), grpc::InsecureServerCredentials()); + builder.AddListeningPort(m_config.listenEndPoint(), grpc::InsecureServerCredentials()); // register the service for (auto const& service : m_bindingServices) { @@ -44,7 +44,7 @@ void GrpcServer::start() } m_server = std::unique_ptr(builder.BuildAndStart()); GRPC_SERVER_LOG(INFO) << LOG_DESC("GrpcServer start success!") - << LOG_KV("endPoint", m_config.endPoint()); + << LOG_KV("listenEndPoint", m_config.listenEndPoint()); } void GrpcServer::stop() diff --git a/cpp/wedpr-protocol/grpc/server/GrpcServer.h b/cpp/wedpr-protocol/grpc/server/GrpcServer.h index 869dcc88..f5ac678e 100644 --- a/cpp/wedpr-protocol/grpc/server/GrpcServer.h +++ b/cpp/wedpr-protocol/grpc/server/GrpcServer.h @@ -18,6 +18,7 @@ * @date 2024-09-03 */ #pragma once +#include "ppc-framework/protocol/EndPoint.h" #include #include #include @@ -27,10 +28,9 @@ namespace ppc::protocol // refer to: https://grpc.io/docs/languages/cpp/callback/ struct GrpcServerConfig { - std::string listenIp; - int listenPort; + ppc::protocol::EndPoint endPoint; - std::string endPoint() const { return listenIp + ":" + std::to_string(listenPort); } + std::string listenEndPoint() const { return endPoint.listenEndPoint(); } }; class GrpcServer { diff --git a/cpp/wedpr-protocol/protobuf/NodeInfoImpl.h b/cpp/wedpr-protocol/protobuf/NodeInfoImpl.h index b0164a58..f5733ade 100644 --- a/cpp/wedpr-protocol/protobuf/NodeInfoImpl.h +++ b/cpp/wedpr-protocol/protobuf/NodeInfoImpl.h @@ -30,12 +30,13 @@ class NodeInfoImpl : public INodeInfo using Ptr = std::shared_ptr; explicit NodeInfoImpl(std::function inner) : m_inner(std::move(inner)) {} + NodeInfoImpl() : m_inner([inner = ppc::proto::NodeInfo()]() mutable { return &inner; }) {} - NodeInfoImpl(bcos::bytesConstRef const& nodeID) - : m_inner([inner = ppc::proto::NodeInfo()]() mutable { return &inner; }) + NodeInfoImpl(bcos::bytesConstRef const& nodeID) : NodeInfoImpl() { m_inner()->set_nodeid(nodeID.data(), nodeID.size()); } + NodeInfoImpl(bcos::bytesConstRef const& nodeID, std::string const& endPoint) : NodeInfoImpl(nodeID) { @@ -83,15 +84,15 @@ class NodeInfoFactory : public INodeInfoFactory { public: using Ptr = std::shared_ptr; - NodeInfoFactory(bcos::bytesConstRef const& nodeID) : INodeInfoFactory(nodeID.toBytes()) {} + NodeInfoFactory() {} ~NodeInfoFactory() override {} - INodeInfo::Ptr build() override { return std::make_shared(bcos::ref(m_nodeID)); } + INodeInfo::Ptr build() override { return std::make_shared(); } - INodeInfo::Ptr build(std::string const& endPoint) override + INodeInfo::Ptr build(bcos::bytesConstRef nodeID, std::string const& endPoint) override { - return std::make_shared(bcos::ref(m_nodeID), endPoint); + return std::make_shared(nodeID, endPoint); } }; } // namespace ppc::protocol \ No newline at end of file diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.cpp b/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.cpp new file mode 100644 index 00000000..23b121d5 --- /dev/null +++ b/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.cpp @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2023 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 FrontFactory.cpp + * @author: yujiechen + * @date 2024-9-04 + */ +#include "FrontFactory.h" +#include "FrontImpl.h" + +using namespace ppc::front; +using namespace ppc::tools; + +IFront::Ptr build(ppc::protocol::INodeInfoFactory::Ptr nodeInfoFactory, + ppc::protocol::MessagePayloadBuilder::Ptr messageFactory, + ppc::protocol::MessageOptionalHeaderBuilder::Ptr routerInfoBuilder, + ppc::gateway::IGateway::Ptr const& gateway, FrontConfig::Ptr config) +{ + auto threadPool = std::make_shared("front", config->threadPoolSize()); + auto nodeInfo = nodeInfoFactory->build( + bcos::bytesConstRef((bcos::byte*)config->nodeID().data(), config->nodeID().size()), + config->selfEndPoint().entryPoint()); + return std::make_shared(threadPool, nodeInfo, messageFactory, routerInfoBuilder, + gateway, std::make_shared()); +} \ No newline at end of file diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.h b/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.h new file mode 100644 index 00000000..e383725c --- /dev/null +++ b/cpp/wedpr-transport/ppc-front/ppc-front/FrontFactory.h @@ -0,0 +1,42 @@ +/** + * Copyright (C) 2023 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 FrontFactory.h + * @author: yujiechen + * @date 2024-9-04 + */ +#pragma once +#include "ppc-framework/front/IFront.h" +#include "ppc-tools/src/config/PPCConfig.h" +#include "ppc-framework/front/FrontConfig.h" +#include "ppc-framework/protocol/INodeInfo.h" +#include "ppc-framework/gateway/IGateway.h" + +namespace ppc::front +{ +class FrontFactory +{ +public: + using Ptr = std::shared_ptr; + FrontFactory() = default; + virtual ~FrontFactory() = default; + + IFront::Ptr build(ppc::protocol::INodeInfoFactory::Ptr nodeInfoFactory, + ppc::protocol::MessagePayloadBuilder::Ptr messageFactory, + ppc::protocol::MessageOptionalHeaderBuilder::Ptr routerInfoBuilder, + ppc::gateway::IGateway::Ptr const& gateway, + FrontConfig::Ptr config); +}; +} \ No newline at end of file diff --git a/cpp/wedpr-transport/ppc-gateway/CMakeLists.txt b/cpp/wedpr-transport/ppc-gateway/CMakeLists.txt index 05c438e2..2d4b7364 100644 --- a/cpp/wedpr-transport/ppc-gateway/CMakeLists.txt +++ b/cpp/wedpr-transport/ppc-gateway/CMakeLists.txt @@ -7,16 +7,12 @@ find_package(Boost COMPONENTS filesystem) file(GLOB_RECURSE SRCS ppc-gateway/*.cpp) -find_package(tarscpp REQUIRED) add_library(${GATEWAY_TARGET} ${SRCS}) target_link_libraries(${GATEWAY_TARGET} PUBLIC ${TOOLS_TARGET} jsoncpp_static Boost::filesystem ${BCOS_BOOSTSSL_TARGET} ${BCOS_UTILITIES_TARGET} ${HTTP_TARGET} ${PROTOCOL_TARGET} - ${TARS_PROTOCOL_TARGET} ${PB_PROTOCOL_TARGET} tarscpp::tarsservant tarscpp::tarsutil TBB::tbb) + ${TARS_PROTOCOL_TARGET} ${PB_PROTOCOL_TARGET} TBB::tbb) -if (APPLE) - # target_compile_options(${GATEWAY_TARGET} PRIVATE -faligned-allocation) -endif () # ut if (TESTS) diff --git a/cpp/wedpr-transport/sdk/CMakeLists.txt b/cpp/wedpr-transport/sdk/CMakeLists.txt new file mode 100644 index 00000000..1211d63a --- /dev/null +++ b/cpp/wedpr-transport/sdk/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.14) +project(ppc-transport-sdk VERSION ${VERSION}) + +file(GLOB_RECURSE SRCS *.cpp) + +add_library(${WEDPR_TRANSPORT_SDK_TARGET} ${SRCS}) +target_link_libraries(${WEDPR_TRANSPORT_SDK_TARGET} PUBLIC + ${FRONT_TARGET} ${SERVICE_CLIENT_TARGET} ${SERVICE_SERVER_TARGET}) \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.cpp b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp new file mode 100644 index 00000000..223d4c69 --- /dev/null +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2023 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 ProTransportImpl.cpp + * @author: yujiechen + * @date 2024-09-04 + */ +#include "ProTransportImpl.h" +#include "protobuf/src/v1/MessageImpl.h" +#include "wedpr-protocol/grpc/server/FrontServer.h" + +using namespace ppc::front; +using namespace ppc::protocol; +ProTransportImpl::ProTransportImpl(ppc::Front::FrontConfig::Ptr config) + : m_config(std::move(config)) +{ + GrpcServerConfig{config->selfEndPoint()}; + m_server = std::make_shared(GrpcServerConfig); + + FrontFactory frontFactory; + grpc::ChannelArguments channelConfig; + channelConfig.SetLoadBalancingPolicyName(m_config->loadBalancePolicy()); + auto gateway = std::make_shared(channelConfig, m_config->gatewayEndPoints()); + m_front = frontFactory.build(std::make_shared(), + std::make_shared(), + std::make_shared(), gateway, config); + auto frontService = std::make_shared(msgBuilder, m_front); + + // register the frontService + m_server->registerService(frontService); +} \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.h b/cpp/wedpr-transport/sdk/ProTransportImpl.h new file mode 100644 index 00000000..628fed53 --- /dev/null +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.h @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2023 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 ProTransportImpl.h + * @author: yujiechen + * @date 2024-09-04 + */ +#pragma once + +#include "TransportImpl.h" +#include "wedpr-protocol/grpc/client/GatewayClient.h" +#include "wedpr-protocol/grpc/server/GrpcServer.h" + +namespace ppc::sdk +{ +class ProTransportImpl : public Transport +{ +public: + using Ptr = std::shared_ptr; + ProTransportImpl(ppc::Front::FrontConfig::Ptr config); + + void start() override + { + m_server->start(); + m_front->start(); + } + void stop() override + { + m_server->stop(); + m_front->stop(); + } + +protected: + ppc::protocol::GrpcServer::Ptr m_server; +}; +} // namespace ppc::sdk \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/Transport.h b/cpp/wedpr-transport/sdk/Transport.h new file mode 100644 index 00000000..eddb414f --- /dev/null +++ b/cpp/wedpr-transport/sdk/Transport.h @@ -0,0 +1,38 @@ +/** + * Copyright (C) 2023 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 Transport.h + * @author: yujiechen + * @date 2024-09-04 + */ +#pragma once +#include "ppc-framework/front/IFront.h" +namespace ppc::sdk +{ +class Transport +{ +public: + Transport() = default; + virtual ~Transport() = default; + + virtual void start() { m_front->start(); } + virtual void stop() { m_front->stop(); } + + virtual ppc::front::IFront::Ptr const& getFront() { return m_front; } + +protected: + ppc::front::IFront::Ptr m_front; +}; +} // namespace ppc::sdk \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/TransportBuilder.h b/cpp/wedpr-transport/sdk/TransportBuilder.h new file mode 100644 index 00000000..a113310e --- /dev/null +++ b/cpp/wedpr-transport/sdk/TransportBuilder.h @@ -0,0 +1,57 @@ +/** + * Copyright (C) 2023 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 TransportBuilder.h + * @author: yujiechen + * @date 2024-09-04 + */ +#pragma once +#include "ProTransportImpl.h" +#include "Transport.h" +#include "TransportImpl.h" +#include +namespace ppc::sdk +{ +enum class SDKMode : uint8_t +{ + AIR = 0x00, + PRO = 0x01, +}; +class TransportBuilder +{ +public: + using Ptr = std::shared_ptr; + TransportBuilder() = default; + virtual ~TransportBuilder() = default; + + Transport::Ptr build(SDKMode mode, ppc::Front::FrontConfig::Ptr config, + ppc::gateway::IGateway::Ptr const& gateway) + { + switch (mode) + { + case SDKMode::AIR: + { + return std::make_shared(config, gateway); + } + case SDKMode::PRO: + { + return std::make_shared(config); + } + default: + throw std::exception("Unsupported sdk mode, only support AIR/PRO mode!"); + } + } +}; +} // namespace ppc::sdk \ No newline at end of file diff --git a/cpp/wedpr-transport/sdk/TransportImpl.h b/cpp/wedpr-transport/sdk/TransportImpl.h new file mode 100644 index 00000000..47222e4b --- /dev/null +++ b/cpp/wedpr-transport/sdk/TransportImpl.h @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2023 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 TransportImpl.h + * @author: yujiechen + * @date 2024-09-04 + */ +#pragma once +#include "Transport.h" +#include "ppc-framework/gateway/IGateway.h" +#include "ppc-front/FrontFactory.h" +#include "protobuf/NodeInfoImpl.h" +#include "protocol/src/v1/MessageHeaderImpl.h" +#include "protocol/src/v1/MessagePayloadImpl.h" + +namespace ppc::sdk +{ +class TransportImpl : public Transport +{ +public: + TransportImpl(ppc::Front::FrontConfig::Ptr config, ppc::gateway::IGateway::Ptr const& gateway) + : m_config(std::move(config)) + { + FrontFactory frontFactory; + m_front = frontFactory.build(std::make_shared(), + std::make_shared(), + std::make_shared(), gateway, m_config); + } + ~TransportImpl() override = default; + +protected: + ppc::Front::FrontConfig::Ptr m_config; +}; +} // namespace ppc::sdk