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
This commit is contained in:
Stein Magnus Jodal 2014-05-06 23:17:15 +02:00
parent 1b5c21452f
commit 2979de0f94
6 changed files with 47 additions and 37 deletions

View File

@ -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

View File

@ -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.

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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())