Skip to content

Commit

Permalink
add sdk-wrapper for transport (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull authored Sep 4, 2024
1 parent 3c14d5b commit 0a7d99f
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ set(ALL_SOURCE_LIST
wedpr-helper/libhelper wedpr-helper/ppc-tools
wedpr-storage/ppc-io wedpr-storage/ppc-storage
wedpr-transport/ppc-gateway wedpr-transport/ppc-front
wedpr-transport/ppc-http wedpr-transport/ppc-rpc
wedpr-transport/ppc-http wedpr-transport/ppc-rpc wedpr-transport/sdk
wedpr-computing/ppc-psi wedpr-computing/ppc-mpc wedpr-computing/ppc-pir ${CEM_SOURCE})

if(BUILD_WEDPR_TOOLKIT)
Expand All @@ -100,7 +100,7 @@ if(BUILD_WEDPR_TOOLKIT)
include(swig)
message(STATUS "Getting SWIG for Windows: ...DONE")
endif()
add_subdirectory(wedpr-toolkit-wrapper)
add_subdirectory(wedpr-transport/sdk-wrapper)
endif()

if(BUILD_ALL)
Expand Down
1 change: 1 addition & 0 deletions cpp/cmake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ macro(print_config NAME)
message("-- DEMO Enable DEMO ${ENABLE_DEMO}")
message("-- BUILD_SDK BUILD SDK ${BUILD_SDK}")
message("-- BUILD_UDF BUILD UDF ${BUILD_UDF}")
message("-- BUILD_WEDPR_TOOLKIT BUILD_WEDPR_TOOLKIT ${BUILD_WEDPR_TOOLKIT}")
message("-- DEBUG ${DEBUG}")
message("------------------------------------------------------------------------")
message("")
Expand Down
4 changes: 2 additions & 2 deletions cpp/cmake/TargetSettings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ set(BOOST_UNIT_TEST Boost::unit_test_framework)
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})
set(WEDPR_PYTHON_TRANSPORT "wedpr_python_transport")
set(WEDPR_PYTHON_TRANSPORT_DIR ${PROJECT_BINARY_DIR}/python/${WEDPR_PYTHON_TRANSPORT})
104 changes: 104 additions & 0 deletions cpp/cmake/python.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
if(NOT BUILD_PYTHON)
return()
endif()

# Use latest UseSWIG module (3.14) and Python3 module (3.18)
cmake_minimum_required(VERSION 3.18)

# Will need swig
set(CMAKE_SWIG_FLAGS)
find_package(SWIG REQUIRED)
include(UseSWIG)

if(${SWIG_VERSION} VERSION_GREATER_EQUAL 4)
list(APPEND CMAKE_SWIG_FLAGS "-doxygen")
endif()

if(UNIX AND NOT APPLE)
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
endif()

# Find Python 3
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
list(APPEND CMAKE_SWIG_FLAGS "-py3" "-DPY3")

function(search_python_module)
set(options NO_VERSION)
set(oneValueArgs NAME PACKAGE)
set(multiValueArgs "")
cmake_parse_arguments(MODULE
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
message(STATUS "Searching python module: \"${MODULE_NAME}\"")
if(${MODULE_NO_VERSION})
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}"
RESULT_VARIABLE _RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(MODULE_VERSION "unknown")
else()
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}; print(${MODULE_NAME}.__version__)"
RESULT_VARIABLE _RESULT
OUTPUT_VARIABLE MODULE_VERSION
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if(${_RESULT} STREQUAL "0")
message(STATUS "Found python module: \"${MODULE_NAME}\" (found version \"${MODULE_VERSION}\")")
else()
if(FETCH_PYTHON_DEPS)
message(WARNING "Can't find python module: \"${MODULE_NAME}\", install it using pip...")
execute_process(
COMMAND ${Python3_EXECUTABLE} -m pip install --user ${MODULE_PACKAGE}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
message(FATAL_ERROR "Can't find python module: \"${MODULE_NAME}\", please install it using your system package manager.")
endif()
endif()
endfunction()

# Find if a python builtin module is available.
# e.g
# search_python_internal_module(
# NAME
# mypy_protobuf
# )
function(search_python_internal_module)
set(options "")
set(oneValueArgs NAME)
set(multiValueArgs "")
cmake_parse_arguments(MODULE
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
message(STATUS "Searching python module: \"${MODULE_NAME}\"")
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${MODULE_NAME}"
RESULT_VARIABLE _RESULT
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(${_RESULT} STREQUAL "0")
message(STATUS "Found python internal module: \"${MODULE_NAME}\"")
else()
message(FATAL_ERROR "Can't find python internal module \"${MODULE_NAME}\", please install it using your system package manager.")
endif()
endfunction()

# Look for python module wheel
search_python_module(
NAME setuptools
PACKAGE setuptools)
search_python_module(
NAME wheel
PACKAGE wheel)
5 changes: 5 additions & 0 deletions cpp/wedpr-protocol/protocol/src/v1/MessageHeaderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,10 @@ class MessageOptionalHeaderBuilderImpl : public MessageOptionalHeaderBuilder
{
return std::make_shared<MessageOptionalHeaderImpl>(optionalHeader);
}

MessageOptionalHeader::Ptr build() override
{
return std::make_shared<MessageOptionalHeaderImpl>();
}
};
} // namespace ppc::protocol
10 changes: 10 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project(wedpr-transport-swig VERSION ${VERSION})

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})

