diff options
Diffstat (limited to 'nephilim/mpd.py')
-rw-r--r-- | nephilim/mpd.py | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/nephilim/mpd.py b/nephilim/mpd.py index cae2bcb..97aa54f 100644 --- a/nephilim/mpd.py +++ b/nephilim/mpd.py @@ -15,6 +15,8 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import socket +import logging +from PyQt4 import QtCore HELLO_PREFIX = "OK MPD " @@ -46,8 +48,15 @@ class _NotConnected(object): def _dummy(*args): raise ConnectionError("Not connected") -class MPDClient(object): +class MPDClient(QtCore.QObject): + # public + logger = None + + # SIGNALS + connect_changed = QtCore.pyqtSignal(bool) def __init__(self): + QtCore.QObject.__init__(self) + self.logger = logging.getLogger('mpclient.mpdsocket') self._reset() self._commands = { # Admin Commands @@ -272,15 +281,6 @@ class MPDClient(object): self._commandlist = None raise - def _hello(self): - line = self._rfile.readline() - if not line.endswith("\n"): - raise ConnectionError("Connection lost while reading MPD hello") - line = line.rstrip("\n") - if not line.startswith(HELLO_PREFIX): - raise ProtocolError("Got invalid MPD hello: '%s'" % line) - self.mpd_version = line[len(HELLO_PREFIX):].strip() - def _reset(self): self.mpd_version = None self._commandlist = None @@ -288,9 +288,9 @@ class MPDClient(object): self._rfile = _NotConnected() self._wfile = _NotConnected() - def connect(self, host, port): + def connect_mpd(self, host, port): if self._sock: - raise ConnectionError("Already connected") + self.logger.error('Already connected.') msg = "getaddrinfo returns an empty list" try: flags = socket.AI_ADDRCONFIG @@ -300,10 +300,11 @@ class MPDClient(object): try: self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._sock.connect(host) - except socket.error, msg: + except socket.error, e: if self._sock: self._sock.close() self._sock = None + self.logger.error('Error connecting to MPD: %s.'%e) else: for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, @@ -312,27 +313,39 @@ class MPDClient(object): try: self._sock = socket.socket(af, socktype, proto) self._sock.connect(sa) - except socket.error, msg: + except socket.error, e: if self._sock: self._sock.close() self._sock = None + self.logger.error('Error connecting to MPD: %s.'%e) continue break if not self._sock: - raise socket.error(msg) - self._rfile = self._sock.makefile("rb") - self._wfile = self._sock.makefile("wb") - try: - self._hello() - except: - self.disconnect() - raise + return + self._rfile = self._sock.makefile('rb') + self._wfile = self._sock.makefile('wb') + + # read MPD hello + line = self._rfile.readline() + if not line.endswith("\n"): + self.logger.error('Connnection lost while reading MPD hello') + self.disconnect_mpd() + return False + line = line.rstrip("\n") + if not line.startswith(HELLO_PREFIX): + self.logger.error('Got invalid MPD hello: %s' % line) + self.disconnect_mpd() + return + self.mpd_version = line[len(HELLO_PREFIX):].strip() + + self.connect_changed.emit(True) - def disconnect(self): + def disconnect_mpd(self): self._rfile.close() self._wfile.close() self._sock.close() self._reset() + self.connect_changed.emit(False) def command_list_ok_begin(self): if self._commandlist is not None: |