def startUdpListenerSimple()

in python/awsgs.py [0:0]


def startUdpListenerSimple(listenerPort, bufferSize, outputFile):

    #   Starts a UDP server and writes received data to a file, no packet processing

    localIP             = "127.0.0.1"
    numPacketsReceived  = 0
    numPacketsProcessed = 0
    totalDataSize       = 0
    inMemoryPayload     = b''
    inMemoryPayload = io.BytesIO(b'')
    outputMessageEveryNPackets = 1000

    try:
        UDPServerSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #   Set the socket timeout to 120 seconds to trigger post-
        #   -processing after 2 mins of no data
        UDPServerSocket.settimeout(120)
        UDPServerSocket.bind((localIP, listenerPort))
        print("\nUDP server listening on %s:%s" % (localIP, listenerPort) )
    except Exception as e:

        if e.errno == 1:
            print(" (ERR) [ERROR_ROOT_REQUIRED] Socket open error: %s" % e )
        else:
            print(" (ERR) [ERROR_CANT_OPEN_SOCKET] Socket open error: %s" % e )
        return 0

    #   Open file handle
    try:
        f = open(outputFile, "wb")
    except Exception as e:
        print(" (ERR) File open error: %s" % e )
        return 0

    while(True):

        try:
            bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
            message          = bytesAddressPair[0]

            try:
                #   Option 1: Write received data to file
                #   Will cause packets to be dropped on a slow disk
                #f = open(outputFile, "wb+")
                #num_bytes_written = f.write(message)
                #f.flush()
                #os.fsync(f.fileno())
                #return num_bytes_written

                #   Test 2: Write unprocessed data to inMemoryBuffer (ByteIO Stream)
                #inMemoryPayload = inMemoryPayload + message
                #inMemoryPayload.write(message)

                #   Test 3: Extract VRT payload from the data then write data to ByteIO Stream (in-memory buffer)
                payload = extractVrtPayloadFromBin(message)
                inMemoryPayload.write(payload)

                #   Test 4: Extract VRT payload from the data then write data to file
                #payload = extractVrtPayloadFromBin(message)
                #inMemoryPayload.write(payload)

                if numPacketsReceived == 0:
                    #   Provide output for first packet only
                    print ("Received first packet. Size: %d Bytes" % (len(message)) )
                    print ("VRT Payload Size: %d Bytes" % (len(payload)) )

            except Exception as e:
                print(" (ERR) Packet processing error: %s" % e )
                break

            numPacketsReceived += 1

        except socket.timeout:
            #   No data received within the configured timeout period
            print ("Num Packets Received: %d" % (numPacketsReceived) )

            #   Get data from the BytesIO in-memory stream
            bufferData = inMemoryPayload.getvalue()
            totalBytesReceived = len(bufferData)
            if numPacketsReceived > 1 and totalBytesReceived > 0:

                #   Assume the transmission has finished
                #   Write received data to file
                try:
                    print("Writing memory buffer (%d Bytes) to output file..." % (totalBytesReceived) )
                    num_bytes_written = f.write(bufferData)

                    #   Make sure the buffer is flushed correctly
                    f.flush()
                    os.fsync(f.fileno())
                    f.close()

                    #   Clear the memory buffer
                    inMemoryPayload = b''

                    print("%d Bytes written to output file" % (num_bytes_written) )
                    exit()

                except Exception as e:
                    print("(ERR) File write error: %s" % e )
                    return 0


        except Exception as e:
            print("(ERR) Socket receive error: %s" % e )
            return 0