Merge branch 'develop' into feature/http-startup

Conflicts:
	mopidy/http/actor.py
This commit is contained in:
Stein Magnus Jodal 2014-07-17 00:27:50 +02:00
commit 2cf37679a6
3 changed files with 35 additions and 49 deletions

View File

@ -31,8 +31,10 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
self.hostname = config['http']['hostname'] self.hostname = config['http']['hostname']
self.port = config['http']['port'] self.port = config['http']['port']
self.zeroconf_name = config['http']['zeroconf'] self.zeroconf_name = config['http']['zeroconf']
self.zeroconf_service = None self.zeroconf_http = None
self.zeroconf_mopidy_http = None
try: try:
logger.debug('Starting HTTP server') logger.debug('Starting HTTP server')
@ -46,10 +48,23 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
def on_start(self): def on_start(self):
threading.Thread(target=self._startup).start() 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): 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) tornado.ioloop.IOLoop.instance().add_callback(self._shutdown)
def _startup(self): def _startup(self):
@ -131,36 +146,3 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
)) ))
logger.debug('Loaded static HTTP extension: %s', static['name']) logger.debug('Loaded static HTTP extension: %s', static['name'])
return result 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()

View File

@ -18,6 +18,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener):
self.hostname = network.format_hostname(config['mpd']['hostname']) self.hostname = network.format_hostname(config['mpd']['hostname'])
self.port = config['mpd']['port'] self.port = config['mpd']['port']
self.zeroconf_name = config['mpd']['zeroconf'] self.zeroconf_name = config['mpd']['zeroconf']
self.zeroconf_service = None self.zeroconf_service = None
@ -43,13 +44,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener):
self.zeroconf_service = zeroconf.Zeroconf( self.zeroconf_service = zeroconf.Zeroconf(
stype='_mpd._tcp', name=self.zeroconf_name, stype='_mpd._tcp', name=self.zeroconf_name,
host=self.hostname, port=self.port) host=self.hostname, port=self.port)
self.zeroconf_service.publish()
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.')
def on_stop(self): def on_stop(self):
if self.zeroconf_service: if self.zeroconf_service:

View File

@ -17,7 +17,10 @@ _AVAHI_PUBLISHFLAGS_NONE = 0
def _is_loopback_address(host): 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): def _convert_text_to_dbus_bytes(text):
@ -56,6 +59,10 @@ class Zeroconf(object):
self.name = template.safe_substitute( self.name = template.safe_substitute(
hostname=self.host or socket.getfqdn(), port=self.port) 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): def publish(self):
"""Publish the service. """Publish the service.
@ -64,11 +71,11 @@ class Zeroconf(object):
if _is_loopback_address(self.host): if _is_loopback_address(self.host):
logger.debug( logger.debug(
'Zeroconf publish on loopback interface is not supported.') '%s: Publish on loopback interface is not supported.', self)
return False return False
if not dbus: if not dbus:
logger.debug('Zeroconf publish failed: dbus not installed.') logger.debug('%s: dbus not installed; publish failed.', self)
return False return False
try: try:
@ -76,7 +83,7 @@ class Zeroconf(object):
if not bus.name_has_owner('org.freedesktop.Avahi'): if not bus.name_has_owner('org.freedesktop.Avahi'):
logger.debug( logger.debug(
'Zeroconf publish failed: Avahi service not running.') '%s: Avahi service not running; publish failed.', self)
return False return False
server = dbus.Interface( server = dbus.Interface(
@ -95,9 +102,10 @@ class Zeroconf(object):
self.domain, self.host, dbus.UInt16(self.port), text) self.domain, self.host, dbus.UInt16(self.port), text)
self.group.Commit() self.group.Commit()
logger.debug('%s: Published', self)
return True return True
except dbus.exceptions.DBusException as e: except dbus.exceptions.DBusException as e:
logger.debug('Zeroconf publish failed: %s', e) logger.debug('%s: Publish failed: %s', self, e)
return False return False
def unpublish(self): def unpublish(self):
@ -109,7 +117,8 @@ class Zeroconf(object):
if self.group: if self.group:
try: try:
self.group.Reset() self.group.Reset()
logger.debug('%s: Unpublished', self)
except dbus.exceptions.DBusException as e: except dbus.exceptions.DBusException as e:
logger.debug('Zeroconf unpublish failed: %s', e) logger.debug('%s: Unpublish failed: %s', self, e)
finally: finally:
self.group = None self.group = None