-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amit lissack
committed
Oct 23, 2021
1 parent
b7a53bd
commit ac6b618
Showing
3 changed files
with
52 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 38 additions & 4 deletions
42
api/src/opentrons/hardware_control/emulation/run_emulator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,56 @@ | ||
import asyncio | ||
import logging | ||
from typing import Optional | ||
|
||
from opentrons.hardware_control.emulation.abstract_emulator import AbstractEmulator | ||
from opentrons.hardware_control.emulation.connection_handler import ConnectionHandler | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
async def run_emulator_client(host: str, port: int, emulator: AbstractEmulator) -> None: | ||
"""Run an emulator as a client.""" | ||
async def run_emulator_client(host: str, port: int, emulator: AbstractEmulator, retries: int = 3, interval_seconds:float=0.1) -> None: | ||
"""Run an emulator as a client. | ||
Args: | ||
host: Host to connect to. | ||
port: Port to connect on. | ||
emulator: The emulator instance. | ||
retries: Number of retries on a failed connection attempt. | ||
interval_seconds: How long to wait between retries. | ||
Returns: | ||
None | ||
""" | ||
log.info(f"Connecting to {emulator.__class__.__name__} at {host}:{port}") | ||
r, w = await asyncio.open_connection(host, port) | ||
|
||
r: Optional[asyncio.StreamReader] = None | ||
w: Optional[asyncio.StreamWriter] = None | ||
for i in range(retries): | ||
try: | ||
r, w = await asyncio.open_connection(host, port) | ||
break | ||
except IOError: | ||
log.error(f"{emulator.__class__.__name__} failed to connect on try {i + 1}. Retrying in {interval_seconds} seconds.") | ||
await asyncio.sleep(interval_seconds) | ||
|
||
if r is None or w is None: | ||
raise IOError(f"Failed to connect to {emulator.__class__.__name__} at {host}:{port} after {retries} retries.") | ||
|
||
connection = ConnectionHandler(emulator) | ||
await connection(r, w) | ||
|
||
|
||
async def run_emulator_server(host: str, port: int, emulator: AbstractEmulator) -> None: | ||
"""Run an emulator as a server.""" | ||
"""Run an emulator as a server. | ||
Args: | ||
host: Host to listen on. | ||
port: Port to listen on. | ||
emulator: Emaulator instance. | ||
Returns: | ||
None | ||
""" | ||
log.info(f"Starting {emulator.__class__.__name__} at {host}:{port}") | ||
server = await asyncio.start_server(ConnectionHandler(emulator), host, port) | ||
await server.serve_forever() |