From 2979de0f945edd2ffa4ac765a46dc664a8b71223 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 6 May 2014 23:17:15 +0200 Subject: [PATCH] http: Polish Mopidy-HTTP Zeroconf service Builds upon #725 to: - Remove extra config for Mopidy-HTTP Zeroconf service - Refactor Zeroconf setup code in the HTTP frontend a bit - Add documentation --- docs/changelog.rst | 7 ++++ docs/ext/http.rst | 3 ++ mopidy/http/__init__.py | 1 - mopidy/http/actor.py | 71 ++++++++++++++++++++------------------- mopidy/http/ext.conf | 1 - tests/http/test_events.py | 1 - 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7bcca996..43f73945 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -33,6 +33,13 @@ Feature release. :meth:`~mopidy.ext.Extension.register_gstreamer_elements`. Use :meth:`mopidy.ext.Extension.setup` instead, as most extensions already do. +**HTTP frontend** + +- If Zeroconf is enabled, we now announce the ``_mopidy-http._tcp`` service in + addition to ``_http._tcp``. This is to make it easier to automatically find + Mopidy's HTTP server among other Zeroconf-published HTTP servers on the + local network. + **MPD frontend** - Proper command tokenization for MPD requests. This replaces the old regex diff --git a/docs/ext/http.rst b/docs/ext/http.rst index 1b5b0119..34c77828 100644 --- a/docs/ext/http.rst +++ b/docs/ext/http.rst @@ -108,4 +108,7 @@ See :ref:`config` for general help on configuring Mopidy. Name of the HTTP service when published through Zeroconf. The variables ``$hostname`` and ``$port`` can be used in the name. + If set, the Zeroconf services ``_http._tcp`` and ``_mopidy-http._tcp`` will + be published. + Set to an empty string to disable Zeroconf for HTTP. diff --git a/mopidy/http/__init__.py b/mopidy/http/__init__.py index c51eaf29..25e2dd46 100644 --- a/mopidy/http/__init__.py +++ b/mopidy/http/__init__.py @@ -22,7 +22,6 @@ class Extension(ext.Extension): schema['port'] = config.Port() schema['static_dir'] = config.Path(optional=True) schema['zeroconf'] = config.String(optional=True) - schema['zeroconf-websocket'] = config.String(optional=True) return schema def validate_environment(self): diff --git a/mopidy/http/actor.py b/mopidy/http/actor.py index 6510e6cf..32e5074a 100644 --- a/mopidy/http/actor.py +++ b/mopidy/http/actor.py @@ -25,10 +25,8 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): self.hostname = config['http']['hostname'] self.port = config['http']['port'] - self.zeroconf_http_name = config['http']['zeroconf'] - self.zeroconf_http_service = None - self.zeroconf_websocket_name = config['http']['zeroconf-websocket'] - self.zeroconf_websocket_service = None + self.zeroconf_name = config['http']['zeroconf'] + self.zeroconf_service = None self._setup_server() self._setup_websocket_plugin() @@ -95,39 +93,11 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): logger.debug('Starting HTTP server') cherrypy.engine.start() logger.info('HTTP server running at %s', cherrypy.server.base()) - - if self.zeroconf_http_name: - self.zeroconf_http_service = zeroconf.Zeroconf( - stype='_http._tcp', name=self.zeroconf_http_name, - host=self.hostname, port=self.port) - - if self.zeroconf_http_service.publish(): - logger.debug( - 'Registered HTTP with Zeroconf as "%s"', - self.zeroconf_http_service.name) - else: - logger.debug('Registering HTTP with Zeroconf failed.') - - if self.zeroconf_websocket_name: - self.zeroconf_websocket_service = zeroconf.Zeroconf( - stype='_mopidy-http._tcp', name=self.zeroconf_websocket_name, - host=self.hostname, port=self.port) - - if self.zeroconf_websocket_service.publish(): - logger.debug( - 'Registered mopidy-http with Zeroconf as "%s"', - self.zeroconf_websocket_service.name) - else: - logger.debug('Registering mopidy-http with Zeroconf failed.') + self._publish_zeroconf() def on_stop(self): - if self.zeroconf_http_service: - self.zeroconf_http_service.unpublish() - - if self.zeroconf_websocket_service: - self.zeroconf_websocket_service.unpublish() - logger.debug('Stopping HTTP server') + self._unpublish_zeroconf() cherrypy.engine.exit() logger.info('Stopped HTTP server') @@ -137,6 +107,39 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): message = json.dumps(event, cls=models.ModelJSONEncoder) cherrypy.engine.publish('websocket-broadcast', TextMessage(message)) + def _publish_zeroconf(self): + if not self.zeroconf_name: + return + + self.zeroconf_http_service = zeroconf.Zeroconf( + stype='_http._tcp', name=self.zeroconf_name, + host=self.hostname, port=self.port) + + if self.zeroconf_http_service.publish(): + logger.debug( + 'Registered HTTP with Zeroconf as "%s"', + self.zeroconf_http_service.name) + else: + logger.debug('Registering HTTP with Zeroconf failed.') + + self.zeroconf_mopidy_http_service = zeroconf.Zeroconf( + stype='_mopidy-http._tcp', name=self.zeroconf_name, + host=self.hostname, port=self.port) + + if self.zeroconf_mopidy_http_service.publish(): + logger.debug( + 'Registered Mopidy-HTTP with Zeroconf as "%s"', + self.zeroconf_mopidy_http_service.name) + else: + logger.debug('Registering Mopidy-HTTP with Zeroconf failed.') + + def _unpublish_zeroconf(self): + if self.zeroconf_http_service: + self.zeroconf_http_service.unpublish() + + if self.zeroconf_mopidy_http_service: + self.zeroconf_mopidy_http_service.unpublish() + class RootResource(object): pass diff --git a/mopidy/http/ext.conf b/mopidy/http/ext.conf index 39bd6866..d35229bc 100644 --- a/mopidy/http/ext.conf +++ b/mopidy/http/ext.conf @@ -4,4 +4,3 @@ hostname = 127.0.0.1 port = 6680 static_dir = zeroconf = Mopidy HTTP server on $hostname -zeroconf-websocket = Mopidy websocket server on $hostname \ No newline at end of file diff --git a/tests/http/test_events.py b/tests/http/test_events.py index 2d96b42f..dbfa8413 100644 --- a/tests/http/test_events.py +++ b/tests/http/test_events.py @@ -29,7 +29,6 @@ class HttpEventsTest(unittest.TestCase): 'port': 6680, 'static_dir': None, 'zeroconf': '', - 'zeroconf-websocket': '', } } self.http = actor.HttpFrontend(config=config, core=mock.Mock())