<commit_before>#!/usr/bin/python

import socket
import struct
import sys
import serial
import time
import select
import terminal_colors as tc
import argparse

app_description = "LED Multicast Commander v.0.0 10-1-2017"
timeout_in_seconds = 10
multicast_group = '192.168.3.11'
server_port = 10000
response_wait = 0.1
verbose_mode = False

global s
s = serial.Serial("/dev/ttyS0", 115200)

response_wait = 0.1

parser = argparse.ArgumentParser(description=app_description)

parser.add_argument("-v", "--verbose",           dest="verbose", action='store_true',   help='display verbose info (False)')
parser.add_argument("-i", "--ipaddr",            dest="ipaddr",  default='192.168.3.11', help='multicast group IP address (192.168.3.11)')
parser.add_argument("-p", "--port",    type=int, dest="port",    default=10000,         help='multicast port (10000)')
parser.add_argument("-t", "--timeout", type=int, dest="timeout", default=10,            help='receiving period in seconds (10)')

args = parser.parse_args()
verbose_mode = args.verbose
multicast_group_ip = args.ipaddr
server_port = args.port
timeout_in_seconds = args.timeout
server_address = ('', server_port)

# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def flush_input():
    s.flushInput()

def flush_output():
    s.flushOutput()

def wait_for_ack():
    time.sleep(response_wait)
    while s.inWaiting() <= 0:
        pass
    time.sleep(response_wait)
    while s.inWaiting() > 0:
        print s.read(s.inWaiting()),
    print

def command(cmd_text):
    s.write((cmd_text + ':').encode())
    wait_for_ack()

global watermark
watermark = 0

def handle_command(cmd_text):
    global watermark
    new_watermark = 0

    cmd = ""
    if ";" in cmd_text:
        sequence, cmd = cmd_text.split(";")
        try:
            new_watermark = int(sequence)
            if new_watermark > 0 and new_watermark <= watermark:
                print >>sys.stderr, 'skipping duplicate command'
                return    
            watermark = new_watermark
        except ValueError:
            # if not a number, reset watermark and issue command
            watermark = 0
    else:
       # no sequence number, issue command and reset watermark
        cmd = cmd_text
        watermark = 0
    print >>sys.stderr, 'issuing command...'

    command(":::3:pau:3:pau")
    command("3:cnt:" + cmd)
    flush_output();

multicast_group = multicast_group_ip
server_address = ('', server_port)

print tc.magenta("\n" + app_description + "\n")
if verbose_mode:
    print tc.yellow("verbose mode")
print tc.cyan("multicast group IP: ") + tc.green(multicast_group_ip)
print tc.cyan("server port: ") + tc.green(str(server_port))
print tc.cyan("receiving period: ") + tc.green(str(timeout_in_seconds))

# Receive/respond loop
while True:
    # Create the socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Bind to the server address
    sock.bind(server_address)

    # Tell the operating system to add the socket to the multicast group on all interfaces.
    group = socket.inet_aton(multicast_group)<commit_msg>save version 4 ahead of more changes<commit_after>#!/usr/bin/python

import socket
import struct
import sys
import serial
import time
import select
import terminal_colors as tc
import argparse

app_description = "LED Multicast Commander v.0.0 10-1-2017"
timeout_in_seconds = 10
multicast_group = '192.168.3.11'
server_port = 10000
response_wait = 0.1
verbose_mode = False

global s
s = serial.Serial("/dev/ttyS0", 115200)

response_wait = 0.1

parser = argparse.ArgumentParser(description=app_description)

parser.add_argument("-v", "--verbose",           dest="verbose", action='store_true',   help='display verbose info (False)')
parser.add_argument("-i", "--ipaddr",            dest="ipaddr",  default='192.168.3.11', help='multicast group IP address (192.168.3.11)')
parser.add_argument("-p", "--port",    type=int, dest="port",    default=10000,         help='multicast port (10000)')
parser.add_argument("-t", "--timeout", type=int, dest="timeout", default=10,            help='receiving period in seconds (10)')

args = parser.parse_args()
verbose_mode = args.verbose
multicast_group_ip = args.ipaddr
server_port = args.port
timeout_in_seconds = args.timeout
server_address = ('', server_port)

# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def flush_input():
    s.flushInput()

def flush_output():
    s.flushOutput()

def wait_for_ack():
    time.sleep(response_wait)
    while s.inWaiting() <= 0:
        pass
    time.sleep(response_wait)
    while s.inWaiting() > 0:
        print s.read(s.inWaiting()),
    print

def command(cmd_text):
    s.write((cmd_text + ':').encode())
    wait_for_ack()

global watermark
watermark = 0

def handle_command(cmd_text):
    global watermark
    new_watermark = 0

    cmd = ""
    if ";" in cmd_text:
        sequence, cmd = cmd_text.split(";")
        try:
            new_watermark = int(sequence)
            if new_watermark > 0 and new_watermark <= watermark:
                print >>sys.stderr, 'skipping duplicate command'
                return    
            watermark = new_watermark
        except ValueError:
            # if not a number, reset watermark and issue command
            watermark = 0
    else:
       # no sequence number, issue command and reset watermark
        cmd = cmd_text
        watermark = 0
    print >>sys.stderr, 'issuing command...'

    command(":::3:pau")
    command("3:cnt:" + cmd)
    flush_output();

multicast_group = multicast_group_ip
server_address = ('', server_port)

print tc.magenta("\n" + app_description + "\n")
if verbose_mode:
    print tc.yellow("verbose mode")
print tc.cyan("multicast group IP: ") + tc.green(multicast_group_ip)
print tc.cyan("server port: ") + tc.green(str(server_port))
print tc.cyan("receiving period: ") + tc.green(str(timeout_in_seconds))

# Receive/respond loop
while True:
    # Create the socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    # Bind to the server address
    sock.bind(server_address)

    # Tell the operating system to add the socket to the multicast group on all interfaces.
    group = socket.inet_aton(multicast_group)
{'fraction_non_alphanumeric': 0.12205670350792888, 'fraction_numerical': 0.024987986544930323, 'mean_word_length': 3.3756131744919413, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4474984', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2012, 'n_words': 503}