SMACC2
Loading...
Searching...
No Matches
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(
46 std::launch::async,
47 [=]()
48 {
49 RCLCPP_WARN_STREAM(
50 rclcpp::get_logger("smacc2"), "[ClRosLaunch static] starting ros launch thread ");
51
52 std::stringstream cmd;
53 cmd << "ros2 launch " << packageName << " " << launchFileName;
54
55 std::array<char, 128> buffer;
56 std::string result;
57 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.str().c_str(), "r"), pclose);
58 if (!pipe)
59 {
60 throw std::runtime_error("popen() failed!");
61 }
62
63 std::stringstream ss;
64 bool cancelled = false;
65 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr &&
66 !(cancelled = cancelCondition()))
67 {
68 ss << buffer.data();
69 }
70
71 result = ss.str();
72 RCLCPP_WARN_STREAM(
73 rclcpp::get_logger("smacc2"), "[ClRosLaunch static]] RESULT = \n " << ss.str());
74 return result;
75 });
76}
77
78} // namespace client_bases
79} // 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)