From c098ac961f21d12f1908a4e791ea711bcd2a1ea0 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 17 Jul 2014 00:19:06 +0200 Subject: [PATCH 1/4] zeroconf: Improve log messages --- mopidy/zeroconf.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mopidy/zeroconf.py b/mopidy/zeroconf.py index 1111975f..fff6ff27 100644 --- a/mopidy/zeroconf.py +++ b/mopidy/zeroconf.py @@ -56,6 +56,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 +68,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 +80,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 +99,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 +114,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 From ece59f5deb5f793c392788ea1a1495e50f95edd0 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 17 Jul 2014 00:19:42 +0200 Subject: [PATCH 2/4] http: Simplify Zeroconf publishing --- mopidy/http/actor.py | 55 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/mopidy/http/actor.py b/mopidy/http/actor.py index 96c71c8c..e0758461 100644 --- a/mopidy/http/actor.py +++ b/mopidy/http/actor.py @@ -31,16 +31,32 @@ 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 + self.app = None 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): @@ -125,36 +141,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() From b685f2ae63e921b53dc6fbca8ee01d9fa7240ffa Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 17 Jul 2014 00:22:39 +0200 Subject: [PATCH 3/4] mpd: Simplify Zeroconf publishing --- mopidy/mpd/actor.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) 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: From 47c507b8a76108fd673f608e74bbbb0a4716cc61 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 17 Jul 2014 00:25:16 +0200 Subject: [PATCH 4/4] zeroconf: ::ffff:127.* is also loopback addresses --- mopidy/zeroconf.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mopidy/zeroconf.py b/mopidy/zeroconf.py index fff6ff27..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):