Example #1
0
def test_key_is_not_none_when_no_value_is_given():
    # Given

    # When
    udp_socket = UDPSocket(socket_port=50013).start()

    # Then
    assert udp_socket.get_key() is not None
    udp_socket.stop()
    time.sleep(.5)
Example #2
0
def test_new_udp_socket_correctly_setup_an_empty_queue():
    # Given

    # When
    udp_socket = UDPSocket(socket_port=50005).start()

    # Then
    assert collections.Counter(udp_socket.queue) == collections.Counter([])
    udp_socket.stop()
    time.sleep(.5)
Example #3
0
def test_new_udp_socket_correctly_set_socket_ip():
    # Given
    socket_ip = "127.0.0.1"

    # When
    udp_socket = UDPSocket(socket_ip=socket_ip, socket_port=50002).start()

    # Then
    assert udp_socket.socket_ip == socket_ip
    udp_socket.stop()
    time.sleep(.5)
Example #4
0
def test_new_udp_socket_correctly_set_socket_port():
    # Given
    socket_port = 50003

    # When
    udp_socket = UDPSocket(socket_port=socket_port).start()

    # Then
    assert udp_socket.socket_port == socket_port
    udp_socket.stop()
    time.sleep(.5)
Example #5
0
def test_new_udp_socket_correctly_set_buffer_size():
    # Given
    buffer_size = 1024

    # When
    udp_socket = UDPSocket(buffer_size=buffer_size, socket_port=50001).start()

    # Then
    assert udp_socket.buffer_size == buffer_size
    udp_socket.stop()
    time.sleep(.5)
Example #6
0
def test_new_udp_socket_correctly_set_encryption_key():
    # Given
    key = generate_key_32()

    # When
    udp_socket = UDPSocket(key=key, socket_port=50012).start()

    # Then
    assert udp_socket.get_key() == key
    udp_socket.stop()
    time.sleep(.5)
Example #7
0
def test_new_udp_socket_correctly_set_max_queue_size():
    # Given
    max_queue_size = 1000

    # When
    udp_socket = UDPSocket(max_queue_size=max_queue_size,
                           socket_port=50004).start()

    # Then
    assert udp_socket.max_queue_size == max_queue_size
    udp_socket.stop()
    time.sleep(.5)
Example #8
0
def test_new_socket_creates_a_fernet_encoder():
    # Given
    key = generate_key_32()

    # When
    udp_socket = UDPSocket(key=key, socket_port=50014).start()

    # Then
    assert udp_socket.encoder is not None
    assert type(udp_socket.encoder) is ChaCha20Poly1305
    udp_socket.stop()
    time.sleep(.5)
Example #9
0
def test_new_udp_socket_correctly_setup_a_socket_using_udp_protocol():
    # Given

    # When
    udp_socket = UDPSocket(socket_port=50006).start()

    # Then
    assert udp_socket.socket.type == socket.SocketKind.SOCK_DGRAM
    assert udp_socket.socket.family == socket.AddressFamily.AF_INET
    assert udp_socket.socket.proto == socket.IPPROTO_UDP
    udp_socket.stop()
    time.sleep(.5)
Example #10
0
 def _setup(self) -> NoReturn:
     """Initialization of the process."""
     must_listen = self.role == VideoStream.CONSUMER
     self.udp_socket = UDPSocket(socket_ip=self.socket_ip, socket_port=self.socket_port,
                                 encryption_in_transit=self.encryption_in_transit,
                                 max_queue_size=self.max_queue_size,
                                 buffer_size=self.buffer_size, key=self.key, enable_multicast=self.enable_multicast,
                                 multicast_ttl=self.multicast_ttl, must_listen=must_listen)
     self.udp_socket.start()
     self.eye = None if self.from_source is None else Eye(src=self.from_source, run_new_process=False).start()
     self.im = self.im.start()
     self.is_running = True
Example #11
0
def test_new_udp_socket_correctly_set_multicast_ttl():
    # Given
    multicast_ttl = 3

    # When
    udp_socket = UDPSocket(multicast_ttl=multicast_ttl,
                           socket_port=50021).start()

    # Then
    assert udp_socket.multicast_ttl is multicast_ttl
    udp_socket.stop()
    time.sleep(.5)
Example #12
0
def test_new_udp_socket_correctly_set_enable_multicast():
    # Given
    enable_multicast = True

    # When
    udp_socket = UDPSocket(enable_multicast=enable_multicast,
                           socket_port=50020).start()

    # Then
    assert udp_socket.enable_multicast is enable_multicast
    udp_socket.stop()
    time.sleep(.5)
