Skip to content

Commit

Permalink
Add command lock (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHjelmare authored Oct 11, 2021
1 parent 560230d commit 7723c6c
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions aiovlc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def __init__(
self.host = host
self.password = password
self.port = port
self.reader: asyncio.StreamReader | None = None
self.writer: asyncio.StreamWriter | None = None
self._command_lock = asyncio.Lock()
self._reader: asyncio.StreamReader | None = None
self._writer: asyncio.StreamWriter | None = None

async def __aenter__(self) -> Client:
"""Connect the client with context manager."""
Expand All @@ -65,27 +66,27 @@ async def __aexit__(
async def connect(self) -> None:
"""Connect the client."""
try:
self.reader, self.writer = await asyncio.open_connection(
self._reader, self._writer = await asyncio.open_connection(
host=self.host, port=self.port
)
except OSError as err:
raise ConnectError(f"Failed to connect: {err}") from err

async def disconnect(self) -> None:
"""Disconnect the client."""
assert self.writer is not None
assert self._writer is not None
try:
self.writer.close()
await self.writer.wait_closed()
self._writer.close()
await self._writer.wait_closed()
except OSError:
pass

async def read(self, read_until: str = TERMINATOR) -> str:
"""Return a decoded message."""
assert self.reader is not None
assert self._reader is not None

try:
read = await self.reader.readuntil(read_until.encode("utf-8"))
read = await self._reader.readuntil(read_until.encode("utf-8"))
except asyncio.LimitOverrunError as err:
raise ConnectReadError(err) from err
except asyncio.IncompleteReadError as err:
Expand All @@ -102,11 +103,11 @@ async def read(self, read_until: str = TERMINATOR) -> str:

async def write(self, command: str) -> None:
"""Write a command to the connection."""
assert self.writer is not None
assert self._writer is not None

try:
self.writer.write(command.encode("utf-8"))
await self.writer.drain()
self._writer.write(command.encode("utf-8"))
await self._writer.drain()
except OSError as err:
raise ConnectError(f"Failed to write: {err}") from err

Expand All @@ -131,10 +132,11 @@ async def login(self) -> None:

async def send_command(self, command_string: str) -> list[str]:
"""Send a command and return the output."""
LOGGER.debug("Sending command: %s", command_string.strip())
await self.write(command_string)
command_output = (await self.read("> ")).split("\r\n")[:-1]
LOGGER.debug("Command output: %s", command_output)
async with self._command_lock:
LOGGER.debug("Sending command: %s", command_string.strip())
await self.write(command_string)
command_output = (await self.read("> ")).split("\r\n")[:-1]
LOGGER.debug("Command output: %s", command_output)
if command_output:
if re.match(
r"Unknown command `.*'\. Type `help' for help\.", command_output[0]
Expand Down

0 comments on commit 7723c6c

Please sign in to comment.