Apollo  6.0
Open source self driving car software
intra_transmitter.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_TRANSMITTER_INTRA_TRANSMITTER_H_
18 #define CYBER_TRANSPORT_TRANSMITTER_INTRA_TRANSMITTER_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "cyber/common/log.h"
26 
27 namespace apollo {
28 namespace cyber {
29 namespace transport {
30 
31 template <typename M>
32 class IntraTransmitter : public Transmitter<M> {
33  public:
34  using MessagePtr = std::shared_ptr<M>;
35 
36  explicit IntraTransmitter(const RoleAttributes& attr);
37  virtual ~IntraTransmitter();
38 
39  void Enable() override;
40  void Disable() override;
41 
42  bool Transmit(const MessagePtr& msg, const MessageInfo& msg_info) override;
43 
44  private:
45  uint64_t channel_id_;
46  IntraDispatcherPtr dispatcher_;
47 };
48 
49 template <typename M>
50 IntraTransmitter<M>::IntraTransmitter(const RoleAttributes& attr)
51  : Transmitter<M>(attr),
52  channel_id_(attr.channel_id()),
53  dispatcher_(nullptr) {}
54 
55 template <typename M>
57  Disable();
58 }
59 
60 template <typename M>
62  if (!this->enabled_) {
63  dispatcher_ = IntraDispatcher::Instance();
64  this->enabled_ = true;
65  }
66 }
67 
68 template <typename M>
70  if (this->enabled_) {
71  dispatcher_ = nullptr;
72  this->enabled_ = false;
73  }
74 }
75 
76 template <typename M>
78  const MessageInfo& msg_info) {
79  if (!this->enabled_) {
80  ADEBUG << "not enable.";
81  return false;
82  }
83 
84  dispatcher_->OnMessage(channel_id_, msg, msg_info);
85  return true;
86 }
87 
88 } // namespace transport
89 } // namespace cyber
90 } // namespace apollo
91 
92 #endif // CYBER_TRANSPORT_TRANSMITTER_INTRA_TRANSMITTER_H_
std::shared_ptr< M > MessagePtr
Definition: intra_transmitter.h:34
bool enabled_
Definition: endpoint.h:45
void Disable() override
Definition: intra_transmitter.h:69
bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info) override
Definition: intra_transmitter.h:77
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void Enable() override
Definition: intra_transmitter.h:61
Definition: intra_transmitter.h:32
Definition: intra_dispatcher.h:251
#define ADEBUG
Definition: log.h:41
IntraTransmitter(const RoleAttributes &attr)
Definition: intra_transmitter.h:50
Definition: message_info.h:30
virtual ~IntraTransmitter()
Definition: intra_transmitter.h:56
void OnMessage(uint64_t channel_id, const std::shared_ptr< MessageT > &message, const MessageInfo &message_info)
Definition: intra_dispatcher.h:285
Definition: transmitter.h:36