Example #13
0
def test_udp_socket_can_start_and_stop_its_thread():
    # Given
    socket_ip = "127.0.0.1"
    socket_port = 50007

    # When
    udp_socket = UDPSocket(socket_ip=socket_ip,
                           socket_port=socket_port).start()
    udp_socket.stop()
    time.sleep(.5)

    # Then
    assert udp_socket.is_running is False
Example #14
0
def test_new_udp_socket_correctly_set_encryption_in_transit():
    # Given
    encryption_in_transit = False

    # When
    udp_socket = UDPSocket(encryption_in_transit=encryption_in_transit,
                           socket_port=50000).start()

    # Then
    assert udp_socket.encryption_in_transit is encryption_in_transit

    udp_socket.stop()
    time.sleep(.5)
Example #15
0
def test_udp_socket_send_encrypted_messages_when_encryption_in_transit_set_to_true(
):
    # Given
    key = generate_key_32()
    msg = b"tests"
    socket_ip = "127.0.0.1"
    socket_port = 50017
    test_socket_port = 50018
    n_msg = 2
    test_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
                                socket.IPPROTO_UDP)
    test_socket.bind((socket_ip, test_socket_port))
    test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    udp_socket = UDPSocket(key=key,
                           socket_ip=socket_ip,
                           socket_port=socket_port,
                           max_queue_size=n_msg,
                           encryption_in_transit=True).start()

    # When
    udp_socket.sendto(msg, (socket_ip, test_socket_port))
    rcv_msg = test_socket.recv(100)
    time.sleep(.1)

    udp_socket.stop()
    test_socket.close()
    time.sleep(.5)

    # Then
    assert rcv_msg != msg
    assert msg == udp_socket._decrypt(rcv_msg)
Example #16
0
def test_socket_can_receive_message_while_running():
    # Given
    socket_ip = "127.0.0.1"
    socket_port = 50008
    msg = str.encode("tests", "utf8")

    # When
    udp_socket = UDPSocket(socket_ip=socket_ip,
                           socket_port=socket_port).start()
    udp_socket.socket.sendto(msg, (socket_ip, socket_port))
    time.sleep(.1)

    # Then
    assert len(udp_socket.queue) == 1
    assert udp_socket.queue[0][1] == (socket_ip, socket_port)
    assert udp_socket.queue[0][0] == msg
    udp_socket.stop()
    time.sleep(.5)
Example #17
0
def test_change_key_correctly_change_the_key():
    # Given
    key_old = generate_key_32()
    key_new = generate_key_32()

    # When
    udp_socket = UDPSocket(key=key_old, socket_port=50015).start()
    udp_socket.change_key(key_new)

    # Then
    assert udp_socket.get_key() == key_new
    udp_socket.stop()
    time.sleep(.5)
Example #18
0
def test_change_key_correctly_change_the_fernet_encoder():
    # Given
    key_old = generate_key_32()
    key_new = generate_key_32()
    msg = b"tests"

    # When
    udp_socket = UDPSocket(key=key_old, socket_port=50016).start()
    msg_crypt = udp_socket._encrypt(msg)
    msg_decrypt_old = udp_socket._decrypt(msg_crypt)
    udp_socket.change_key(key_new)

    # Then
    assert msg_decrypt_old == msg

    udp_socket.stop()
    time.sleep(.5)
Example #19
0
def test_pull_allow_to_get_first_message_in_the_queue():
    # Given
    socket_ip = "127.0.0.1"
    socket_port = 50011
    n_msg = 2

    # When
    udp_socket = UDPSocket(socket_ip=socket_ip,
                           socket_port=socket_port,
                           max_queue_size=n_msg).start()
    udp_socket.sendto(bytes([0]), (socket_ip, socket_port))
    udp_socket.sendto(bytes([1]), (socket_ip, socket_port))
    time.sleep(.1)
    result = udp_socket.pull()

    # Then
    assert result[0] == bytes([0])
    assert len(udp_socket.queue) == 1
    udp_socket.stop()
    time.sleep(.5)
Example #20
0
def test_udp_socket_can_read_unencrypted_messages_when_encryption_in_transit_set_to_true(
):
    # Given
    msg = b"tests"
    socket_ip = "127.0.0.1"
    socket_port = 50022
    n_msg = 2
    udp_socket = UDPSocket(socket_ip=socket_ip,
                           socket_port=socket_port,
                           max_queue_size=n_msg,
                           encryption_in_transit=True).start()

    # When
    udp_socket.sendto(msg, (socket_ip, socket_port), skip_encryption=True)
    time.sleep(.1)

    udp_socket.stop()
    time.sleep(.5)

    # Then
    assert udp_socket.pull()[0] == msg
