From e1f92d2910f1c0c88ecb668b75c6ee3917a72042 Mon Sep 17 00:00:00 2001 From: cyjseagull Date: Wed, 11 Sep 2024 21:16:32 +0800 Subject: [PATCH] update java-sdk wrapper --- cpp/cmake/TargetSettings.cmake | 5 +- cpp/ppc-framework/front/FrontConfig.h | 2 + cpp/ppc-framework/front/IFront.h | 119 +++++++++++++++++- cpp/ppc-framework/protocol/Message.h | 5 + .../ppc-front/ppc-front/Front.cpp | 2 +- .../ppc-front/ppc-front/FrontImpl.cpp | 9 +- .../ppc-front/ppc-front/FrontImpl.h | 4 +- .../sdk-wrapper/java/CMakeLists.txt | 9 +- .../sdk-wrapper/java/bindings/build.gradle | 61 +++++---- .../wedpr/sdk/jni/common/JniLibLoader.java | 8 +- .../java/swig/wedpr_java_transport.i | 108 ++++++++++++++-- cpp/wedpr-transport/sdk/ProTransportImpl.cpp | 12 +- cpp/wedpr-transport/sdk/ProTransportImpl.h | 2 +- cpp/wedpr-transport/sdk/Transport.cpp | 33 +++++ cpp/wedpr-transport/sdk/Transport.h | 30 ++++- cpp/wedpr-transport/sdk/TransportImpl.h | 13 +- 16 files changed, 349 insertions(+), 73 deletions(-) create mode 100644 cpp/wedpr-transport/sdk/Transport.cpp diff --git a/cpp/cmake/TargetSettings.cmake b/cpp/cmake/TargetSettings.cmake index 1b30601a..47573b12 100644 --- a/cpp/cmake/TargetSettings.cmake +++ b/cpp/cmake/TargetSettings.cmake @@ -140,5 +140,6 @@ set(WEDPR_TRANSPORT_SDK_TARGET wedpr-transport-sdk) set(WEDPR_PYTHON_TRANSPORT "wedpr_python_transport") set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT}) -set(WEDPR_JAVA_TRANSPORT "wedpr_java_transport") -set(WEDPR_JAVA_TRANSPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/java/${WEDPR_JAVA_TRANSPORT}) \ No newline at end of file +set(WEDPR_JAVA_TRANSPORT "wedpr_java_transport_jni") +set(WEDPR_JAVA_TRANSPORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/java/generated/${WEDPR_JAVA_TRANSPORT}) +set(WEDPR_JAVA_TRANSPORT_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/wedpr-transport/sdk-wrapper/java/bindings/src/main/resources/META-INF/native) diff --git a/cpp/ppc-framework/front/FrontConfig.h b/cpp/ppc-framework/front/FrontConfig.h index aa1938da..8af6ca05 100644 --- a/cpp/ppc-framework/front/FrontConfig.h +++ b/cpp/ppc-framework/front/FrontConfig.h @@ -73,6 +73,8 @@ class FrontConfig virtual std::vector const& getComponents() const { return m_components; } void setComponents(std::vector const& components) { m_components = components; } + void addComponent(std::string const& component) { m_components.emplace_back(component); } + std::vector& mutableComponents() { return m_components; } protected: diff --git a/cpp/ppc-framework/front/IFront.h b/cpp/ppc-framework/front/IFront.h index 76cdbcad..726733f3 100644 --- a/cpp/ppc-framework/front/IFront.h +++ b/cpp/ppc-framework/front/IFront.h @@ -41,6 +41,59 @@ class IFrontClient virtual void onReceiveMessage( ppc::protocol::Message::Ptr const& _msg, ppc::protocol::ReceiveMsgFunc _callback) = 0; }; + +///////// the callback definition for sdk wrapper ///////// +class ErrorCallback +{ +public: + using Ptr = std::shared_ptr; + ErrorCallback() = default; + virtual ~ErrorCallback() {} + + virtual void onError(bcos::Error::Ptr error) = 0; +}; + +class MessageDispatcherHandler +{ +public: + using Ptr = std::shared_ptr; + MessageDispatcherHandler() = default; + virtual ~MessageDispatcherHandler() {} + + virtual void onMessage(ppc::protocol::Message::Ptr msg) = 0; +}; + +class SendResponseHandler +{ +public: + using Ptr = std::shared_ptr; + SendResponseHandler(ppc::protocol::SendResponseFunction responseFunc) + : m_responseFunc(responseFunc) + {} + virtual ~SendResponseHandler() {} + + virtual void sendResponse(std::shared_ptr&& payload) + { + m_responseFunc(std::move(payload)); + } + +private: + ppc::protocol::SendResponseFunction m_responseFunc; +}; + +class IMessageHandler +{ +public: + using Ptr = std::shared_ptr; + IMessageHandler() = default; + virtual ~IMessageHandler() {} + + virtual void onMessage(bcos::Error::Ptr e, ppc::protocol::Message::Ptr msg, + SendResponseHandler sendResponseHandler) = 0; +}; + +///////// the callback definition for sdk wrapper ///////// + class IFront : virtual public IFrontClient { public: @@ -71,8 +124,21 @@ class IFront : virtual public IFrontClient virtual void registerTopicHandler( std::string const& topic, ppc::protocol::MessageDispatcherCallback callback) = 0; + virtual void registerTopicHandler( + std::string const& topic, MessageDispatcherHandler::Ptr callback) + { + registerTopicHandler(topic, populateMessageDispatcherCallback(callback)); + } + virtual void registerMessageHandler( std::string const& componentType, ppc::protocol::MessageDispatcherCallback callback) = 0; + + virtual void registerMessageHandler( + std::string const& componentType, MessageDispatcherHandler::Ptr callback) + { + registerMessageHandler(componentType, populateMessageDispatcherCallback(callback)); + } + /** * @brief async send message * @@ -88,16 +154,31 @@ class IFront : virtual public IFrontClient * @param timeout timeout * @param callback callback */ - virtual void asyncSendMessage(ppc::protocol::RouteType routeType, + virtual void asyncSendMessage(uint16_t routeType, ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout, ppc::protocol::ReceiveMsgFunc errorCallback, ppc::protocol::MessageCallback callback) = 0; + virtual void asyncSendMessage(uint16_t routeType, + ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, + long timeout, ErrorCallback::Ptr errorCallback, IMessageHandler::Ptr msgHandler) + { + asyncSendMessage(routeType, routeInfo, std::move(payload), seq, timeout, + populateErrorCallback(errorCallback), populateMsgCallback(msgHandler)); + } + virtual void asyncSendResponse(bcos::bytes const& dstNode, std::string const& traceID, bcos::bytes&& payload, int seq, ppc::protocol::ReceiveMsgFunc errorCallback) = 0; + virtual void asyncSendResponse(bcos::bytes const& dstNode, std::string const& traceID, + bcos::bytes&& payload, int seq, ErrorCallback::Ptr errorCallback) + { + asyncSendResponse( + dstNode, traceID, std::move(payload), seq, populateErrorCallback(errorCallback)); + } + // the sync interface for async_send_message - virtual bcos::Error::Ptr push(ppc::protocol::RouteType routeType, + virtual bcos::Error::Ptr push(uint16_t routeType, ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout) = 0; @@ -131,6 +212,40 @@ class IFront : virtual public IFrontClient * @param topic the topic to unregister */ virtual bcos::Error::Ptr unRegisterTopic(std::string const& topic) = 0; + + +private: + ppc::protocol::ReceiveMsgFunc populateErrorCallback(ErrorCallback::Ptr errorCallback) + { + if (errorCallback == nullptr) + { + return nullptr; + } + return [errorCallback](bcos::Error::Ptr error) { errorCallback->onError(error); }; + } + + ppc::protocol::MessageDispatcherCallback populateMessageDispatcherCallback( + MessageDispatcherHandler::Ptr handler) + { + if (handler == nullptr) + { + return nullptr; + } + return [handler](ppc::protocol::Message::Ptr msg) { handler->onMessage(msg); }; + } + + ppc::protocol::MessageCallback populateMsgCallback(IMessageHandler::Ptr msgHandler) + { + if (msgHandler == nullptr) + { + return nullptr; + } + return [msgHandler](bcos::Error::Ptr e, ppc::protocol::Message::Ptr msg, + ppc::protocol::SendResponseFunction resFunc) { + SendResponseHandler sendResponseHandler(resFunc); + msgHandler->onMessage(e, msg, sendResponseHandler); + }; + } }; class IFrontBuilder diff --git a/cpp/ppc-framework/protocol/Message.h b/cpp/ppc-framework/protocol/Message.h index 3d78b432..8f490643 100644 --- a/cpp/ppc-framework/protocol/Message.h +++ b/cpp/ppc-framework/protocol/Message.h @@ -196,6 +196,11 @@ class Message : virtual public bcos::boostssl::MessageFace MessagePayload::Ptr const& frontMessage() const { return m_frontMessage; } + // Note: swig wrapper require define all methods + virtual bool encode(bcos::bytes& _buffer) = 0; + // encode and return the {header, payload} + virtual bool encode(bcos::boostssl::EncodedMsg& _encodedMsg) = 0; + virtual int64_t decode(bcos::bytesConstRef _buffer) = 0; protected: MessageHeader::Ptr m_header; diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/Front.cpp b/cpp/wedpr-transport/ppc-front/ppc-front/Front.cpp index 23dc4dec..7a205b7b 100644 --- a/cpp/wedpr-transport/ppc-front/ppc-front/Front.cpp +++ b/cpp/wedpr-transport/ppc-front/ppc-front/Front.cpp @@ -134,7 +134,7 @@ void Front::asyncSendMessage(const std::string& _agencyID, front::PPCMessageFace }; } // ROUTE_THROUGH_TOPIC will hold the topic - m_front->asyncSendMessage(RouteType::ROUTE_THROUGH_TOPIC, routeInfo, std::move(data), + m_front->asyncSendMessage((uint16_t)RouteType::ROUTE_THROUGH_TOPIC, routeInfo, std::move(data), _message->seq(), _timeout, _callback, msgCallback); } diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.cpp b/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.cpp index f88f6bc4..58aae243 100644 --- a/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.cpp +++ b/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.cpp @@ -138,7 +138,7 @@ void FrontImpl::asyncSendResponse(bcos::bytes const& dstNode, std::string const& * @param timeout timeout * @param callback callback */ -void FrontImpl::asyncSendMessage(RouteType routeType, MessageOptionalHeader::Ptr const& routeInfo, +void FrontImpl::asyncSendMessage(uint16_t routeType, MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout, ReceiveMsgFunc errorCallback, MessageCallback callback) { @@ -151,8 +151,9 @@ void FrontImpl::asyncSendMessage(RouteType routeType, MessageOptionalHeader::Ptr m_callbackManager->addCallback(traceID, timeout, callback); auto self = weak_from_this(); // send the message to the gateway - asyncSendMessageToGateway(false, std::move(frontMessage), routeType, traceID, routeInfo, - timeout, [self, traceID, routeInfo, errorCallback](bcos::Error::Ptr error) { + asyncSendMessageToGateway(false, std::move(frontMessage), (ppc::protocol::RouteType)routeType, + traceID, routeInfo, timeout, + [self, traceID, routeInfo, errorCallback](bcos::Error::Ptr error) { auto front = self.lock(); if (!front) { @@ -265,7 +266,7 @@ void FrontImpl::onReceiveMessage(Message::Ptr const& msg, ReceiveMsgFunc callbac } // the sync interface for asyncSendMessage -bcos::Error::Ptr FrontImpl::push(RouteType routeType, MessageOptionalHeader::Ptr const& routeInfo, +bcos::Error::Ptr FrontImpl::push(uint16_t routeType, MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout) { auto promise = std::make_shared>(); diff --git a/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.h b/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.h index ca751374..84000656 100644 --- a/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.h +++ b/cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.h @@ -52,7 +52,7 @@ class FrontImpl : public IFront, public IFrontClient, public std::enable_shared_ */ void stop() override; - bcos::Error::Ptr push(ppc::protocol::RouteType routeType, + bcos::Error::Ptr push(uint16_t routeType, ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout) override; /** @@ -70,7 +70,7 @@ class FrontImpl : public IFront, public IFrontClient, public std::enable_shared_ * @param timeout timeout * @param callback callback */ - void asyncSendMessage(ppc::protocol::RouteType routeType, + void asyncSendMessage(uint16_t routeType, ppc::protocol::MessageOptionalHeader::Ptr const& routeInfo, bcos::bytes&& payload, int seq, long timeout, ppc::protocol::ReceiveMsgFunc errorCallback, ppc::protocol::MessageCallback callback) override; diff --git a/cpp/wedpr-transport/sdk-wrapper/java/CMakeLists.txt b/cpp/wedpr-transport/sdk-wrapper/java/CMakeLists.txt index d4d36ffd..806aebfd 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/CMakeLists.txt +++ b/cpp/wedpr-transport/sdk-wrapper/java/CMakeLists.txt @@ -1,19 +1,22 @@ file(GLOB_RECURSE SRCS *.i) set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON) -set(WEDPR_TRANSPORT_PACKAGE "com.webank.wedpr.sdk.jni.transport") +set(WEDPR_TRANSPORT_PACKAGE "com.webank.wedpr.sdk.jni.generated") set_property(SOURCE swig/wedpr_java_transport.i PROPERTY COMPILE_OPTIONS -package ${WEDPR_TRANSPORT_PACKAGE}) +file(MAKE_DIRECTORY ${WEDPR_JAVA_TRANSPORT_DIR}) +file(MAKE_DIRECTORY ${WEDPR_JAVA_TRANSPORT_LIB_DIR}) + swig_add_library(${WEDPR_JAVA_TRANSPORT} TYPE MODULE LANGUAGE java OUTPUT_DIR ${WEDPR_JAVA_TRANSPORT_DIR} SOURCES ${SRCS}) -SET(LIBRARY_OUTPUT_PATH ${WEDPR_JAVA_TRANSPORT_DIR}) -message("LIBRARY_OUTPUT_PATH: ${WEDPR_JAVA_TRANSPORT_DIR}") +SET(LIBRARY_OUTPUT_PATH ${WEDPR_JAVA_TRANSPORT_LIB_DIR}) +message("LIBRARY_OUTPUT_PATH: ${WEDPR_JAVA_TRANSPORT_LIB_DIR}") set_target_properties(${WEDPR_JAVA_TRANSPORT} PROPERTIES SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/build.gradle b/cpp/wedpr-transport/sdk-wrapper/java/bindings/build.gradle index c455b8ad..bbceb950 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/build.gradle +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/build.gradle @@ -3,9 +3,24 @@ plugins { id 'idea' id 'java-library' id 'maven-publish' - //id 'org.ajoberstar.grgit' version '4.1.1' - //id "de.undercouch.download" version "4.1.2" -// id 'com.github.sherter.google-java-format' version '0.9' + id 'org.ajoberstar.grgit' version '4.1.1' + id "de.undercouch.download" version "4.1.2" + id 'com.github.sherter.google-java-format' version '0.9' +} + +// Additional attribute definition +ext { + if (!project.hasProperty("ossrhUsername")) { + ossrhUsername="xxx" + } + + if (!project.hasProperty("ossrhPassword")) { + ossrhPassword="xxx" + } + lombokVersion = "1.18.32" + junitVersion = "4.13.2" + log4jVersion = "2.23.0" + jmhVersion = "1.36" } println("Notice: current gradle version is " + gradle.gradleVersion) @@ -23,15 +38,15 @@ repositories { mavenLocal() } -// googleJavaFormat { -// toolVersion = '1.7' -// options style: 'AOSP' -// source = sourceSets*.allJava -// include '**/*.java' -// exclude '**/*Test.java' -// exclude '**/Test*.java' -// exclude '**/Mock*.java' -// } + googleJavaFormat { + toolVersion = '1.7' + options style: 'AOSP' + source = sourceSets*.allJava + include '**/*.java' + exclude '**/*Test.java' + exclude '**/Test*.java' + exclude '**/Mock*.java' + } sourceSets { main { @@ -61,28 +76,20 @@ sourceSets { dependencies { api ('org.slf4j:slf4j-api:1.7.36') - jmhImplementation 'org.openjdk.jmh:jmh-core:1.36' - jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.36' + implementation("org.projectlombok:lombok:${lombokVersion}") + jmhImplementation ("org.openjdk.jmh:jmh-core:${jmhVersion}") + jmhAnnotationProcessor ("org.openjdk.jmh:jmh-generator-annprocess:${jmhVersion}") + + testImplementation ("org.slf4j:slf4j-log4j12:${log4jVersion}") + testImplementation ("junit:junit:${junitVersion}") - testImplementation ('org.slf4j:slf4j-log4j12:1.7.36') - testImplementation ('junit:junit:4.13.2') + annotationProcessor("org.projectlombok:lombok:${lombokVersion}") } archivesBaseName = 'wedpr-java-transport-jni' group = 'com.webank.wedpr' version = '1.0.0' -// Additional attribute definition -ext { - if (!project.hasProperty("ossrhUsername")) { - ossrhUsername="xxx" - } - - if (!project.hasProperty("ossrhPassword")) { - ossrhPassword="xxx" - } -} - jar { exclude '**/*.xml' exclude '**/*.properties' diff --git a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/common/JniLibLoader.java b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/common/JniLibLoader.java index 9af0301e..87dbe8e7 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/common/JniLibLoader.java +++ b/cpp/wedpr-transport/sdk-wrapper/java/bindings/src/main/java/com/webank/wedpr/sdk/jni/common/JniLibLoader.java @@ -51,13 +51,13 @@ public final class JniLibLoader { // 1. initialize workdir // -Dcom.webank.wedpr.workdir - String workdir = System.getProperty("com.webank.wedpr.workdir"); + String workdir = System.getProperty("com.webank.wedpr.sdk.jni.workdir"); if (workdir != null) { try { File f = new File(workdir); f.mkdirs(); WORKDIR = f; - logger.info("initialize workdir, -Dcom.webank.wedpr.workdir: {}", WORKDIR); + logger.info("initialize workdir, -Dcom.webank.wedpr.sdk.jni.workdir: {}", WORKDIR); } catch (Exception e) { logger.debug("initialize workdir, cannot mkdir workdir: {}, e: ", workdir, e); workdir = null; @@ -101,9 +101,9 @@ public static String getLibName(String baseName) { } else if (osName.contains(MAC)) { String arch = getArch(); if ("arm".equals(arch)) { - return "lib" + baseName + "-aarch64" + ".dylib"; + return "lib" + baseName + "-aarch64" + ".jnilib"; } - return "lib" + baseName + ".dylib"; + return "lib" + baseName + ".jnilib"; } else { throw new RuntimeException("unrecognized OS: " + osName); } diff --git a/cpp/wedpr-transport/sdk-wrapper/java/swig/wedpr_java_transport.i b/cpp/wedpr-transport/sdk-wrapper/java/swig/wedpr_java_transport.i index 05a504c8..f742a29c 100644 --- a/cpp/wedpr-transport/sdk-wrapper/java/swig/wedpr_java_transport.i +++ b/cpp/wedpr-transport/sdk-wrapper/java/swig/wedpr_java_transport.i @@ -1,4 +1,4 @@ -%module wedpr_java_transport +%module(directors="1") wedpr_java_transport %include "stdint.i" %include "typemaps.i" @@ -25,50 +25,138 @@ PRIMITIVE_TYPEMAP(unsigned long int, long long); #endif // defined(SWIGWORDSIZE64) #endif // defined(SWIGJAVA) + %include %include %include %include %include - +// shared_ptr definition %shared_ptr(ppc::front::FrontConfig); +%shared_ptr(ppc::front::IFront); +%shared_ptr(ppc::front::IFrontClient); +// the callbacks +%shared_ptr(ppc::front::ErrorCallback); +%shared_ptr(ppc::front::MessageDispatcherHandler); +%shared_ptr(ppc::front::IMessageHandler); + +%shared_ptr(ppc::gateway::IGateway); %shared_ptr(bcos::Error); +%shared_ptr(bcos::bytes); %shared_ptr(ppc::protocol::Message); -%shared_ptr(ppc::protocol::MessageOptionalHeader) +%shared_ptr(ppc::protocol::MessageOptionalHeader); +%shared_ptr(ppc::protocol::MessageHeader); +%shared_ptr(ppc::protocol::MessagePayload); +%shared_ptr(ppc::protocol::MessageBuilder); +%shared_ptr(ppc::protocol::MessageHeaderBuilder); +%shared_ptr(ppc::protocol::MessagePayloadBuilder); +%shared_ptr(ppc::protocol::MessageOptionalHeaderBuilder); +%shared_ptr(ppc::protocol::GrpcConfig); %shared_ptr(ppc::sdk::Transport); + %{ #define SWIG_FILE_WITH_INIT +#include #include #include "wedpr-transport/sdk/TransportBuilder.h" #include "wedpr-transport/sdk/Transport.h" #include "ppc-framework/front/IFront.h" #include "ppc-framework/protocol/RouteType.h" #include "ppc-framework/front/FrontConfig.h" +#include "ppc-framework/protocol/GrpcConfig.h" #include +#include "ppc-framework/protocol/EndPoint.h" +#include "ppc-framework/protocol/Message.h" +#include "ppc-framework/protocol/MessagePayload.h" %} +namespace ppc::sdk{ + class Transport; + class TransportBuilder; +} + +namespace ppc::gateway{ + class IGateway; +} + +namespace ppc::protocol{ + class Message; + class MessageHeader; + class MessagePayload; + class MessageOptionalHeader; + class MessageBuilder; + class MessageHeaderBuilder; + class MessagePayloadBuilder; + class MessageOptionalHeaderBuilder; + class EndPoint; + class GrpcConfig; + class RouteType; +} + namespace ppc::front{ class FrontConfig; class IFront; + class IFrontClient; class FrontImpl; class FrontBuilderImpl; - class RouteType; class GatewayEndPoint; + class ErrorCallback; + class MessageDispatcherHandler; + class IMessageHandler; + class SendResponseHandler; } -namespace ppc::sdk{ - class Transport; - class TransportBuilder; +namespace std{ + class vector; } -%template(SharedFrontConfig) std::shared_ptr; +namespace bcos{ + using byte = uint8_t; + using bytes = std::vector; + class Error; +} + +// define shared_ptr objects %template(SharedBcosError) std::shared_ptr; + +%template(SharedFrontConfig) std::shared_ptr; +%template(SharedGrpcConfig) std::shared_ptr; + +%template(SharedFront) std::shared_ptr; +%template(SharedFrontClient) std::shared_ptr; + +%template(SharedErrorCallback) std::shared_ptr; +%template(SharedMessageDispatcherHandler) std::shared_ptr; +%template(SharedIMessageHandler) std::shared_ptr; + +%template(SharedGateway) std::shared_ptr; + %template(SharedMessage) std::shared_ptr; +%template(SharedMessageHeader) std::shared_ptr; +%template(SharedMessagePayload) std::shared_ptr; %template(SharedRouteInfo) std::shared_ptr; -%template(SharedTransport) std::shared_ptr; + +%template(SharedMessageBuilder) std::shared_ptr; +%template(SharedMessageHeaderBuilder) std::shared_ptr; +%template(SharedMessagePayloadBuilder) std::shared_ptr; +%template(SharedRouteInfoBuilder) std::shared_ptr; + +%template(ubytes) std::vector; +%template(ibytes) std::vector; + +%feature("director") ppc::front::ErrorCallback; + +// define the interface should been exposed +%include "ppc-framework/front/FrontConfig.h" +%include "ppc-framework/protocol/EndPoint.h" +%include "ppc-framework/protocol/GrpcConfig.h" +%include "ppc-framework/protocol/Message.h" +%include "ppc-framework/protocol/MessagePayload.h" + +%include "ppc-framework/front/IFront.h" %include "wedpr-transport/sdk/TransportBuilder.h" %include "wedpr-transport/sdk/Transport.h" -%include "ppc-framework/front/IFront.h" \ No newline at end of file + diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.cpp b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp index 3763e339..b2a50563 100644 --- a/cpp/wedpr-transport/sdk/ProTransportImpl.cpp +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.cpp @@ -19,7 +19,6 @@ */ #include "ProTransportImpl.h" #include "Common.h" -#include "protocol/src/v1/MessageImpl.h" #include "wedpr-protocol/grpc/client/GatewayClient.h" #include "wedpr-protocol/grpc/server/FrontServer.h" #include "wedpr-protocol/grpc/server/GrpcServer.h" @@ -30,7 +29,7 @@ using namespace ppc::sdk; ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config, int keepAlivePeriodMs) - : m_config(std::move(config)), m_keepAlivePeriodMs(keepAlivePeriodMs) + : Transport(config), m_keepAlivePeriodMs(keepAlivePeriodMs) { // Note: since the config has been moved away, should not use the `config`, use `m_config` // instead default enable health-check @@ -40,13 +39,10 @@ ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config, int keep FrontFactory frontFactory; m_gateway = std::make_shared(m_config->grpcConfig(), m_config->gatewayGrpcTarget()); - m_front = frontFactory.build(std::make_shared(), - std::make_shared(), - std::make_shared(), m_gateway, m_config); + m_front = frontFactory.build(std::make_shared(), m_msgPayloadBuilder, + m_routeInfoBuilder, m_gateway, m_config); - auto msgBuilder = - std::make_shared(std::make_shared()); - m_frontService = std::make_shared(msgBuilder, m_front); + m_frontService = std::make_shared(m_msgBuilder, m_front); // register the frontService m_server->registerService(m_frontService); } diff --git a/cpp/wedpr-transport/sdk/ProTransportImpl.h b/cpp/wedpr-transport/sdk/ProTransportImpl.h index 76e2ce3a..cb5d4873 100644 --- a/cpp/wedpr-transport/sdk/ProTransportImpl.h +++ b/cpp/wedpr-transport/sdk/ProTransportImpl.h @@ -20,6 +20,7 @@ #pragma once #include "TransportImpl.h" #include "bcos-utilities/Timer.h" +#include "protocol/src/v1/MessageImpl.h" namespace ppc::protocol { @@ -43,7 +44,6 @@ class ProTransportImpl : public Transport, public std::enable_shared_from_this

