Apollo  6.0
Open source self driving car software
rtps_receiver.h
Go to the documentation of this file.
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 
24 namespace apollo {
25 namespace cyber {
26 namespace transport {
27 
28 template <typename M>
29 class 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 
45 template <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 
53 template <typename M>
55  Disable();
56 }
57 
58 template <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 
69 template <typename M>
71  if (!this->enabled_) {
72  return;
73  }
74  dispatcher_->RemoveListener<M>(this->attr_);
75  this->enabled_ = false;
76 }
77 
78 template <typename M>
79 void 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 
86 template <typename M>
87 void 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_
bool enabled_
Definition: endpoint.h:45
Definition: receiver.h:32
void RemoveListener(const RoleAttributes &self_attr)
Definition: dispatcher.h:144
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: rtps_dispatcher.h:48
void AddListener(const RoleAttributes &self_attr, const MessageListener< MessageT > &listener)
Definition: rtps_dispatcher.h:82
Definition: rtps_receiver.h:29
RoleAttributes attr_
Definition: endpoint.h:47
void Disable() override
Definition: rtps_receiver.h:70
virtual ~RtpsReceiver()
Definition: rtps_receiver.h:54
RtpsReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
Definition: rtps_receiver.h:46
void Enable() override
Definition: rtps_receiver.h:59
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition: receiver.h:36