Add extra zeroconf stuff in http to announce mopidy-http as it's own service

This is intended to help clients using the websocket api discover mopidy
This commit is contained in:
Sam Willcocks 2014-04-27 03:30:16 +01:00
parent 203b13aad7
commit 4e8f1c7d54
2 changed files with 27 additions and 9 deletions

View File

@ -22,6 +22,7 @@ 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,8 +25,10 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
self.hostname = config['http']['hostname']
self.port = config['http']['port']
self.zeroconf_name = config['http']['zeroconf']
self.zeroconf_service = None
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._setup_server()
self._setup_websocket_plugin()
@ -94,21 +96,36 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
cherrypy.engine.start()
logger.info('HTTP server running at %s', cherrypy.server.base())
if self.zeroconf_name:
self.zeroconf_service = zeroconf.Zeroconf(
stype='_http._tcp', name=self.zeroconf_name,
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_service.publish():
if self.zeroconf_http_service.publish():
logger.debug(
'Registered HTTP with Zeroconf as "%s"',
self.zeroconf_service.name)
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.')
def on_stop(self):
if self.zeroconf_service:
self.zeroconf_service.unpublish()
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')
cherrypy.engine.exit()