Esempio n. 1
0
def zmq_import_poll(sock: zmq.Socket, timeout: float):
    """
    Author:
    Alexander Heilmeier

    Description:
    Handles incoming ZMQ messages and allows polling.

    Inputs:
    sock:       ZMQ socket (see below how to create it)
    timeout:    [s] timeout for polling

    How to create a ZMQ socket to import data?
    import zmq
    zmq_context = zmq.Context()
    sock = zmq_context.socket(zmq.PUB)
    sock.connect("tcp://%s:%s" % (ip, port))
    sock.setsockopt_string(zmq.SUBSCRIBE, zmq_topic])
    """

    # ------------------------------------------------------------------------------------------------------------------
    # PREPARATION ----------------------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------------------

    # timeout conversion s -> ms
    timeout *= 1000.0

    # set standard return
    data = None

    # ------------------------------------------------------------------------------------------------------------------
    # CHECK IF BUFFER ALREADY STORES VALUES ----------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------------------

    # non blocking socket to empty buffer
    try:
        while True:
            sock.recv_string(flags=zmq.NOBLOCK)
            data = sock.recv_pyobj(flags=zmq.NOBLOCK)
    except zmq.Again:
        pass

    # ------------------------------------------------------------------------------------------------------------------
    # WAIT FOR A SHORT TIME IF NO DATA WAS STORED IN THE BUFFER --------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------------------

    if data is None:
        # use poll to be able to set a timeout -> will return 1 if an event occurs, otherwise 0
        events = sock.poll(timeout=timeout)

        if events:
            # non blocking socket to empty buffer
            try:
                while True:
                    sock.recv_string(flags=zmq.NOBLOCK)
                    data = sock.recv_pyobj(flags=zmq.NOBLOCK)
            except zmq.Again:
                pass

    return data
def downloadFile(fileName: str, masterSocket: Socket):
    myMessegeSend = {'REQ_TYPE': 'download'}
    myMessegeSend['FILE_NAME'] = fileName
    masterSocket.send_pyobj(myMessegeSend)
    print("i have send a message")
    listMachines = masterSocket.recv_pyobj()
    print(listMachines)

    context = zmq.Context()
    downloadSocketDk = context.socket(zmq.REQ)
    for IP, port in listMachines['DK_INFO']:
        print(IP, port)
        downloadSocketDk.connect("tcp://" + str(IP) + ":%s" % port)
        break
    Message = {"VIDEO_NAME": fileName}
    downloadSocketDk.send_pyobj(Message)
    video = downloadSocketDk.recv_pyobj()
    print(video)
    saveVideo(video, fileName)
def uploadFile(videoPath: str, socketClientMaster: Socket):
    '''
    steps :
    1-request upload 2-recieve information of the data keeper
    3-establish connection 4-send the video
    '''
    myMessegeSend = {'REQ_TYPE': 'upload'}
    socketClientMaster.send_pyobj(myMessegeSend)
    myMessegeRec = socketClientMaster.recv_pyobj()
    if (myMessegeRec['STATUS'] == 'fail'):
        print(
            'no ports are free at the current state please run again after a while'
        )
        exit()
    portNumberUpload = myMessegeRec['PORT_NUMBER']
    ip = myMessegeRec['IP']
    print(myMessegeRec)
    socketClientDataKeeper = establishUploadConnection(portNumberUpload, ip)
    readSendVideo(videoPath, socketClientDataKeeper)
Esempio n. 4
0
def processor(
        src_socket: Socket,
        dst_socket: Socket,
        func:
    GDKC = identity,  # GDKC(cv2.cvtColor, kwargs=dict(code=cv2.COLOR_BGR2GRAY))
) -> None:
    """

    :param src_socket:
    :param dst_socket:
    :param func:
    """
    while True:
        dst_socket.send_pyobj(
            func(src_socket.recv_pyobj()),
            copy=False,
            track=False,
            # flags=zmq.NOBLOCK
        )
def zmq_import(sock: zmq.Socket,
               blocking: bool = False,
               datatype: str = "pyobj",
               use_buffer: bool = False):
    """
    Author:
    Alexander Heilmeier & Tim Stahl

    Description:
    Handles incoming ZMQ messages.

    Inputs:
    sock:       ZMQ socket (see below how to create it)
    blocking:   set if socket should be blocking (i.e. wait until new message is received) or not
    datatype:   string that indicates if it should be received as Python object (pyobj), json (json) or string (str)
    use_buffer: flag to indicate if the buffer should be returned completely (True) or only the latest message (False)

    Hint: Conversion of json objects to their original data type is handled by PyZMQ and therefore must not be done by
    hand.

    How to create a ZMQ socket to import data?
    import zmq
    zmq_context = zmq.Context()
    sock = zmq_context.socket(zmq.PUB)
    sock.connect("tcp://%s:%s" % (ip, port))
    sock.setsockopt_string(zmq.SUBSCRIBE, zmq_topic])
    """

    # ------------------------------------------------------------------------------------------------------------------
    # Specify called receive handle ------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------------------

    if datatype == "pyobj":
        sock_recv_fct = lambda **kwargs: sock.recv_pyobj(**kwargs)
    elif datatype == "json":
        sock_recv_fct = lambda **kwargs: sock.recv_json(**kwargs)
    elif datatype == "str":
        sock_recv_fct = lambda **kwargs: sock.recv_string(**kwargs)
    else:
        raise RuntimeError("Specified datatype is not supported!")

    # ------------------------------------------------------------------------------------------------------------------
    # FUNCTION BODY ----------------------------------------------------------------------------------------------------
    # ------------------------------------------------------------------------------------------------------------------

    data = None
    buffer = []

    # non blocking socket to empty buffer
    try:
        while True:
            sock.recv_string(flags=zmq.NOBLOCK)
            data = sock_recv_fct(flags=zmq.NOBLOCK)

            if use_buffer:
                buffer.append(data)

    except zmq.Again:
        pass

    # if no data was received above and blocking is True, wait for new data
    if blocking and not use_buffer and data is None:
        sock.recv_string()
        data = sock_recv_fct()

    elif blocking and use_buffer and not buffer:
        sock.recv_string()
        data = sock_recv_fct()
        buffer.append(data)

    if use_buffer and buffer:
        return buffer
    elif use_buffer:
        return None
    else:
        return data
Esempio n. 6
0
def recv_pyobj(socket: zmq.Socket, **kw) -> Any:
    """
    Receive python object.
    """
    return socket.recv_pyobj(**kw)