Apollo  6.0
Open source self driving car software
intra_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_INTRA_RECEIVER_H_
18 #define CYBER_TRANSPORT_RECEIVER_INTRA_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 IntraReceiver : public Receiver<M> {
30  public:
31  IntraReceiver(const RoleAttributes& attr,
32  const typename Receiver<M>::MessageListener& msg_listener);
33  virtual ~IntraReceiver();
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  IntraDispatcherPtr 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_ = IntraDispatcher::Instance();
51 }
52 
53 template <typename M>
55  Disable();
56 }
57 
58 template <typename M>
60  if (this->enabled_) {
61  return;
62  }
63 
64  dispatcher_->AddListener<M>(
65  this->attr_, std::bind(&IntraReceiver<M>::OnNewMessage, this,
66  std::placeholders::_1, std::placeholders::_2));
67  this->enabled_ = true;
68 }
69 
70 template <typename M>
72  if (!this->enabled_) {
73  return;
74  }
75 
76  dispatcher_->RemoveListener<M>(this->attr_);
77  this->enabled_ = false;
78 }
79 
80 template <typename M>
81 void IntraReceiver<M>::Enable(const RoleAttributes& opposite_attr) {
82  dispatcher_->AddListener<M>(
83  this->attr_, opposite_attr,
84  std::bind(&IntraReceiver<M>::OnNewMessage, this, std::placeholders::_1,
85  std::placeholders::_2));
86 }
87 
88 template <typename M>
89 void IntraReceiver<M>::Disable(const RoleAttributes& opposite_attr) {
90  dispatcher_->RemoveListener<M>(this->attr_, opposite_attr);
91 }
92 
93 } // namespace transport
94 } // namespace cyber
95 } // namespace apollo
96 
97 #endif // CYBER_TRANSPORT_RECEIVER_INTRA_RECEIVER_H_
bool enabled_
Definition: endpoint.h:45
Definition: receiver.h:32
void AddListener(const RoleAttributes &self_attr, const MessageListener< MessageT > &listener)
Definition: intra_dispatcher.h:345
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void RemoveListener(const RoleAttributes &self_attr)
Definition: intra_dispatcher.h:399
Definition: intra_dispatcher.h:251
IntraReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
Definition: intra_receiver.h:46
RoleAttributes attr_
Definition: endpoint.h:47
Definition: intra_receiver.h:29
void Enable() override
Definition: intra_receiver.h:59
virtual ~IntraReceiver()
Definition: intra_receiver.h:54
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition: receiver.h:36
void Disable() override
Definition: intra_receiver.h:71