Skip to content

Commit

Permalink
Send announcement on server start
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Jan 12, 2025
1 parent 4fbac1d commit e1cbf72
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions async_upnp_client/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ async def async_start(self) -> None:
sock=sock,
)

await self.async_wait_for_transport_protocol()

# Announce and reschedule self.
self._announce_next()

Expand All @@ -855,25 +857,40 @@ async def async_stop(self) -> None:
self._send_byebyes()
self._transport.close()

async def async_wait_for_transport_protocol(self) -> None:
"""Wait for the protocol to become available."""
loop = 0
max_loops = 5
while loop < max_loops:
await asyncio.sleep(0.1)
if (
self._transport is not None
and self._transport.get_protocol() is not None
):
break

loop += 1

if loop > max_loops:
raise UpnpError("Failed to get protocol")

def _announce_next(self) -> None:
"""Announce next advertisement."""
_LOGGER.debug("Announcing")
assert self._transport

protocol = cast(SsdpProtocol, self._transport.get_protocol())
# Protocol can be None when it is not yet initialized.
if protocol:
start_line = "NOTIFY * HTTP/1.1"
headers = next(self._advertisements)
packet = build_ssdp_packet(start_line, headers)
start_line = "NOTIFY * HTTP/1.1"
headers = next(self._advertisements)
packet = build_ssdp_packet(start_line, headers)

_LOGGER.debug(
"Sending advertisement, NTS: %s, NT: %s, USN: %s",
headers["NTS"],
headers["NT"],
headers["USN"],
)
protocol.send_ssdp_packet(packet, self.target)
_LOGGER.debug(
"Sending advertisement, NTS: %s, NT: %s, USN: %s",
headers["NTS"],
headers["NT"],
headers["USN"],
)
protocol.send_ssdp_packet(packet, self.target)

# Reschedule self.
self._cancel_announce = self._loop.call_later(
Expand Down

0 comments on commit e1cbf72

Please sign in to comment.