Skip to content

Commit

Permalink
erase front dependency to bcos-boostssl
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Oct 23, 2024
1 parent 7c69e64 commit 0f65fd8
Show file tree
Hide file tree
Showing 27 changed files with 460 additions and 1,207 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cpp_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ jobs:
- name: Build for windows
if: runner.os == 'Windows'
run: |
mkdir -p cpp/build && cd cpp/build && cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DTESTS=OFF -DBUILD_WEDPR_TOOLKIT=ON -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake .. && MSBuild /version && MSBuild wedpr-component.sln /p:Configuration=Release /p:Platform=x64
mkdir -p cpp/build && cd cpp/build && cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DTESTS=OFF -DBUILD_WEDPR_TOOLKIT=ON -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_TOOLCHAIN_FILE=c:/vcpkg/scripts/buildsystems/vcpkg.cmake .. && MSBuild /version && MSBuild WeDPR-Component.sln /p:Configuration=Release /p:Platform=x64
- name: Build for linux
if: runner.os == 'Linux'
run: |
Expand Down
2 changes: 1 addition & 1 deletion cpp/cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ endif()
if(BUILD_ALL OR BUILD_WEDPR_TOOLKIT)
find_package(TBB REQUIRED)
find_package(gRPC REQUIRED)
find_package(jsoncpp REQUIRED)
endif()
##### the sdk dependencies #####
if(BUILD_ALL OR BUILD_SDK OR BUILD_UDF)
Expand All @@ -29,7 +30,6 @@ endif()

##### the full-dependencies #####
if(BUILD_ALL)
find_package(jsoncpp REQUIRED)
find_package(${BCOS_BOOSTSSL_TARGET} REQUIRED)
# tcmalloc
include(ProjectTCMalloc)
Expand Down
59 changes: 14 additions & 45 deletions cpp/ppc-framework/protocol/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "RouteType.h"
#include "ppc-framework/Helper.h"
#include "ppc-framework/libwrapper/Buffer.h"
#include <bcos-boostssl/interfaces/MessageFace.h>
#include <bcos-utilities/Common.h>
#include <bcos-utilities/DataConvertUtility.h>
#include <bcos-utilities/Error.h>
Expand Down Expand Up @@ -183,35 +182,16 @@ class MessageHeader
uint16_t mutable m_length;
};

class Message : virtual public bcos::boostssl::MessageFace
class Message
{
public:
using Ptr = std::shared_ptr<Message>;
Message() = default;
~Message() override {}
virtual ~Message() {}

virtual MessageHeader::Ptr header() const { return m_header; }
virtual void setHeader(MessageHeader::Ptr header) { m_header = std::move(header); }

/// the overloaed implementation ===
uint16_t version() const override { return m_header->version(); }
void setVersion(uint16_t version) override { m_header->setVersion(version); }
uint16_t packetType() const override { return m_header->packetType(); }
void setPacketType(uint16_t packetType) override { m_header->setPacketType(packetType); }
std::string const& seq() const override { return m_header->traceID(); }
void setSeq(std::string traceID) override { m_header->setTraceID(traceID); }
uint16_t ext() const override { return m_header->ext(); }
void setExt(uint16_t ext) override { m_header->setExt(ext); }

bool isRespPacket() const override { return m_header->isRespPacket(); }
void setRespPacket() override { m_header->setRespPacket(); }

virtual uint32_t length() const override
{
return m_header->length() + (m_payload ? m_payload->size() : 0);
}

std::shared_ptr<bcos::bytes> payload() const override { return m_payload; }
// for swig wrapper
OutputBuffer payloadBuffer() const
{
Expand All @@ -222,11 +202,6 @@ class Message : virtual public bcos::boostssl::MessageFace
return OutputBuffer{(unsigned char*)m_payload->data(), m_payload->size()};
}

void setPayload(std::shared_ptr<bcos::bytes> _payload) override
{
m_payload = std::move(_payload);
}

void setFrontMessage(MessagePayload::Ptr frontMessage)
{
m_frontMessage = std::move(frontMessage);
Expand All @@ -236,10 +211,18 @@ class Message : virtual public bcos::boostssl::MessageFace

// 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;

virtual uint32_t length() const
{
return m_header->length() + (m_payload ? m_payload->size() : 0);
}
virtual std::shared_ptr<bcos::bytes> payload() const { return m_payload; }
virtual void setPayload(std::shared_ptr<bcos::bytes> _payload)
{
m_payload = std::move(_payload);
}

protected:
MessageHeader::Ptr m_header;
// Note: allocate here in case of wsService nullptr access caused coredump
Expand All @@ -259,12 +242,12 @@ class MessageHeaderBuilder
virtual MessageOptionalHeader::Ptr build(MessageOptionalHeader::Ptr const& optionalHeader) = 0;
};

class MessageBuilder : public bcos::boostssl::MessageFaceFactory
class MessageBuilder
{
public:
using Ptr = std::shared_ptr<MessageBuilder>;
MessageBuilder() = default;
~MessageBuilder() override = default;
virtual ~MessageBuilder() = default;

virtual Message::Ptr build() = 0;
virtual Message::Ptr build(bcos::bytesConstRef buffer) = 0;
Expand Down Expand Up @@ -320,20 +303,6 @@ inline std::string printMessage(Message::Ptr const& _msg)
}
return stringstream.str();
}