Example #21
0
def test_socket_can_received_multiple_message_while_running():
    # Given
    socket_ip = "127.0.0.1"
    socket_port = 50010
    msg = str.encode("tests", "utf8")
    n_msg = 10

    # When
    udp_socket = UDPSocket(socket_ip=socket_ip,
                           socket_port=socket_port,
                           max_queue_size=n_msg).start()
    for i in range(n_msg):
        udp_socket.sendto(msg, (socket_ip, socket_port))
    time.sleep(.1)
    udp_socket.stop()
    time.sleep(.5)

    # Then
    assert len(udp_socket.queue) == n_msg
Example #22
0
class VideoStream:
    """A class to manage video stream.

    This class inherit from Process to run the VideoStream on a different CPU core than parent process.

        Constants :
            EMITTER : Value that tell the VideoStream will send video stream.
            CONSUMER : Value that tell the VideoStream will receive video stream.

        Attributes :
            internal_pipe : Internal side of the pipe used for communication with the process.
            external_pipe : External side of the pipe used for communication with the process.
            im : The ImageManager used for video stream.
            role : Tell if the VideoStream is emitter or consumer.
            opened_topics : A list of VideoTopic waiting for completion.
            udp_socket : The UDPSocket used for sending or receiving data.
            socket_ip : The ip used to bind the socket.
            socket_port : The port used to bind the socket.
            encryption_in_transit : Define if the messages must be encrypted.
            max_queue_size : The max size of message queue.
            buffer_size : The max size of the received message buffer.
            is_running : Tell if the process is running.
            key : The encryption key used to encrypt message. If no value is provided it will generate a new one.
            enable_multicast : Specify if the socket can use multicast.
            multicast_ttl : The TTL used for multicast.
            subs_list : A list of tuples containing ip address and port of subscribers.
            use_rcv_img_buffer : A bool that tell if received image are stored in a buffer or in a single variable.
            rcv_img_buffer : A buffer to store incoming image.
            from_source : Specify the source to use if needed.
            eye : The Eye object used to stream if from_source is not None.
            run_new_process : Specify if the Eye object must be run in a new process.
            async_msg_generation: Specify if the messages representing the image must be generated asynchronously.
            encoding: Define the encoding used to send images.
            encoding_param : Parameters used to encode image. See cv2.imencode for more details.
    """
    EMITTER = "emitter"
    CONSUMER = "consumer"

    def __init__(self, role: Optional[str] = EMITTER, max_packet_size: Optional[int] = 60000,
                 socket_ip: Optional[str] = "127.0.0.1",
                 socket_port: Optional[int] = 50000, encryption_in_transit: Optional[bool] = False,
                 max_queue_size: Optional[int] = 100, buffer_size: Optional[int] = 65543,
                 key: Optional[Union[None, bytes]] = None, enable_multicast: Optional[bool] = False,
                 multicast_ttl: Optional[int] = 2, use_rcv_img_buffer: Optional[bool] = False,
                 from_source: Optional[Union[int, str]] = None, run_new_process: Optional[bool] = True,
                 async_msg_generation: Optional[bool] = False, encoding: Optional[int] = 0,
                 encoding_param: Optional[Union[dict, None]] = None):
        """Create a new VideoStream object with given parameter.

        :param role: Tell if the VideoStream is emitter or consumer.
        :param max_packet_size: The max size of a packet (in byte).
        :param socket_ip: The ip used to bind the socket.
        :param socket_port: The port used to bind the socket.
        :param encryption_in_transit: Define if the messages must be encrypted.
        :param max_queue_size: The max size of message queue.
        :param buffer_size: The max size of the received message buffer.
        :param key: The encryption key used to encrypt message. If no value is provided it will generate a new one.
        :param enable_multicast: Specify if the socket can use multicast.
        :param multicast_ttl: A list of tuples containing ip address and port of subscribers.
        :param use_rcv_img_buffer: A bool that tell if received image are stored in a buffer or in a single variable.
        :param from_source: Make the VideoStream stream from a source.
        :param run_new_process: Specify if the Eye object must be run in a new process.
        :param async_msg_generation: Specify if the messages representing the image must be generated asynchronously.
        :param encoding: Define the encoding used to send images.
        :param encoding_param: Parameters used to encode image. See cv2.imencode for more details.
        """
        self.internal_pipe, self.external_pipe = mp.Pipe()
        if role != VideoStream.EMITTER and role != VideoStream.CONSUMER:
            raise ValueError
        self.role = role
        self.im: ImageManager = ImageManager(max_packet_size=max_packet_size, async_msg_generation=async_msg_generation,
                                             encoding=encoding, encoding_param=encoding_param)
        self.opened_topics: List[VideoTopic] = []
        self.udp_socket: Union[UDPSocket, None] = None
        self.socket_ip = socket_ip
        self.socket_port = socket_port
        self.encryption_in_transit: bool = encryption_in_transit
        self.max_queue_size: int = max_queue_size
        self.buffer_size: int = buffer_size
        self.is_running: bool = False
        self.enable_multicast: bool = enable_multicast
        self.multicast_ttl: int = multicast_ttl
        if self.enable_multicast:
            raise NotImplementedError
        self.key: bytes = key
        self.subs_list: List[Tuple[str, int]] = []
        self.tm = TopicManager()
        self.use_rcv_img_buffer = use_rcv_img_buffer
        self.rcv_img_buffer: List[np.array] = []
        if use_rcv_img_buffer is False:
            self.rcv_img_buffer.append(None)
        self.from_source = from_source
        self.eye: Union[None, Eye] = None
        self.run_new_process = run_new_process
        self.async_msg_generation = async_msg_generation
        self.encoding = encoding
        self.encoding_param = encoding_param if encoding_param is not None else {}

    def start(self) -> NoReturn:
        """Start a new thread or a new process for asynchronous camera reading.

        :return eye: The current instance of the class.
        """
        if self.run_new_process is False:
            self._start()
        else:
            mp.Process(target=self._start).start()
        return self

    def _start(self) -> NoReturn:
        """Start the thread of the class."""
        Thread(target=self._work, args=()).start()

    def _refresh_image(self, new_image: np.array) -> NoReturn:
        """Change the value of current image by the value of new_image.

        :param new_image: The new image to send.
        """
        self.im.refresh_image(new_image)

    def refresh_image(self, new_image: np.array) -> NoReturn:
        """External call to _refresh_image.

        :param new_image: The new image to send.
        """
        if self.run_new_process is False:
            return self._refresh_image(new_image)
        self.external_pipe.send((VideoStream._refresh_image, {"new_image": new_image}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _get_current_image(self) -> np.array:
        """Return the current value of current image.

        :return current_image: The current value of current image.
        """
        return self.im.current_image

    def get_current_image(self) -> np.array:
        """External call to _get_current_image

        :return current_image: The current value of current image.
        """
        if self.run_new_process is False:
            return self._get_current_image()
        self.external_pipe.send((VideoStream._get_current_image, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _work(self) -> NoReturn:
        """The main process of the VideoStream."""
        self._setup()
        self._loop()

    def _setup(self) -> NoReturn:
        """Initialization of the process."""
        must_listen = self.role == VideoStream.CONSUMER
        self.udp_socket = UDPSocket(socket_ip=self.socket_ip, socket_port=self.socket_port,
                                    encryption_in_transit=self.encryption_in_transit,
                                    max_queue_size=self.max_queue_size,
                                    buffer_size=self.buffer_size, key=self.key, enable_multicast=self.enable_multicast,
                                    multicast_ttl=self.multicast_ttl, must_listen=must_listen)
        self.udp_socket.start()
        self.eye = None if self.from_source is None else Eye(src=self.from_source, run_new_process=False).start()
        self.im = self.im.start()
        self.is_running = True

    def _loop(self) -> NoReturn:
        """The main loop of the process."""
        max_topic = 2 ** (8 * UDPMessage.TOPIC_LENGTH)
        img_topic = 0
        while self.is_running:
            # Manage external call of class method when using Process class.
            if self.run_new_process and self.internal_pipe.poll():
                command = self.internal_pipe.recv()
                if type(command) is tuple:
                    self.internal_pipe.send(command[0](self, **command[1]))
            # Send image packets if the VideoStream object is emitter.
            if self.role == VideoStream.EMITTER:
                if self.eye is not None:
                    self.im.refresh_image(self.eye.read())
                self.cast(img_topic)
                img_topic = (img_topic + 1) % max_topic
                if self.run_new_process:
                    VideoStream.delay(1)

            # Receive packets if the VideoStream object is consumer.
            if self.role == VideoStream.CONSUMER:
                while self.udp_socket.in_waiting():
                    msg = UDPMessage.from_bytes(self.udp_socket.pull()[0])
                    if type(msg) is not UDPMessage:
                        continue
                    self.tm.add_message(msg)
                if self.tm.in_waiting():
                    if self.use_rcv_img_buffer:
                        self.rcv_img_buffer.append(self.tm.pull())
                    else:
                        self.rcv_img_buffer[0] = self.tm.pull()

    def _stop(self) -> NoReturn:
        """Stop the process and its UDPSocket."""
        self.is_running = False
        self.udp_socket.stop()
        if self.im.async_msg_generation is True:
            self.im.stop()
        if self.eye is not None:
            self.eye.stop()

    def stop(self) -> NoReturn:
        """External call to _stop"""
        if self.run_new_process is False:
            return self._stop()
        self.external_pipe.send((VideoStream._stop, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _get_is_running(self) -> bool:
        """Return True if the process is currently running.

        :return is_running: A bool that tell if the process is currently running.
        """
        return self.is_running

    def get_is_running(self):
        """External call to _get_is_running.

        :return is_running: A bool that tell if the process is currently running.
        """
        if self.run_new_process is False:
            return self._get_is_running()
        self.external_pipe.send((VideoStream._get_is_running, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _add_subscriber(self, address_port) -> NoReturn:
        """Add a subscriber in the list of subscriber.

        :param address_port: A tuple containing the ip address and the port of the new subscriber.
        """
        self.subs_list.append(address_port)

    def add_subscriber(self, address_port) -> NoReturn:
        """External call to _add_subscriber.

        :param address_port: A tuple containing the ip address and the port of the new subscriber.
        """
        if self.run_new_process is False:
            return self._add_subscriber(address_port)
        self.external_pipe.send((VideoStream._add_subscriber, {"address_port": address_port}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _get_subs_list(self) -> List[Tuple]:
        """Return the list of subscribers.

        :return subs_list: The list of subscribers.
        """
        return self.subs_list

    def get_subs_list(self) -> List[Tuple]:
        """External call to _get_subs_list.

        :return subs_list: The list of subscribers.
        """
        if self.run_new_process is False:
            return self._get_subs_list()
        self.external_pipe.send((VideoStream._get_subs_list, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _remove_subscriber(self, index: int) -> NoReturn:
        """Remove a subscriber from the list of subscriber.

        :param index: The index of the subscriber to remove.
        """
        self.subs_list.pop(index)

    def remove_subscriber(self, index: int) -> NoReturn:
        """External call to _remove_subscriber.

        :param index: The index of the subscriber to remove.
        """
        if self.run_new_process is False:
            return self._remove_subscriber(index)
        self.external_pipe.send((VideoStream._remove_subscriber, {"index": index}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def cast(self, topic: int) -> NoReturn:
        """Send the current image using given topic number.

        :param topic: The number of the topic used to send the image.
        """
        if np.array_equiv(self.im.current_image, np.array([])) or len(self.subs_list) == 0:
            return
        for msg_to_send in self.im.get_messages(topic):
            for sub in self.subs_list:
                self.udp_socket.sendto(msg_to_send, sub)
            if self.run_new_process:
                VideoStream.delay(1)

    def _get_rcv_img(self) -> np.array:
        """Return the received image.

        :return rcv_img: The received image.
        """
        if len(self.rcv_img_buffer) == 0:
            return None
        if self.use_rcv_img_buffer is False:
            return self.rcv_img_buffer[0]
        return self.rcv_img_buffer.pop(0)

    def get_rcv_img(self):
        """External call to _get_rcv_img.

        :return rcv_img: The received image.
        """
        if self.run_new_process is False:
            return self._get_rcv_img()
        self.external_pipe.send((VideoStream._get_rcv_img, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    def _get_key(self) -> bytes:
        """Return the key used by the socket for encryption.

        :return: The encryption key of the server.
        """
        return self.udp_socket.get_key()

    def get_key(self) -> bytes:
        """External call to _get_key.

        :return: The encryption key of the server.
        """
        if self.run_new_process is False:
            return self._get_key()
        self.external_pipe.send((VideoStream._get_key, {}))
        while self.external_pipe.poll() is False:
            pass
        return self.external_pipe.recv()

    @staticmethod
    def delay(delay_ms: int) -> NoReturn:
        """Wait for delay_ms microseconds.

        :param delay_ms: The delay duration in ms
        """
        t_stop = np.int64(delay_ms * 10) + np.int64(np.float64(time.time()) * np.float64(10000000))
        while np.int64(np.float64(time.time()) * np.float64(10000000)) <= t_stop:
            pass