Apollo 10.0
自动驾驶开放平台
rtps_receiver.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_TRANSPORT_RECEIVER_RTPS_RECEIVER_H_
18#define CYBER_TRANSPORT_RECEIVER_RTPS_RECEIVER_H_
19
20#include "cyber/common/log.h"
23
24namespace apollo {
25namespace cyber {
26namespace transport {
27
28template <typename M>
29class RtpsReceiver : public Receiver<M> {
30 public:
31 RtpsReceiver(const RoleAttributes& attr,
32 const typename Receiver<M>::MessageListener& msg_listener);
33 virtual ~RtpsReceiver();
34
35 void Enable() override;
36 void Disable() override;
37
38 void Enable(const RoleAttributes& opposite_attr) override;
39 void Disable(const RoleAttributes& opposite_attr) override;
40
41 private:
42 RtpsDispatcherPtr dispatcher_;
43};
44
45template <typename M>
47 const RoleAttributes& attr,
48 const typename Receiver<M>::MessageListener& msg_listener)
49 : Receiver<M>(attr, msg_listener) {
50 dispatcher_ = RtpsDispatcher::Instance();
51}
52
53template <typename M>
55 Disable();
56}
57
58template <typename M>
60 if (this->enabled_) {
61 return;
62 }
63 dispatcher_->AddListener<M>(
64 this->attr_, std::bind(&RtpsReceiver<M>::OnNewMessage, this,
65 std::placeholders::_1, std::placeholders::_2));
66 this->enabled_ = true;
67}
68
69template <typename M>
71 if (!this->enabled_) {
72 return;
73 }
74 dispatcher_->RemoveListener<M>(this->attr_);
75 this->enabled_ = false;
76}
77
78template <typename M>
79void RtpsReceiver<M>::Enable(const RoleAttributes& opposite_attr) {
80 dispatcher_->AddListener<M>(
81 this->attr_, opposite_attr,
82 std::bind(&RtpsReceiver<M>::OnNewMessage, this, std::placeholders::_1,
83 std::placeholders::_2));
84}
85
86template <typename M>
87void RtpsReceiver<M>::Disable(const RoleAttributes& opposite_attr) {
88 dispatcher_->RemoveListener<M>(this->attr_, opposite_attr);
89}
90
91} // namespace transport
92} // namespace cyber
93} // namespace apollo
94
95#endif // CYBER_TRANSPORT_RECEIVER_RTPS_RECEIVER_H_
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition receiver.h:36
RtpsReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
class register implement
Definition arena_queue.h:37