# Add subdirectories for each language if desired
option(BUILD_PYTHON "Build Python SWIG module" ON)
if(BUILD_PYTHON)
add_subdirectory(python)
endif()
54 changes: 54 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
file(GLOB_RECURSE SRCS *.i)
set_source_files_properties(${SRCS} PROPERTIES CPLUSPLUS ON)

swig_add_library(
${WEDPR_PYTHON_TRANSPORT}
TYPE MODULE
LANGUAGE python
OUTPUT_DIR ${WEDPR_PYTHON_TRANSPORT_DIR}
SOURCES ${SRCS}
)

message("#### Python3_INCLUDE_DIRS: ${Python3_INCLUDE_DIRS}")
target_include_directories(
${WEDPR_PYTHON_TRANSPORT}
PRIVATE
../include
${Python3_INCLUDE_DIRS}
)
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES ON)

# note: macOS is APPLE and also UNIX !
if(APPLE)
set_property(TARGET ${WEDPR_PYTHON_TRANSPORT} APPEND PROPERTY
LINK_FLAGS "-flat_namespace -undefined suppress"
)
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES
SUFFIX ".so"
INSTALL_RPATH "@loader_path;@loader_path/../../${WEDPR_PYTHON_TRANSPORT}/.libs"
)
elseif(UNIX)
set_target_properties(${WEDPR_PYTHON_TRANSPORT} PROPERTIES
INSTALL_RPATH "$ORIGIN:$ORIGIN/../../${WEDPR_PYTHON_TRANSPORT}/.libs"
)
endif()
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${FRONT_TARGET})

SET(LIBRARY_OUTPUT_PATH ${WEDPR_PYTHON_TRANSPORT_DIR}/)

# Variable PYTHON_LIBRARIES can contains keyword `optimized`
# which won't be interpreted inside a generator expression.
# i.e. we can't use: $<$<PLATFORM_ID:Windows>:${PYTHON_LIBRARIES}>
# see: https://cmake.org/cmake/help/git-stage/command/target_link_libraries.html#command:target_link_libraries
if(MSVC)
target_link_libraries(${WEDPR_PYTHON_TRANSPORT} PRIVATE ${Python3_LIBRARIES})
endif()

