<commit_before>#!/usr/bin/env python

"""
    UdpApp python script
        Server send udp packets every NSECONDS
        Client listening the UDP_PORT
        [Copy to your /usr/bin directory for call in shell]

@author: arthurgodoy
"""
import sys
import socket
import json
import uuid
from time import time, sleep
from commum.DataCollector import UdpAppCollector

UDP_PORT=8885 #Port Listening/Sending

NSECONDS=0.030 #Send Period

MULTICAST_IP = "10.0.2.254"
MULTICAST_MAC = "ca:fe:ca:fe:ca:fe"

def parse_packet(packet_data):
    parsed_data = json.loads(packet_data)
    src_uuid = parsed_data['src_uuid']
    packet_number = parsed_data['packet_number']
    timestamp = parsed_data['timestamp']
    return src_uuid, packet_number, timestamp

def udpserver( IPs, PORT ):
    SRC_UUID = str(uuid.uuid4()) #Get a UUID for this server
    PKT_LONG = long(0) #Start a packet counter
    print "SRC UUID:", SRC_UUID
    print "UDP target IP:", IPs
    print "UDP target port:", UDP_PORT
    print "Sending interval:", NSECONDS, " seconds"

    sock = socket.socket( socket.AF_INET,        # Internet
                          socket.SOCK_DGRAM )    # UDP

    #Send each NSECONDS for each client
    while True:
        for UDP_IP in IPs:
            message = {'src_uuid' : SRC_UUID, 'packet_number' : str( PKT_LONG ), 'timestamp' : repr( time() )}
            sock.sendto(json.dumps(message), (UDP_IP, UDP_PORT) )
        
        PKT_LONG += 1
        sleep( NSECONDS )


#Register the multicast ip and mac on the arp table.
#os.system('arp -s ' + MULTICAST_IP + ' ' + MULTICAST_MAC)

#Arguments should be 2 or more
if len( sys.argv ) < 2:
    print " "
    print "Usage ="
    print "      Server:"
    print "              Unicast=    -s CLIENT_IP1 CLIENT_IP2 CLIENT_IPN"
    print "              Multicast=  -m"
    print "      Client:"
    print "              -c"
    print " "

#Server
elif sys.argv[1] == "-s":
    UDP_CLIENTS=sys.argv[2:] #Get the arguments starting from the third
    udpserver( UDP_CLIENTS, UDP_PORT )
    
#Client
elif sys.argv[1] == "-c":
    HOST_NAME = ""
    if len(sys.argv) >= 3:
        HOST_NAME=sys.argv[2]     #Third argument it's the binding IP

    UDP_IP = ""
    
    sock = socket.socket( socket.AF_INET,     # Internet
                          socket.SOCK_DGRAM ) # UDP
    sock.bind( (UDP_IP,UDP_PORT) )
    
    uuidstr = HOST_NAME + '---' + str(uuid.uuid4())
    dc = UdpAppCollector('udpapp-' + HOST_NAME + '---')
    
    header = "SOURCE;PACKET_ID;SENDED;RECEIVED;"
    data, addr = sock.recvfrom( 1024 )   # buffer size is 1024 bytes
    local_timestamp = repr( time() )
    source_id, packet_number, server_timestamp = parse_packet(data)
    print source_id + '; ' + packet_number + '; ' + server_timestamp + '; ' + local_timestamp
    dc.collect_first_package(source_id, packet_number, server_timestamp, local_timestamp)
    sock.settimeout(1) #timeout setted to 1s.
    interrupted_flow = False
    current_source_id = source_id
    last_packet_number = long(packet_number)
    last_server_timestamp = float(server_timestamp)
    last_local_timestamp = float(local_timestamp)
    while True:
        try:
            data, addr = sock.recvfrom( 1024 )   # buffer size is 1024 bytes
            local_timestamp = repr( time() )
            source_id, packet_number, server_timestamp = parse_packet(data)
            if interrupted_flow:
                interrupted_flow = False
                print 'Resumed Flow!'
                dc.collect_resumed_flow(source_id, packet_number, server_timestamp, local_timestamp)
            
            print source_id + '; ' + packet_number + '; ' + server_timestamp + '; ' + local_timestamp
            if current_source_id != source_id:
                dc.collect_source_changed(source_id, packet_number, server_timestamp, local_timestamp)
            elif last_packet_number - long(packet_number) > 1:
                dc.collect_package_lost(source_id, str(last_packet_number), 
                                        packet_number, server_timestamp, local_timestamp)
            
            # Save the informations
            current_source_id = source_id<commit_msg>Adicionado os parametros de formatação dos logs<commit_after>#!/usr/bin/env python

"""
    UdpApp python script
        Server send udp packets every NSECONDS
        Client listening the UDP_PORT
        [Copy to your /usr/bin directory for call in shell]

@author: arthurgodoy
"""
import sys
import socket
import json
import uuid
from time import time, sleep
from commum.DataCollector import UdpAppCollector

