]> git.meshlink.io Git - catta/blob - avahi-python/avahi-publish-address.in
move python tools from avahi-utils to avahi-python
[catta] / avahi-python / avahi-publish-address.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, string
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 <name> <address>" % sys.argv[0]
37     sys.exit(retval)
38
39 if len(sys.argv) != 3:
40     usage(2)
41
42 name = sys.argv[1]
43 address = sys.argv[2]
44
45 group = None
46 n_rename = 0
47
48 def remove_address():
49     global group
50     
51     if not (group is None):
52         group.Free()
53         group = None
54
55 def add_address():
56     global server, group, name, address
57     assert group is None
58
59     print "Adding address '%s' for '%s' ..." % (name, address)
60     group = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
61     group.connect_to_signal('StateChanged', entry_group_state_changed)
62     group.AddAddress(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), name, address)
63     group.Commit()
64
65 def entry_group_state_changed(state):
66     global name, server, n_rename, main_loop
67     
68     if state == avahi.ENTRY_GROUP_ESTABLISHED:
69         print "Address established."
70     elif state == avahi.ENTRY_GROUP_COLLISION:
71
72         n_rename = n_rename + 1
73         if n_rename >= 12:
74             print "ERROR: No suitable name found after %i retries, exiting." % n_rename
75             main_loop.quit()
76         else:
77             hn = name.split('.')
78             hn[0] = server.GetAlternativeHostName(hn[0])
79             name = string.join(hn, '.')
80             print "WARNING: Address/host name collision, changing name to '%s' ..." % name
81             remove_address()
82             add_address()
83
84 main_loop = gobject.MainLoop()
85
86 bus = dbus.SystemBus()
87 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
88
89 add_address()
90
91 try:
92     main_loop.run()
93 except KeyboardInterrupt, k:
94     pass
95
96 remove_address()