#include "boost/program_options.hpp" #include <signal.h> #include <string> #include <sstream> #include <iostream> #include <thread> #include <ros/ros.h> #include "std_msgs/String.h" #include "mogo_reporter/mogo_reporter.h" #include "master.h" #include "slave.h" #include "configure.h" namespace po = boost::program_options; std::atomic<bool> gShutdown( false ); std::atomic<bool> gShouldRestart( true ); /** * Handle SIGTERM to allow the recorder to cleanup by requesting a shutdown. * \param signal */ void signal_handler( int signal) { ( void ) signal; ros::requestShutdown(); gShutdown.store( true ); gShouldRestart.store( false ); } int main( int argc, char ** argv) { ros::init(argc, argv, "record_cache" , ros::init_options::AnonymousName); MOGO_MSG_INIT_CFG( "record_cache" ); gShouldRestart.store( false ); setlocale(LC_ALL, "" ); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); po::options_description desc( "Allowed options" ); desc.add_options() ( "help,h" , "produce help message" ) ( "file,f" ,po::value<std::string>(), "config file path" ) ( "create,c" , "create a record task" ) ( "master" , "run as master" ) ( "slave" , "run as slave" ); po::positional_options_description p; po::variables_map vm; try { po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); } catch ( const boost::program_options::invalid_command_line_syntax &e) { ROS_ERROR( "Error reading options: %s" , e.what()); return 1 ; } catch ( const boost::program_options::unknown_option &e) { ROS_ERROR( "Unknown options:%s" , e.what()); return 1 ; } if (vm.count( "help" )) { std::cout << desc << std::endl; exit( 0 ); } if (vm.count( "create" )) { } if (vm.count( "master" )) { } if (vm.count( "slave" )) { } if (vm.count( "file" )) { record_cache::Configure::instance()->load(vm[ "file" ].as<std::string>()); } std::thread t([](){ while (!gShutdown.load()) { if (!ros::master::check() && !gShouldRestart.load()) { ROS_WARN_STREAM( "master offline, request shutdown!" ); ros::requestShutdown(); gShouldRestart.store( true ); break ; } std::this_thread::sleep_for(std::chrono::milliseconds( 200 )); } }); t.detach(); ros::NodeHandle node_handle( "~" ); std::shared_ptr<record_cache::TaskManager> managerPtr; std::string hostname; char * str = getenv( "ROS_HOSTNAME" ); if (!str) { hostname = "unknown" ; } else { hostname = str; } ROS_DEBUG_STREAM( "HOST:" << ros::master::getHost()); std::string master = ros::master::getHost(); if (hostname == master) { managerPtr = std::make_shared<record_cache::Master>(); } else { managerPtr = std::make_shared<record_cache::Slave>(); } double pre_allocate_count_other; node_handle.param( "pre_allocate_count_other" , pre_allocate_count_other, static_cast< double >( 1.5 )); managerPtr->setPreAllocateCountOther(pre_allocate_count_other); double pre_allocate_count_106; node_handle.param( "pre_allocate_count_106" , pre_allocate_count_106, static_cast< double >( 2.5 )); managerPtr->setPreAllocateCount106(pre_allocate_count_106); int pre_allocate_pice_size = 0 ; node_handle.param( "pre_allocate_pice_size" , pre_allocate_pice_size, 100 ); managerPtr->setPreAllocatePiceSize(pre_allocate_pice_size); int trigger_mast_running_tasks_num = 3 ; node_handle.param( "trigger_mast_running_tasks_num" , trigger_mast_running_tasks_num, 3 ); managerPtr->setTriggerMastRunningTasksNum(trigger_mast_running_tasks_num); int trigger_mast_bduration = 10 ; node_handle.param( "trigger_mast_bduration" , trigger_mast_bduration, 10 ); managerPtr->setTriggerMastBduration(trigger_mast_bduration); int filter_record_interval = 5 ; node_handle.param( "filter_record_interval" , filter_record_interval, 5 ); managerPtr->setFilterRecordInterval(filter_record_interval); int single_msg_mast_size = 15 ; node_handle.param( "single_msg_mast_size" , single_msg_mast_size, 15 ); managerPtr->setSingleMsgMastSize(single_msg_mast_size); int bags_max_disk_space = 150 ; node_handle.param( "bags_max_disk_space" , bags_max_disk_space, 150 ); managerPtr->setbagsMaxDiskSpace(bags_max_disk_space); int record_time_split_size_gnss = 3600 ; node_handle.param( "record_time_split_size_gnss" , record_time_split_size_gnss, 3600 ); managerPtr->setRecordTimeSplitSizeGnss(record_time_split_size_gnss); int record_time_split_size_test = 1800 ; node_handle.param( "record_time_split_size_test" , record_time_split_size_test, 1800 ); managerPtr->setRecordTimeSplitSizeTest(record_time_split_size_test); // TODO 调整spin线程数量 ros::AsyncSpinner s( 0 ); s.start(); managerPtr->start(); ros::waitForShutdown(); sleep( 1 ); if (gShouldRestart.load()) { return 8 ; } return 0 ; } |