提交 a03a9469 编写于 作者: G gruminions 提交者: Jiangtao Hu

framework: decouple transport and rtps_dispatcher

上级 eda23af0
...@@ -78,6 +78,7 @@ bool Init() { ...@@ -78,6 +78,7 @@ bool Init() {
// Initialize internal static objects // Initialize internal static objects
common::GlobalData::Instance(); common::GlobalData::Instance();
transport::Transport::Instance();
service_discovery::TopologyManager::Instance(); service_discovery::TopologyManager::Instance();
scheduler::Scheduler::Instance(); scheduler::Scheduler::Instance();
TaskManager::Instance(); TaskManager::Instance();
...@@ -152,7 +153,7 @@ void Shutdown() { ...@@ -152,7 +153,7 @@ void Shutdown() {
TaskManager::Instance()->Shutdown(); TaskManager::Instance()->Shutdown();
scheduler::Scheduler::Instance()->ShutDown(); scheduler::Scheduler::Instance()->ShutDown();
service_discovery::TopologyManager::Instance()->Shutdown(); service_discovery::TopologyManager::Instance()->Shutdown();
transport::Transport::Shutdown(); transport::Transport::Instance()->Shutdown();
SetState(STATE_SHUTDOWN); SetState(STATE_SHUTDOWN);
} }
......
...@@ -96,7 +96,7 @@ auto ReceiverManager<MessageT>::GetReceiver( ...@@ -96,7 +96,7 @@ auto ReceiverManager<MessageT>::GetReceiver(
const std::string& channel_name = role_attr.channel_name(); const std::string& channel_name = role_attr.channel_name();
if (receiver_map_.count(channel_name) == 0) { if (receiver_map_.count(channel_name) == 0) {
receiver_map_[channel_name] = receiver_map_[channel_name] =
transport::Transport::CreateReceiver<MessageT>( transport::Transport::Instance()->CreateReceiver<MessageT>(
role_attr, [](const std::shared_ptr<MessageT>& msg, role_attr, [](const std::shared_ptr<MessageT>& msg,
const transport::MessageInfo& msg_info, const transport::MessageInfo& msg_info,
const proto::RoleAttributes& reader_attr) { const proto::RoleAttributes& reader_attr) {
......
...@@ -71,7 +71,8 @@ bool Writer<MessageT>::Init() { ...@@ -71,7 +71,8 @@ bool Writer<MessageT>::Init() {
if (init_.exchange(true)) { if (init_.exchange(true)) {
return true; return true;
} }
transmitter_ = transport::Transport::CreateTransmitter<MessageT>(role_attr_); transmitter_ =
transport::Transport::Instance()->CreateTransmitter<MessageT>(role_attr_);
RETURN_VAL_IF_NULL(transmitter_, false); RETURN_VAL_IF_NULL(transmitter_, false);
this->role_attr_.set_id(transmitter_->id().HashValue()); this->role_attr_.set_id(transmitter_->id().HashValue());
channel_manager_ = channel_manager_ =
......
...@@ -115,8 +115,9 @@ bool Client<Request, Response>::Init() { ...@@ -115,8 +115,9 @@ bool Client<Request, Response>::Init() {
role.set_channel_id(channel_id); role.set_channel_id(channel_id);
role.mutable_qos_profile()->CopyFrom( role.mutable_qos_profile()->CopyFrom(
transport::QosProfileConf::QOS_PROFILE_SERVICES_DEFAULT); transport::QosProfileConf::QOS_PROFILE_SERVICES_DEFAULT);
request_transmitter_ = transport::Transport::CreateTransmitter<Request>( auto transport = transport::Transport::Instance();
role, proto::OptionalMode::RTPS); request_transmitter_ =
transport->CreateTransmitter<Request>(role, proto::OptionalMode::RTPS);
if (request_transmitter_ == nullptr) { if (request_transmitter_ == nullptr) {
AERROR << "Create request pub failed."; AERROR << "Create request pub failed.";
return false; return false;
...@@ -130,7 +131,7 @@ bool Client<Request, Response>::Init() { ...@@ -130,7 +131,7 @@ bool Client<Request, Response>::Init() {
role.set_channel_name(response_channel_); role.set_channel_name(response_channel_);
channel_id = common::GlobalData::RegisterChannel(response_channel_); channel_id = common::GlobalData::RegisterChannel(response_channel_);
role.set_channel_id(channel_id); role.set_channel_id(channel_id);
response_receiver_ = transport::Transport::CreateReceiver<Response>( response_receiver_ = transport->CreateReceiver<Response>(
role, role,
[=](const std::shared_ptr<Response>& request, [=](const std::shared_ptr<Response>& request,
const transport::MessageInfo& message_info, const transport::MessageInfo& message_info,
......
...@@ -90,8 +90,9 @@ bool Service<Request, Response>::Init() { ...@@ -90,8 +90,9 @@ bool Service<Request, Response>::Init() {
role.set_channel_id(channel_id); role.set_channel_id(channel_id);
role.mutable_qos_profile()->CopyFrom( role.mutable_qos_profile()->CopyFrom(
transport::QosProfileConf::QOS_PROFILE_SERVICES_DEFAULT); transport::QosProfileConf::QOS_PROFILE_SERVICES_DEFAULT);
response_transmitter_ = transport::Transport::CreateTransmitter<Response>( auto transport = transport::Transport::Instance();
role, proto::OptionalMode::RTPS); response_transmitter_ =
transport->CreateTransmitter<Response>(role, proto::OptionalMode::RTPS);
if (response_transmitter_ == nullptr) { if (response_transmitter_ == nullptr) {
AERROR << " Create response pub failed."; AERROR << " Create response pub failed.";
return false; return false;
...@@ -104,7 +105,7 @@ bool Service<Request, Response>::Init() { ...@@ -104,7 +105,7 @@ bool Service<Request, Response>::Init() {
role.set_channel_name(request_channel_); role.set_channel_name(request_channel_);
channel_id = common::GlobalData::RegisterChannel(request_channel_); channel_id = common::GlobalData::RegisterChannel(request_channel_);
role.set_channel_id(channel_id); role.set_channel_id(channel_id);
request_receiver_ = transport::Transport::CreateReceiver<Request>( request_receiver_ = transport->CreateReceiver<Request>(
role, role,
[=](const std::shared_ptr<Request>& request, [=](const std::shared_ptr<Request>& request,
const transport::MessageInfo& message_info, const transport::MessageInfo& message_info,
......
...@@ -48,13 +48,13 @@ class HybridTransceiverTest : public ::testing::Test { ...@@ -48,13 +48,13 @@ class HybridTransceiverTest : public ::testing::Test {
attr.set_channel_id(common::Hash(channel_name_)); attr.set_channel_id(common::Hash(channel_name_));
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT);
transmitter_a_ = std::make_shared<HybridTransmitter<proto::UnitTest>>( transmitter_a_ = std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
attr.set_process_id(54321); attr.set_process_id(54321);
attr.mutable_qos_profile()->CopyFrom( attr.mutable_qos_profile()->CopyFrom(
QosProfileConf::QOS_PROFILE_TOPO_CHANGE); QosProfileConf::QOS_PROFILE_TOPO_CHANGE);
transmitter_b_ = std::make_shared<HybridTransmitter<proto::UnitTest>>( transmitter_b_ = std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
} }
virtual void TearDown() { virtual void TearDown() {
...@@ -71,9 +71,9 @@ TEST_F(HybridTransceiverTest, constructor) { ...@@ -71,9 +71,9 @@ TEST_F(HybridTransceiverTest, constructor) {
RoleAttributes attr; RoleAttributes attr;
TransmitterPtr transmitter = TransmitterPtr transmitter =
std::make_shared<HybridTransmitter<proto::UnitTest>>( std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
ReceiverPtr receiver = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, nullptr, Transport::participant()); attr, nullptr, Transport::Instance()->participant());
EXPECT_EQ(transmitter->seq_num(), 0); EXPECT_EQ(transmitter->seq_num(), 0);
...@@ -103,7 +103,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_no_param) { ...@@ -103,7 +103,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_no_param) {
(void)attr; (void)attr;
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
receiver->Enable(); receiver->Enable();
...@@ -155,7 +155,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) { ...@@ -155,7 +155,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE);
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
...@@ -167,7 +167,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) { ...@@ -167,7 +167,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
auto msg = std::make_shared<proto::UnitTest>(); auto msg = std::make_shared<proto::UnitTest>();
msg->set_class_name("HybridTransceiverTest"); msg->set_class_name("HybridTransceiverTest");
...@@ -208,7 +208,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) { ...@@ -208,7 +208,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE);
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
...@@ -220,7 +220,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) { ...@@ -220,7 +220,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_process"); std::string case_name("enable_and_disable_with_param_same_process");
...@@ -280,7 +280,7 @@ TEST_F(HybridTransceiverTest, ...@@ -280,7 +280,7 @@ TEST_F(HybridTransceiverTest,
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE);
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
...@@ -292,7 +292,7 @@ TEST_F(HybridTransceiverTest, ...@@ -292,7 +292,7 @@ TEST_F(HybridTransceiverTest,
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_host_diff_proc"); std::string case_name("enable_and_disable_with_param_same_host_diff_proc");
...@@ -348,7 +348,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) { ...@@ -348,7 +348,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_TOPO_CHANGE);
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
...@@ -360,7 +360,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) { ...@@ -360,7 +360,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_host_diff_proc"); std::string case_name("enable_and_disable_with_param_same_host_diff_proc");
......
...@@ -74,7 +74,7 @@ TEST(RtpsDispatcherTest, on_message) { ...@@ -74,7 +74,7 @@ TEST(RtpsDispatcherTest, on_message) {
recv_msg->CopyFrom(*msg); recv_msg->CopyFrom(*msg);
}); });
auto transmitter = Transport::CreateTransmitter<proto::Chatter>( auto transmitter = Transport::Instance()->CreateTransmitter<proto::Chatter>(
self_attr, proto::OptionalMode::RTPS); self_attr, proto::OptionalMode::RTPS);
EXPECT_TRUE(transmitter != nullptr); EXPECT_TRUE(transmitter != nullptr);
......
...@@ -42,9 +42,9 @@ class RtpsTransceiverTest : public ::testing::Test { ...@@ -42,9 +42,9 @@ class RtpsTransceiverTest : public ::testing::Test {
attr.set_channel_name(channel_name_); attr.set_channel_name(channel_name_);
attr.set_channel_id(common::Hash(channel_name_)); attr.set_channel_id(common::Hash(channel_name_));
transmitter_a_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>( transmitter_a_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
transmitter_b_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>( transmitter_b_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
transmitter_a_->Enable(); transmitter_a_->Enable();
transmitter_b_->Enable(); transmitter_b_->Enable();
...@@ -64,7 +64,7 @@ TEST_F(RtpsTransceiverTest, constructor) { ...@@ -64,7 +64,7 @@ TEST_F(RtpsTransceiverTest, constructor) {
RoleAttributes attr; RoleAttributes attr;
TransmitterPtr transmitter = TransmitterPtr transmitter =
std::make_shared<RtpsTransmitter<proto::UnitTest>>( std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
ReceiverPtr receiver = ReceiverPtr receiver =
std::make_shared<RtpsReceiver<proto::UnitTest>>(attr, nullptr); std::make_shared<RtpsReceiver<proto::UnitTest>>(attr, nullptr);
......
...@@ -66,8 +66,9 @@ TEST(ShmDispatcherTest, on_message) { ...@@ -66,8 +66,9 @@ TEST(ShmDispatcherTest, on_message) {
Identity oppo_id; Identity oppo_id;
oppo_attr.set_id(oppo_id.HashValue()); oppo_attr.set_id(oppo_id.HashValue());
auto transmitter = Transport::CreateTransmitter<message::RawMessage>( auto transmitter =
oppo_attr, proto::OptionalMode::SHM); Transport::Instance()->CreateTransmitter<message::RawMessage>(
oppo_attr, proto::OptionalMode::SHM);
EXPECT_TRUE(transmitter != nullptr); EXPECT_TRUE(transmitter != nullptr);
auto send_msg = std::make_shared<message::RawMessage>("raw_message"); auto send_msg = std::make_shared<message::RawMessage>("raw_message");
......
...@@ -30,9 +30,9 @@ using TransmitterPtr = std::shared_ptr<Transmitter<proto::UnitTest>>; ...@@ -30,9 +30,9 @@ using TransmitterPtr = std::shared_ptr<Transmitter<proto::UnitTest>>;
using ReceiverPtr = std::shared_ptr<Receiver<proto::UnitTest>>; using ReceiverPtr = std::shared_ptr<Receiver<proto::UnitTest>>;
TEST(TransportTest, constructor) { TEST(TransportTest, constructor) {
Transport transport_a; auto transport_a = Transport::Instance();
Transport transport_b; auto transport_b = Transport::Instance();
EXPECT_EQ(transport_a.participant(), transport_b.participant()); EXPECT_EQ(transport_a->participant(), transport_b->participant());
} }
TEST(TransportTest, create_transmitter) { TEST(TransportTest, create_transmitter) {
...@@ -45,19 +45,23 @@ TEST(TransportTest, create_transmitter) { ...@@ -45,19 +45,23 @@ TEST(TransportTest, create_transmitter) {
attr.set_id(id.HashValue()); attr.set_id(id.HashValue());
TransmitterPtr intra = TransmitterPtr intra =
Transport::CreateTransmitter<proto::UnitTest>(attr, OptionalMode::INTRA); Transport::Instance()->CreateTransmitter<proto::UnitTest>(
attr, OptionalMode::INTRA);
EXPECT_EQ(typeid(*intra), typeid(IntraTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*intra), typeid(IntraTransmitter<proto::UnitTest>));
TransmitterPtr shm = TransmitterPtr shm =
Transport::CreateTransmitter<proto::UnitTest>(attr, OptionalMode::SHM); Transport::Instance()->CreateTransmitter<proto::UnitTest>(
attr, OptionalMode::SHM);
EXPECT_EQ(typeid(*shm), typeid(ShmTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*shm), typeid(ShmTransmitter<proto::UnitTest>));
TransmitterPtr rtps = TransmitterPtr rtps =
Transport::CreateTransmitter<proto::UnitTest>(attr, OptionalMode::RTPS); Transport::Instance()->CreateTransmitter<proto::UnitTest>(
attr, OptionalMode::RTPS);
EXPECT_EQ(typeid(*rtps), typeid(RtpsTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*rtps), typeid(RtpsTransmitter<proto::UnitTest>));
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT);
TransmitterPtr hybrid = Transport::CreateTransmitter<proto::UnitTest>(attr); TransmitterPtr hybrid =
Transport::Instance()->CreateTransmitter<proto::UnitTest>(attr);
EXPECT_EQ(typeid(*hybrid), typeid(HybridTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*hybrid), typeid(HybridTransmitter<proto::UnitTest>));
} }
...@@ -70,21 +74,21 @@ TEST(TransportTest, create_receiver) { ...@@ -70,21 +74,21 @@ TEST(TransportTest, create_receiver) {
auto listener = [](const std::shared_ptr<proto::UnitTest>&, auto listener = [](const std::shared_ptr<proto::UnitTest>&,
const MessageInfo&, const RoleAttributes&) {}; const MessageInfo&, const RoleAttributes&) {};
ReceiverPtr intra = Transport::CreateReceiver<proto::UnitTest>( ReceiverPtr intra = Transport::Instance()->CreateReceiver<proto::UnitTest>(
attr, listener, OptionalMode::INTRA); attr, listener, OptionalMode::INTRA);
EXPECT_EQ(typeid(*intra), typeid(IntraReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*intra), typeid(IntraReceiver<proto::UnitTest>));
ReceiverPtr shm = Transport::CreateReceiver<proto::UnitTest>( ReceiverPtr shm = Transport::Instance()->CreateReceiver<proto::UnitTest>(
attr, listener, OptionalMode::SHM); attr, listener, OptionalMode::SHM);
EXPECT_EQ(typeid(*shm), typeid(ShmReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*shm), typeid(ShmReceiver<proto::UnitTest>));
ReceiverPtr rtps = Transport::CreateReceiver<proto::UnitTest>( ReceiverPtr rtps = Transport::Instance()->CreateReceiver<proto::UnitTest>(
attr, listener, OptionalMode::RTPS); attr, listener, OptionalMode::RTPS);
EXPECT_EQ(typeid(*rtps), typeid(RtpsReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*rtps), typeid(RtpsReceiver<proto::UnitTest>));
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT);
ReceiverPtr hybrid = ReceiverPtr hybrid =
Transport::CreateReceiver<proto::UnitTest>(attr, listener); Transport::Instance()->CreateReceiver<proto::UnitTest>(attr, listener);
EXPECT_EQ(typeid(*hybrid), typeid(HybridReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*hybrid), typeid(HybridReceiver<proto::UnitTest>));
} }
......
...@@ -6,15 +6,14 @@ cc_library( ...@@ -6,15 +6,14 @@ cc_library(
name = "transport", name = "transport",
srcs = [ srcs = [
"transport.cc", "transport.cc",
"dispatcher/rtps_dispatcher.cc",
], ],
hdrs = [ hdrs = [
"transport.h", "transport.h",
"dispatcher/rtps_dispatcher.h",
], ],
deps = [ deps = [
"history", "history",
"intra_dispatcher", "intra_dispatcher",
"rtps_dispatcher",
"shm_dispatcher", "shm_dispatcher",
"hybrid_receiver", "hybrid_receiver",
"intra_receiver", "intra_receiver",
...@@ -137,6 +136,20 @@ cc_test( ...@@ -137,6 +136,20 @@ cc_test(
], ],
) )
cc_library(
name = "rtps_dispatcher",
srcs = [ "dispatcher/rtps_dispatcher.cc", ],
hdrs = [ "dispatcher/rtps_dispatcher.h", ],
deps = [
"//cyber/proto:role_attributes_cc_proto",
"//cyber/message:message_traits",
"dispatcher",
"attributes_filler",
"sub_listener",
"participant",
],
)
cc_test( cc_test(
name = "rtps_dispatcher_test", name = "rtps_dispatcher_test",
size = "small", size = "small",
...@@ -146,7 +159,7 @@ cc_test( ...@@ -146,7 +159,7 @@ cc_test(
deps = [ deps = [
"//cyber:cyber_core", "//cyber:cyber_core",
"//cyber/proto:chatter_cc_proto", "//cyber/proto:chatter_cc_proto",
"@gtest//:main", "@gtest",
], ],
) )
......
...@@ -20,11 +20,14 @@ namespace apollo { ...@@ -20,11 +20,14 @@ namespace apollo {
namespace cyber { namespace cyber {
namespace transport { namespace transport {
Dispatcher::Dispatcher() : shutdown_(false) {} Dispatcher::Dispatcher() : is_shutdown_(false) {}
Dispatcher::~Dispatcher() { Shutdown(); } Dispatcher::~Dispatcher() { Shutdown(); }
void Dispatcher::Shutdown() { ADEBUG << "Shutdown"; } void Dispatcher::Shutdown() {
is_shutdown_.exchange(true);
ADEBUG << "Shutdown";
}
bool Dispatcher::HasChannel(uint64_t channel_id) { bool Dispatcher::HasChannel(uint64_t channel_id) {
return msg_listeners_.Has(channel_id); return msg_listeners_.Has(channel_id);
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#ifndef CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_ #ifndef CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_
#define CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_ #define CYBER_TRANSPORT_DISPATCHER_DISPATCHER_H_
#include <atomic>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
...@@ -76,7 +77,7 @@ class Dispatcher { ...@@ -76,7 +77,7 @@ class Dispatcher {
bool HasChannel(uint64_t channel_id); bool HasChannel(uint64_t channel_id);
protected: protected:
bool shutdown_; std::atomic<bool> is_shutdown_;
// key: channel_id of message // key: channel_id of message
AtomicHashMap<uint64_t, ListenerHandlerBasePtr> msg_listeners_; AtomicHashMap<uint64_t, ListenerHandlerBasePtr> msg_listeners_;
base::AtomicRWLock rw_lock_; base::AtomicRWLock rw_lock_;
...@@ -85,6 +86,9 @@ class Dispatcher { ...@@ -85,6 +86,9 @@ class Dispatcher {
template <typename MessageT> template <typename MessageT>
void Dispatcher::AddListener(const RoleAttributes& self_attr, void Dispatcher::AddListener(const RoleAttributes& self_attr,
const MessageListener<MessageT>& listener) { const MessageListener<MessageT>& listener) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id(); uint64_t channel_id = self_attr.channel_id();
std::shared_ptr<ListenerHandler<MessageT>> handler; std::shared_ptr<ListenerHandler<MessageT>> handler;
...@@ -105,6 +109,9 @@ template <typename MessageT> ...@@ -105,6 +109,9 @@ template <typename MessageT>
void Dispatcher::AddListener(const RoleAttributes& self_attr, void Dispatcher::AddListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr, const RoleAttributes& opposite_attr,
const MessageListener<MessageT>& listener) { const MessageListener<MessageT>& listener) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id(); uint64_t channel_id = self_attr.channel_id();
std::shared_ptr<ListenerHandler<MessageT>> handler; std::shared_ptr<ListenerHandler<MessageT>> handler;
...@@ -123,6 +130,9 @@ void Dispatcher::AddListener(const RoleAttributes& self_attr, ...@@ -123,6 +130,9 @@ void Dispatcher::AddListener(const RoleAttributes& self_attr,
template <typename MessageT> template <typename MessageT>
void Dispatcher::RemoveListener(const RoleAttributes& self_attr) { void Dispatcher::RemoveListener(const RoleAttributes& self_attr) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id(); uint64_t channel_id = self_attr.channel_id();
ListenerHandlerBasePtr* handler_base = nullptr; ListenerHandlerBasePtr* handler_base = nullptr;
...@@ -134,6 +144,9 @@ void Dispatcher::RemoveListener(const RoleAttributes& self_attr) { ...@@ -134,6 +144,9 @@ void Dispatcher::RemoveListener(const RoleAttributes& self_attr) {
template <typename MessageT> template <typename MessageT>
void Dispatcher::RemoveListener(const RoleAttributes& self_attr, void Dispatcher::RemoveListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr) { const RoleAttributes& opposite_attr) {
if (is_shutdown_.load()) {
return;
}
uint64_t channel_id = self_attr.channel_id(); uint64_t channel_id = self_attr.channel_id();
ListenerHandlerBasePtr* handler_base = nullptr; ListenerHandlerBasePtr* handler_base = nullptr;
......
...@@ -49,6 +49,9 @@ template <typename MessageT> ...@@ -49,6 +49,9 @@ template <typename MessageT>
void IntraDispatcher::OnMessage(uint64_t channel_id, void IntraDispatcher::OnMessage(uint64_t channel_id,
const std::shared_ptr<MessageT>& message, const std::shared_ptr<MessageT>& message,
const MessageInfo& message_info) { const MessageInfo& message_info) {
if (is_shutdown_.load()) {
return;
}
ADEBUG << "intra on message, channel:" ADEBUG << "intra on message, channel:"
<< common::GlobalData::GetChannelById(channel_id); << common::GlobalData::GetChannelById(channel_id);
ListenerHandlerBasePtr* handler_base = nullptr; ListenerHandlerBasePtr* handler_base = nullptr;
......
...@@ -16,29 +16,35 @@ ...@@ -16,29 +16,35 @@
#include "cyber/transport/dispatcher/rtps_dispatcher.h" #include "cyber/transport/dispatcher/rtps_dispatcher.h"
#include "cyber/transport/transport.h"
namespace apollo { namespace apollo {
namespace cyber { namespace cyber {
namespace transport { namespace transport {
RtpsDispatcher::RtpsDispatcher() {} RtpsDispatcher::RtpsDispatcher() : participant_(nullptr) {}
RtpsDispatcher::~RtpsDispatcher() { Shutdown(); } RtpsDispatcher::~RtpsDispatcher() { Shutdown(); }
void RtpsDispatcher::Shutdown() { void RtpsDispatcher::Shutdown() {
if (shutdown_) { if (is_shutdown_.exchange(true)) {
return; return;
} }
shutdown_ = true;
std::lock_guard<std::mutex> lock(subs_mutex_); {
for (auto& item : subs_) { std::lock_guard<std::mutex> lock(subs_mutex_);
item.second.sub = nullptr; for (auto& item : subs_) {
item.second.sub = nullptr;
}
} }
participant_ = nullptr;
} }
void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) { void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) {
if (participant_ == nullptr) {
AWARN << "please set participant firstly.";
return;
}
uint64_t channel_id = self_attr.channel_id(); uint64_t channel_id = self_attr.channel_id();
std::lock_guard<std::mutex> lock(subs_mutex_); std::lock_guard<std::mutex> lock(subs_mutex_);
if (subs_.count(channel_id) > 0) { if (subs_.count(channel_id) > 0) {
...@@ -55,7 +61,7 @@ void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) { ...@@ -55,7 +61,7 @@ void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) {
std::placeholders::_2, std::placeholders::_3)); std::placeholders::_2, std::placeholders::_3));
new_sub.sub = eprosima::fastrtps::Domain::createSubscriber( new_sub.sub = eprosima::fastrtps::Domain::createSubscriber(
Transport::participant()->fastrtps_participant(), sub_attr, participant_->fastrtps_participant(), sub_attr,
new_sub.sub_listener.get()); new_sub.sub_listener.get());
RETURN_IF_NULL(new_sub.sub); RETURN_IF_NULL(new_sub.sub);
subs_[channel_id] = new_sub; subs_[channel_id] = new_sub;
...@@ -64,6 +70,10 @@ void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) { ...@@ -64,6 +70,10 @@ void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) {
void RtpsDispatcher::OnMessage(uint64_t channel_id, void RtpsDispatcher::OnMessage(uint64_t channel_id,
const std::shared_ptr<std::string>& msg_str, const std::shared_ptr<std::string>& msg_str,
const MessageInfo& msg_info) { const MessageInfo& msg_info) {
if (is_shutdown_.load()) {
return;
}
ListenerHandlerBasePtr* handler_base = nullptr; ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) { if (msg_listeners_.Get(channel_id, &handler_base)) {
auto handler = auto handler =
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "cyber/message/message_traits.h" #include "cyber/message/message_traits.h"
#include "cyber/transport/dispatcher/dispatcher.h" #include "cyber/transport/dispatcher/dispatcher.h"
#include "cyber/transport/rtps/attributes_filler.h" #include "cyber/transport/rtps/attributes_filler.h"
#include "cyber/transport/rtps/participant.h"
#include "cyber/transport/rtps/sub_listener.h" #include "cyber/transport/rtps/sub_listener.h"
namespace apollo { namespace apollo {
...@@ -59,6 +60,10 @@ class RtpsDispatcher : public Dispatcher { ...@@ -59,6 +60,10 @@ class RtpsDispatcher : public Dispatcher {
const RoleAttributes& opposite_attr, const RoleAttributes& opposite_attr,
const MessageListener<MessageT>& listener); const MessageListener<MessageT>& listener);
void set_participant(const ParticipantPtr& participant) {
participant_ = participant;
}
private: private:
void OnMessage(uint64_t channel_id, void OnMessage(uint64_t channel_id,
const std::shared_ptr<std::string>& msg_str, const std::shared_ptr<std::string>& msg_str,
...@@ -68,6 +73,8 @@ class RtpsDispatcher : public Dispatcher { ...@@ -68,6 +73,8 @@ class RtpsDispatcher : public Dispatcher {
std::unordered_map<uint64_t, Subscriber> subs_; std::unordered_map<uint64_t, Subscriber> subs_;
std::mutex subs_mutex_; std::mutex subs_mutex_;
ParticipantPtr participant_;
DECLARE_SINGLETON(RtpsDispatcher) DECLARE_SINGLETON(RtpsDispatcher)
}; };
...@@ -75,8 +82,8 @@ template <typename MessageT> ...@@ -75,8 +82,8 @@ template <typename MessageT>
void RtpsDispatcher::AddListener(const RoleAttributes& self_attr, void RtpsDispatcher::AddListener(const RoleAttributes& self_attr,
const MessageListener<MessageT>& listener) { const MessageListener<MessageT>& listener) {
auto listener_adapter = [listener]( auto listener_adapter = [listener](
const std::shared_ptr<std::string>& msg_str, const std::shared_ptr<std::string>& msg_str,
const MessageInfo& msg_info) { const MessageInfo& msg_info) {
auto msg = std::make_shared<MessageT>(); auto msg = std::make_shared<MessageT>();
RETURN_IF(!message::ParseFromString(*msg_str, msg.get())); RETURN_IF(!message::ParseFromString(*msg_str, msg.get()));
listener(msg, msg_info); listener(msg, msg_info);
...@@ -91,8 +98,8 @@ void RtpsDispatcher::AddListener(const RoleAttributes& self_attr, ...@@ -91,8 +98,8 @@ void RtpsDispatcher::AddListener(const RoleAttributes& self_attr,
const RoleAttributes& opposite_attr, const RoleAttributes& opposite_attr,
const MessageListener<MessageT>& listener) { const MessageListener<MessageT>& listener) {
auto listener_adapter = [listener]( auto listener_adapter = [listener](
const std::shared_ptr<std::string>& msg_str, const std::shared_ptr<std::string>& msg_str,
const MessageInfo& msg_info) { const MessageInfo& msg_info) {
auto msg = std::make_shared<MessageT>(); auto msg = std::make_shared<MessageT>();
RETURN_IF(!message::ParseFromString(*msg_str, msg.get())); RETURN_IF(!message::ParseFromString(*msg_str, msg.get()));
listener(msg, msg_info); listener(msg, msg_info);
......
...@@ -75,7 +75,7 @@ TEST(RtpsDispatcherTest, on_message) { ...@@ -75,7 +75,7 @@ TEST(RtpsDispatcherTest, on_message) {
recv_msg->CopyFrom(*msg); recv_msg->CopyFrom(*msg);
}); });
auto transmitter = Transport::CreateTransmitter<proto::Chatter>( auto transmitter = Transport::Instance()->CreateTransmitter<proto::Chatter>(
self_attr, proto::OptionalMode::RTPS); self_attr, proto::OptionalMode::RTPS);
EXPECT_TRUE(transmitter != nullptr); EXPECT_TRUE(transmitter != nullptr);
...@@ -105,3 +105,11 @@ TEST(RtpsDispatcherTest, shutdown) { ...@@ -105,3 +105,11 @@ TEST(RtpsDispatcherTest, shutdown) {
} // namespace transport } // namespace transport
} // namespace cyber } // namespace cyber
} // namespace apollo } // namespace apollo
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
apollo::cyber::transport::Transport::Instance();
auto res = RUN_ALL_TESTS();
apollo::cyber::transport::Transport::Instance()->Shutdown();
return res;
}
...@@ -30,10 +30,10 @@ ShmDispatcher::ShmDispatcher() : host_id_(0), sfd_(-1) { Init(); } ...@@ -30,10 +30,10 @@ ShmDispatcher::ShmDispatcher() : host_id_(0), sfd_(-1) { Init(); }
ShmDispatcher::~ShmDispatcher() { Shutdown(); } ShmDispatcher::~ShmDispatcher() { Shutdown(); }
void ShmDispatcher::Shutdown() { void ShmDispatcher::Shutdown() {
if (shutdown_) { if (is_shutdown_.exchange(true)) {
return; return;
} }
shutdown_ = true;
segments_.clear(); segments_.clear();
if (thread_.joinable()) { if (thread_.joinable()) {
thread_.join(); thread_.join();
...@@ -54,6 +54,9 @@ void ShmDispatcher::AddSegment(const RoleAttributes& self_attr) { ...@@ -54,6 +54,9 @@ void ShmDispatcher::AddSegment(const RoleAttributes& self_attr) {
void ShmDispatcher::OnMessage(uint64_t channel_id, void ShmDispatcher::OnMessage(uint64_t channel_id,
const std::shared_ptr<std::string>& msg_str, const std::shared_ptr<std::string>& msg_str,
const MessageInfo& msg_info) { const MessageInfo& msg_info) {
if (is_shutdown_.load()) {
return;
}
ListenerHandlerBasePtr* handler_base = nullptr; ListenerHandlerBasePtr* handler_base = nullptr;
if (msg_listeners_.Get(channel_id, &handler_base)) { if (msg_listeners_.Get(channel_id, &handler_base)) {
auto handler = auto handler =
...@@ -69,7 +72,7 @@ void ShmDispatcher::ThreadFunc() { ...@@ -69,7 +72,7 @@ void ShmDispatcher::ThreadFunc() {
std::string msg_info_str(""); std::string msg_info_str("");
std::shared_ptr<std::string> msg_str_ptr = std::make_shared<std::string>(); std::shared_ptr<std::string> msg_str_ptr = std::make_shared<std::string>();
while (!shutdown_) { while (!is_shutdown_.load()) {
int read_bytes = int read_bytes =
recvfrom(sfd_, buf, 1024, 0, (struct sockaddr*)&local_addr_, recvfrom(sfd_, buf, 1024, 0, (struct sockaddr*)&local_addr_,
reinterpret_cast<socklen_t*>(&addr_len)); reinterpret_cast<socklen_t*>(&addr_len));
......
...@@ -67,8 +67,9 @@ TEST(ShmDispatcherTest, on_message) { ...@@ -67,8 +67,9 @@ TEST(ShmDispatcherTest, on_message) {
Identity oppo_id; Identity oppo_id;
oppo_attr.set_id(oppo_id.HashValue()); oppo_attr.set_id(oppo_id.HashValue());
auto transmitter = Transport::CreateTransmitter<message::RawMessage>( auto transmitter =
oppo_attr, proto::OptionalMode::SHM); Transport::Instance()->CreateTransmitter<message::RawMessage>(
oppo_attr, proto::OptionalMode::SHM);
EXPECT_TRUE(transmitter != nullptr); EXPECT_TRUE(transmitter != nullptr);
auto send_msg = std::make_shared<message::RawMessage>("raw_message"); auto send_msg = std::make_shared<message::RawMessage>("raw_message");
......
...@@ -48,6 +48,10 @@ void Participant::Shutdown() { ...@@ -48,6 +48,10 @@ void Participant::Shutdown() {
} }
eprosima::fastrtps::Participant* Participant::fastrtps_participant() { eprosima::fastrtps::Participant* Participant::fastrtps_participant() {
if (shutdown_.load()) {
return nullptr;
}
std::lock_guard<std::mutex> lck(mutex_); std::lock_guard<std::mutex> lck(mutex_);
if (fastrtps_participant_ != nullptr) { if (fastrtps_participant_ != nullptr) {
return fastrtps_participant_; return fastrtps_participant_;
......
...@@ -50,12 +50,12 @@ class HybridTransceiverTest : public ::testing::Test { ...@@ -50,12 +50,12 @@ class HybridTransceiverTest : public ::testing::Test {
attr.set_channel_id(common::Hash(channel_name_)); attr.set_channel_id(common::Hash(channel_name_));
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT);
transmitter_a_ = std::make_shared<HybridTransmitter<proto::UnitTest>>( transmitter_a_ = std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
attr.set_process_id(common::GlobalData::Instance()->ProcessId() + 1); attr.set_process_id(common::GlobalData::Instance()->ProcessId() + 1);
attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT); attr.mutable_qos_profile()->CopyFrom(QosProfileConf::QOS_PROFILE_DEFAULT);
transmitter_b_ = std::make_shared<HybridTransmitter<proto::UnitTest>>( transmitter_b_ = std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
} }
virtual void TearDown() { virtual void TearDown() {
...@@ -72,9 +72,9 @@ TEST_F(HybridTransceiverTest, constructor) { ...@@ -72,9 +72,9 @@ TEST_F(HybridTransceiverTest, constructor) {
RoleAttributes attr; RoleAttributes attr;
TransmitterPtr transmitter = TransmitterPtr transmitter =
std::make_shared<HybridTransmitter<proto::UnitTest>>( std::make_shared<HybridTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
ReceiverPtr receiver = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, nullptr, Transport::participant()); attr, nullptr, Transport::Instance()->participant());
EXPECT_EQ(transmitter->seq_num(), 0); EXPECT_EQ(transmitter->seq_num(), 0);
...@@ -104,7 +104,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_no_param) { ...@@ -104,7 +104,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_no_param) {
(void)attr; (void)attr;
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
receiver->Enable(); receiver->Enable();
...@@ -156,7 +156,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) { ...@@ -156,7 +156,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, attr,
...@@ -167,7 +167,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) { ...@@ -167,7 +167,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_no_relation) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
auto msg = std::make_shared<proto::UnitTest>(); auto msg = std::make_shared<proto::UnitTest>();
msg->set_class_name("HybridTransceiverTest"); msg->set_class_name("HybridTransceiverTest");
...@@ -208,7 +208,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) { ...@@ -208,7 +208,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, attr,
...@@ -219,7 +219,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) { ...@@ -219,7 +219,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_same_process) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_process"); std::string case_name("enable_and_disable_with_param_same_process");
...@@ -279,7 +279,7 @@ TEST_F(HybridTransceiverTest, ...@@ -279,7 +279,7 @@ TEST_F(HybridTransceiverTest,
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, attr,
...@@ -290,7 +290,7 @@ TEST_F(HybridTransceiverTest, ...@@ -290,7 +290,7 @@ TEST_F(HybridTransceiverTest,
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_host_diff_proc"); std::string case_name("enable_and_disable_with_param_same_host_diff_proc");
...@@ -346,7 +346,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) { ...@@ -346,7 +346,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>( ReceiverPtr receiver_b = std::make_shared<HybridReceiver<proto::UnitTest>>(
attr, attr,
...@@ -357,7 +357,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) { ...@@ -357,7 +357,7 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) {
std::lock_guard<std::mutex> lock(mtx); std::lock_guard<std::mutex> lock(mtx);
msgs.emplace_back(*msg); msgs.emplace_back(*msg);
}, },
Transport::participant()); Transport::Instance()->participant());
std::string class_name("HybridTransceiverTest"); std::string class_name("HybridTransceiverTest");
std::string case_name("enable_and_disable_with_param_same_host_diff_proc"); std::string case_name("enable_and_disable_with_param_same_host_diff_proc");
...@@ -395,7 +395,8 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) { ...@@ -395,7 +395,8 @@ TEST_F(HybridTransceiverTest, enable_and_disable_with_param_diff_host) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
apollo::cyber::transport::Transport::Instance();
auto res = RUN_ALL_TESTS(); auto res = RUN_ALL_TESTS();
apollo::cyber::transport::Transport::Shutdown(); apollo::cyber::transport::Transport::Instance()->Shutdown();
return res; return res;
} }
...@@ -44,9 +44,9 @@ class RtpsTransceiverTest : public ::testing::Test { ...@@ -44,9 +44,9 @@ class RtpsTransceiverTest : public ::testing::Test {
attr.set_channel_name(channel_name_); attr.set_channel_name(channel_name_);
attr.set_channel_id(common::Hash(channel_name_)); attr.set_channel_id(common::Hash(channel_name_));
transmitter_a_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>( transmitter_a_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
transmitter_b_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>( transmitter_b_ = std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
transmitter_a_->Enable(); transmitter_a_->Enable();
transmitter_b_->Enable(); transmitter_b_->Enable();
...@@ -66,7 +66,7 @@ TEST_F(RtpsTransceiverTest, constructor) { ...@@ -66,7 +66,7 @@ TEST_F(RtpsTransceiverTest, constructor) {
RoleAttributes attr; RoleAttributes attr;
TransmitterPtr transmitter = TransmitterPtr transmitter =
std::make_shared<RtpsTransmitter<proto::UnitTest>>( std::make_shared<RtpsTransmitter<proto::UnitTest>>(
attr, Transport::participant()); attr, Transport::Instance()->participant());
ReceiverPtr receiver = ReceiverPtr receiver =
std::make_shared<RtpsReceiver<proto::UnitTest>>(attr, nullptr); std::make_shared<RtpsReceiver<proto::UnitTest>>(attr, nullptr);
...@@ -154,7 +154,8 @@ TEST_F(RtpsTransceiverTest, enable_and_disable) { ...@@ -154,7 +154,8 @@ TEST_F(RtpsTransceiverTest, enable_and_disable) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
apollo::cyber::transport::Transport::Instance();
auto res = RUN_ALL_TESTS(); auto res = RUN_ALL_TESTS();
apollo::cyber::transport::Transport::Shutdown(); apollo::cyber::transport::Transport::Instance()->Shutdown();
return res; return res;
} }
...@@ -16,50 +16,45 @@ ...@@ -16,50 +16,45 @@
#include "cyber/transport/transport.h" #include "cyber/transport/transport.h"
#include <atomic>
#include <mutex>
#include "cyber/common/global_data.h" #include "cyber/common/global_data.h"
namespace apollo { namespace apollo {
namespace cyber { namespace cyber {
namespace transport { namespace transport {
static std::atomic<bool> shutdown_ = {false}; Transport::Transport()
static std::mutex participant_mutex_; : is_shutdown_(false),
participant_(nullptr),
ParticipantPtr Transport::participant_ = nullptr; intra_dispatcher_(nullptr),
shm_dispatcher_(nullptr),
Transport::Transport() {} rtps_dispatcher_(nullptr) {
CreateParticipant();
intra_dispatcher_ = IntraDispatcher::Instance();
shm_dispatcher_ = ShmDispatcher::Instance();
rtps_dispatcher_ = RtpsDispatcher::Instance();
rtps_dispatcher_->set_participant(participant_);
}
Transport::~Transport() {} Transport::~Transport() { Shutdown(); }
void Transport::Shutdown() { void Transport::Shutdown() {
if (shutdown_.exchange(true)) { if (is_shutdown_.exchange(true)) {
return; return;
} }
intra_dispatcher_->Shutdown();
shm_dispatcher_->Shutdown();
rtps_dispatcher_->Shutdown();
if (participant_ != nullptr) { if (participant_ != nullptr) {
participant_->Shutdown(); participant_->Shutdown();
participant_ = nullptr; participant_ = nullptr;
} }
} }
ParticipantPtr Transport::CreateParticipant() { void Transport::CreateParticipant() {
std::string participant_name = std::string participant_name =
common::GlobalData::Instance()->HostName() + "+" + common::GlobalData::Instance()->HostName() + "+" +
std::to_string(common::GlobalData::Instance()->ProcessId()); std::to_string(common::GlobalData::Instance()->ProcessId());
return std::make_shared<Participant>(participant_name, 11512); participant_ = std::make_shared<Participant>(participant_name, 11512);
}
ParticipantPtr Transport::participant() {
if (participant_ != nullptr) {
return participant_;
}
std::lock_guard<std::mutex> lck(participant_mutex_);
if (participant_ == nullptr) {
participant_ = CreateParticipant();
}
return participant_;
} }
} // namespace transport } // namespace transport
......
...@@ -17,10 +17,15 @@ ...@@ -17,10 +17,15 @@
#ifndef CYBER_TRANSPORT_TRANSPORT_H_ #ifndef CYBER_TRANSPORT_TRANSPORT_H_
#define CYBER_TRANSPORT_TRANSPORT_H_ #define CYBER_TRANSPORT_TRANSPORT_H_
#include <atomic>
#include <memory> #include <memory>
#include <string> #include <string>
#include "cyber/common/macros.h"
#include "cyber/proto/transport_conf.pb.h" #include "cyber/proto/transport_conf.pb.h"
#include "cyber/transport/dispatcher/intra_dispatcher.h"
#include "cyber/transport/dispatcher/rtps_dispatcher.h"
#include "cyber/transport/dispatcher/shm_dispatcher.h"
#include "cyber/transport/qos/qos_profile_conf.h" #include "cyber/transport/qos/qos_profile_conf.h"
#include "cyber/transport/receiver/hybrid_receiver.h" #include "cyber/transport/receiver/hybrid_receiver.h"
#include "cyber/transport/receiver/intra_receiver.h" #include "cyber/transport/receiver/intra_receiver.h"
...@@ -42,35 +47,43 @@ using apollo::cyber::proto::OptionalMode; ...@@ -42,35 +47,43 @@ using apollo::cyber::proto::OptionalMode;
class Transport { class Transport {
public: public:
Transport();
virtual ~Transport(); virtual ~Transport();
static void Shutdown(); void Shutdown();
template <typename M> template <typename M>
static auto CreateTransmitter(const RoleAttributes& attr, auto CreateTransmitter(const RoleAttributes& attr,
const OptionalMode& mode = OptionalMode::HYBRID) const OptionalMode& mode = OptionalMode::HYBRID) ->
-> typename std::shared_ptr<Transmitter<M>>; typename std::shared_ptr<Transmitter<M>>;
template <typename M> template <typename M>
static auto CreateReceiver( auto CreateReceiver(const RoleAttributes& attr,
const RoleAttributes& attr, const typename Receiver<M>::MessageListener& msg_listener,
const typename Receiver<M>::MessageListener& msg_listener, const OptionalMode& mode = OptionalMode::HYBRID) ->
const OptionalMode& mode = OptionalMode::HYBRID) ->
typename std::shared_ptr<Receiver<M>>; typename std::shared_ptr<Receiver<M>>;
static ParticipantPtr participant(); ParticipantPtr participant() const { return participant_; }
private: private:
static ParticipantPtr CreateParticipant(); void CreateParticipant();
static ParticipantPtr participant_; std::atomic<bool> is_shutdown_;
ParticipantPtr participant_;
IntraDispatcherPtr intra_dispatcher_;
ShmDispatcherPtr shm_dispatcher_;
RtpsDispatcherPtr rtps_dispatcher_;
DECLARE_SINGLETON(Transport)
}; };
template <typename M> template <typename M>
auto Transport::CreateTransmitter(const RoleAttributes& attr, auto Transport::CreateTransmitter(const RoleAttributes& attr,
const OptionalMode& mode) -> const OptionalMode& mode) ->
typename std::shared_ptr<Transmitter<M>> { typename std::shared_ptr<Transmitter<M>> {
if (is_shutdown_.load()) {
AINFO << "transport has been shut down.";
return nullptr;
}
std::shared_ptr<Transmitter<M>> transmitter = nullptr; std::shared_ptr<Transmitter<M>> transmitter = nullptr;
RoleAttributes modified_attr = attr; RoleAttributes modified_attr = attr;
if (!modified_attr.has_qos_profile()) { if (!modified_attr.has_qos_profile()) {
...@@ -109,6 +122,10 @@ auto Transport::CreateReceiver( ...@@ -109,6 +122,10 @@ auto Transport::CreateReceiver(
const RoleAttributes& attr, const RoleAttributes& attr,
const typename Receiver<M>::MessageListener& msg_listener, const typename Receiver<M>::MessageListener& msg_listener,
const OptionalMode& mode) -> typename std::shared_ptr<Receiver<M>> { const OptionalMode& mode) -> typename std::shared_ptr<Receiver<M>> {
if (is_shutdown_.load()) {
AINFO << "transport has been shut down.";
return nullptr;
}
std::shared_ptr<Receiver<M>> receiver = nullptr; std::shared_ptr<Receiver<M>> receiver = nullptr;
RoleAttributes modified_attr = attr; RoleAttributes modified_attr = attr;
if (!modified_attr.has_qos_profile()) { if (!modified_attr.has_qos_profile()) {
......
...@@ -30,9 +30,9 @@ using TransmitterPtr = std::shared_ptr<Transmitter<proto::UnitTest>>; ...@@ -30,9 +30,9 @@ using TransmitterPtr = std::shared_ptr<Transmitter<proto::UnitTest>>;
using ReceiverPtr = std::shared_ptr<Receiver<proto::UnitTest>>; using ReceiverPtr = std::shared_ptr<Receiver<proto::UnitTest>>;
TEST(TransportTest, constructor) { TEST(TransportTest, constructor) {
Transport transport_a; auto transport_a = Transport::Instance();
Transport transport_b; auto transport_b = Transport::Instance();
EXPECT_EQ(transport_a.participant(), transport_b.participant()); EXPECT_EQ(transport_a->participant(), transport_b->participant());
} }
TEST(TransportTest, create_transmitter) { TEST(TransportTest, create_transmitter) {
...@@ -45,11 +45,13 @@ TEST(TransportTest, create_transmitter) { ...@@ -45,11 +45,13 @@ TEST(TransportTest, create_transmitter) {
attr.set_id(id.HashValue()); attr.set_id(id.HashValue());
TransmitterPtr intra = TransmitterPtr intra =
Transport::CreateTransmitter<proto::UnitTest>(attr, OptionalMode::INTRA); Transport::Instance()->CreateTransmitter<proto::UnitTest>(
attr, OptionalMode::INTRA);
EXPECT_EQ(typeid(*intra), typeid(IntraTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*intra), typeid(IntraTransmitter<proto::UnitTest>));
TransmitterPtr shm = TransmitterPtr shm =
Transport::CreateTransmitter<proto::UnitTest>(attr, OptionalMode::SHM); Transport::Instance()->CreateTransmitter<proto::UnitTest>(
attr, OptionalMode::SHM);
EXPECT_EQ(typeid(*shm), typeid(ShmTransmitter<proto::UnitTest>)); EXPECT_EQ(typeid(*shm), typeid(ShmTransmitter<proto::UnitTest>));
} }
...@@ -62,11 +64,11 @@ TEST(TransportTest, create_receiver) { ...@@ -62,11 +64,11 @@ TEST(TransportTest, create_receiver) {
auto listener = [](const std::shared_ptr<proto::UnitTest>&, auto listener = [](const std::shared_ptr<proto::UnitTest>&,
const MessageInfo&, const RoleAttributes&) {}; const MessageInfo&, const RoleAttributes&) {};
ReceiverPtr intra = Transport::CreateReceiver<proto::UnitTest>( ReceiverPtr intra = Transport::Instance()->CreateReceiver<proto::UnitTest>(
attr, listener, OptionalMode::INTRA); attr, listener, OptionalMode::INTRA);
EXPECT_EQ(typeid(*intra), typeid(IntraReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*intra), typeid(IntraReceiver<proto::UnitTest>));
ReceiverPtr shm = Transport::CreateReceiver<proto::UnitTest>( ReceiverPtr shm = Transport::Instance()->CreateReceiver<proto::UnitTest>(
attr, listener, OptionalMode::SHM); attr, listener, OptionalMode::SHM);
EXPECT_EQ(typeid(*shm), typeid(ShmReceiver<proto::UnitTest>)); EXPECT_EQ(typeid(*shm), typeid(ShmReceiver<proto::UnitTest>));
} }
...@@ -77,7 +79,8 @@ TEST(TransportTest, create_receiver) { ...@@ -77,7 +79,8 @@ TEST(TransportTest, create_receiver) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
apollo::cyber::transport::Transport::Instance();
auto res = RUN_ALL_TESTS(); auto res = RUN_ALL_TESTS();
apollo::cyber::transport::Transport::Shutdown(); apollo::cyber::transport::Transport::Instance()->Shutdown();
return res; return res;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册