1 #!/usr/bin/env @PYTHON@
5 # This file is part of avahi.
7 # avahi is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation; either version 2 of the
10 # License, or (at your option) any later version.
12 # avahi is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 # License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with avahi; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 import avahi, dbus, gobject, sys, getopt
26 except ImportError, e:
29 def usage(retval = 0):
30 print "%s [options] <name> <type> <port> [<txt> ...]\n" % sys.argv[0]
31 print " -h --help Show this help"
32 print " -d --domain Domain where to register this service"
33 print " -H --host Host where this service resides"
37 opts, args = getopt.getopt(sys.argv[1:], "d:H:", ["help", "domain=", "host="])
38 except getopt.GetoptError:
45 if o in ("-h", "--help"):
48 if o in ("-d", "--domain"):
51 if o in ("-H", "--host"):
55 sys.stderr.write("Invalid number of arguments\n")
63 # python-dbus doesn't allow transmission of empty arrays, therefore we "fix" it with a bogus entry
65 txt.append("python-dbus=brain-damage")
73 if not (group is None):
78 global server, group, name, stype, domain, host, port, txt
81 print "Adding service '%s' of type '%s' ..." % (name, stype)
82 group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
83 group.connect_to_signal('StateChanged', entry_group_state_changed)
84 group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, stype, domain, host, dbus.UInt16(port), txt)
87 def entry_group_state_changed(state):
88 global name, server, n_rename
90 if state == avahi.ENTRY_GROUP_ESTABLISHED:
91 print "Service established."
92 elif state == avahi.ENTRY_GROUP_COLLISION:
94 n_rename = n_rename + 1
96 print "ERROR: No suitable service name found after %i retries, exiting." % n_rename
99 name = server.GetAlternativeServiceName(name)
100 print "WARNING: Service name collision, changing name to '%s' ..." % name
104 def server_state_changed(state):
105 if state == avahi.SERVER_COLLISION:
106 print "WARNING: Server name collision"
108 elif state == avahi.SERVER_RUNNING:
111 main_loop = gobject.MainLoop()
113 bus = dbus.SystemBus()
114 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
115 server.connect_to_signal("StateChanged", server_state_changed)
116 server_state_changed(server.GetState())
120 except KeyboardInterrupt, k: