import socket
import struct
import time
import thread
import argparse

NC_PORT = 8000

parser = argparse.ArgumentParser(description='Mininet demo')
parser.add_argument('--client-ip', help='IP of client',
                    type=str, action="store", required=True)

args = parser.parse_args()

CLIENT_IP = args.client_ip
# CLIENT_IP = "192.168.0.105"

len_key = 16
counter = 0
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((CLIENT_IP, NC_PORT))
while True:
    packet, addr = s.recvfrom(1024)
    # print packet
    counter = counter + 1
    base = 0
    chain_id = struct.unpack(">I", packet[base:base + 4])[0]
    base += 4
    exec_id = struct.unpack(">I", packet[base:base + 4])[0]
    base += 4
    function_id = struct.unpack(">I", packet[base:base + 4])[0]
    base += 1
    data = struct.unpack(">I", packet[base:base + 4])[0]
    base += 4
    function_count = struct.unpack("B", packet[base])[0]
    print chain_id, exec_id, data, "function_id", function_id, function_count
    data = []
    for i in range(1, 6):
        data.append(int(struct.unpack("B", packet[i+base])[0]))
    print data