scenario_simulator_v2 C++ API
launch.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
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 #ifndef CONCEALER__LAUNCH_HPP_
16 #define CONCEALER__LAUNCH_HPP_
17 
18 #ifdef CONCEALER_ISOLATE_STANDARD_OUTPUT
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #endif
24 
25 #include <boost/algorithm/string.hpp>
26 #include <concealer/execute.hpp>
27 #include <cstdlib>
28 #include <iostream>
29 #include <string>
30 #include <system_error>
31 #include <type_traits>
32 #include <vector>
33 
34 namespace concealer
35 {
36 template <typename... Ts>
37 auto ros2_launch(const std::string & package, const std::string & file, Ts &&... xs)
38 {
39 #ifdef CONCEALER_ISOLATE_STANDARD_OUTPUT
40  const std::string log_filename = "/tmp/scenario_test_runner/autoware-output.txt";
41  const auto fd = ::open(log_filename.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
42  ::dup2(fd, STDOUT_FILENO);
43  ::dup2(fd, STDERR_FILENO);
44  ::close(fd);
45 #endif
46 
47  const auto process_id = fork();
48 
49  const std::vector<std::string> argv{
50  "python3",
51  boost::algorithm::replace_all_copy(dollar("which ros2"), "\n", ""),
52  "launch", // NOTE: The command 'ros2' is a Python script.
53  package,
54  file,
55  std::forward<decltype(xs)>(xs)...};
56 
57  if (process_id < 0) {
58  throw std::system_error(errno, std::system_category());
59  } else if (process_id == 0 and execute(argv) < 0) {
60  std::cout << std::system_error(errno, std::system_category()).what() << std::endl;
61  std::exit(EXIT_FAILURE);
62  } else {
63  return process_id;
64  }
65 }
66 } // namespace concealer
67 
68 #endif // CONCEALER__LAUNCH_HPP_
Definition: autoware.hpp:30
auto ros2_launch(const std::string &package, const std::string &file, Ts &&... xs)
Definition: launch.hpp:37
auto dollar(const std::string &command) -> std::string
Definition: execute.cpp:56
auto execute(const std::vector< std::string > &) -> int
Definition: execute.cpp:35
pid_t process_id
Definition: record.cpp:25
Definition: junit5.hpp:25
std::string string
Definition: junit5.hpp:26