* fix Server introspection data
[platform/upstream/avahi.git] / avahi-utils / avahi-publish-service
1 #!/usr/bin/python2.4
2 # -*-python-*-
3 # $Id$
4
5 import avahi, dbus, gobject, sys, getopt
6
7 try:
8     import dbus.glib
9 except ImportError, e:
10     pass
11
12 def usage(retval = 0):
13     print "%s [options] <name> <type> <port> [<txt> ...]\n" % sys.argv[0]
14     print "   -h --help      Show this help"
15     print "   -d --domain    Domain where to register this service"
16     print "   -H --host      Host where this service resides"
17     sys.exit(retval)
18
19 try:
20     opts, args = getopt.getopt(sys.argv[1:], "d:H:", ["help", "domain=", "host="])
21 except getopt.GetoptError:
22     usage(2)
23
24 domain = ""
25 host = ""
26     
27 for o, a in opts:
28     if o in ("-h", "--help"):
29         usage()
30
31     if o in ("-d", "--domain"):
32         domain = a
33
34     if o in ("-H", "--host"):
35         host = a
36
37 if len(args) < 3:
38     sys.stderr.write("Invalid number of arguments\n")
39     sys.exit(1)
40
41 name = args[0]
42 stype = args[1]
43 port = int(args[2])
44 txt = args[3:]
45
46 # python-dbus doesn't allow transmission of empty arrays, therefore we "fix" it with a bogus entry
47 if len(txt) == 0:
48     txt.append("python-dbus=brain-damage")
49
50 group = None
51 n_rename = 0
52
53 def remove_service():
54     global group
55
56     if not (group is None):
57         group.Free()
58         group = None
59
60 def add_service():
61     global server, group, name, stype, domain, host, port, txt
62     assert group is None
63
64     print "Adding service '%s' of type '%s' ..." % (name, stype)
65     group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
66     group.connect_to_signal('StateChanged', entry_group_state_changed)
67     group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, stype, domain, host, dbus.UInt16(port), txt)
68     group.Commit()
69
70 def entry_group_state_changed(state):
71     global name, server, n_rename
72     
73     if state == avahi.ENTRY_GROUP_ESTABLISHED:
74         print "Service established."
75     elif state == avahi.ENTRY_GROUP_COLLISION:
76
77         n_rename = n_rename + 1
78         if n_rename >= 12:
79             print "ERROR: No suitable service name found after %i retries, exiting." % n_rename
80             main_loop.quit()
81         else:
82             name = server.GetAlternativeServiceName(name)
83             print "WARNING: Service name collision, changing name to '%s' ..." % name
84             remove_service()
85             add_service()
86
87 def server_state_changed(state):
88     if state == avahi.SERVER_COLLISION:
89         print "WARNING: Server name collision"
90         remove_service()
91     elif state == avahi.SERVER_RUNNING:
92         add_service()
93
94 main_loop = gobject.MainLoop()
95
96 bus = dbus.SystemBus()
97 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
98 server.connect_to_signal("StateChanged", server_state_changed)
99 server_state_changed(server.GetState())
100
101 try:
102     main_loop.run()
103 except KeyboardInterrupt, k:
104     pass
105
106 remove_service()