<s> import sys <EOL> import socket <EOL> import time <EOL> from random import randint <EOL> from rosbridge_library . util import json <EOL> tcp_socket_timeout = <NUM_LIT:10> <EOL> max_msg_length = <NUM_LIT> <EOL> rosbridge_ip = "<STR_LIT:localhost>" <EOL> rosbridge_port = <NUM_LIT> <EOL> service_type = "<STR_LIT>" <EOL> service_name = "<STR_LIT>" <EOL> send_fragment_size = <NUM_LIT:1000> <EOL> send_fragment_delay = <NUM_LIT> <EOL> receive_fragment_size = <NUM_LIT:10> <EOL> receive_message_intervall = <NUM_LIT:0.0> <EOL> def calculate_service_response ( request ) : <EOL> request_object = json . loads ( request ) <EOL> args = request_object [ "<STR_LIT:args>" ] <EOL> message = { "<STR_LIT:data>" : { "<STR_LIT:data>" : <NUM_LIT> } } <EOL> """<STR_LIT>""" <EOL> service_response_data = message <EOL> response_object = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : request_object [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT:data>" : service_response_data <EOL> } <EOL> response_message = json . dumps ( response_object ) <EOL> return response_message <EOL> buffer = "<STR_LIT>" <EOL> def connect_tcp_socket ( ) : <EOL> tcp_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> tcp_sock . settimeout ( tcp_socket_timeout ) <EOL> tcp_sock . connect ( ( rosbridge_ip , rosbridge_port ) ) <EOL> return tcp_sock <EOL> def advertise_service ( ) : <EOL> advertise_message_object = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : service_type , <EOL> "<STR_LIT>" : service_name , <EOL> "<STR_LIT>" : receive_fragment_size , <EOL> "<STR_LIT>" : receive_message_intervall <EOL> } <EOL> advertise_message = json . dumps ( advertise_message_object ) <EOL> tcp_socket . send ( str ( advertise_message ) ) <EOL> def unadvertise_service ( ) : <EOL> unadvertise_message_object = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : service_name <EOL> } <EOL> unadvertise_message = json . dumps ( unadvertise_message_object ) <EOL> tcp_socket . send ( str ( unadvertise_message ) ) <EOL> def wait_for_service_request ( ) : <EOL> data = None <EOL> global buffer <EOL> try : <EOL> done = False <EOL> global buffer <EOL> while not done : <EOL> incoming = tcp_socket . recv ( max_msg_length ) <EOL> if incoming == '<STR_LIT>' : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> buffer = buffer + incoming <EOL> try : <EOL> data_object = json . loads ( buffer ) <EOL> if data_object [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> data = buffer <EOL> done = True <EOL> return data <EOL> except Exception , e : <EOL> pass <EOL> try : <EOL> result_string = buffer . split ( "<STR_LIT>" ) <EOL> result = [ ] <EOL> for fragment in result_string : <EOL> if fragment [ <NUM_LIT:0> ] != "<STR_LIT:{>" : <EOL> fragment = "<STR_LIT:{>" + fragment <EOL> if fragment [ len ( fragment ) - <NUM_LIT:1> ] != "<STR_LIT:}>" : <EOL> fragment = fragment + "<STR_LIT:}>" <EOL> result . append ( json . loads ( fragment ) ) <EOL> try : <EOL> fragment_count = len ( result ) <EOL> announced = int ( result [ <NUM_LIT:0> ] [ "<STR_LIT>" ] ) <EOL> if fragment_count == announced : <EOL> reconstructed = "<STR_LIT>" <EOL> sorted_result = [ None ] * fragment_count <EOL> unsorted_result = [ ] <EOL> for fragment in result : <EOL> unsorted_result . append ( fragment ) <EOL> sorted_result [ int ( fragment [ "<STR_LIT>" ] ) ] = fragment <EOL> for fragment in sorted_result : <EOL> reconstructed = reconstructed + fragment [ "<STR_LIT:data>" ] <EOL> buffer = "<STR_LIT>" <EOL> done = True <EOL> print "<STR_LIT>" , len ( result ) , "<STR_LIT>" <EOL> return reconstructed <EOL> except Exception , e : <EOL> print "<STR_LIT>" , buffer <EOL> print e <EOL> except Exception , e : <EOL> print "<STR_LIT>" , buffer <EOL> print e <EOL> pass <EOL> except Exception , e : <EOL> pass <EOL> return data <EOL> def send_service_response ( response ) : <EOL> tcp_socket . send ( response ) <EOL> def list_of_fragments ( full_message , fragment_size ) : <EOL> message_id = randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> fragments = [ ] <EOL> cursor = <NUM_LIT:0> <EOL> while cursor < len ( full_message ) : <EOL> fragment_begin = cursor <EOL> if len ( full_message ) < cursor + fragment_size : <EOL> fragment_end = len ( full_message ) <EOL> cursor = len ( full_message ) <EOL> else : <EOL> fragment_end = cursor + fragment_size <EOL> cursor += fragment_size <EOL> fragment = full_message [ fragment_begin : fragment_end ] <EOL> fragments . append ( fragment ) <EOL> fragmented_messages_list = [ ] <EOL> if len ( fragments ) > <NUM_LIT:1> : <EOL> for count , fragment in enumerate ( fragments ) : <EOL> fragmented_message_object = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : str ( message_id ) , <EOL> "<STR_LIT:data>" : str ( fragment ) , <EOL> "<STR_LIT>" : count , <EOL> "<STR_LIT>" : len ( fragments ) <EOL> } <EOL> fragmented_message = json . dumps ( fragmented_message_object ) <EOL> fragmented_messages_list . append ( fragmented_message ) <EOL> else : <EOL> fragmented_messages_list . append ( str ( fragment ) ) <EOL> return fragmented_messages_list <EOL> tcp_socket = connect_tcp_socket ( ) <EOL> advertise_service ( ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> while True : <EOL> data = None <EOL> try : <EOL> data = wait_for_service_request ( ) <EOL> if data == '<STR_LIT>' : <EOL> break <EOL> elif data != None and len ( data ) > <NUM_LIT:0> : <EOL> response = calculate_service_response ( data ) <EOL> print "<STR_LIT>" <EOL> fragment_list = list_of_fragments ( response , send_fragment_size ) <EOL> print "<STR_LIT>" , len ( fragment_list ) , "<STR_LIT>" <EOL> for fragment in fragment_list : <EOL> send_service_response ( fragment ) <EOL> time . sleep ( send_fragment_delay ) <EOL> except Exception , e : <EOL> print e <EOL> pass <EOL> except KeyboardInterrupt : <EOL> try : <EOL> unadvertise_service ( ) <EOL> tcp_socket . close ( ) <EOL> except Exception , e : <EOL> print e <EOL> print "<STR_LIT>" </s>
