]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-publish-service.in
* Show some messages when pygtk or twisted is missing for the execution of the python...
[catta] / avahi-utils / avahi-publish-service.in
1 #!@PYTHON@
2 # -*-python-*-
3 # $Id$
4
5 # This file is part of avahi.
6 #
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.
11 #
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.
16 #
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
20 # USA.
21
22 import sys, getopt
23
24 try:
25     import avahi, gobject, dbus
26 except ImportError:
27     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
28     sys.exit(1)
29
30 try:
31     import dbus.glib
32 except ImportError, e:
33     pass
34
35 def usage(retval = 0):
36     print "%s [options] <name> <type> <port> [<txt> ...]\n" % sys.argv[0]
37     print "   -h --help      Show this help"
38     print "   -d --domain    Domain where to register this service"
39     print "   -H --host      Host where this service resides"
40     sys.exit(retval)
41
42 try:
43     opts, args = getopt.getopt(sys.argv[1:], "d:H:", ["help", "domain=", "host="])
44 except getopt.GetoptError:
45     pass
46
47 domain = ""
48 host = ""
49     
50 for o, a in opts:
51     if o in ("-h", "--help"):
52         usage()
53
54     if o in ("-d", "--domain"):
55         domain = a
56
57     if o in ("-H", "--host"):
58         host = a
59
60 if len(args) < 3:
61     usage(2)
62
63 name = args[0]
64 stype = args[1]
65 port = int(args[2])
66 txt = args[3:]
67
68 # python-dbus doesn't allow transmission of empty arrays, therefore we "fix" it with a bogus entry
69 if len(txt) == 0:
70     txt.append("python-dbus=brain-damage")
71
72 group = None
73 n_rename = 0
74
75 def remove_service():
76     global group
77
78     if not group is None:
79         group.Reset()
80
81 def add_service():
82     global server, group, name, stype, domain, host, port, txt
83
84     if group is None:
85         group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
86
87     assert group.IsEmpty()
88
89     print "Adding service '%s' of type '%s' ..." % (name, stype)
90     group.connect_to_signal('StateChanged', entry_group_state_changed)
91     group.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, name, stype, domain, host, dbus.UInt16(port), txt)
92     group.Commit()
93
94 def entry_group_state_changed(state):
95     global name, server, n_rename
96     
97     if state == avahi.ENTRY_GROUP_ESTABLISHED:
98         print "Service established."
99     elif state == avahi.ENTRY_GROUP_COLLISION:
100
101         n_rename = n_rename + 1
102         if n_rename >= 12:
103             print "ERROR: No suitable service name found after %i retries, exiting." % n_rename
104             main_loop.quit()
105         else:
106             name = server.GetAlternativeServiceName(name)
107             print "WARNING: Service name collision, changing name to '%s' ..." % name
108             remove_service()
109             add_service()
110
111 def server_state_changed(state):
112     if state == avahi.SERVER_COLLISION:
113         print "WARNING: Server name collision"
114         remove_service()
115     elif state == avahi.SERVER_RUNNING:
116         add_service()
117
118 main_loop = gobject.MainLoop()
119
120 bus = dbus.SystemBus()
121 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
122 server.connect_to_signal("StateChanged", server_state_changed)
123 server_state_changed(server.GetState())
124
125 try:
126     main_loop.run()
127 except KeyboardInterrupt, k:
128     pass
129
130 if not group is None:
131     group.Free()