def __init__(self, listen_on, service_name, service_type=XPRA_MDNS_TYPE, text_dict={}): self.publishers = [] try: bus = init_system_bus() except Exception as e: log.warn("failed to connect to the system dbus: %s", e) log.warn(" either start a dbus session or disable mdns support") return for host, port in listen_on: iface_index = get_interface_index(host) log("iface_index(%s)=%s", host, iface_index) td = text_dict if SHOW_INTERFACE and if_indextoname and iface_index is not None: td = text_dict.copy() td["iface"] = if_indextoname(iface_index) txt = [] if text_dict: for k,v in text_dict.items(): txt.append("%s=%s" % (k,v)) if host=="0.0.0.0": host = "" else: try: import socket host = socket.gethostbyaddr(host)[0] except: pass self.publishers.append(AvahiPublisher(bus, service_name, port, service_type, domain="", host=host, text=txt, interface=iface_index))
def main(): import glib import random, signal port = int(20000*random.random())+10000 host = "" name = "test service" bus = init_system_bus() publisher = AvahiPublisher(bus, name, port, stype=XPRA_MDNS_TYPE, host=host, text=["somename:somevalue"]) assert publisher glib.idle_add(publisher.start) signal.signal(signal.SIGTERM, exit) glib.MainLoop().run()
def __init__(self, service_type, mdns_found=None, mdns_add=None, mdns_remove=None): log("AvahiListener%s", (service_type, mdns_found, mdns_add, mdns_remove)) try: self.bus = init_system_bus() assert self.bus except Exception as e: log.warn("failed to connect to the system dbus: %s", e) log.warn(" either start a dbus session or disable mdns support") return self.sdref = None self.readers = [] self.resolvers = [] self.service_type = service_type self.mdns_found = mdns_found self.mdns_add = mdns_add self.mdns_remove = mdns_remove self.server = None
def setup_dbus_signals(self): try: import xpra.dbus assert xpra.dbus except ImportError as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.info("dbus support is not installed") dbuslog.info(" no support for power events") return try: from xpra.dbus.common import init_system_bus bus = init_system_bus() self.system_bus = bus dbuslog("setup_dbus_signals() system bus=%s", bus) except Exception as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.error("Error setting up dbus signals:") dbuslog.error(" %s", e) return #the UPower signals: try: bus_name = 'org.freedesktop.UPower' dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name)) iface_name = 'org.freedesktop.UPower' self.upower_resuming_match = bus.add_signal_receiver(self.resuming_callback, 'Resuming', iface_name, bus_name) self.upower_sleeping_match = bus.add_signal_receiver(self.sleeping_callback, 'Sleeping', iface_name, bus_name) dbuslog("listening for 'Resuming' and 'Sleeping' signals on %s", iface_name) except Exception as e: dbuslog("failed to setup UPower event listener: %s", e) #the "logind" signals: try: bus_name = 'org.freedesktop.login1' dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name)) def sleep_event_handler(suspend): if suspend: self.sleeping_callback() else: self.resuming_callback() iface_name = 'org.freedesktop.login1.Manager' self.login1_match = bus.add_signal_receiver(sleep_event_handler, 'PrepareForSleep', iface_name, bus_name) dbuslog("listening for 'PrepareForSleep' signal on %s", iface_name) except Exception as e: dbuslog("failed to setup login1 event listener: %s", e)
def init_dbus_listener(): if not CUPS_DBUS: return False global dbus_init log("init_dbus_listener() dbus_init=%s", dbus_init) if dbus_init is None: try: from xpra.dbus.common import init_system_bus system_bus = init_system_bus() log("system bus: %s", system_bus) sig_match = system_bus.add_signal_receiver(handle_dbus_signal, path=DBUS_PATH, dbus_interface=DBUS_IFACE) log("system_bus.add_signal_receiver(..)=%s", sig_match) dbus_init = True except Exception: if sys.platform.startswith("darwin"): log("no dbus on osx") else: log.error("failed to initialize dbus cups event listener", exc_info=True) dbus_init = False return dbus_init
def setup_dbus_signals(self): try: import xpra.dbus assert xpra.dbus except ImportError as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.info("dbus support is not installed") dbuslog.info(" no support for power events") return try: from xpra.dbus.common import init_system_bus, init_session_bus except ImportError as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.error("Error: dbus bindings are missing,") dbuslog.error(" cannot setup event listeners:") dbuslog.error(" %s", e) return try: bus = init_system_bus() self.system_bus = bus dbuslog("setup_dbus_signals() system bus=%s", bus) except Exception as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.error("Error setting up dbus signals:") dbuslog.error(" %s", e) else: #the UPower signals: try: bus_name = 'org.freedesktop.UPower' dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name)) iface_name = 'org.freedesktop.UPower' self.upower_resuming_match = bus.add_signal_receiver( self.resuming_callback, 'Resuming', iface_name, bus_name) self.upower_sleeping_match = bus.add_signal_receiver( self.sleeping_callback, 'Sleeping', iface_name, bus_name) dbuslog( "listening for 'Resuming' and 'Sleeping' signals on %s", iface_name) except Exception as e: dbuslog("failed to setup UPower event listener: %s", e) #the "logind" signals: try: bus_name = 'org.freedesktop.login1' dbuslog("bus has owner(%s)=%s", bus_name, bus.name_has_owner(bus_name)) def sleep_event_handler(suspend): if suspend: self.sleeping_callback() else: self.resuming_callback() iface_name = 'org.freedesktop.login1.Manager' self.login1_match = bus.add_signal_receiver( sleep_event_handler, 'PrepareForSleep', iface_name, bus_name) dbuslog("listening for 'PrepareForSleep' signal on %s", iface_name) except Exception as e: dbuslog("failed to setup login1 event listener: %s", e) if DBUS_SCREENSAVER: try: session_bus = init_session_bus() self.session_bus = session_bus dbuslog("setup_dbus_signals() session bus=%s", session_bus) except Exception as e: dbuslog("setup_dbus_signals()", exc_info=True) dbuslog.error("Error setting up dbus signals:") dbuslog.error(" %s", e) else: #screensaver signals: try: bus_name = "org.gnome.ScreenSaver" iface_name = bus_name self.screensaver_match = bus.add_signal_receiver( self.ActiveChanged, "ActiveChanged", iface_name, bus_name) dbuslog("listening for 'ActiveChanged' signal on %s", iface_name) except Exception as e: dbuslog.warn( "Warning: failed to setup screensaver event listener: %s", e)
def __init__(self, server=None): if os.getuid() == 0: bus = init_system_bus() else: bus = init_session_bus() DBUS_Server_Base.__init__(self, bus, server, BUS_NAME)