UDP_PORT=8885 #Port Listening/Sending

NSECONDS=0.030 #Send Period

MULTICAST_IP = "10.0.2.254"
MULTICAST_MAC = "ca:fe:ca:fe:ca:fe"

def parse_packet(packet_data):
    parsed_data = json.loads(packet_data)
    src_uuid = parsed_data['src_uuid']
    packet_number = parsed_data['packet_number']
    timestamp = parsed_data['timestamp']
    return src_uuid, packet_number, timestamp

def udpserver( IPs, PORT ):
    SRC_UUID = str(uuid.uuid4()) #Get a UUID for this server
    PKT_LONG = long(0) #Start a packet counter
    print "SRC UUID:", SRC_UUID
    print "UDP target IP:", IPs
    print "UDP target port:", UDP_PORT
    print "Sending interval:", NSECONDS, " seconds"

    sock = socket.socket( socket.AF_INET,        # Internet
                          socket.SOCK_DGRAM )    # UDP

    #Send each NSECONDS for each client
    while True:
        for UDP_IP in IPs:
            message = {'src_uuid' : SRC_UUID, 'packet_number' : str( PKT_LONG ), 'timestamp' : repr( time() )}
            sock.sendto(json.dumps(message), (UDP_IP, UDP_PORT) )
        
        PKT_LONG += 1
        sleep( NSECONDS )


#Register the multicast ip and mac on the arp table.
#os.system('arp -s ' + MULTICAST_IP + ' ' + MULTICAST_MAC)

#Arguments should be 2 or more
if len( sys.argv ) < 2:
    print " "
    print "Usage ="
    print "      Server:"
    print "              Unicast=    -s CLIENT_IP1 CLIENT_IP2 CLIENT_IPN"
    print "              Multicast=  -m"
    print "      Client:"
    print "              -c HOST_NAME"
    print " "

#Server
elif sys.argv[1] == "-s":
    UDP_CLIENTS=sys.argv[2:] #Get the arguments starting from the third
    udpserver( UDP_CLIENTS, UDP_PORT )
    
#Client
elif sys.argv[1] == "-c":
    HOST_NAME = ""
	FORMAT = 'human'
    if len(sys.argv) >= 3:
        if sys.argv[2] == "--format-csv":
            FORMAT = 'csv'
        elif sys.argv[2] == "--format-human":
            FORMAT = 'human'
        else:
            HOST_NAME = sys.argv[2]
            FORMAT = 'human'
        if len(sys.argv) >= 4:
            HOST_NAME = sys.argv[4]

    UDP_IP = ""
    
    sock = socket.socket( socket.AF_INET,     # Internet
                          socket.SOCK_DGRAM ) # UDP
    sock.bind( (UDP_IP,UDP_PORT) )
    
    uuidstr = HOST_NAME + '---' + str(uuid.uuid4())
    dc = UdpAppCollector(prename = 'udpapp-' + HOST_NAME + '---', format = FORMAT)
    
    #header = "SOURCE;PACKET_ID;SENDED;RECEIVED;"
    data, addr = sock.recvfrom( 1024 )   # buffer size is 1024 bytes
    local_timestamp = repr( time() )
    source_id, packet_number, server_timestamp = parse_packet(data)
    print source_id + '; ' + packet_number + '; ' + server_timestamp + '; ' + local_timestamp
    dc.collect_first_package(source_id, packet_number, server_timestamp, local_timestamp)
    sock.settimeout(1) #timeout setted to 1s.
    interrupted_flow = False
    current_source_id = source_id
    last_packet_number = long(packet_number)
    last_server_timestamp = float(server_timestamp)
    last_local_timestamp = float(local_timestamp)
    while True:
        try:
            data, addr = sock.recvfrom( 1024 )   # buffer size is 1024 bytes
            local_timestamp = repr( time() )
            source_id, packet_number, server_timestamp = parse_packet(data)
            if interrupted_flow:
                interrupted_flow = False
                print 'Resumed Flow!'
                dc.collect_resumed_flow(source_id, packet_number, server_timestamp, local_timestamp)
            
            print source_id + '; ' + packet_number + '; ' + server_timestamp + '; ' + local_timestamp
            if current_source_id != source_id:
                dc.collect_source_changed(source_id, packet_number, server_timestamp, local_timestamp)
            elif last_packet_number - long(packet_number) > 1:
                dc.collect_package_lost(source_id, str(last_packet_number), 
                                        packet_number, server_timestamp, local_timestamp)
            
            # Save the informations
            current_source_id = source_id
{'fraction_non_alphanumeric': 0.09949156459440721, 'fraction_numerical': 0.011324243124566674, 'mean_word_length': 2.4759036144578315, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4117998', 'n_tokens_mistral': 2857, 'n_tokens_neox': 2733, 'n_words': 672}