# Configure setup.py and copy to output directory
file(GENERATE OUTPUT ${WEDPR_PYTHON_TRANSPORT_DIR}/__init__.py CONTENT "__version__ = \"${PYTHON_TOOLKIT_VERSION}\"\n")
set(SETUP_PY_IN ${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in)
set(SETUP_PY_OUT ${WEDPR_PYTHON_TRANSPORT_DIR}/setup.py)
configure_file(${SETUP_PY_IN} ${SETUP_PY_OUT})

message(STATUS "CMAKE_INSTALL_INCLUDEDIR => ${CMAKE_INSTALL_INCLUDEDIR}")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR => ${CMAKE_CURRENT_SOURCE_DIR}")
35 changes: 35 additions & 0 deletions cpp/wedpr-transport/sdk-wrapper/python/setup.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import setuptools.command.install
import shutil
from distutils.sysconfig import get_python_lib


class CompiledLibInstall(setuptools.command.install.install):
"""
Specialized install to install to python libs
"""

def run(self):
"""
Run method called by setup
:return:
"""
# Get filenames from CMake variable
filenames = '${PYTHON_INSTALL_FILES}'.split(';')

# Directory to install to
install_dir = get_python_lib()

# Install files
[shutil.copy(filename, install_dir) for filename in filenames]


if __name__ == '__main__':
setuptools.setup(
name='wedpr-python-transport',
version='1.0.0-rc1',
packages=['wedpr-python-transport'],
license='Apache License 2.0',
author='wedpr',
author_email='[email protected]',
cmdclass={'install': CompiledLibInstall}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%module wedpr_python_toolkit

%include <stdint.i>
%include <cpointer.i>
%include <std_vector.i>
%include <std_string.i>
%include <std_shared_ptr.i>


%shared_ptr(ppc::front::FrontConfig);
%shared_ptr(bcos::Error);
%shared_ptr(ppc::protocol::Message);
%shared_ptr(ppc::protocol::MessageOptionalHeader)
%shared_ptr(ppc::sdk::Transport);

%{
#define SWIG_FILE_WITH_INIT
#include <stdint.h>
#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 <bcos-utilities/Error.h>
%}

namespace ppc::front{
class FrontConfig;
class IFront;
class FrontImpl;
class FrontBuilderImpl;
class RouteType;
class GatewayEndPoint;
}

namespace ppc::sdk{
class Transport;
class TransportBuilder;
}

%template(SharedFrontConfig) std::shared_ptr<ppc::front::FrontConfig>;
%template(SharedBcosError) std::shared_ptr<bcos::Error>;
%template(SharedMessage) std::shared_ptr<ppc::protocol::Message>;
%template(SharedRouteInfo) std::shared_ptr<ppc::protocol::MessageOptionalHeader>;
%template(SharedTransport) std::shared_ptr<ppc::sdk::Transport>;

%include "wedpr-transport/sdk/TransportBuilder.h"
%include "wedpr-transport/sdk/Transport.h"
%include "ppc-framework/front/IFront.h"
14 changes: 10 additions & 4 deletions cpp/wedpr-transport/sdk/ProTransportImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
* @date 2024-09-04
*/
#include "ProTransportImpl.h"
#include "protobuf/src/v1/MessageImpl.h"
#include "protocol/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)
using namespace ppc::sdk;


ProTransportImpl::ProTransportImpl(ppc::front::FrontConfig::Ptr config)
: m_config(std::move(config))
{
GrpcServerConfig{config->selfEndPoint()};
m_server = std::make_shared<GrpcServer>(GrpcServerConfig);
GrpcServerConfig grpcServerConfig{config->selfEndPoint()};
m_server = std::make_shared<GrpcServer>(grpcServerConfig);

FrontFactory frontFactory;
grpc::ChannelArguments channelConfig;
Expand All @@ -36,6 +39,9 @@ ProTransportImpl::ProTransportImpl(ppc::Front::FrontConfig::Ptr config)
m_front = frontFactory.build(std::make_shared<NodeInfoFactory>(),
std::make_shared<MessagePayloadBuilderImpl>(),
std::make_shared<MessageOptionalHeaderBuilderImpl>(), gateway, config);

auto msgBuilder =
std::make_shared<MessageBuilderImpl>(std::make_shared<MessageHeaderBuilderImpl>());
auto frontService = std::make_shared<FrontServer>(msgBuilder, m_front);

// register the frontService
Expand Down
3 changes: 2 additions & 1 deletion cpp/wedpr-transport/sdk/ProTransportImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ProTransportImpl : public Transport
{
public:
using Ptr = std::shared_ptr<ProTransportImpl>;
ProTransportImpl(ppc::Front::FrontConfig::Ptr config);
ProTransportImpl(ppc::front::FrontConfig::Ptr config);

void start() override
{
Expand All @@ -43,6 +43,7 @@ class ProTransportImpl : public Transport
}

protected:
ppc::front::FrontConfig::Ptr m_config;
ppc::protocol::GrpcServer::Ptr m_server;
};
} // namespace ppc::sdk
1 change: 1 addition & 0 deletions cpp/wedpr-transport/sdk/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ppc::sdk
class Transport
{
public:
using Ptr = std::shared_ptr<Transport>;
Transport() = default;
virtual ~Transport() = default;

Expand Down
4 changes: 2 additions & 2 deletions cpp/wedpr-transport/sdk/TransportBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TransportBuilder
TransportBuilder() = default;
virtual ~TransportBuilder() = default;

Transport::Ptr build(SDKMode mode, ppc::Front::FrontConfig::Ptr config,
Transport::Ptr build(SDKMode mode, ppc::front::FrontConfig::Ptr config,
ppc::gateway::IGateway::Ptr const& gateway)
{
switch (mode)
Expand All @@ -50,7 +50,7 @@ class TransportBuilder
return std::make_shared<ProTransportImpl>(config);
}
default:
throw std::exception("Unsupported sdk mode, only support AIR/PRO mode!");
throw std::runtime_error("Unsupported sdk mode, only support AIR/PRO mode!");
}
}
};
Expand Down
Loading

0 comments on commit 0a7d99f

Please sign in to comment.