in python/awsgs.py [0:0]
def startUdpListenerBin(listenerPort, bufferSize, outputFile):
# Starts a UDP server and writes received data to a file
localIP = "127.0.0.1"
numPacketsReceived = 0
numPacketsProcessed = 0
totalDataSize = 0
inMemoryPayload = b''
outputMessageEveryNPackets = 1000
try:
UDPServerSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Set the socket timeout to 10 seconds
UDPServerSocket.settimeout(10)
#UDPServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 3000000)
UDPServerSocket.bind((localIP, listenerPort))
print("\nUDP server listening on %s:%s" % (localIP, listenerPort) )
print("Output file: %s\n" % (outputFile))
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", buffering=bufferSize)
except Exception as e:
print(" (ERR) File open error: %s" % e )
return 0
while(True):
# Read data into string
try:
bytesAddressPair = UDPServerSocket.recvfrom(bufferSize)
message = bytesAddressPair[0]
#address = str(bytesAddressPair[1][0])
#appendToRawFile(outputFile, message)
numPacketsReceived += 1
payloadLength = 0
payload = extractVrtPayloadFromBin(message)
# left to test if above function is causing the receive buffer issues
#payload = message
payloadLength = len(payload)
if payloadLength < 1:
print("[%d] Packet not a valid VITA 49 format" % numPacketsReceived)
else:
numPacketsProcessed += 1
# Add payload to inMemory payload chunk
inMemoryPayload = inMemoryPayload + payload
# Print out message every 1000 packets
# If we print on ever packet STDOUT cant keep up
#if numPacketsProcessed % outputMessageEveryNPackets == 0 or numPacketsReceived == 1:
# print("[%d] Receiving Data. Total payload %d Bytes" % (numPacketsReceived, len(inMemoryPayload)) )
except socket.timeout:
if len(inMemoryPayload) > 0:
# Write received data to file
try:
print("Writing memory buffer to output file...")
#f = open(outputFile, "ab", buffering=8192)
num_bytes_written = f.write(inMemoryPayload)
# Make sure the buffer is flushed correctly
f.flush()
os.fsync(f.fileno())
# Clear the memory buffer
inMemoryPayload = b''
print("%d Payload Bytes written from %d packets" % (num_bytes_written, numPacketsProcessed) )
totalDataSize += num_bytes_written
except Exception as e:
print("(ERR) File write error: %s" % e )
return 0
print('')
print('Status Update:')
print(" %d packets received" % (numPacketsReceived))
print(" %d packets processed successfully" % (numPacketsProcessed))
print(" %d Total Bytes written to %s" % (totalDataSize, outputFile))