Ejemplo n.º 1
0
async def hello_process(
    server: "CPIAServer", message: Message, planet: Optional[str] = None
) -> Message:
    """Run the process hello command.

    This command creates a process the first time it's run.
    """
    if planet is None:
        planet = "Jupiter"

    if "hello_process" not in server.store:
        server.store["hello_process"] = create_process(server, create_state)

    recv, send = server.store["hello_process"]

    await send(planet)

    try:
        old_planet, new_planet = await recv()
    except ReceiveError:
        return message

    LOGGER.info(
        "Hello! The old planet was %s. The new planet is %s", old_planet, new_planet
    )

    reply = message.copy()
    reply.data["old_planet"] = old_planet
    reply.data["new_planet"] = new_planet

    return reply
Ejemplo n.º 2
0
async def hello_persistent(
    server: "CPIAServer", message: Message, planet: Optional[str] = None
) -> Message:
    """Run the persistent hello command.

    This command creates a state the first time it's run.
    """
    if planet is None:
        planet = "Jupiter"

    if "hello_persistent_state" not in server.store:
        server.store["hello_persistent_state"] = create_state()

    command_task = server.store["hello_persistent_state"]

    old_planet, new_planet = command_task(planet)

    LOGGER.info(
        "Hello! The old planet was %s. The new planet is %s", old_planet, new_planet
    )

    reply = message.copy()
    reply.data["old_planet"] = old_planet
    reply.data["new_planet"] = new_planet

    return reply
Ejemplo n.º 3
0
async def hello(
    server: "CPIAServer", message: Message, planet: Optional[str] = None
) -> Message:
    """Run the hello command."""
    if planet is None:
        planet = "Jupiter"
    LOGGER.info("Hello %s!", planet)
    return message
Ejemplo n.º 4
0
async def hello_slow(
    server: "CPIAServer", message: Message, planet: Optional[str] = None
) -> Message:
    """Run the slow hello command."""
    if planet is None:
        planet = "Jupiter"

    result = await server.run_process_job(do_cpu_work)

    LOGGER.info("Hello %s! The result is %s", planet, result)

    reply = message.copy()
    reply.data["result"] = result

    return reply
Ejemplo n.º 5
0
async def tcp_client(message: str,
                     host: str = "127.0.0.1",
                     port: int = 8555) -> None:
    """Connect to server and send message."""
    reader, writer = await asyncio.open_connection(host, port)
    data = await reader.readline()
    version_msg = data.decode()
    LOGGER.debug("Version message: %s", version_msg.strip())

    LOGGER.info("Send: %r", message)
    writer.write(message.encode())
    await writer.drain()

    data = await reader.readline()
    LOGGER.info("Received: %r", data.decode())

    LOGGER.debug("Closing the connection")
    writer.close()
    await writer.wait_closed()