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{
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 {
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 try
66 {
67 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr &&
69 {
70 ss << buffer.data();
71 if (cancelled) // break_pipe
72 {
74 rclcpp::get_logger("smacc2"), "[ClRosLaunch static] cancelling ros launch thread ");
75
76 // break pipe
77 pclose(pipe.release());
78
79 break;
80 }
81 }
82 }
83 catch (const std::exception & e)
84 {
85 std::cerr << e.what() << '\n';
86 }
87
88 result = ss.str();
90 rclcpp::get_logger("smacc2"), "[ClRosLaunch static]] RESULT = \n " << ss.str());
91 return result;
92 });
93}
94
95} // namespace client_bases
96} // 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)