inline std::string printWsMessage(bcos::boostssl::MessageFace::Ptr const& _msg)
{
if (!_msg)
{
return "nullptr";
}
std::ostringstream stringstream;
stringstream << LOG_KV("rsp", _msg->isRespPacket()) << LOG_KV("traceID", _msg->seq())
<< LOG_KV("packetType", _msg->packetType()) << LOG_KV("length", _msg->length())
<< LOG_KV("ext", _msg->ext());
return stringstream.str();
}

// function to send response
using SendResponseFunction = std::function<void(std::shared_ptr<bcos::bytes>&& payload)>;
using ReceiveMsgFunc = std::function<void(bcos::Error::Ptr)>;
Expand Down
77 changes: 77 additions & 0 deletions cpp/ppc-framework/protocol/P2PMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 Message.h
* @author: yujiechen
* @date 2024-08-22
*/
#pragma once
#include "Message.h"
#include <bcos-boostssl/interfaces/MessageFace.h>
namespace ppc::protocol
{
// the wrapper for use boostssl service
class P2PMessage : virtual public bcos::boostssl::MessageFace, public Message
{
public:
using Ptr = std::shared_ptr<P2PMessage>;
P2PMessage() = default;
~P2PMessage() override {}

/// the overloaed implementation ===
uint16_t version() const override { return m_header->version(); }
void setVersion(uint16_t version) override { m_header->setVersion(version); }
uint16_t packetType() const override { return m_header->packetType(); }
void setPacketType(uint16_t packetType) override { m_header->setPacketType(packetType); }
std::string const& seq() const override { return m_header->traceID(); }
void setSeq(std::string traceID) override { m_header->setTraceID(traceID); }
uint16_t ext() const override { return m_header->ext(); }
void setExt(uint16_t ext) override { m_header->setExt(ext); }

bool isRespPacket() const override { return m_header->isRespPacket(); }
void setRespPacket() override { m_header->setRespPacket(); }

uint32_t length() const override { return Message::length(); }
std::shared_ptr<bcos::bytes> payload() override { return Message::payload(); }
void setPayload(std::shared_ptr<bcos::bytes> _payload) override
{
Message::setPayload(std::move(_payload));
}
// encode and return the {header, payload}
virtual bool encode(bcos::boostssl::EncodedMsg& _encodedMsg) = 0;
};


class P2PMessageBuilder : virtual public bcos::boostssl::MessageFaceFactory, public MessageBuilder
{
public:
using Ptr = std::shared_ptr<P2PMessageBuilder>;
P2PMessageBuilder() = default;
~P2PMessageBuilder() override = default;
};
inline std::string printWsMessage(bcos::boostssl::MessageFace::Ptr const& _msg)
{
if (!_msg)
{
return "nullptr";
}
std::ostringstream stringstream;
stringstream << LOG_KV("rsp", _msg->isRespPacket()) << LOG_KV("traceID", _msg->seq())
<< LOG_KV("packetType", _msg->packetType()) << LOG_KV("length", _msg->length())
<< LOG_KV("ext", _msg->ext());
return stringstream.str();
}

} // namespace ppc::protocol
4 changes: 2 additions & 2 deletions cpp/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@
},
"tarscpp",
"tbb",
"libxml2",
"jsoncpp"
"libxml2"
]
},
"sdk": {
Expand All @@ -148,6 +147,7 @@
"version>=": "1.51.1"
},
"tarscpp",
"jsoncpp",
"tbb"
]
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/wedpr-protocol/protocol/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file(GLOB_RECURSE SRCS *.cpp)
add_library(${PROTOCOL_TARGET} ${SRCS})

