Skip to content

Commit b9e839c

Browse files
committed
Refs #24527: Refactor extract_rpc_info
Signed-off-by: Carlos Ferreira González <carlosferreira@eprosima.com>
1 parent 57b841f commit b9e839c

1 file changed

Lines changed: 73 additions & 82 deletions

File tree

ddsenabler_participants/src/cpp/rpc/RpcStructs.cpp

Lines changed: 73 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@ namespace ddsenabler {
2323
namespace participants {
2424

2525
/**
26-
* Try to strip the trailing ACTION_INFIX ("/_action/") from a name produced by the topic parser.
26+
* Remove the ACTION_INFIX ("/_action/") substring from @p name wherever it occurs.
2727
*
2828
* @param [in, out] name The name to be stripped.
29-
* @return true and updates `name` if the infix was found at the end; false otherwise.
29+
* @return true and updates @p name if the infix was found; false otherwise.
3030
*/
3131
static bool strip_action_infix(
3232
std::string& name)
3333
{
34-
const std::size_t infix_len = std::strlen(ACTION_INFIX);
35-
if (name.size() < infix_len ||
36-
name.substr(name.size() - infix_len) != ACTION_INFIX)
34+
const std::size_t pos = name.find(ACTION_INFIX);
35+
if (pos == std::string::npos)
3736
{
3837
return false;
3938
}
40-
name = name.substr(0, name.size() - infix_len);
39+
name.erase(pos, std::strlen(ACTION_INFIX));
4140
return true;
4241
}
4342

@@ -134,45 +133,42 @@ void RpcInfo::extract_rpc_info()
134133
service_type = ServiceType::REQUEST;
135134
service_name = base;
136135

137-
// Check if it's an action topic by looking for the action suffixes and infix in the remaining base name
136+
if (strip_action_infix(base))
137+
{
138+
rpc_type = RpcType::ACTION;
139+
}
140+
141+
if (rpc_type != RpcType::ACTION)
142+
{
143+
rpc_type = RpcType::SERVICE;
144+
return;
145+
}
146+
147+
// Check the action type suffix, and extract the action name
138148
if (base.size() >= (std::strlen(ACTION_GOAL_SUFFIX)) &&
139149
base.substr(base.size() - (std::strlen(ACTION_GOAL_SUFFIX))) == ACTION_GOAL_SUFFIX)
140150
{
141-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_GOAL_SUFFIX)));
142-
if (strip_action_infix(candidate))
143-
{
144-
action_name = candidate;
145-
rpc_type = RpcType::ACTION;
146-
action_type = ActionType::GOAL;
147-
return;
148-
}
151+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_GOAL_SUFFIX)));
152+
action_type = ActionType::GOAL;
149153
}
150154
else if (base.size() >= (std::strlen(ACTION_RESULT_SUFFIX)) &&
151155
base.substr(base.size() - (std::strlen(ACTION_RESULT_SUFFIX))) == ACTION_RESULT_SUFFIX)
152156
{
153-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_RESULT_SUFFIX)));
154-
if (strip_action_infix(candidate))
155-
{
156-
action_name = candidate;
157-
rpc_type = RpcType::ACTION;
158-
action_type = ActionType::RESULT;
159-
return;
160-
}
157+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_RESULT_SUFFIX)));
158+
action_type = ActionType::RESULT;
161159
}
162160
else if (base.size() >= (std::strlen(ACTION_CANCEL_SUFFIX)) &&
163161
base.substr(base.size() - (std::strlen(ACTION_CANCEL_SUFFIX))) == ACTION_CANCEL_SUFFIX)
164162
{
165-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_CANCEL_SUFFIX)));
166-
if (strip_action_infix(candidate))
167-
{
168-
action_name = candidate;
169-
rpc_type = RpcType::ACTION;
170-
action_type = ActionType::CANCEL;
171-
return;
172-
}
163+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_CANCEL_SUFFIX)));
164+
action_type = ActionType::CANCEL;
165+
}
166+
else
167+
{
168+
EPROSIMA_LOG_ERROR(DDSENABLER_RPC_UTILS,
169+
"Failed to extract RPC info from topic name " << topic_name << ": unknown action request type suffix");
170+
throw std::runtime_error("Unknown action request type suffix");
173171
}
174-
175-
rpc_type = RpcType::SERVICE;
176172
return;
177173
}
178174

@@ -186,78 +182,73 @@ void RpcInfo::extract_rpc_info()
186182
service_type = ServiceType::REPLY;
187183
service_name = base;
188184

