SMACC2
smacc_ros_launch_client.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
22
23namespace smacc2
24{
25namespace client_bases
26{
27ClRosLaunch::ClRosLaunch(std::string packageName, std::string launchFilename)
28: packageName_(packageName), launchFileName_(launchFilename), cancellationToken_(false)
29{
30}
31
33
35{
37 packageName_, launchFileName_, [this]() { return this->cancellationToken_.load(); });
38}
39
41
42std::future<std::string> ClRosLaunch::executeRosLaunch(
43 std::string packageName, std::string launchFileName, std::function<bool()> cancelCondition)
44{
45 return std::async(std::launch::async, [=]() {
46 RCLCPP_WARN_STREAM(
47 rclcpp::get_logger("smacc2"), "[ClRosLaunch static] starting ros launch thread ");
48
49 std::stringstream cmd;
50 cmd << "ros2 launch " << packageName << " " << launchFileName;
51
52 std::array<char, 128> buffer;
53 std::string result;
54 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.str().c_str(), "r"), pclose);
55 if (!pipe)
56 {
57 throw std::runtime_error("popen() failed!");
58 }
59
60 std::stringstream ss;
61 bool cancelled = false;
62 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr &&
63 !(cancelled = cancelCondition()))
64 {
65 ss << buffer.data();
66 }
67
68 result = ss.str();
69 RCLCPP_WARN_STREAM(
70 rclcpp::get_logger("smacc2"), "[ClRosLaunch static]] RESULT = \n " << ss.str());
71 return result;
72 });
73}
74
75} // namespace client_bases
76} // namespace smacc2
ClRosLaunch(std::string packageName, std::string launchFilename)
static std::future< std::string > executeRosLaunch(std::string packageName, std::string launchFilename, std::function< bool()> cancelCondition)