void IksConnection::connect() { sz_log(10, "IksConnection(%p):connect", this); auto self = shared_from_this(); state = CONNECTING; socket->connect(); connect_timeout_timer.expires_from_now( boost::posix_time::seconds( 20 ) ); connect_timeout_timer.async_wait([self] ( const bs::error_code& ec ) { if ( ec == bae::operation_aborted ) return; sz_log(10, "IksConnection(%p):connect_timeout_timer kicks in, state:%d", self.get(), int(self->state)); switch (self->state) { case CONNECTING: self->schedule_reconnect(); break; default: break; } }); }
void IksConnection::schedule_keepalive() { sz_log(10, "IksConnection(%p): schedule_keepalive", this); auto self = shared_from_this(); keepalive_timer.expires_from_now( boost::posix_time::seconds( 5 ) ); keepalive_timer.async_wait([self] ( const bs::error_code& ec ) { if ( ec == bae::operation_aborted ) return; self->send_command("i_will_not_buy_this_record_it_is_scratched", "" , [self] ( const bs::error_code &ec , const std::string& status , std::string& data ) { if ( !ec && self->state == CONNECTED ) self->schedule_keepalive(); bs::error_code _ec; self->keepalive_timeout_timer.cancel(_ec); return IksCmdStatus::cmd_done; }); self->keepalive_timeout_timer.expires_from_now( boost::posix_time::seconds( 20 ) ); self->keepalive_timeout_timer.async_wait([self] ( const bs::error_code& ec ) { if ( ec == bae::operation_aborted ) return; sz_log(5, "IksConnection(%p): keepalive timeout timer kicked in, state:%d" , self.get(), int(self->state)); switch (self->state) { case CONNECTED: break; default: return; } auto _ec = make_error_code( bs::errc::stream_timeout ); std::string empty; for ( auto& i : self->commands ) i.second( _ec , "" , empty ); self->commands.clear(); self->connection_error_sig( _ec ); self->schedule_reconnect(); }); }); }
bool daemonize( ba::io_service& service ) { try { // Inform the io_service that we are about to become a daemon. service.notify_fork(ba::io_service::fork_prepare); if( pid_t pid = fork() ) { if (pid > 0) { service.notify_fork(ba::io_service::fork_parent); exit(0); } else { sz_log(0, "Daemonize: First fork failed: %s", strerror(errno)); return false; } } setsid(); (void)chdir("/"); umask(0); service.notify_fork(ba::io_service::fork_prepare); if( pid_t pid = fork() ) { if (pid > 0) { service.notify_fork(ba::io_service::fork_parent); exit(0); } else { sz_log(0, "Daemonize: Second fork failed: %s", strerror(errno)); return false; } } close(0); close(1); close(2); if (open("/dev/null", O_RDONLY) < 0) { sz_log(0, "Daemonize: Unable to open /dev/null: %s", strerror(errno)); return false; } // Inform the io_service that we have finished becoming a daemon. service.notify_fork(boost::asio::io_service::fork_child); } catch (std::exception& e) { sz_log(0, "Daemonize: Exception: %s", e.what()); return false; } return true; }
void SzbaseProt::set_current_set( Set::const_ptr s , ProbeType pt ) { current_set = s; current_pt = pt; if( !current_set ) { sub_set.cancel(); return; } /** Prevent from sending values double if they values changed on subscribe */ boost::signals2::shared_connection_block block(conn_param_value); sub_set = vars.get_updater().subscribe_params( *s , pt ); for( auto itr=current_set->begin() ; itr!=current_set->end() ; ++itr ) { auto p = vars.get_params().get_param( *itr ); if( p ) send_cmd( new ValueSnd(p,pt) ); else sz_log(0, "Unknown param (%s) in set (%s)", itr->c_str() , s->get_name().c_str() ); } }
void IksConnection::handle_error( const bs::error_code& ec ) { if ( ec == bae::operation_aborted ) return; sz_log(5, "IksConnection(%p):handle_error, error: %s, state: %d", this, ec.message().c_str(), int(state)); switch (state) { case CONNECTED: break; case CONNECTING: schedule_reconnect(); return; case WAITING: return; } std::string empty; for ( auto& i : commands ) i.second( ec , "" , empty ); commands.clear(); connection_error_sig(ec); bs::error_code _ec; keepalive_timer.cancel(_ec); schedule_reconnect(); }
void Set::from_json( const bp::ptree& ptree ) { /* TODO: Verify ptree (19/03/2014 20:48, jkotur) */ set_desc = ptree; params.clear(); name = set_desc.get<std::string>("@name"); auto& pdesc = set_desc.get_child("params"); /** Convert orders to doubles and find maximal order value */ double max_order = 0; for( auto ic=pdesc.begin() ; ic!=pdesc.end() ; ++ic ) { auto so = ic->second.get_optional<double>("@order"); if( !so ) continue; try { double o = boost::lexical_cast<double>(*so); ic->second.put("@order",o); max_order = std::max( max_order , o ); } catch( boost::bad_lexical_cast& e ) { sz_log(0, "Invalid order in param %s" , ic->second.get<std::string>("@name").c_str()); ic->second.erase("@order"); } } convert_color_names_to_hex(); /** Put params without order at the end */ int i = 0; for( auto ic=pdesc.begin() ; ic!=pdesc.end() ; ++ic ) { auto o = ic->second.get_optional<double>("@order"); if( !o ) ic->second.put("@order",max_order+ ++i); } for( auto ic=pdesc.begin() ; ic!=pdesc.end() ; ++ic ) { bp::ptree pt = ic->second; pt.erase("@order"); params.insert( ParamId { ic->second.get<std::string>("@name") , ic->second.get<double>("@order") , pt } ); } set_desc.erase("params"); generate_colors_like_draw3(); update_hash(); }
void LocationsMgr::add_locations( const CfgSections& cfg ) { for( auto itr=cfg.begin() ; itr!=cfg.end() ; ++itr ) try { add_location( itr->first , itr->second ); } catch( config_error& e ) { sz_log(1,"Invalid configuration at %s: %s" , itr->first.c_str() , e.what() ); } }
void IksConnection::schedule_reconnect() { sz_log(10, "IksConnection(%p): schedule_reconnect", this); auto self = shared_from_this(); self->disconnect(); reconnect_timer.expires_from_now(boost::posix_time::seconds(1)); reconnect_timer.async_wait([self] (const bs::error_code& ec) { if (ec == bae::operation_aborted) return; sz_log(10, "IksConnection(%p): schedule reconnect timer ticked, connecting", self.get()); self->connect(); }); state = WAITING; }
location_connection::location_connection(IPKContainer* container, boost::asio::io_service& io, const std::string& location, const std::string& server, const std::string& port, const std::string& defined_param_prefix) : m_container(container), m_location(location), m_connection(std::make_shared<IksConnection>(io, server, port)), m_defined_param_prefix(defined_param_prefix), m_connected(false) { sz_log(10, "location_connection::location_connection(%p), m_connection(%p), location: %s" , this, m_connection.get(), location.c_str() ); }
void AsioHandler::handle_connect(const bs::error_code& error) { if( handle_error(error) || !is_valid() ) return; const auto& e = client.socket_.remote_endpoint(); sz_log(3, "+++ Connected to %s:%d", e.address().to_string().c_str(), e.port()); client.emit_connected( &client ); do_read_line(); }
IksConnection::IksConnection( ba::io_service& io , const std::string& server , const std::string& port) : next_cmd_id(0) , socket( std::make_shared<TcpClientSocket>( io , server , port , *this ) ) , keepalive_timer( io ) , keepalive_timeout_timer( io ) , reconnect_timer( io ) , connect_timeout_timer( io ) { sz_log( 10 , "IksConnection::IksConnection(%p) socket:(%p)" , this , socket.get() ); }
void IksConnection::handle_connected() { sz_log(10, "IksConnection(%p):handle_connected", this); bs::error_code _ec; connect_timeout_timer.cancel(_ec); state = CONNECTED; connected_sig(); schedule_keepalive(); }
void SzbaseWrapper::purge_cache() { size_t size_in_bytes, blocks_count; auto cache = base->cache(); cache->cache_size( size_in_bytes , blocks_count ); if( size_in_bytes > base_cache_high_water_mark ) { size_t to_purge = size_in_bytes - base_cache_low_water_mark; sz_log(5, "Purging cache, cache size %zu, purging %zu bytes", size_in_bytes, to_purge); cache->remove( to_purge ); } }
void IksConnection::handle_read_line( ba::streambuf& buf ) { std::istream is( &buf ); std::string tag; std::string data; IksCmdId id; std::getline(is, tag, ' '); is >> id; is.ignore(1); std::getline(is, data); if ( !is ) { sz_log(1, "IksConnection(%p):handle_read_line invalid response from server, tag: %s, data: %s" , this, tag.c_str(), data.c_str()); auto ec = make_error_code( iks_client_error::invalid_server_response ); std::string empty; for ( auto& i : commands ) i.second( ec , "" , empty ); commands.clear(); connection_error_sig( ec ); schedule_reconnect(); return; } sz_log(10, "IksConnection(%p): received message: tag: %s, id:%d data: %s" , this, tag.c_str(), int(id), data.c_str() ); auto i = commands.find(id); if ( i != commands.end() ) { if ( i->second( make_error_code( bs::errc::success ), tag , data ) == cmd_done) commands.erase(i); } else cmd_sig( tag, id, data ); }
void AsioHandler::handle_read_line( const bs::error_code& error, size_t bytes ) { if( handle_error(error) || !is_valid() ) return; ba::streambuf::const_buffers_type bufs = client.read_buffer.data(); std::string line( ba::buffers_begin(bufs), ba::buffers_begin(bufs) + bytes - 1 ); client.read_buffer.consume( bytes ); boost::algorithm::trim( line ); sz_log(9, ">>> %s", line.c_str()); try { client.emit_line_received( line ); } catch( const std::exception& e ) { sz_log(9, "Exception occurred during emit_line_received: %s" , e.what() ); } do_read_line(); }
bool AsioHandler::handle_error( const bs::error_code& error ) { if( !error ) return false; sz_log(3, "--- TcpClient disconnected (%s)", error.message().c_str() ); if( is_valid() ) { client.emit_disconnected( &client ); invalidate(); } return true; }
void location_connection::connect_to_location() { auto self = shared_from_this(); m_connection->send_command("connect" , m_location, [self] ( const bs::error_code& ec, const std::string& status, std::string& data ) { sz_log( 5 , "location_connection(%p)::connect_to_location error: %s" , self.get() , ec.message().c_str() ); if ( ec ) { self->connection_error_sig( ec ); return IksCmdStatus::cmd_done; } if ( status != "k" ) { sz_log( 5 , "location_connection(%p)::connect_to_location not ok from server" " error: %s , data: %s" , self.get(), status.c_str() , data.c_str() ); self->connection_error_sig( make_iks_error_code( data ) ); return IksCmdStatus::cmd_done; } sz_log( 10 , "location_connection(%p)::connected " , self.get() ); self->m_connected = true; self->add_defined_params(); self->send_cached(); self->connected_sig(); return IksCmdStatus::cmd_done; }); }
void IksConnection::send_command( IksCmdId id , const std::string& cmd , const std::string& data ) { sz_log(10, "IksConnection(%p):send_command id:%d cmd:%s data:\"%s\"", this , int(id), cmd.c_str(), data.c_str() ); std::ostringstream os; os << cmd << " " << id; if ( data.size() ) os << " " << data; os << "\n"; socket->write( os.str() ); }
void IksConnection::disconnect() { sz_log(10, "IksConnection(%p):disconnect", this); socket->close(); }