Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reconnect support to snapcast server control. #17

Merged
merged 1 commit into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions snapcast/control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


@asyncio.coroutine
def create_server(loop, host, port=CONTROL_PORT):
def create_server(loop, host, port=CONTROL_PORT, reconnect=False):
"""Server factory."""
server = Snapserver(loop, host, port)
server = Snapserver(loop, host, port, reconnect)
yield from server.start()
return server
29 changes: 26 additions & 3 deletions snapcast/control/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

STREAM_ONUPDATE = 'Stream.OnUpdate'

SERVER_RECONNECT_DELAY = 5

_EVENTS = [SERVER_ONUPDATE, CLIENT_ONVOLUMECHANGED, CLIENT_ONLATENCYCHANGED,
CLIENT_ONNAMECHANGED, CLIENT_ONCONNECT, CLIENT_ONDISCONNECT,
GROUP_ONMUTE, GROUP_ONSTREAMCHANGED, STREAM_ONUPDATE]
Expand All @@ -51,10 +53,11 @@ class Snapserver(object):
"""Represents a snapserver."""

# pylint: disable=too-many-instance-attributes
def __init__(self, loop, host, port=CONTROL_PORT):
def __init__(self, loop, host, port=CONTROL_PORT, reconnect=False):
"""Initialize."""
self._loop = loop
self._port = port
self._reconnect = reconnect
self._clients = {}
self._streams = {}
self._groups = {}
Expand All @@ -79,13 +82,30 @@ def __init__(self, loop, host, port=CONTROL_PORT):

def start(self):
"""Initiate server connection."""
_, self._protocol = yield from self._loop.create_connection(
lambda: SnapcastProtocol(self._callbacks), self._host, self._port)
yield from self._do_connect()
_LOGGER.info('connected to snapserver on %s:%s', self._host, self._port)
status = yield from self.status()
self.synchronize(status)
self._on_server_connect()

@asyncio.coroutine
def _do_connect(self):
"""Perform the connection to the server."""
_, self._protocol = yield from self._loop.create_connection(
lambda: SnapcastProtocol(self._callbacks), self._host, self._port)

def _reconnect_cb(self):
"""Callback to reconnect to the server."""
@asyncio.coroutine
def try_reconnect():
"""Actual coroutine ro try to reconnect or reschedule."""
try:
yield from self._do_connect()
except IOError:
self._loop.call_later(SERVER_RECONNECT_DELAY,
self._reconnect_cb)
asyncio.ensure_future(try_reconnect())

@asyncio.coroutine
def _transact(self, method, params=None):
"""Wrap requests."""
Expand Down Expand Up @@ -202,8 +222,11 @@ def _on_server_connect(self):

def _on_server_disconnect(self, exception):
"""Handle server disconnection."""
self._protocol = None
if self._on_disconnect_callback_func and callable(self._on_disconnect_callback_func):
self._on_disconnect_callback_func(exception)
if self._reconnect:
self._reconnect_cb()

def _on_server_update(self, data):
"""Handle server update."""
Expand Down