Skip to content
Merged
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
16 changes: 14 additions & 2 deletions snapcast/control/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,30 @@ def _do_connect(self):

def _reconnect_cb(self):
"""Callback to reconnect to the server."""
_LOGGER.info('try reconnect')
@asyncio.coroutine
def try_reconnect():
"""Actual coroutine ro try to reconnect or reschedule."""
try:
yield from self._do_connect()
except IOError:
except OSError:
self._loop.call_later(SERVER_RECONNECT_DELAY,
self._reconnect_cb)
else:
status = yield from self.status()
self.synchronize(status)
self._on_server_connect()
asyncio.ensure_future(try_reconnect())

@asyncio.coroutine
def _transact(self, method, params=None):
"""Wrap requests."""
result, error = yield from self._protocol.request(method, params)
result = error = None
try:
result, error = yield from self._protocol.request(method, params)
except:
_LOGGER.warning('could not send request')
error = 'could not send request'
return result or error

@property
Expand Down Expand Up @@ -303,11 +313,13 @@ def _request(self, method, identifier, key=None, value=None, parameters=None):

def _on_server_connect(self):
"""Handle server connection."""
_LOGGER.info('Server connected')
if self._on_connect_callback_func and callable(self._on_connect_callback_func):
self._on_connect_callback_func()

def _on_server_disconnect(self, exception):
"""Handle server disconnection."""
_LOGGER.info('Server disconnected')
if self._on_disconnect_callback_func and callable(self._on_disconnect_callback_func):
self._on_disconnect_callback_func(exception)
if not self._is_stopped:
Expand Down