Skip to content

Commit c44b086

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

1 file changed

Lines changed: 75 additions & 82 deletions

File tree

ddsenabler_participants/src/cpp/rpc/RpcStructs.cpp

Lines changed: 75 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,43 @@ 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 <<
170+
": unknown action request type suffix");
171+
throw std::runtime_error("Unknown action request type suffix");
173172
}
174-
175-
rpc_type = RpcType::SERVICE;
176173
return;
177174
}
178175

@@ -186,78 +183,74 @@ void RpcInfo::extract_rpc_info()
186183
service_type = ServiceType::REPLY;
187184
service_name = base;
188185

186+
if (strip_action_infix(base))
187+
{
188+
rpc_type = RpcType::ACTION;
189+
}
190+
191+
if (rpc_type != RpcType::ACTION)
192+
{
193+
rpc_type = RpcType::SERVICE;
194+
return;
195+
}
196+
189197
if (base.size() >= (std::strlen(ACTION_GOAL_SUFFIX)) &&
190198
base.substr(base.size() - (std::strlen(ACTION_GOAL_SUFFIX))) == ACTION_GOAL_SUFFIX)
191199
{
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-
}
200+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_GOAL_SUFFIX)));
201+
action_type = ActionType::GOAL;
200202
}
201203
else if (base.size() >= (std::strlen(ACTION_RESULT_SUFFIX)) &&
202204
base.substr(base.size() - (std::strlen(ACTION_RESULT_SUFFIX))) == ACTION_RESULT_SUFFIX)
203205
{
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-
}
206+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_RESULT_SUFFIX)));
207+
action_type = ActionType::RESULT;
212208
}
213209
else if (base.size() >= (std::strlen(ACTION_CANCEL_SUFFIX)) &&
214210
base.substr(base.size() - (std::strlen(ACTION_CANCEL_SUFFIX))) == ACTION_CANCEL_SUFFIX)
215211
{
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-
}
212+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_CANCEL_SUFFIX)));
213+
action_type = ActionType::CANCEL;
214+
}
215+
else
216+
{
217+
EPROSIMA_LOG_ERROR(DDSENABLER_RPC_UTILS,
218+
"Failed to extract RPC info from topic name " << topic_name <<
219+
": unknown action reply type suffix");
220+
throw std::runtime_error("Unknown action reply type suffix");
224221
}
225-
226-
rpc_type = RpcType::SERVICE;
227222
return;
228223
}
229224

230225
// Check for action feedback/status topics
231-
if (Protocol::ROS2 != protocol) // Feedback and Status topics only exist for ROS2 actions
226+
if (strip_action_infix(base))
227+
{
228+
rpc_type = RpcType::ACTION;
229+
}
230+
if (Protocol::ROS2 != protocol || rpc_type != RpcType::ACTION) // Feedback and Status topics only exist for ROS2 actions
232231
{
233232
return;
234233
}
235234
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))
235+
if (base.size() >= (std::strlen(ACTION_FEEDBACK_SUFFIX)) &&
236+
base.substr(base.size() - (std::strlen(ACTION_FEEDBACK_SUFFIX))) ==
237+
(ACTION_FEEDBACK_SUFFIX))
239238
{
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;
239+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_FEEDBACK_SUFFIX)));
240+
action_type = ActionType::FEEDBACK;
248241
}
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))
242+
else if (base.size() >= (std::strlen(ACTION_STATUS_SUFFIX)) &&
243+
base.substr(base.size() - (std::strlen(ACTION_STATUS_SUFFIX))) ==
244+
(ACTION_STATUS_SUFFIX))
252245
{
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;
246+
action_name = base.substr(0, base.size() - (std::strlen(ACTION_STATUS_SUFFIX)));
247+
action_type = ActionType::STATUS;
248+
}
249+
else
250+
{
251+
EPROSIMA_LOG_ERROR(DDSENABLER_RPC_UTILS,
252+
"Failed to extract RPC info from topic name " << topic_name << ": unknown topic type suffix");
253+
throw std::runtime_error("Unknown topic type suffix");
261254
}
262255

263256
return;

0 commit comments

Comments
 (0)