Commit 31f34991 authored by Philipp Hörist's avatar Philipp Hörist

Zeroconf: Fix name conflict

Pass kDNSServiceFlagsNoAutoRename to pybonjour so we dont end up with
invalid names like `user@machine (2)`

Refactor alternativ name suggestion into own method
parent 55cded2e
Pipeline #4421 passed with stages
in 3 minutes and 2 seconds
......@@ -240,17 +240,19 @@ class Zeroconf:
elif errorCode == pybonjour.kDNSServiceErr_NameConflict:
log.error('Error while adding service. %s', errorCode)
parts = self.username.split(' ')
self.name_conflictCB(self._get_alternativ_name(self.username))
else:
self.error_CB(_('Error while adding service. %s') % str(errorCode))
# check if last part is a number and if, increment it
@staticmethod
def _get_alternativ_name(name):
if name[-2] == '-':
try:
stripped = str(int(parts[-1]))
number = int(name[-1])
except Exception:
stripped = 1
alternative_name = self.username + str(stripped + 1)
self.name_conflictCB(alternative_name)
else:
self.error_CB(_('Error while adding service. %s') % str(errorCode))
return '%s-1' % name
return '%s-%s' % (name[:-2], number + 1)
return '%s-1' % name
# make zeroconf-valid names
def replace_show(self, show):
......@@ -280,6 +282,7 @@ class Zeroconf:
self.txt = txt
try:
self.service_sdRef = pybonjour.DNSServiceRegister(
flags=pybonjour.kDNSServiceFlagsNoAutoRename,
name=self.name,
regtype=self.stype,
port=self.port,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment