<s> from __future__ import absolute_import <EOL> import time <EOL> import logging <EOL> import tornado . ioloop <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Client ( object ) : <EOL> def __init__ ( self , io_loop = None , ** kwargs ) : <EOL> self . io_loop = io_loop <EOL> if not self . io_loop : <EOL> self . io_loop = tornado . ioloop . IOLoop . instance ( ) <EOL> tornado . ioloop . PeriodicCallback ( self . _check_last_recv_timestamps , <EOL> <NUM_LIT> * <NUM_LIT:1000> , <EOL> io_loop = self . io_loop ) . start ( ) <EOL> def _on_connection_identify ( self , conn , data , ** kwargs ) : <EOL> logger . info ( '<STR_LIT>' % ( conn . id , self . name , data ) ) <EOL> def _on_connection_identify_response ( self , conn , data , ** kwargs ) : <EOL> logger . info ( '<STR_LIT>' % ( conn . id , self . name , data ) ) <EOL> if conn . tls_v1 and not data . get ( '<STR_LIT>' ) : <EOL> logger . warning ( '<STR_LIT>' , <EOL> conn . id , self . name ) <EOL> if conn . snappy and not data . get ( '<STR_LIT>' ) : <EOL> logger . warning ( '<STR_LIT>' , <EOL> conn . id , self . name ) <EOL> def _on_connection_auth ( self , conn , data , ** kwargs ) : <EOL> logger . info ( '<STR_LIT>' % ( conn . id , self . name ) ) <EOL> def _on_connection_auth_response ( self , conn , data , ** kwargs ) : <EOL> metadata = [ ] <EOL> if data . get ( '<STR_LIT>' ) : <EOL> metadata . append ( "<STR_LIT>" % data [ '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> metadata . append ( "<STR_LIT>" % data [ '<STR_LIT>' ] ) <EOL> if data . get ( '<STR_LIT>' ) : <EOL> metadata . append ( data [ '<STR_LIT>' ] ) <EOL> logger . info ( '<STR_LIT>' % ( conn . id , self . name , '<STR_LIT:U+0020>' . join ( metadata ) ) ) <EOL> def _on_connection_error ( self , conn , error , ** kwargs ) : <EOL> if kwargs : <EOL> logger . error ( '<STR_LIT>' , conn . id , self . name , kwargs ) <EOL> logger . error ( '<STR_LIT>' , conn . id , self . name , error ) <EOL> def _check_last_recv_timestamps ( self ) : <EOL> now = time . time ( ) <EOL> def is_stale ( conn ) : <EOL> timestamp = conn . last_recv_timestamp <EOL> return ( now - timestamp ) > ( ( conn . heartbeat_interval * <NUM_LIT:2> ) / <NUM_LIT> ) <EOL> stale_connections = [ conn for conn in self . conns . values ( ) if is_stale ( conn ) ] <EOL> for conn in stale_connections : <EOL> timestamp = conn . last_recv_timestamp <EOL> logger . warning ( '<STR_LIT>' , <EOL> conn . id , self . name , ( now - timestamp ) ) <EOL> conn . close ( ) <EOL> def _on_heartbeat ( self , conn ) : <EOL> logger . info ( '<STR_LIT>' % ( conn . id , self . name ) ) <EOL> self . heartbeat ( conn ) <EOL> def heartbeat ( self , conn ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
