예제 #1
0
"""
SWBS unit test, client script.
"""

import swbs

client = swbs.Client("127.0.0.1", 42069, b"AoAoAoAoAoAoAoAo")
client.connect()
client.send(b"Hello world.")
print(client.receive())
client.disconnect()
예제 #2
0
"""
SWBS unit test, client script.
"""

import swbs

client = swbs.Client(
    "127.0.0.1", 42069,
    b"DOP!DOP!DOP!DOP!")  # same as other client script, different key
client.connect()
while True:
    client.send(b"Midnight dream."
                )  # different message to test if multi-client relaying works
    print(client.receive())
예제 #3
0
    def network_init(self,
                     host: str = "arbiter.local",
                     port: int = 999,
                     key: Union[str, bytes, None] = None,
                     key_is_path: bool = False) -> None:
        """
        Built-in network initialization, when invoked, tries to connect as a \
            client to specified host controller.

        If one is not specified, tries arbiter.local as hostname.
        If connection fails, initializes self temporarily into a Host \
            instance, waiting for a controller or a plain client pointing the \
                agent to a controller.

        Client/controller should send b"CONTROLLER" or \
            b"POINT <HOSTNAME HERE>" respectively. If agent receives neither, \
                restarts socket. If signaled to be controller, re-initializes \
                    as client to controller with specified or default port.

        If signaled to another host, connects to supplied host with specified \
            or default port.

        Operations are done on port 999 (connecting to and listening on) \
            unless specified otherwise in parameters. AES is disabled by \
                default, unless the key value is specified otherwise. \
                    Is blocking if entering Host state.

        :param host: expected controller hostname, default arbiter.local
        :type host: str
        :param port: port connecting to in client mode, and listening on in
            host
        :type port: int
        :param key: if key_is_path is False, key string, otherwise path to key
            file, default False disabling AES
        :type key: Union[str, bytes, None]
        :param key_is_path: if True, key parameter is treated as path to key
            file for reading from, default False
        :type key_is_path: bool
        """
        try:
            self.network = swbs.Client(host, port, key, key_is_path)
            self.network.connect()
            return None
        except socket.error:
            self.network.close()
            self.network = swbs.Host(port, key, key_is_path=key_is_path)
            while True:
                self.network.listen()
                self.network.send("KINETIC WAITING FOR CONTROLLER")
                signal = self.network.receive()
                controller = None  # placeholder for scope
                if signal == "CONTROLLER":
                    controller = self.network.client_address[0]
                elif signal[:5] == "POINT":
                    # this prevents a dictionary switch statement,
                    # find a rewrite
                    controller = signal.split(" ")[1]
                if controller is not None:
                    self.network.disconnect()
                    self.network = swbs.Client(self.network.client_address[0],
                                               port, key, key_is_path)
                    self.network.connect()
                    return None
                else:
                    self.network.restart()
예제 #4
0
# Another example, with AES disabled
import swbs

client = swbs.Client("127.0.0.1", 42069, b"SilentDayWeDream")
client.connect()
client.send(b"Hello world.", no_encrypt=True)
print(client.receive())
client.disconnect()
예제 #5
0
        def broadcast_stream(self,
                             host: str,
                             port: int,
                             key: Union[str, bytes, None],
                             key_is_path: bool = False,
                             restart_delay: Union[int, None] = 1,
                             debug: bool = False) -> None:
            """
            Create swbs.Client instance, with host, port, and key parameters \
                pointing to a swbs.Host or swbs.Server instance, or other \
                    compatible swbs.Instance derivative.

            Is a blocking function.

            When using SWBS to receive bytes from TX, set parameter \
                return_bytes to True.

            TODO more compression

            Decode the image on the receiving end with,

            cv2.imdecode(numpy.frombuffer(host.receive(500000), numpy.uint8), \
                cv2.IMREAD_COLOR)

            If self.stream is None, returns None before execution starts.
            If ComponentError is raised when collecting VideoStream image, \
                restarts camera and then waits 1 second, unless the delay is \
                    specified otherwise, before resuming.

            :param host: hostname of host to connect to
            :type host: str
            :param port: port that host is listening on
            :type port: int
            :param key: AES encryption key, if None, AES is disabled
            :type key: Union[str, bytes, None]
            :param key_is_path: if True key parameter is treated as path to
                file containing encryption key, default False
            :type key_is_path: bool
            :param restart_delay: delay in seconds after a video stream
                restart due to an exception being raised, if None delay is
                disabled, default 1
            :type restart_delay: Union[int, None]
            :param debug: print MD5 hash of image frame to stdout if True,
                also show raw image with cv2.imshow, default False
            :type debug: bool
            """
            if self.stream is None:
                return None
            streamer: swbs.Client = swbs.Client(host, port, key, key_is_path)
            streamer.connect()
            while True:
                try:
                    frame = self.collect_stream(debug).tobytes()
                    if debug is True:
                        # yes, this fetches the MD5 class from swbs,
                        # imported from Pycryptodomex, no I feel no shame
                        print(swbs.MD5.new(frame).hexdigest())
                    streamer.send(frame)
                except ComponentError:
                    Sensors.USBCamera.stop_stream(self)
                    Sensors.USBCamera.start_stream(self)
                    if restart_delay is not None:
                        sleep(restart_delay)
예제 #6
0
"""
unit test for swbs.Client to swbs.Server
just connects, no other activity
"""

import swbs
import time

client = swbs.Client("localhost", 42069, b"DOP!DOP!DOP!DOP!")
client.connect()
time.sleep(5)
client.disconnect()
예제 #7
0
# Example from before, with AES disabled
import swbs

client = swbs.Client("127.0.0.1", 42069, None)
client.connect()
client.send(b"Hello world.")
print(client.receive())
client.disconnect()