Apollo 11.0
自动驾驶开放平台
manager.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_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
36namespace apollo {
37namespace cyber {
38namespace service_discovery {
39
40using proto::ChangeMsg;
43using proto::RoleAttributes;
44using proto::RoleType;
45
51class 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_
Base class for management of Topology elements.
Definition manager.h:51
void Notify(const ChangeMsg &msg)
Definition manager.cc:186
bool StartDiscovery(RtpsParticipant *participant)
Startup topology discovery
Definition manager.cc:50
void Convert(const RoleAttributes &attr, RoleType role, OperateType opt, ChangeMsg *msg)
Definition manager.cc:170
eprosima::fastrtps::PublisherAttributes RtpsPublisherAttr
Definition manager.h:58
std::function< void(const ChangeMsg &)> ChangeFunc
Definition manager.h:54
bool Join(const RoleAttributes &attr, RoleType role, bool need_publish=true)
Join the topology
Definition manager.cc:98
bool IsFromSameProcess(const ChangeMsg &msg)
Definition manager.cc:220
bool Publish(const ChangeMsg &msg)
Definition manager.cc:203
void RemoveChangeListener(const ChangeConnection &conn)
Remove our listener for topology change.
Definition manager.cc:135
eprosima::fastrtps::Subscriber * subscriber_
Definition manager.h:163
virtual ~Manager()
Destroy the Manager object
Definition manager.cc:48
virtual void Dispose(const ChangeMsg &msg)=0
bool CreatePublisher(RtpsParticipant *participant)
Definition manager.cc:140
eprosima::fastrtps::Participant RtpsParticipant
Definition manager.h:57
bool CreateSubscriber(RtpsParticipant *participant)
Definition manager.cc:151
std::atomic< bool > is_discovery_started_
Definition manager.h:155
Manager()
Construct a new Manager object
Definition manager.cc:35
void OnRemoteChange(const std::string &msg_str)
Definition manager.cc:188
virtual void OnTopoModuleLeave(const std::string &host_name, int process_id)=0
Called when a process' topology manager instance leave
ChangeConnection AddChangeListener(const ChangeFunc &func)
Add topology change listener, when topology changed, func will be called.
Definition manager.cc:131
eprosima::fastrtps::Publisher * publisher_
Definition manager.h:161
bool Leave(const RoleAttributes &attr, RoleType role)
Leave the topology
Definition manager.cc:115
eprosima::fastrtps::SubscriberAttributes RtpsSubscriberAttr
Definition manager.h:59
virtual bool Check(const RoleAttributes &attr)=0
void StopDiscovery()
Stop topology discovery
Definition manager.cc:65
virtual bool NeedPublish(const ChangeMsg &msg) const
Definition manager.cc:165
virtual void Shutdown()
Shutdown module
Definition manager.cc:89
class register implement
Definition arena_queue.h:37