def send_to_client(client_name, message):
    for client_socket in sockets_list:
        if client_socket != server_socket:  # if the socket is not the server socket
            # send left message
            if clients[client_socket]['data'] == client_name:
                c.print_debug(f"Sending message to {client_name} client: Time: {message}")
                sf.send_message(client_socket, message, c.SERVER)
Esempio n. 2
0
def record(r_led, g_led, event_manager, processing_complete, processed_frames):
    print('recording')
    g_led.Update(100, force=True)
    r_led.Update(1, force=True)
    processing_complete.wait()
    processing_complete.clear()
    event_manager.update(recording)

    errors = 0
    while True:  # wait here until all frames have been processed and sent to server
        try:
            # get the processed frames from queue
            n_frame, ball_candidates = processed_frames.get_nowait()
            # print(f"rec: {n_frame}")
            message = sf.MyMessage(c.TYPE_BALLS, (n_frame, ball_candidates))
            if not sf.send_message(client.client_socket, message, c.CLIENT):
                errors += 1
                print(f"error: {errors}")

        # if processing complete and no more data to send to server
        except queue.Empty:
            if processing_complete.is_set() and (processed_frames.qsize()
                                                 == 0):
                event_manager.clear()
                break
    # if there were no transmission errors send True, else send False
    if errors == 0:
        message = sf.MyMessage(c.TYPE_DONE, True)
    else:
        message = sf.MyMessage(c.TYPE_DONE, False)
    sf.send_message(client.client_socket, message, c.CLIENT)
    return
    def connect_to_server(self):
        '''
        Attempt to connect to the server, if connection refused, wait 1s then try again

        Return: True once connected
        '''
        while True:
            try:
                print(f"Connecting to server on {c.SERVER_IP}:{c.PORT}...")
                self.socket.connect((c.SERVER_IP, c.PORT))  # connect to server
                print(
                    f"Connection Established to server on {c.SERVER_IP}:{c.PORT}"
                )
                sf.send_message(self.socket, self.name,
                                c.CLIENT)  # send name to server
                return True

            except (socket.timeout, TimeoutError) as e:
                print(
                    "Connection could not be established to server, trying again..."
                )
                continue

            except socket.error as e:
                if e.errno != errno.EALREADY:
                    raise e

            except Exception as e:
                print('Error', str(e))
Esempio n. 4
0
def connect_to_server(name):
    # attempt to connect to the server, if connection refused, wait 1s then try again
    while True:
        try:
            client_socket.connect((c.IP, c.PORT))  # connect to server
            print(f"Connection Established to server on {c.IP}:{c.PORT}")
            client_socket.setblocking(True)
            sf.send_message(client_socket, name,
                            c.CLIENT)  # send name to server
            time.sleep(1)
        except socket.error as e:
            print(
                "Connection could not be established to server, trying again..."
            )
            time.sleep(1)
            continue
        except socket.timeout as e:
            print(
                "Connection could not be established to server, trying again..."
            )
            time.sleep(1)
            continue
        except Exception as e:
            print('Error', str(e))
            raise sf.CommError("Communication Error") from e
        break
    def send_to_server(self, message_type, message_data):
        '''
        Send message to the server given the message type and data

        Return True if sent successfully
        '''
        message = sf.MyMessage(message_type, message_data)
        sf.send_message(self.socket, message, c.CLIENT)
        return True
Esempio n. 6
0
def stream(r_led, g_led, event_manager, processing_complete, processed_frames):
    print('recording stream')
    message_list = []
    g_led.Update(1, force=True)
    r_led.Update(1, force=True)
    processing_complete.wait()
    processing_complete.clear()
    event_manager.update(record_stream)
    errors = 0

    while True:
        try:
            # get the frames from queue
            n_frame, frame_buf = processed_frames.get_nowait()

            if event_manager.record_stream.is_set():
                print(f"calib: {n_frame}")
                # y_data is a numpy array hxw with 8 bit greyscale brightness values
                y_data = np.frombuffer(frame_buf,
                                       dtype=np.float32,
                                       count=w * h).reshape(
                                           (h, w)).astype(np.uint8)
                message = sf.MyMessage(c.TYPE_IMG, (n_frame, y_data))
                if not sf.send_message(client.client_socket, message,
                                       c.CLIENT):
                    errors += 1
                    print(f"error: {errors}")

                message_list.extend(client.read_all_server_messages())
                for message in message_list:
                    if message['data'].type == c.TYPE_DONE:
                        event_manager.clear()

        except queue.Empty:
            if not event_manager.record_stream.is_set(
            ) and processed_frames.qsize(
            ) == 0:  # if the recording has finished
                print('stream done')
                processing_complete.set()
                break

    # if there were no transmission errors send True, else send False
    if errors == 0:
        message = sf.MyMessage(c.TYPE_DONE, True)
    else:
        message = sf.MyMessage(c.TYPE_DONE, False)

    sf.send_message(client.client_socket, message, c.CLIENT)
    def send_to_client(self, client_name, message):
        '''
        Send message to the client specified by client_name

        Return: True if sent, False if error
        '''
        try:
            for client_socket in self.sockets_list:
                if client_socket != self.socket:  # if the socket is not the server socket
                    # send left message
                    if self.clients[client_socket]['data'] == client_name:
                        c.print_debug(
                            f"Sending message to {client_name} client: Time: {message}"
                        )
                        sf.send_message(client_socket, message, c.SERVER)
            return True

        except sf.CommError as e:
            raise sf.CommError(e)
            return False
Esempio n. 8
0

client_socket = socket.socket(socket.AF_INET,
                              socket.SOCK_STREAM)  # create IPV4 socket for
client_socket.settimeout(c.SOCKET_TIMEOUT)

if __name__ == "__main__":
    connect_to_server()
    counter = 0
    while True:
        time.sleep(1 / 100)

        # ----   send messages to the server     ---- #
        message = f"[{counter}] Time:{datetime.now()}"
        c.print_debug(f"Sending message to Server: {message}")
        if not sf.send_message(client_socket, message, c.CLIENT):
            sys.exit()  # there was a problem sending the message

        message_list = read_all_server_messages()
        for message in message_list:
            try:
                if message['data'].type == c.TYPE_REC:
                    print('record')
                    print(message['data'].message)
                elif message['data'].type == c.TYPE_CAP:
                    print('capture')
                    print(message['data'].message)
            except:
                print('unrecognised message format')

        counter += 1