185+
if (strip_action_infix(base))
186+
{
187+
rpc_type = RpcType::ACTION;
188+
}
189+
190+
if (rpc_type != RpcType::ACTION)
191+
{
192+
rpc_type = RpcType::SERVICE;
193+
return;
194+
}
195+
189196
if (base.size() >= (std::strlen(ACTION_GOAL_SUFFIX)) &&
190197
base.substr(base.size() - (std::strlen(ACTION_GOAL_SUFFIX))) == ACTION_GOAL_SUFFIX)
191198
{
192-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_GOAL_SUFFIX)));
193-
if (strip_action_infix(candidate))
194-
{
195-
action_name = candidate;
196-
rpc_type = RpcType::ACTION;
197-
action_type = ActionType::GOAL;
198-
return;
199-
}
199+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_GOAL_SUFFIX)));
200+
action_type = ActionType::GOAL;
200201
}
201202
else if (base.size() >= (std::strlen(ACTION_RESULT_SUFFIX)) &&
202203
base.substr(base.size() - (std::strlen(ACTION_RESULT_SUFFIX))) == ACTION_RESULT_SUFFIX)
203204
{
204-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_RESULT_SUFFIX)));
205-
if (strip_action_infix(candidate))
206-
{
207-
action_name = candidate;
208-
rpc_type = RpcType::ACTION;
209-
action_type = ActionType::RESULT;
210-
return;
211-
}
205+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_RESULT_SUFFIX)));
206+
action_type = ActionType::RESULT;
212207
}
213208
else if (base.size() >= (std::strlen(ACTION_CANCEL_SUFFIX)) &&
214209
base.substr(base.size() - (std::strlen(ACTION_CANCEL_SUFFIX))) == ACTION_CANCEL_SUFFIX)
215210
{
216-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_CANCEL_SUFFIX)));
217-
if (strip_action_infix(candidate))
218-
{
219-
action_name = candidate;
220-
rpc_type = RpcType::ACTION;
221-
action_type = ActionType::CANCEL;
222-
return;
223-
}
211+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_CANCEL_SUFFIX)));
212+
action_type = ActionType::CANCEL;
213+
}
214+
else
215+
{
216+
EPROSIMA_LOG_ERROR(DDSENABLER_RPC_UTILS,
217+
"Failed to extract RPC info from topic name " << topic_name << ": unknown action reply type suffix");
218+
throw std::runtime_error("Unknown action reply type suffix");
224219
}
225-
226-
rpc_type = RpcType::SERVICE;
227220
return;
228221
}
229222

230223
// Check for action feedback/status topics
231-
if (Protocol::ROS2 != protocol) // Feedback and Status topics only exist for ROS2 actions
224+
if (strip_action_infix(base))
225+
{
226+
rpc_type = RpcType::ACTION;
227+
}
228+
if (Protocol::ROS2 != protocol || rpc_type != RpcType::ACTION) // Feedback and Status topics only exist for ROS2 actions
232229
{
233230
return;
234231
}
235232
base = base.substr(topic_prefix.length());
236-
if (base.size() >= (std::strlen(ACTION_FEEDBACK_SUFFIX) + 1) &&
237-
base.substr(base.size() - (std::strlen(ACTION_FEEDBACK_SUFFIX) + 1)) ==
238-
(std::string("/") + ACTION_FEEDBACK_SUFFIX))
233+
if (base.size() >= (std::strlen(ACTION_FEEDBACK_SUFFIX)) &&
234+
base.substr(base.size() - (std::strlen(ACTION_FEEDBACK_SUFFIX))) ==
235+
(ACTION_FEEDBACK_SUFFIX))
239236
{
240-
std::string candidate = base.substr(0, base.size() - std::strlen(ACTION_FEEDBACK_SUFFIX));
241-
if (strip_action_infix(candidate))
242-
{
243-
action_name = candidate;
244-
rpc_type = RpcType::ACTION;
245-
action_type = ActionType::FEEDBACK;
246-
}
247-
return;
237+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_FEEDBACK_SUFFIX)));
238+
action_type = ActionType::FEEDBACK;
248239
}
249-
if (base.size() >= (std::strlen(ACTION_STATUS_SUFFIX) + 1) &&
250-
base.substr(base.size() - (std::strlen(ACTION_STATUS_SUFFIX) + 1)) ==
251-
(std::string("/") + ACTION_STATUS_SUFFIX))
240+
else if (base.size() >= (std::strlen(ACTION_STATUS_SUFFIX)) &&
241+
base.substr(base.size() - (std::strlen(ACTION_STATUS_SUFFIX))) ==
242+
(ACTION_STATUS_SUFFIX))
252243
{
253-
std::string candidate = base.substr(0, base.size() - (std::strlen(ACTION_STATUS_SUFFIX)));
254-
if (strip_action_infix(candidate))
255-
{
256-
action_name = candidate;
257-
rpc_type = RpcType::ACTION;
258-
action_type = ActionType::STATUS;
259-
}
260-
return;
244+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_STATUS_SUFFIX)));
245+
action_type = ActionType::STATUS;
246+
}
247+
else
248+
{
249+
EPROSIMA_LOG_ERROR(DDSENABLER_RPC_UTILS,
250+
"Failed to extract RPC info from topic name " << topic_name << ": unknown topic type suffix");
251+
throw std::runtime_error("Unknown topic type suffix");
261252
}
262253

263254
return;

0 commit comments

Comments
 (0)