target_link_libraries(${PROTOCOL_TARGET} PUBLIC ${BCOS_BOOSTSSL_TARGET} ${CPU_FEATURES_LIB} jsoncpp_static)
target_link_libraries(${PROTOCOL_TARGET} PUBLIC ${CPU_FEATURES_LIB} jsoncpp_static)
18 changes: 0 additions & 18 deletions cpp/wedpr-protocol/protocol/src/v1/MessageImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ bool MessageImpl::encode(bcos::bytes& _buffer)
return true;
}

bool MessageImpl::encode(bcos::boostssl::EncodedMsg& encodedMsg)
{
try
{
// header
m_header->encode(encodedMsg.header);
// assign the payload back
encodedMsg.payload = m_payload;
return true;
}
catch (std::exception const& e)
{
PROTOCOL_LOG(WARNING) << LOG_DESC("encode message failed")
<< LOG_KV("error", boost::diagnostic_information(e));
return false;
}
}

int64_t MessageImpl::decode(bytesConstRef buffer)
{
if (buffer.size() > m_maxMessageLen)
Expand Down
15 changes: 3 additions & 12 deletions cpp/wedpr-protocol/protocol/src/v1/MessageImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#pragma once
#include "ppc-framework/Common.h"
#include "ppc-framework/protocol/Message.h"
#include "ppc-utilities/Utilities.h"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
Expand All @@ -46,11 +45,9 @@ class MessageImpl : public Message
~MessageImpl() override = default;

bool encode(bcos::bytes& _buffer) override;
// encode and return the {header, payload}
bool encode(bcos::boostssl::EncodedMsg& _encodedMsg) override;
int64_t decode(bcos::bytesConstRef _buffer) override;

private:
protected:
MessageHeaderBuilder::Ptr m_headerBuilder;

// default max message length is 100MB
Expand Down Expand Up @@ -92,20 +89,14 @@ class MessageBuilderImpl : public MessageBuilder
return msg;
}

bcos::boostssl::MessageFace::Ptr buildMessage() override
{
return std::make_shared<MessageImpl>(m_msgHeaderBuilder, m_maxMessageLen);
}

virtual MessageOptionalHeader::Ptr build(MessageOptionalHeader::Ptr const& optionalHeader)
{
return m_msgHeaderBuilder->build(optionalHeader);
}
std::string newSeq() override { return generateUUID(); }

private:
protected:
MessageHeaderBuilder::Ptr m_msgHeaderBuilder;
// default max message length is 100MB
size_t m_maxMessageLen = 100 * 1024 * 1024;
};
} // namespace ppc::protocol
} // namespace ppc::protocol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include "bcos-boostssl/websocket/WsInitializer.h"
#include "ppc-gateway/p2p/Service.h"
#include "ppc-gateway/p2p/router/RouterTableImpl.h"
#include "ppc-gateway/protocol/P2PMessageImpl.h"
#include "ppc-tools/src/config/PPCConfig.h"
#include "protocol/src/v1/MessageHeaderImpl.h"
#include "protocol/src/v1/MessageImpl.h"

