import struct
from pathlib import Path

import zmq
import time
import sys


def exit():
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REQ)
    sock.connect('tcp://11.6.1.66:5556')
    # sock.send(b'file' + Path('/home/lambda/source/scarlet/flandre/config/64-1.txt').read_bytes())
    sock.send(b'exit')


def cmd(c: str):
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REQ)
    sock.connect('tcp://11.6.1.66:5556')
    sock.send(c.encode())
    print(sock.recv().decode())


def file():
    ctx = zmq.Context()
    sock = ctx.socket(zmq.REQ)
    sock.connect('tcp://11.6.1.66:5556')
    sock.send(b'file' + Path('/home/lambda/source/scarlet/flandre/config/64-1.txt').read_bytes())


def test():
    ctx = zmq.Context()
    sock = ctx.socket(zmq.PULL)
    sock.connect('tcp://11.6.1.66:5555')
    while True:
        s = sock.recv()
        print(struct.unpack_from('=iqi', s))
        print(s[4 + 8 + 4:].__len__())
        break


if __name__ == '__main__':
    if len(sys.argv) < 2:
        test()
    else:
        globals()[sys.argv[1]](*sys.argv[2:])
