From c115cf123f8be2725885ec885417c076f990aaeb Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Fri, 28 Sep 2012 00:46:44 +0200 Subject: [PATCH] MPD: Use core actor passed to frontend --- mopidy/frontends/mpd/__init__.py | 7 ++++--- mopidy/frontends/mpd/dispatcher.py | 23 +++++++---------------- tests/frontends/mpd/protocol/__init__.py | 2 +- tests/frontends/mpd/status_test.py | 2 +- 4 files changed, 13 insertions(+), 21 deletions(-) diff --git a/mopidy/frontends/mpd/__init__.py b/mopidy/frontends/mpd/__init__.py index f8c7a9ef..d7eeaaa3 100644 --- a/mopidy/frontends/mpd/__init__.py +++ b/mopidy/frontends/mpd/__init__.py @@ -32,7 +32,8 @@ class MpdFrontend(actor.ThreadingActor, core.CoreListener): port = settings.MPD_SERVER_PORT try: - network.Server(hostname, port, protocol=MpdSession, + network.Server(hostname, port, + protocol=MpdSession, protocol_kwargs={'core': core}, max_connections=settings.MPD_SERVER_MAX_CONNECTIONS) except IOError as error: logger.error(u'MPD server startup failed: %s', locale_decode(error)) @@ -76,9 +77,9 @@ class MpdSession(network.LineProtocol): encoding = protocol.ENCODING delimiter = r'\r?\n' - def __init__(self, connection): + def __init__(self, connection, core=None): super(MpdSession, self).__init__(connection) - self.dispatcher = dispatcher.MpdDispatcher(self) + self.dispatcher = dispatcher.MpdDispatcher(session=self, core=core) def on_start(self): logger.info(u'New MPD connection from [%s]:%s', self.host, self.port) diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index 1f2af153..c29cdf4d 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -27,12 +27,12 @@ class MpdDispatcher(object): _noidle = re.compile(r'^noidle$') - def __init__(self, session=None): + def __init__(self, session=None, core=None): self.authenticated = False self.command_list = False self.command_list_ok = False self.command_list_index = None - self.context = MpdContext(self, session=session) + self.context = MpdContext(self, session=session, core=core) def handle_request(self, request, current_command_list_index=None): """Dispatch incoming requests to the correct handler.""" @@ -221,27 +221,18 @@ class MpdContext(object): #: The current :class:`mopidy.frontends.mpd.MpdSession`. session = None + #: The Mopidy core API. An instance of :class:`mopidy.core.Core`. + core = None + #: The active subsystems that have pending events. events = None #: The subsytems that we want to be notified about in idle mode. subscriptions = None - def __init__(self, dispatcher, session=None): + def __init__(self, dispatcher, session=None, core=None): self.dispatcher = dispatcher self.session = session + self.core = core self.events = set() self.subscriptions = set() - self._core = None - - @property - def core(self): - """ - The Mopidy core. An instance of :class:`mopidy.core.Core`. - """ - if self._core is None: - core_refs = ActorRegistry.get_by_class(core.Core) - assert len(core_refs) == 1, \ - 'Expected exactly one running core instance.' - self._core = core_refs[0].proxy() - return self._core diff --git a/tests/frontends/mpd/protocol/__init__.py b/tests/frontends/mpd/protocol/__init__.py index a2dafb9b..041b6532 100644 --- a/tests/frontends/mpd/protocol/__init__.py +++ b/tests/frontends/mpd/protocol/__init__.py @@ -27,7 +27,7 @@ class BaseTestCase(unittest.TestCase): self.core = core.Core.start(backend=self.backend).proxy() self.connection = MockConnection() - self.session = mpd.MpdSession(self.connection) + self.session = mpd.MpdSession(self.connection, core=self.core) self.dispatcher = self.session.dispatcher self.context = self.dispatcher.context diff --git a/tests/frontends/mpd/status_test.py b/tests/frontends/mpd/status_test.py index 3a5bdcbe..6322ec36 100644 --- a/tests/frontends/mpd/status_test.py +++ b/tests/frontends/mpd/status_test.py @@ -22,7 +22,7 @@ class StatusHandlerTest(unittest.TestCase): def setUp(self): self.backend = dummy.DummyBackend.start(audio=None).proxy() self.core = core.Core.start(backend=self.backend).proxy() - self.dispatcher = dispatcher.MpdDispatcher() + self.dispatcher = dispatcher.MpdDispatcher(core=self.core) self.context = self.dispatcher.context def tearDown(self):