int main( int ac, char** av ) { try { comma::command_line_options options( ac, av, usage ); const std::vector< std::string >& names = options.unnamed( "--no-discard,--verbose,-v,--no-flush,--output-number-of-clients,--clients,--exit-on-no-clients,-e", "-.+" ); if( names.empty() ) { std::cerr << "io-publish: please specify at least one stream; use '-' for stdout" << std::endl; return 1; } const boost::array< comma::signal_flag::signals, 2 > signals = { { comma::signal_flag::sigint, comma::signal_flag::sigterm } }; comma::signal_flag is_shutdown( signals ); publish p( names , options.value( "-s,--size", 0 ) * options.value( "-m,--multiplier", 1 ) , !options.exists( "--no-discard" ) , !options.exists( "--no-flush" ) , options.exists( "--output-number-of-clients,--clients" ) , options.exists( "--exit-on-no-clients,-e" ) ); //ProfilerStart( "io-publish.prof" ); { while( std::cin.good() && !is_shutdown && p.read() ); //ProfilerStop(); } if( is_shutdown ) { std::cerr << "io-publish: interrupted by signal" << std::endl; } return 0; } catch( std::exception& ex ) { if( comma::last_error::value() == EINTR || comma::last_error::value() == EBADF ) { return 0; } std::cerr << "io-publish: " << ex.what() << std::endl; } catch( ... ) { if( comma::last_error::value() == EINTR || comma::last_error::value() == EBADF ) { return 0; } std::cerr << "io-publish: unknown exception" << std::endl; } return 1; }
int main( int ac, char** av ) { try { const boost::array< comma::signal_flag::signals, 2 > signals = { { comma::signal_flag::sigint, comma::signal_flag::sigterm } }; comma::signal_flag is_shutdown( signals ); comma::command_line_options options( ac, av ); if( options.exists( "--help" ) || options.exists( "-h" ) ) { usage(); } std::vector< std::string > names = options.unnamed( "--no-discard,--verbose,-v,--no-flush", "-n,--number,-m,--multiplier,-b,--buffer,-s,--size" ); unsigned int n = options.value( "-n,--number", 0 ); unsigned int packet_size = options.value( "-s,--size", 0 ) * options.value( "-m,--multiplier", 1 ); unsigned int buffer_size = options.value( "-b,--buffer", 0 ); bool discard = !options.exists( "--no-discard" ); bool flush = !options.exists( "--no-flush" ); bool binary = packet_size != 0; if( names.empty() ) { std::cerr << "io-publish: please specify at least one file ('-' for stdout)" << std::endl; usage(); } if( binary ) { //ProfilerStart( "io-publish.prof" ); { comma::io::applications::publish publish( names, n, buffer_size, packet_size, discard ); while( !is_shutdown && publish.read_bytes() ); //ProfilerStop(); } } else { comma::io::applications::publish publish( names, n, 1, 0, discard, flush ); while( !is_shutdown && std::cin.good() && !std::cin.eof() ) { publish.read_line(); } } if( is_shutdown ) { std::cerr << "io-publish: interrupted by signal" << std::endl; } return 0; } catch( std::exception& ex ) { #ifndef WIN32 if( comma::last_error::value() == EINTR || comma::last_error::value() == EBADF ) { return 0; } #endif std::cerr << "io-publish: " << ex.what() << std::endl; } catch( ... ) { #ifndef WIN32 if( comma::last_error::value() == EINTR || comma::last_error::value() == EBADF ) { return 0; } #endif std::cerr << "io-publish: unknown exception" << std::endl; } return 1; }
void GraphListener::remove_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph) { if (!node_graph) { throw std::invalid_argument("node is nullptr"); } std::lock_guard<std::mutex> shutdown_lock(shutdown_mutex_); if (is_shutdown()) { // If shutdown, then the run loop has been joined, so we can remove them directly. return remove_node_(&node_graph_interfaces_, node_graph); } // Otherwise, first interrupt and lock against the run loop to safely remove the node. // Acquire the nodes mutex using the barrier to prevent the run loop from // re-locking the nodes mutex after being interrupted. acquire_nodes_lock_( &node_graph_interfaces_barrier_mutex_, &node_graph_interfaces_mutex_, &interrupt_guard_condition_); // Store the now acquired node_graph_interfaces_mutex_ in the scoped lock using adopt_lock. std::lock_guard<std::mutex> nodes_lock(node_graph_interfaces_mutex_, std::adopt_lock); remove_node_(&node_graph_interfaces_, node_graph); }
int main( int ac, char** av ) { try { comma::signal_flag is_shutdown(comma::signal_flag::hard); comma::command_line_options options( ac, av, usage ); if( options.exists( "--bash-completion" )) bash_completion( ac, av ); options.assert_mutually_exclusive( "--by-lower,--by-upper,--nearest,--realtime" ); if( options.exists( "--by-upper" )) { method = how::by_upper; } if( options.exists( "--nearest" )) { method = how::nearest; } if( options.exists( "--realtime" )) { method = how::realtime; } timestamp_only = options.exists( "--timestamp-only,--time-only" ); select_only = options.exists( "--do-not-append,--select" ); if( select_only && timestamp_only ) { std::cerr << "csv-time-join: --timestamp-only specified with --select, ignoring --timestamp-only" << std::endl; } bool discard_bounding = options.exists( "--discard-bounding" ); boost::optional< unsigned int > buffer_size = options.optional< unsigned int >( "--buffer" ); if( options.exists( "--bound" ) ) { bound = boost::posix_time::microseconds( static_cast<unsigned int>(options.value< double >( "--bound" ) * 1000000 )); } stdin_csv = comma::csv::options( options, "t" ); std::vector< std::string > unnamed = options.unnamed( "--by-lower,--by-upper,--nearest,--realtime,--select,--do-not-append,--timestamp-only,--time-only,--discard-bounding", "--binary,-b,--delimiter,-d,--fields,-f,--bound,--buffer,--verbose,-v" ); std::string properties; bool stdin_first = true; switch( unnamed.size() ) { case 0: std::cerr << "csv-time-join: please specify bounding source" << std::endl; return 1; case 1: properties = unnamed[0]; break; case 2: if( unnamed[0] == "-" ) { properties = unnamed[1]; } else if( unnamed[1] == "-" ) { properties = unnamed[0]; stdin_first = false; } else { std::cerr << "csv-time-join: expected either '- <bounding>' or '<bounding> -'; got : " << comma::join( unnamed, ' ' ) << std::endl; return 1; } break; default: std::cerr << "csv-time-join: expected either '- <bounding>' or '<bounding> -'; got : " << comma::join( unnamed, ' ' ) << std::endl; return 1; } comma::name_value::parser parser( "filename" ); bounding_csv = parser.get< comma::csv::options >( properties ); if( bounding_csv.fields.empty() ) { bounding_csv.fields = "t"; } comma::csv::input_stream< Point > stdin_stream( std::cin, stdin_csv ); #ifdef WIN32 if( stdin_csv.binary() ) { _setmode( _fileno( stdout ), _O_BINARY ); } #endif // #ifdef WIN32 comma::io::istream bounding_istream( comma::split( properties, ';' )[0] , bounding_csv.binary() ? comma::io::mode::binary : comma::io::mode::ascii ); comma::csv::input_stream< Point > bounding_stream( *bounding_istream, bounding_csv ); #ifndef WIN32 comma::io::select select; comma::io::select bounding_stream_select; select.read().add( 0 ); select.read().add( bounding_istream.fd() ); bounding_stream_select.read().add( bounding_istream.fd() ); #endif // #ifndef WIN32 const Point* p = NULL; if( method == how::realtime ) { #ifndef WIN32 bool end_of_input = false; bool end_of_bounds = false; boost::optional<timestring_t> joined_line; while (!is_shutdown && !end_of_input) { if ( !bounding_stream.ready() && !stdin_stream.ready() ) { select.wait(boost::posix_time::milliseconds(1)); } if ( !is_shutdown && !end_of_input && ( stdin_stream.ready() || ( select.check() && select.read().ready( comma::io::stdin_fd ) ) ) ) { p = stdin_stream.read(); if( p ) { timestring_t input_line = std::make_pair( get_time( *p ), stdin_stream.last() ); if( joined_line ) { output( input_line, *joined_line, stdin_first ); } } else { comma::verbose << "end of input stream" << std::endl; end_of_input = true; } } if ( !is_shutdown && !end_of_bounds && ( bounding_stream.ready() || ( select.check() && select.read().ready( bounding_istream.fd() )))) { p = bounding_stream.read(); if( p ) { joined_line = std::make_pair( get_time( *p ), bounding_stream.last() ); } else { comma::verbose << "end of bounding stream" << std::endl; end_of_bounds = true; } } } if (is_shutdown) { comma::verbose << "got a signal" << std::endl; return 0; } #else COMMA_THROW(comma::exception, "--realtime mode not supported in WIN32"); #endif } else { std::deque<timestring_t> bounding_queue; bool next = true; bool bounding_data_available; bool upper_bound_added = false; // add a fake entry for an lower bound to allow stdin before first bound to match bounding_queue.push_back( std::make_pair( boost::posix_time::neg_infin, "" )); while( ( stdin_stream.ready() || ( std::cin.good() && !std::cin.eof() ) ) ) { if( !std::cin.good() ) { select.read().remove( 0 ); } if( !bounding_istream->good() ) { select.read().remove( bounding_istream.fd() ); } bounding_data_available = bounding_stream.ready() || ( bounding_istream->good() && !bounding_istream->eof() ); #ifdef WIN32 bool bounding_stream_ready = true; bool stdin_stream_ready = true; #else // #ifdef WIN32 //check so we do not block bool bounding_stream_ready = bounding_stream.ready(); bool stdin_stream_ready = stdin_stream.ready(); if( next ) { if( !bounding_stream_ready || !stdin_stream_ready ) { if( !bounding_stream_ready && !stdin_stream_ready ) { select.wait( boost::posix_time::milliseconds(10) ); } else { select.check(); } if( select.read().ready( bounding_istream.fd() )) { bounding_stream_ready = true; } if( select.read().ready(0) ) { stdin_stream_ready=true; } } } else { if( !bounding_stream_ready ) { bounding_stream_select.wait( boost::posix_time::milliseconds(10) ); if( bounding_stream_select.read().ready( bounding_istream.fd() )) { bounding_stream_ready=true; } } } #endif //#ifdef WIN32 //keep storing available bounding data if( bounding_stream_ready ) { if( !buffer_size || bounding_queue.size() < *buffer_size || discard_bounding ) { const Point* q = bounding_stream.read(); if( q ) { bounding_queue.push_back( std::make_pair( get_time( *q ), bounding_stream.last() )); } else { bounding_data_available=false; } } if( buffer_size && bounding_queue.size() > *buffer_size && discard_bounding ) { bounding_queue.pop_front(); } } if( !upper_bound_added && bounding_istream->eof() ) { // add a fake entry for an upper bound to allow stdin data above last bound to match bounding_queue.push_back( std::make_pair( boost::posix_time::pos_infin, "" )); upper_bound_added = true; } //if we are done with the last bounded point get next if( next ) { if(!stdin_stream_ready) { continue; } p = stdin_stream.read(); if( !p ) { break; } } boost::posix_time::ptime t = get_time(*p); //get bound while(bounding_queue.size()>=2) { if( t < bounding_queue[1].first ) { break; } bounding_queue.pop_front(); } if(bounding_queue.size()<2) { //bound not found //do we have more data? if(!bounding_data_available) { break; } next=false; continue; } //bound available if( method == how::by_lower && t < bounding_queue.front().first ) { next = true; continue; } bool is_first = ( method == how::by_lower ) || ( method == how::nearest && ( t - bounding_queue[0].first ) < ( bounding_queue[1].first - t )); const timestring_t& chosen_bound = is_first ? bounding_queue[0] : bounding_queue[1];; timestring_t input_line = std::make_pair( t, stdin_stream.last() ); output( input_line, chosen_bound, stdin_first ); next=true; } } return 0; } catch( std::exception& ex ) { std::cerr << "csv-time-join: " << ex.what() << std::endl; } catch( ... ) { std::cerr << "csv-time-join: unknown exception" << std::endl; } }