diff --git a/programs/beekeeper/core/include/core/time_manager_base.hpp b/programs/beekeeper/core/include/core/time_manager_base.hpp index 5673512a2f..87a06a7f1f 100644 --- a/programs/beekeeper/core/include/core/time_manager_base.hpp +++ b/programs/beekeeper/core/include/core/time_manager_base.hpp @@ -45,6 +45,9 @@ class time_manager_base session_data_index items; + bool run( const types::timepoint_t& now, const session_data& s_data, std::vector& modified_items ); + void modify_times( const std::vector& modified_items ); + protected: virtual void send_auto_lock_error_message( const std::string& message ){ /*not implemented here*/ }; @@ -58,6 +61,7 @@ class time_manager_base void change( const std::string& token, const types::timepoint_t& time, bool refresh_only_active ); void run(); + void run( const std::string& token ); void close( const std::string& token ); }; diff --git a/programs/beekeeper/core/session_base.cpp b/programs/beekeeper/core/session_base.cpp index 59bd8dbbcd..04b9a81382 100644 --- a/programs/beekeeper/core/session_base.cpp +++ b/programs/beekeeper/core/session_base.cpp @@ -17,7 +17,7 @@ void session_base::set_timeout( const std::chrono::seconds& t ) void session_base::check_timeout() { FC_ASSERT( time ); - time->run(); + time->run( token ); refresh_timeout( true/*refresh_only_active*/ ); } diff --git a/programs/beekeeper/core/time_manager_base.cpp b/programs/beekeeper/core/time_manager_base.cpp index eb67bd3e8d..57a5d68988 100644 --- a/programs/beekeeper/core/time_manager_base.cpp +++ b/programs/beekeeper/core/time_manager_base.cpp @@ -10,48 +10,40 @@ time_manager_base::~time_manager_base() { } -void time_manager_base::run() +bool time_manager_base::run( const types::timepoint_t& now, const session_data& s_data, std::vector& modified_items ) { - std::vector _modified_items; - + if( now >= s_data.time ) { - const auto& _now = std::chrono::system_clock::now(); - - auto& _idx_time = items.get(); - auto _it = _idx_time.begin(); - - while( _it != _idx_time.end() ) + auto _microseconds = std::chrono::duration_cast( now - s_data.time ); + if( _microseconds.count() / 1000 > 0 ) + dlog("lock activated: actual time is longer ${differ}[ms] than timeout time", ("differ", _microseconds.count() / 1000)); + else + dlog("lock activated: actual time is equals to timeout time"); + auto _result = exception::exception_handler([&]() + { + modified_items.emplace_back( s_data.token ); + return ""; + } + ); + if( !_result.second ) { - if( _now >= _it->time ) - { - auto _microseconds = std::chrono::duration_cast( _now - _it->time ); - if( _microseconds.count() / 1000 > 0 ) - dlog("lock activated: actual time is longer ${differ}[ms] than timeout time", ("differ", _microseconds.count() / 1000)); - else - dlog("lock activated: actual time is equals to timeout time"); - auto _result = exception::exception_handler([&]() - { - _modified_items.emplace_back( _it->token ); - return ""; - } - ); - if( !_result.second ) - { - send_auto_lock_error_message( _result.first ); - return; //First error during locking finishes whole procedure - } - } - ++_it; + send_auto_lock_error_message( _result.first ); + return false; //First error during locking finishes whole procedure } } - if( _modified_items.empty() ) + return true; +} + +void time_manager_base::modify_times( const std::vector& modified_items ) +{ + if( modified_items.empty() ) return; auto& _idx_token = items.get(); auto _result = exception::exception_handler([&]() { - for( auto& token : _modified_items ) + for( auto& token : modified_items ) { auto _found = _idx_token.find( token ); FC_ASSERT( _found != _idx_token.end() ); @@ -68,6 +60,42 @@ void time_manager_base::run() send_auto_lock_error_message( _result.first ); } +void time_manager_base::run() +{ + std::vector _modified_items; + + auto& _idx_time = items.get(); + auto _it = _idx_time.begin(); + + const auto& _now = std::chrono::system_clock::now(); + + while( _it != _idx_time.end() ) + { + if( !run( _now, *_it, _modified_items ) ) + return; + ++_it; + } + + modify_times( _modified_items ); +} + +void time_manager_base::run( const std::string& token ) +{ + std::vector _modified_items; + + auto& _idx = items.get(); + const auto& _found = _idx.find( token ); + + FC_ASSERT( _found != _idx.end() ); + + const auto& _now = std::chrono::system_clock::now(); + + if( !run( _now, *_found, _modified_items ) ) + return; + + modify_times( _modified_items ); +} + void time_manager_base::add( const std::string& token, types::lock_method_type&& lock_method, types::notification_method_type&& notification_method ) { auto& _idx = items.get(); @@ -79,6 +107,8 @@ void time_manager_base::change( const std::string& token, const types::timepoint auto& _idx = items.get(); const auto& _found = _idx.find( token ); + FC_ASSERT( _found != _idx.end() ); + _idx.modify( _found, [&time, &refresh_only_active]( session_data &sd ) { if( !refresh_only_active || ( refresh_only_active && sd.time != types::timepoint_t::max() ) )