def wait_for_service_request():                                                 # receive data from rosbridge
    data = None
    global buffer

    try:
        done = False
        global buffer
        while not done:
            incoming = tcp_socket.recv(max_msg_length)                          # get data from socket
            if incoming == '':
                print "connection closed by peer"
                sys.exit(1)
            buffer = buffer + incoming                                          # append data to buffer
            try:                                                                # try to parse JSON from buffer
                data_object = json.loads(buffer)
                if data_object["op"] == "call_service":
                    data = buffer
                    done = True
                    return data                                                 # if parsing was successful --> return data string
            except Exception, e:
                #print "direct_access error:"
                #print e
                pass
               
            #print "trying to defragment"
            try:                                                                # opcode was not "call_service" -> try to defragment
                result_string = buffer.split("}{")                              # split buffer into fragments and re-fill with curly brackets
                result = []
                for fragment in result_string:
                    if fragment[0] != "{":
                        fragment = "{"+fragment
                    if fragment[len(fragment)-1] != "}":
                        fragment = fragment + "}"
                    result.append(json.loads(fragment))

                try:                                                            # try to defragment when received all fragments
                    fragment_count = len(result)
                    announced = int(result[0]["total"])

                    if fragment_count == announced:
                        reconstructed = ""
                        sorted_result = [None] * fragment_count                 # sort fragments..
                        unsorted_result = []
                        for fragment in result:
                            unsorted_result.append(fragment)
                            sorted_result[int(fragment["num"])] = fragment

                        for fragment in sorted_result:                          # reconstruct from fragments
                            reconstructed = reconstructed + fragment["data"]

                        #print "reconstructed", reconstructed
                        buffer = ""                                             # empty buffer
                        done = True
                        print "reconstructed message from", len(result), "fragments"
                        #print reconstructed
                        return reconstructed
                except Exception, e:
                    print "not possible to defragment:", buffer
                    print e
            except Exception, e:
                print "defrag_error:", buffer
                print e
                pass
    except Exception, e:
        #print "network-error(?):", e
        pass
    return data
dataset/ETHPy150Open RobotWebTools/rosbridge_suite/rosbridge_library/test/experimental/complex_srv+tcp/test_non-ros_service_server_complex-srv.py/wait_for_service_request/original