async def worker_handler(
        reader: asyncio.StreamReader,
        writer: asyncio.StreamWriter,
        port: int,
        handle_finished: asyncio.Event,
    ):
        print(SharedData.green(f"[SS{id_:2}][INFO] --- IN HANDLER ---"))
        await tcp_send(p, writer, delimiter, timeout=timeout)
        print(SharedData.green(f"[SS{id_:2}][INFO] Port {port} is open."))

        writer.close()
        await writer.wait_closed()
        handle_finished.set()
def recv_thread(q: Queue, e: threading.Event):
    c_sock.settimeout(TIMEOUT_FACTOR)
    # making a vague assumption of timeout situation.

    while True:

        try:
            data = c_sock.recv(65536)
        except (ConnectionAbortedError, ConnectionResetError):
            break
        except socket.timeout:
            if e.is_set():
                print(SharedData.green(f"[C][RECV] Event set!"))
                break
        else:
            if data == config.END_MARK:
                print(SharedData.green(f"[C][RECV] Received eof <{data}>"))
                break

            q.put(read_b(data))
def send_thread(q: Queue, e: threading.Event):
    while True:

        try:
            n = q.get(timeout=TIMEOUT_FACTOR)
        except Empty:
            if e.is_set():
                print(SharedData.green("[C][SEND] Event set!"))
                break
        else:
            q.task_done()

            try:
                c_sock.send(write_b(n))
            except (ConnectionAbortedError, ConnectionResetError):
                print(SharedData.red("[C][SEND] Connection reset!"))
                break
            except AttributeError:
                print(
                    SharedData.red(
                        f"[C][SEND] Tried to send <{type(n)}> type value {n}.")
                )
def worker(id_: int, send, recv, event: threading.Event):
    q: Queue
    send: Queue
    recv: Queue

    while not event.is_set():
        # announce server that the worker is ready.
        print(f"[CS{id_:2}][Info] Worker {id_:2} READY.")
        send.put(id_)

        try:
            p = recv.get()
        except Empty:
            continue

        recv.task_done()
        print(f"[CS{id_:2}][Info] Worker {id_:2} received {p}.")

        if p > config.PORT_MAX:
            print(SharedData.cyan(f"[CS{id_:2}][Info] Stop Signal received!"))
            break

        print(f"[CS{id_:2}][Info] Connecting Port {p}.")
        child_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        child_sock.settimeout(config.TIMEOUT)

        try:
            child_sock.connect((host, p))

        except socket.timeout:
            print(SharedData.red(f"[CS{id_:2}][Info] Port {p} Timeout."))

        except OSError:
            print(SharedData.red(f"[CS{id_:2}][Warn] Port {p} in use."))

        else:
            print(SharedData.green(f"[CS{id_:2}][Info] Port {p} is open."))

        child_sock.close()
Exemple #5
0
async def worker(id_: int, host, send, recv, event, delimiter, timeout=None):
    q: asyncio.Queue
    send: asyncio.Queue
    recv: asyncio.Queue
    event: asyncio.Event

    try:
        # if one thread crashes, will trigger event and gradually stop all threads.

        while not event.is_set():

            # announce server that the worker is ready.
            print(f"[CS{id_:2}][INFO] Worker {id_:2} READY.")
            await send.put(id_)

            try:
                p = await asyncio.wait_for(recv.get(), timeout=timeout)
                p = int(p)
                recv.task_done()
            except asyncio.TimeoutError:
                print(
                    SharedData.red(
                        f"[CS{id_:2}][WARN] Worker {id_:2} timeout fetching from Queue."
                    ))
                continue

            except ValueError:
                print(
                    SharedData.cyan(
                        f"[CS{id_:2}][INFO] Stop Signal received!"))
                break

            print(f"[CS{id_:2}][INFO] Connecting Port {p}.")
            try:
                child_recv, child_send = await asyncio.wait_for(
                    asyncio.open_connection(host, p), timeout)

            except asyncio.TimeoutError:
                print(
                    SharedData.purple(f"[CS{id_:2}][INFO] Port {p} timeout."))

            except OSError:
                print(
                    SharedData.red(
                        f"[CS{id_:2}][WARN] Port {p} connection refused."))

            else:
                try:
                    print(await tcp_recv(child_recv,
                                         delimiter,
                                         timeout=timeout))

                except asyncio.TimeoutError:
                    print(
                        SharedData.purple(
                            f"[CS{id_:2}][INFO] Port {p} timeout."))

                except asyncio.IncompleteReadError:
                    print(
                        SharedData.red(
                            f"[CS{id_:2}][WARN] Port {p} disconnected!"))

                else:
                    print(f"[CS{id_:2}][INFO] Port {p} open.")
                    print(
                        SharedData.green(
                            f"[CS{id_:2}][INFO] Port {p} is available."))
                finally:
                    child_send.close()
                    await child_send.wait_closed()

    except Exception:
        # trigger event to stop all threads.
        print(SharedData.red(f"[CS{id_:2}][CRIT] Exception Event set!."))
        event.set()
        raise

    print(SharedData.bold(f"[CS{id_:2}][INFO] Task Finished."))