diff --git a/mopidy/handler.py b/mopidy/handler.py new file mode 100644 index 00000000..0568dd07 --- /dev/null +++ b/mopidy/handler.py @@ -0,0 +1,56 @@ +import logging +import re + +from mopidy import settings + +logger = logging.getLogger('handler') + +class MpdHandler(object): + def __init__(self): + self.request_handlers = [ + (r'^currentsong$', self._currentsong), + (r'^listplaylists$', self._listplaylists), + (r'^lsinfo( "(?P[^"]*)")*$', self._lsinfo), + (r'^ping$', self._ping), + (r'^plchanges "(?P\d+)"$', self._plchanges), + (r'^status$', self._status), + ] + + def handle_request(self, request): + for request_pattern, request_handling_method in self.request_handlers: + matches = re.match(request_pattern, request) + if matches is not None: + groups = matches.groupdict() + return request_handling_method(**groups) + logger.warning(u'Unhandled request: %s', request) + + def _currentsong(self): + return None # TODO + + def _listplaylists(self): + return None # TODO + + def _lsinfo(self, uri): + if uri == u'/': + return self._listplaylists() + return None # TODO + + def _ping(self): + return None + + def _plchanges(self, version): + return None # TODO + + def _status(self): + # TODO + return [ + 'volume: 0', + 'repeat: 0', + 'random: 0', + 'single: 0', + 'consume: 0', + 'playlist: 0', + 'playlistlength: 0', + 'xfade: 0', + 'state: stop', + ] diff --git a/mopidy/session.py b/mopidy/session.py index 97b5ddc5..acad4b24 100644 --- a/mopidy/session.py +++ b/mopidy/session.py @@ -2,14 +2,16 @@ import asynchat import logging from mopidy import get_version, settings +from mopidy.handler import MpdHandler logger = logging.getLogger(u'session') class MpdSession(asynchat.async_chat): - def __init__(self, client_socket, client_address): + def __init__(self, client_socket, client_address, handler=MpdHandler): asynchat.async_chat.__init__(self, sock=client_socket) self.input_buffer = [] self.set_terminator(settings.MPD_LINE_TERMINATOR) + self.handler = handler() self.send_response(u'OK MPD (mopidy %s)' % get_version()) def collect_incoming_data(self, data): @@ -23,7 +25,11 @@ class MpdSession(asynchat.async_chat): self.handle_request(input) def handle_request(self, input): - pass # TODO + response = self.handler.handle_request(input) + if response is not None: + for line in response: + self.send_response(line) + self.send_response(u'OK') def send_response(self, output): logger.debug(u'Output: %s', output)