<commit_before>import dbus
import gobject
import avahi
import threading
import sys
from dbus.mainloop.glib import DBusGMainLoop

DBusGMainLoop( set_as_default=True )

"""
Class for publishing a service on DNS-SD using Avahi.
"""
class ServicePublisher (gobject.GObject):
    __gsignals__ = {
            'error': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING,))
    }
    def __init__(self, name, type, port, txt = "", domain = "", host = ""):
        self.__gobject_init__()
        self._name = name
        self._type = type
        self._port = port
        self._txt = txt
        self._domain = ""
        self._host = ""
        self._group = None
        self._rename_count = 12 # Counter so we only rename after collisions a sensible number of times

        self._bus = dbus.SystemBus()

        self._server = dbus.Interface(
                self._bus.get_object( avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER ),
                avahi.DBUS_INTERFACE_SERVER )

        self._server.connect_to_signal( 'StateChanged', self._server_state_changed )
        print "calling GetState"
        self._server_state_changed( self._server.GetState() )

    def __del__(self):
        if not self._group is None:
            self._group.Free()

    def _add_service(self):
        if self._group is None:
            self._group = dbus.Interface(
                    self._bus.get_object( avahi.DBUS_NAME, self._server.EntryGroupNew()),
                    avahi.DBUS_INTERFACE_ENTRY_GROUP)
            self._group.connect_to_signal('StateChanged', self._entry_group_state_changed)

        print "Adding service '%s' of type '%s' ..." % (self._name, self._type)

        self._group.AddService(
                avahi.IF_UNSPEC,    #interface
                avahi.PROTO_UNSPEC, #protocol
                0,                  #flags
                self._name, self._type,
                self._domain, self._host,
                dbus.UInt16(self._port),
                avahi.string_array_to_txt_array(self._txt))
        self._group.Commit()

    def _remove_service(self):
        if not self._group is None:
            self._group.Reset()

    def _server_state_changed(self, state):
        if state == avahi.SERVER_COLLISION:
            print "WARNING: Server name collision"
            self._remove_service()
        elif state == avahi.SERVER_RUNNING:
            self._add_service()

    def _entry_group_state_changed(self, state, error):
        print "state change: %i" % state

        if state == avahi.ENTRY_GROUP_ESTABLISHED:
            print "Service established."
        elif state == avahi.ENTRY_GROUP_COLLISION:
            self._rename_count = self._rename_count - 1
            if rename_count > 0:
                name = server.GetAlternativeServiceName(name)
                print "WARNING: Service name collision, changing name to '%s' ..." % name
                self._remove_service()
                self._add_service()

            else:
                print "ERROR: No suitable service name found after %i retries" % n_rename
                self.emit("error", "Service name collision failure")
                self._remove_service()
        elif state == avahi.ENTRY_GROUP_FAILURE:
            print "Error in group state changed", error
            return

class ServiceBrowser(gobject.GObject):
    __gsignals__ = {
            'service-found': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING, gobject.TYPE_STRING)),
            'service-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING, gobject.TYPE_STRING))
    }

    def __init__(self, type, domain = ""):
        self.__gobject_init__()
        bus = dbus.SystemBus()<commit_msg>Use an alternate name if service name is taken locally.<commit_after>import dbus
import gobject
import avahi
import threading
import sys
from dbus.mainloop.glib import DBusGMainLoop

DBusGMainLoop( set_as_default=True )

"""
Class for publishing a service on DNS-SD using Avahi.
"""
class ServicePublisher (gobject.GObject):
    __gsignals__ = {
            'error': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING,))
    }
    def __init__(self, name, type, port, txt = "", domain = "", host = ""):
        self.__gobject_init__()
        self._name = name
        self._type = type
        self._port = port
        self._txt = txt
        self._domain = ""
        self._host = ""
        self._group = None
        self._rename_count = 12 # Counter so we only rename after collisions a sensible number of times

        self._bus = dbus.SystemBus()

        self._server = dbus.Interface(
                self._bus.get_object( avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER ),
                avahi.DBUS_INTERFACE_SERVER )

        self._server.connect_to_signal( 'StateChanged', self._server_state_changed )
        print "calling GetState"
        self._server_state_changed( self._server.GetState() )

    def __del__(self):
        if not self._group is None:
            self._group.Free()

    def _add_service(self):
        if self._group is None:
            self._group = dbus.Interface(
                    self._bus.get_object( avahi.DBUS_NAME, self._server.EntryGroupNew()),
                    avahi.DBUS_INTERFACE_ENTRY_GROUP)
            self._group.connect_to_signal('StateChanged', self._entry_group_state_changed)

        print "Adding service '%s' of type '%s' ..." % (self._name, self._type)

        while self._rename_count > 0:
            try:
                self._group.AddService(
                        avahi.IF_UNSPEC,    #interface
                        avahi.PROTO_UNSPEC, #protocol
                        0,                  #flags
                        self._name, self._type,
                        self._domain, self._host,
                        dbus.UInt16(self._port),
                        avahi.string_array_to_txt_array(self._txt))
                self._group.Commit()
                break
            except dbus.exceptions.DBusException, e:
                if e.get_dbus_name() == "org.freedesktop.Avahi.CollisionError":
                    self._name = self._server.GetAlternativeServiceName(self._name)
                    print "WARNING: Service name collision, changing name to '%s' ..." % self._name
                else:
                    throw(e)
                    break

    def _remove_service(self):
        if not self._group is None:
            self._group.Reset()

    def _server_state_changed(self, state):
        if state == avahi.SERVER_COLLISION:
            print "WARNING: Server name collision"
            self._remove_service()
        elif state == avahi.SERVER_RUNNING:
            self._add_service()

    def _entry_group_state_changed(self, state, error):
        print "state change: %i" % state

        if state == avahi.ENTRY_GROUP_ESTABLISHED:
            print "Service established."
        elif state == avahi.ENTRY_GROUP_COLLISION:
            self._rename_count = self._rename_count - 1
            if rename_count > 0:
                self._name = name = self._server.GetAlternativeServiceName(self._name)
                print "WARNING: Service name collision, changing name to '%s' ..." % self._name
                self._remove_service()
                self._add_service()

            else:
                print "ERROR: No suitable service name found after %i retries" % n_rename
                self.emit("error", "Service name collision failure")
                self._remove_service()
        elif state == avahi.ENTRY_GROUP_FAILURE:
            print "Error in group state changed", error
            return

class ServiceBrowser(gobject.GObject):
    __gsignals__ = {
            'service-found': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING, gobject.TYPE_STRING)),
            'service-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,(gobject.TYPE_STRING, gobject.TYPE_STRING))
    }

    def __init__(self, type, domain = ""):
        self.__gobject_init__()
        bus = dbus.SystemBus()
{'fraction_non_alphanumeric': 0.09447746409113422, 'fraction_numerical': 0.0018573551263001485, 'mean_word_length': 2.042184557438795, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7084903', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2244, 'n_words': 509}