From a88cf1e2cada6cc010c36579e38822ee1d84f4fd Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 8 Mar 2011 21:08:45 +0100 Subject: [PATCH] Hook an MpdDispatcher directly onto each MpdSession --- mopidy/frontends/mpd/dispatcher.py | 18 +++++++++++++++--- mopidy/frontends/mpd/session.py | 8 +++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index ab5f2e8c..932eb3e3 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -1,5 +1,8 @@ import re +from pykka.proxy import ActorProxy +from pykka.registry import ActorRegistry + from mopidy.frontends.mpd.exceptions import (MpdAckError, MpdArgError, MpdUnknownCommand) from mopidy.frontends.mpd.protocol import mpd_commands, request_handlers @@ -14,11 +17,20 @@ from mopidy.utils import flatten class MpdDispatcher(object): """ - Dispatches MPD requests to the correct handler. + The MPD session feeds the MPD dispatcher with requests. The dispatcher + finds the correct handler, processes the request and sends the response + back to the MPD session. """ - def __init__(self, backend=None): - self.backend = backend + # XXX Consider merging MpdDispatcher into MpdSession + + def __init__(self, session): + self.session = session + + # TODO-PYKKA: Get reference to backend in a more generic way + backend_refs = ActorRegistry.get_by_class_name('SpotifyBackend') + self.backend = ActorProxy(backend_refs[0]) + self.command_list = False self.command_list_ok = False diff --git a/mopidy/frontends/mpd/session.py b/mopidy/frontends/mpd/session.py index 399c2adb..eb8c4f11 100644 --- a/mopidy/frontends/mpd/session.py +++ b/mopidy/frontends/mpd/session.py @@ -3,6 +3,7 @@ import logging import multiprocessing from mopidy import settings +from mopidy.frontends.mpd.dispatcher import MpdDispatcher from mopidy.frontends.mpd.protocol import ENCODING, LINE_TERMINATOR, VERSION from mopidy.utils.log import indent @@ -10,8 +11,8 @@ logger = logging.getLogger('mopidy.frontends.mpd.session') class MpdSession(asynchat.async_chat): """ - The MPD client session. Keeps track of a single client and passes its - MPD requests to the dispatcher. + The MPD client session. Keeps track of a single client session. Any + requests from the client is passed on to the MPD request dispatcher. """ def __init__(self, server, client_socket, client_socket_address): @@ -22,6 +23,7 @@ class MpdSession(asynchat.async_chat): self.input_buffer = [] self.authenticated = False self.set_terminator(LINE_TERMINATOR.encode(ENCODING)) + self.dispatcher = MpdDispatcher(self) def start(self): """Start a new client session.""" @@ -50,7 +52,7 @@ class MpdSession(asynchat.async_chat): if response is not None: self.send_response(response) return - # TODO-PYKKA: Process request using MpdDispatcher/backend + response = self.dispatcher.handle_request(request) if response is not None: self.handle_response(response)