<s> import rospy <EOL> from rosbridge_library . rosbridge_protocol import RosbridgeProtocol <EOL> import SocketServer <EOL> class RosbridgeTcpSocket ( SocketServer . BaseRequestHandler ) : <EOL> """<STR_LIT>""" <EOL> busy = False <EOL> queue = [ ] <EOL> client_id_seed = <NUM_LIT:0> <EOL> clients_connected = <NUM_LIT:0> <EOL> incoming_buffer = <NUM_LIT> <EOL> socket_timeout = <NUM_LIT:10> <EOL> fragment_timeout = <NUM_LIT> <EOL> delay_between_messages = <NUM_LIT:0> <EOL> max_message_size = None <EOL> def setup ( self ) : <EOL> cls = self . __class__ <EOL> parameters = { <EOL> "<STR_LIT>" : cls . fragment_timeout , <EOL> "<STR_LIT>" : cls . delay_between_messages , <EOL> "<STR_LIT>" : cls . max_message_size <EOL> } <EOL> try : <EOL> self . protocol = RosbridgeProtocol ( cls . client_id_seed , parameters = parameters ) <EOL> self . protocol . outgoing = self . send_message <EOL> cls . client_id_seed += <NUM_LIT:1> <EOL> cls . clients_connected += <NUM_LIT:1> <EOL> self . protocol . log ( "<STR_LIT:info>" , "<STR_LIT>" + str ( cls . clients_connected ) + "<STR_LIT>" ) <EOL> except Exception as exc : <EOL> rospy . logerr ( "<STR_LIT>" , str ( exc ) ) <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> cls = self . __class__ <EOL> self . request . settimeout ( cls . socket_timeout ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> data = self . request . recv ( cls . incoming_buffer ) <EOL> if data . strip ( ) == '<STR_LIT>' : <EOL> break <EOL> elif len ( data . strip ( ) ) > <NUM_LIT:0> : <EOL> self . protocol . incoming ( data . strip ( '<STR_LIT>' ) ) <EOL> else : <EOL> pass <EOL> except Exception , e : <EOL> pass <EOL> self . protocol . log ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> cls = self . __class__ <EOL> cls . clients_connected -= <NUM_LIT:1> <EOL> self . protocol . finish ( ) <EOL> self . protocol . log ( "<STR_LIT:info>" , "<STR_LIT>" + str ( cls . clients_connected ) + "<STR_LIT>" ) <EOL> def send_message ( self , message = None ) : <EOL> """<STR_LIT>""" <EOL> self . request . send ( message ) </s>