m_server; std::shared_ptr m_frontService; int m_keepAlivePeriodMs; diff --git a/cpp/wedpr-transport/sdk/Transport.cpp b/cpp/wedpr-transport/sdk/Transport.cpp new file mode 100644 index 00000000..ea7b112f --- /dev/null +++ b/cpp/wedpr-transport/sdk/Transport.cpp @@ -0,0 +1,33 @@ +/** + * 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.cpp + * @author: yujiechen + * @date 2024-09-04 + */ +#include "Transport.h" +#include "protocol/src/v1/MessageHeaderImpl.h" +#include "protocol/src/v1/MessageImpl.h" +#include "protocol/src/v1/MessagePayloadImpl.h" + +using namespace ppc::sdk; + +Transport::Transport(ppc::front::FrontConfig::Ptr config) + : m_config(std::move(config)), + m_msgPayloadBuilder(std::make_shared()), + m_msgHeaderBuilder(std::make_shared()), + m_msgBuilder(std::make_shared(m_msgHeaderBuilder)), + m_routeInfoBuilder(std::make_shared()) +{} diff --git a/cpp/wedpr-transport/sdk/Transport.h b/cpp/wedpr-transport/sdk/Transport.h index c9c36148..c6175c91 100644 --- a/cpp/wedpr-transport/sdk/Transport.h +++ b/cpp/wedpr-transport/sdk/Transport.h @@ -18,15 +18,19 @@ * @date 2024-09-04 */ #pragma once +#include "ppc-framework/front/FrontConfig.h" #include "ppc-framework/front/IFront.h" #include "ppc-framework/gateway/IGateway.h" +#include "ppc-framework/protocol/Message.h" +#include "ppc-framework/protocol/MessagePayload.h" + namespace ppc::sdk { class Transport { public: using Ptr = std::shared_ptr; - Transport() = default; + Transport(ppc::front::FrontConfig::Ptr config); virtual ~Transport() = default; virtual void start() { m_front->start(); } @@ -36,8 +40,32 @@ class Transport virtual ppc::gateway::IGateway::Ptr const& gateway() const { return m_gateway; } + virtual ppc::protocol::MessagePayloadBuilder::Ptr const& msgPayloadBuilder() const + { + return m_msgPayloadBuilder; + } + virtual ppc::protocol::MessageBuilder::Ptr const& msgBuilder() const { return m_msgBuilder; } + virtual ppc::protocol::MessageHeaderBuilder::Ptr const& msgHeaderBuilder() const + { + return m_msgHeaderBuilder; + } + + virtual ppc::protocol::MessageOptionalHeaderBuilder::Ptr const& routeInfoBuilder() const + { + return m_routeInfoBuilder; + } + + virtual ppc::front::FrontConfig::Ptr const& getConfig() const { return m_config; } + protected: ppc::front::IFront::Ptr m_front; ppc::gateway::IGateway::Ptr m_gateway; + + ppc::front::FrontConfig::Ptr m_config; + + ppc::protocol::MessageHeaderBuilder::Ptr m_msgHeaderBuilder; + ppc::protocol::MessagePayloadBuilder::Ptr m_msgPayloadBuilder; + ppc::protocol::MessageOptionalHeaderBuilder::Ptr m_routeInfoBuilder; + ppc::protocol::MessageBuilder::Ptr m_msgBuilder; }; } // 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 index fbd9e3e6..5ebda37b 100644 --- a/cpp/wedpr-transport/sdk/TransportImpl.h +++ b/cpp/wedpr-transport/sdk/TransportImpl.h @@ -22,8 +22,7 @@ #include "ppc-framework/gateway/IGateway.h" #include "ppc-front/FrontFactory.h" #include "protobuf/src/NodeInfoImpl.h" -#include "protocol/src/v1/MessageHeaderImpl.h" -#include "protocol/src/v1/MessagePayloadImpl.h" + namespace ppc::sdk { @@ -31,17 +30,15 @@ class TransportImpl : public Transport { public: TransportImpl(ppc::front::FrontConfig::Ptr config, ppc::gateway::IGateway::Ptr const& gateway) - : m_config(std::move(config)) + : Transport(config) + { m_gateway = gateway; + ppc::front::FrontFactory frontFactory; m_front = frontFactory.build(std::make_shared(), - std::make_shared(), - std::make_shared(), gateway, m_config); + m_msgPayloadBuilder, m_routeInfoBuilder, gateway, m_config); } ~TransportImpl() override = default; - -protected: - ppc::front::FrontConfig::Ptr m_config; }; } // namespace ppc::sdk