-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathdiff_eprosima.txt
256 lines (246 loc) · 8.83 KB
/
diff_eprosima.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
diff --git a/rmw_fastrtps_cpp/src/rmw_service.cpp b/rmw_fastrtps_cpp/src/rmw_service.cpp
index 7fd67d4..144b24f 100644
--- a/rmw_fastrtps_cpp/src/rmw_service.cpp
+++ b/rmw_fastrtps_cpp/src/rmw_service.cpp
@@ -241,7 +241,7 @@ rmw_create_service(
delete info->pub_listener_;
}
});
- info->pub_listener_ = new (std::nothrow) ServicePubListener();
+ info->pub_listener_ = new (std::nothrow) ServicePubListener(info);
if (!info->pub_listener_) {
RMW_SET_ERROR_MSG("failed to create service response publisher listener");
return nullptr;
diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp
index 20c2ef6..14923a2 100644
--- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp
+++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp
@@ -231,7 +231,7 @@ rmw_create_service(
RMW_SET_ERROR_MSG("failed to get datawriter qos");
goto fail;
}
- info->pub_listener_ = new ServicePubListener();
+ info->pub_listener_ = new ServicePubListener(info);
info->response_publisher_ =
Domain::createPublisher(participant, publisherParam, info->pub_listener_);
if (!info->response_publisher_) {
diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/custom_service_info.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/custom_service_info.hpp
index e2e7ff8..d85f0a6 100644
--- a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/custom_service_info.hpp
+++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/custom_service_info.hpp
@@ -20,6 +20,7 @@
#include <list>
#include <mutex>
#include <unordered_set>
+#include <unordered_map>
#include "fastcdr/FastBuffer.h"
@@ -38,6 +39,14 @@
class ServiceListener;
class ServicePubListener;
+enum class client_present_t
+{
+ FAILURE, // an error occurred when checking
+ MAYBE, // reader not matched, writer still present
+ YES, // reader matched
+ GONE // neither reader nor writer
+};
+
typedef struct CustomServiceInfo
{
rmw_fastrtps_shared_cpp::TypeSupport * request_type_support_{nullptr};
@@ -62,6 +71,92 @@ typedef struct CustomServiceRequest
: buffer_(nullptr) {}
} CustomServiceRequest;
+class ServicePubListener : public eprosima::fastrtps::PublisherListener
+{
+ using subscriptions_set_t =
+ std::unordered_set<eprosima::fastrtps::rtps::GUID_t,
+ rmw_fastrtps_shared_cpp::hash_fastrtps_guid>;
+ using clients_endpoints_map_t =
+ std::unordered_map<eprosima::fastrtps::rtps::GUID_t,
+ eprosima::fastrtps::rtps::GUID_t,
+ rmw_fastrtps_shared_cpp::hash_fastrtps_guid>;
+
+public:
+ explicit ServicePubListener(CustomServiceInfo * info)
+ : info_(info)
+ {
+ (void)info_;
+ }
+
+ void
+ onPublicationMatched(
+ eprosima::fastrtps::Publisher * pub,
+ eprosima::fastrtps::rtps::MatchingInfo & matchingInfo)
+ {
+ (void) pub;
+ std::lock_guard<std::mutex> lock(mutex_);
+ if (eprosima::fastrtps::rtps::MATCHED_MATCHING == matchingInfo.status) {
+ subscriptions_.insert(matchingInfo.remoteEndpointGuid);
+ } else if (eprosima::fastrtps::rtps::REMOVED_MATCHING == matchingInfo.status) {
+ subscriptions_.erase(matchingInfo.remoteEndpointGuid);
+ auto endpoint = clients_endpoints_.find(matchingInfo.remoteEndpointGuid);
+ if (endpoint != clients_endpoints_.end()) {
+ clients_endpoints_.erase(endpoint->second);
+ clients_endpoints_.erase(matchingInfo.remoteEndpointGuid);
+ }
+ } else {
+ return;
+ }
+ cv_.notify_all();
+ }
+
+ template<class Rep, class Period>
+ bool
+ wait_for_subscription(
+ const eprosima::fastrtps::rtps::GUID_t & guid,
+ const std::chrono::duration<Rep, Period> & rel_time)
+ {
+ auto guid_is_present = [this, guid]() RCPPUTILS_TSA_REQUIRES(mutex_)->bool
+ {
+ return subscriptions_.find(guid) != subscriptions_.end();
+ };
+
+ std::unique_lock<std::mutex> lock(mutex_);
+ return cv_.wait_for(lock, rel_time, guid_is_present);
+ }
+
+ client_present_t
+ check_for_subscription(
+ const eprosima::fastrtps::rtps::GUID_t & guid)
+ {
+ // Check if the guid is still in the map
+ if (clients_endpoints_.find(guid) != clients_endpoints_.end()) {
+ // Wait for subscription
+ if (!wait_for_subscription(guid, std::chrono::milliseconds(100))) {
+ return client_present_t::MAYBE;
+ }
+ } else {
+ // Client has gone
+ return client_present_t::GONE;
+ }
+ return client_present_t::YES;
+ }
+
+ // Accesors
+ clients_endpoints_map_t & clients_endpoints()
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return clients_endpoints_;
+ }
+
+private:
+ CustomServiceInfo * info_;
+ std::mutex mutex_;
+ subscriptions_set_t subscriptions_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
+ clients_endpoints_map_t clients_endpoints_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
+ std::condition_variable cv_;
+};
+
class ServiceListener : public eprosima::fastrtps::SubscriberListener
{
public:
@@ -72,6 +167,21 @@ public:
(void)info_;
}
+ void
+ onSubscriptionMatched(
+ eprosima::fastrtps::Subscriber * sub,
+ eprosima::fastrtps::rtps::MatchingInfo & matchingInfo)
+ {
+ (void) sub;
+ if (eprosima::fastrtps::rtps::REMOVED_MATCHING == matchingInfo.status) {
+ auto endpoint = info_->pub_listener_->clients_endpoints().find(
+ matchingInfo.remoteEndpointGuid);
+ if (endpoint != info_->pub_listener_->clients_endpoints().end()) {
+ info_->pub_listener_->clients_endpoints().erase(endpoint->second);
+ info_->pub_listener_->clients_endpoints().erase(matchingInfo.remoteEndpointGuid);
+ }
+ }
+ }
void
onNewDataMessage(eprosima::fastrtps::Subscriber * sub)
@@ -169,49 +279,4 @@ private:
std::condition_variable * conditionVariable_ RCPPUTILS_TSA_GUARDED_BY(internalMutex_);
};
-class ServicePubListener : public eprosima::fastrtps::PublisherListener
-{
-public:
- ServicePubListener() = default;
-
- template<class Rep, class Period>
- bool wait_for_subscription(
- const eprosima::fastrtps::rtps::GUID_t & guid,
- const std::chrono::duration<Rep, Period> & rel_time)
- {
- auto guid_is_present = [this, guid]() RCPPUTILS_TSA_REQUIRES(mutex_)->bool
- {
- return subscriptions_.find(guid) != subscriptions_.end();
- };
-
- std::unique_lock<std::mutex> lock(mutex_);
- return cv_.wait_for(lock, rel_time, guid_is_present);
- }
-
- void onPublicationMatched(
- eprosima::fastrtps::Publisher * pub,
- eprosima::fastrtps::rtps::MatchingInfo & matchingInfo)
- {
- (void) pub;
- std::lock_guard<std::mutex> lock(mutex_);
- if (eprosima::fastrtps::rtps::MATCHED_MATCHING == matchingInfo.status) {
- subscriptions_.insert(matchingInfo.remoteEndpointGuid);
- } else if (eprosima::fastrtps::rtps::REMOVED_MATCHING == matchingInfo.status) {
- subscriptions_.erase(matchingInfo.remoteEndpointGuid);
- } else {
- return;
- }
- cv_.notify_all();
- }
-
-private:
- using subscriptions_set_t =
- std::unordered_set<eprosima::fastrtps::rtps::GUID_t,
- rmw_fastrtps_shared_cpp::hash_fastrtps_guid>;
-
- std::mutex mutex_;
- subscriptions_set_t subscriptions_ RCPPUTILS_TSA_GUARDED_BY(mutex_);
- std::condition_variable cv_;
-};
-
#endif // RMW_FASTRTPS_SHARED_CPP__CUSTOM_SERVICE_INFO_HPP_
diff --git a/rmw_fastrtps_shared_cpp/src/rmw_request.cpp b/rmw_fastrtps_shared_cpp/src/rmw_request.cpp
index 2c1ef8a..1516acc 100644
--- a/rmw_fastrtps_shared_cpp/src/rmw_request.cpp
+++ b/rmw_fastrtps_shared_cpp/src/rmw_request.cpp
@@ -112,6 +112,14 @@ __rmw_take_request(
delete request.buffer_;
*taken = true;
+
+ // Save both endpoint GUIDs in the clients_endpoints map
+ ServicePubListener * listener = info->pub_listener_;
+ eprosima::fastrtps::rtps::GUID_t related_guid = request.sample_identity_.writer_guid();
+ eprosima::fastrtps::rtps::GUID_t writer_guid =
+ request.sample_info_.sample_identity.writer_guid();
+ listener->clients_endpoints().emplace(related_guid, writer_guid);
+ listener->clients_endpoints().emplace(writer_guid, related_guid);
}
return RMW_RET_OK;
diff --git a/rmw_fastrtps_shared_cpp/src/rmw_response.cpp b/rmw_fastrtps_shared_cpp/src/rmw_response.cpp
index 4ee96af..cb36b27 100644
--- a/rmw_fastrtps_shared_cpp/src/rmw_response.cpp
+++ b/rmw_fastrtps_shared_cpp/src/rmw_response.cpp
@@ -119,9 +119,12 @@ __rmw_send_response(
// Related guid is a reader, so it is the response subscription guid.
// Wait for the response writer to be matched with it.
auto listener = info->pub_listener_;
- if (!listener->wait_for_subscription(related_guid, std::chrono::milliseconds(100))) {
+ client_present_t ret = listener->check_for_subscription(related_guid);
+ if (ret == client_present_t::GONE) {
+ return RMW_RET_OK;
+ } else if (ret == client_present_t::MAYBE) {
RMW_SET_ERROR_MSG("client will not receive response");
- return RMW_RET_ERROR;
+ return RMW_RET_TIMEOUT;
}
}