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