diff --git a/mopidy/http/actor.py b/mopidy/http/actor.py index 9fd4ffca..3324a277 100644 --- a/mopidy/http/actor.py +++ b/mopidy/http/actor.py @@ -31,8 +31,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 = None + self.zeroconf_mopidy_http = None try: logger.debug('Starting HTTP server') @@ -46,10 +48,23 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): def on_start(self): threading.Thread(target=self._startup).start() - self._publish_zeroconf() + + if self.zeroconf_name: + self.zeroconf_http = zeroconf.Zeroconf( + stype='_http._tcp', name=self.zeroconf_name, + host=self.hostname, port=self.port) + self.zeroconf_mopidy_http = zeroconf.Zeroconf( + stype='_mopidy-http._tcp', name=self.zeroconf_name, + host=self.hostname, port=self.port) + self.zeroconf_http.publish() + self.zeroconf_mopidy_http.publish() def on_stop(self): - self._unpublish_zeroconf() + if self.zeroconf_http: + self.zeroconf_http.unpublish() + if self.zeroconf_mopidy_http: + self.zeroconf_mopidy_http.unpublish() + tornado.ioloop.IOLoop.instance().add_callback(self._shutdown) def _startup(self): @@ -131,36 +146,3 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): )) logger.debug('Loaded static HTTP extension: %s', static['name']) return result - - 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() diff --git a/mopidy/mpd/actor.py b/mopidy/mpd/actor.py index 8181e0e7..23d88bf9 100644 --- a/mopidy/mpd/actor.py +++ b/mopidy/mpd/actor.py @@ -18,6 +18,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener): self.hostname = network.format_hostname(config['mpd']['hostname']) self.port = config['mpd']['port'] + self.zeroconf_name = config['mpd']['zeroconf'] self.zeroconf_service = None @@ -43,13 +44,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener): self.zeroconf_service = zeroconf.Zeroconf( stype='_mpd._tcp', name=self.zeroconf_name, host=self.hostname, port=self.port) - - if self.zeroconf_service.publish(): - logger.debug( - 'Registered MPD with Zeroconf as "%s"', - self.zeroconf_service.name) - else: - logger.debug('Registering MPD with Zeroconf failed.') + self.zeroconf_service.publish() def on_stop(self): if self.zeroconf_service: diff --git a/mopidy/zeroconf.py b/mopidy/zeroconf.py index 1111975f..cdd84792 100644 --- a/mopidy/zeroconf.py +++ b/mopidy/zeroconf.py @@ -17,7 +17,10 @@ _AVAHI_PUBLISHFLAGS_NONE = 0 def _is_loopback_address(host): - return host.startswith('127.') or host == '::1' + return ( + host.startswith('127.') or + host.startswith('::ffff:127.') or + host == '::1') def _convert_text_to_dbus_bytes(text): @@ -56,6 +59,10 @@ class Zeroconf(object): self.name = template.safe_substitute( hostname=self.host or socket.getfqdn(), port=self.port) + def __str__(self): + return 'Zeroconf service %s at [%s]:%d' % ( + self.stype, self.host, self.port) + def publish(self): """Publish the service. @@ -64,11 +71,11 @@ class Zeroconf(object): if _is_loopback_address(self.host): logger.debug( - 'Zeroconf publish on loopback interface is not supported.') + '%s: Publish on loopback interface is not supported.', self) return False if not dbus: - logger.debug('Zeroconf publish failed: dbus not installed.') + logger.debug('%s: dbus not installed; publish failed.', self) return False try: @@ -76,7 +83,7 @@ class Zeroconf(object): if not bus.name_has_owner('org.freedesktop.Avahi'): logger.debug( - 'Zeroconf publish failed: Avahi service not running.') + '%s: Avahi service not running; publish failed.', self) return False server = dbus.Interface( @@ -95,9 +102,10 @@ class Zeroconf(object): self.domain, self.host, dbus.UInt16(self.port), text) self.group.Commit() + logger.debug('%s: Published', self) return True except dbus.exceptions.DBusException as e: - logger.debug('Zeroconf publish failed: %s', e) + logger.debug('%s: Publish failed: %s', self, e) return False def unpublish(self): @@ -109,7 +117,8 @@ class Zeroconf(object): if self.group: try: self.group.Reset() + logger.debug('%s: Unpublished', self) except dbus.exceptions.DBusException as e: - logger.debug('Zeroconf unpublish failed: %s', e) + logger.debug('%s: Unpublish failed: %s', self, e) finally: self.group = None