Apollo  6.0
Open source self driving car software
manager.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_SERVICE_DISCOVERY_SPECIFIC_MANAGER_MANAGER_H_
18 #define CYBER_SERVICE_DISCOVERY_SPECIFIC_MANAGER_MANAGER_H_
19 
20 #include <atomic>
21 #include <functional>
22 #include <mutex>
23 #include <string>
24 
25 #include "fastrtps/Domain.h"
26 #include "fastrtps/attributes/PublisherAttributes.h"
27 #include "fastrtps/attributes/SubscriberAttributes.h"
28 #include "fastrtps/participant/Participant.h"
29 #include "fastrtps/publisher/Publisher.h"
30 #include "fastrtps/subscriber/Subscriber.h"
31 
32 #include "cyber/base/signal.h"
33 #include "cyber/proto/topology_change.pb.h"
35 
36 namespace apollo {
37 namespace cyber {
38 namespace service_discovery {
39 
40 using proto::ChangeMsg;
41 using proto::ChangeType;
42 using proto::OperateType;
43 using proto::RoleAttributes;
44 using proto::RoleType;
45 
51 class Manager {
52  public:
54  using ChangeFunc = std::function<void(const ChangeMsg&)>;
56 
57  using RtpsParticipant = eprosima::fastrtps::Participant;
58  using RtpsPublisherAttr = eprosima::fastrtps::PublisherAttributes;
59  using RtpsSubscriberAttr = eprosima::fastrtps::SubscriberAttributes;
60 
64  Manager();
65 
69  virtual ~Manager();
70 
78  bool StartDiscovery(RtpsParticipant* participant);
79 
83  void StopDiscovery();
84 
88  virtual void Shutdown();
89 
99  bool Join(const RoleAttributes& attr, RoleType role,
100  bool need_publish = true);
101 
111  bool Leave(const RoleAttributes& attr, RoleType role);
112 
121 
127  void RemoveChangeListener(const ChangeConnection& conn);
128 
135  virtual void OnTopoModuleLeave(const std::string& host_name,
136  int process_id) = 0;
137 
138  protected:
139  bool CreatePublisher(RtpsParticipant* participant);
140  bool CreateSubscriber(RtpsParticipant* participant);
141 
142  virtual bool Check(const RoleAttributes& attr) = 0;
143  virtual void Dispose(const ChangeMsg& msg) = 0;
144  virtual bool NeedPublish(const ChangeMsg& msg) const;
145 
146  void Convert(const RoleAttributes& attr, RoleType role, OperateType opt,
147  ChangeMsg* msg);
148 
149  void Notify(const ChangeMsg& msg);
150  bool Publish(const ChangeMsg& msg);
151  void OnRemoteChange(const std::string& msg_str);
152  bool IsFromSameProcess(const ChangeMsg& msg);
153 
154  std::atomic<bool> is_shutdown_;
155  std::atomic<bool> is_discovery_started_;
157  ChangeType change_type_;
158  std::string host_name_;
160  std::string channel_name_;
161  eprosima::fastrtps::Publisher* publisher_;
162  std::mutex lock_;
163  eprosima::fastrtps::Subscriber* subscriber_;
165 
167 };
168 
169 } // namespace service_discovery
170 } // namespace cyber
171 } // namespace apollo
172 
173 #endif // CYBER_SERVICE_DISCOVERY_SPECIFIC_MANAGER_MANAGER_H_
std::string host_name_
Definition: manager.h:158
void OnRemoteChange(const std::string &msg_str)
bool StartDiscovery(RtpsParticipant *participant)
Startup topology discovery.
void(* func)(void *)
Definition: routine_context.h:41
int process_id_
Definition: manager.h:159
int allowed_role_
Definition: manager.h:156
std::string channel_name_
Definition: manager.h:160
std::function< void(const ChangeMsg &)> ChangeFunc
Definition: manager.h:54
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual bool Check(const RoleAttributes &attr)=0
void StopDiscovery()
Stop topology discovery.
eprosima::fastrtps::PublisherAttributes RtpsPublisherAttr
Definition: manager.h:58
eprosima::fastrtps::Participant RtpsParticipant
Definition: manager.h:57
eprosima::fastrtps::Publisher * publisher_
Definition: manager.h:161
Base class for management of Topology elements. Manager can Join/Leave the Topology, and Listen the topology change.
Definition: manager.h:51
SubscriberListener * listener_
Definition: manager.h:164
virtual void Shutdown()
Shutdown module.
bool CreateSubscriber(RtpsParticipant *participant)
virtual void OnTopoModuleLeave(const std::string &host_name, int process_id)=0
Called when a process&#39; topology manager instance leave.
Definition: subscriber_listener.h:33
ChangeConnection AddChangeListener(const ChangeFunc &func)
Add topology change listener, when topology changed, func will be called.
bool CreatePublisher(RtpsParticipant *participant)
std::atomic< bool > is_discovery_started_
Definition: manager.h:155
std::atomic< bool > is_shutdown_
Definition: manager.h:154
bool Leave(const RoleAttributes &attr, RoleType role)
Leave the topology.
virtual bool NeedPublish(const ChangeMsg &msg) const
std::mutex lock_
Definition: manager.h:162
ChangeType change_type_
Definition: manager.h:157
eprosima::fastrtps::Subscriber * subscriber_
Definition: manager.h:163
virtual void Dispose(const ChangeMsg &msg)=0
Manager()
Construct a new Manager object.
bool Publish(const ChangeMsg &msg)
virtual ~Manager()
Destroy the Manager object.
bool IsFromSameProcess(const ChangeMsg &msg)
Definition: signal.h:37
void Notify(const ChangeMsg &msg)
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
ChangeSignal signal_
Definition: manager.h:166
void RemoveChangeListener(const ChangeConnection &conn)
Remove our listener for topology change.
bool Join(const RoleAttributes &attr, RoleType role, bool need_publish=true)
Join the topology.
eprosima::fastrtps::SubscriberAttributes RtpsSubscriberAttr
Definition: manager.h:59