using namespace ppc;
using namespace bcos;
Expand Down Expand Up @@ -55,7 +55,7 @@ Service::Ptr GatewayFactory::buildService() const
auto wsInitializer = std::make_shared<WsInitializer>();
// set the messageFactory
wsInitializer->setMessageFactory(
std::make_shared<MessageBuilderImpl>(std::make_shared<MessageHeaderBuilderImpl>()));
std::make_shared<P2PMessageBuilderImpl>(std::make_shared<MessageHeaderBuilderImpl>()));
// set the config
wsInitializer->setConfig(wsConfig);
auto p2pService = std::make_shared<Service>(m_contextConfig->nodeID(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ GatewayImpl::GatewayImpl(Service::Ptr const& service,
std::shared_ptr<boost::asio::io_service> ioService, std::string const& agency)
: m_service(service),
m_msgBuilder(
std::dynamic_pointer_cast<ppc::protocol::MessageBuilder>(service->messageFactory())),
std::dynamic_pointer_cast<ppc::protocol::P2PMessageBuilder>(service->messageFactory())),
m_frontBuilder(frontBuilder),
m_agency(agency),
m_p2pRouterManager(std::make_shared<RouterManager>(service)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class GatewayImpl : public IGateway, public std::enable_shared_from_this<Gateway
private:
bool m_running = false;
Service::Ptr m_service;
ppc::protocol::MessageBuilder::Ptr m_msgBuilder;
ppc::protocol::P2PMessageBuilder::Ptr m_msgBuilder;

ppc::front::IFrontBuilder::Ptr m_frontBuilder;
std::string m_agency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @date 2024-08-26
*/
#pragma once
#include "ppc-framework/protocol/Message.h"
#include "ppc-framework/protocol/P2PMessage.h"
#include "ppc-gateway/gateway/router/GatewayNodeInfo.h"
#include "ppc-gateway/p2p/Service.h"
#include <bcos-utilities/Common.h>
Expand All @@ -31,7 +31,7 @@ class SendMessageWithRetry : public std::enable_shared_from_this<SendMessageWith
public:
using Ptr = std::shared_ptr<SendMessageWithRetry>;
SendMessageWithRetry(Service::Ptr const& service, GatewayNodeInfos&& dstNodeList,
ppc::protocol::Message::Ptr&& p2pMessage, ppc::protocol::ReceiveMsgFunc respFunc,
ppc::protocol::P2PMessage::Ptr&& p2pMessage, ppc::protocol::ReceiveMsgFunc respFunc,
long timeout)
: m_service(service),
m_dstNodeList(std::move(dstNodeList)),
Expand All @@ -54,7 +54,7 @@ class SendMessageWithRetry : public std::enable_shared_from_this<SendMessageWith
// mutex for p2pIDs
mutable bcos::RecursiveMutex x_mutex;
GatewayNodeInfos m_dstNodeList;
ppc::protocol::Message::Ptr m_p2pMessage;
ppc::protocol::P2PMessage::Ptr m_p2pMessage;
Service::Ptr m_service;
ppc::protocol::ReceiveMsgFunc m_respFunc;
long m_timeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace ppc::protocol;
using namespace ppc::gateway;

void MessageCache::insertCache(
std::string const& topic, ppc::protocol::Message::Ptr const& msg, ReceiveMsgFunc callback)
std::string const& topic, ppc::protocol::P2PMessage::Ptr const& msg, ReceiveMsgFunc callback)
{
// hold the message
GATEWAY_LOG(DEBUG) << LOG_BADGE("MessageCache: insertCache") << printMessage(msg);
Expand Down
Loading

0 comments on commit 0f65fd8

Please sign in to comment.