]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-browse.in
* correct the error handling in EntryGroup
[catta] / avahi-utils / avahi-browse.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, avahi.ServiceTypeDatabase
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] <type>\n" % sys.argv[0]
37     print "   -h --help      Show this help"
38     print "   -d --domain    The domain to browse"
39     print "   -a --all       Show all services, regardless of the type"
40     sys.exit(retval)
41
42 try:
43     opts, args = getopt.getopt(sys.argv[1:], "hd:a", ["help", "domain=", "all"])
44 except getopt.GetoptError:
45     usage(2)
46
47 domain = None
48 stype = None
49 show_all = False
50     
51 for o, a in opts:
52     if o in ("-h", "--help"):
53         usage()
54
55     if o in ("-d", "--domain"):
56         domain = a
57
58     if o in ("-a", "--all"):
59         show_all = True
60
61 if len(args) > 1:
62     usage(2)
63
64 if len(args) >= 1:
65     stype = args[0];
66
67 if stype is None and not show_all:
68     usage(2)
69
70 service_type_browsers = {}
71 service_browsers = {}
72
73 service_type_db = avahi.ServiceTypeDatabase.ServiceTypeDatabase()
74
75 def siocgifname(interface):
76     global server
77     
78     if interface <= 0:
79         return "any"
80     else:
81         return server.GetNetworkInterfaceNameByIndex(interface)
82
83 def lookup_service_type(stype):
84     global service_type_db
85
86     try:
87         return service_type_db[stype]
88     except KeyError:
89         return "n/a"
90
91 def service_resolved(interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
92     print "Service data for service '%s' of type '%s' (%s) in domain '%s' on %s.%i:" % (name, stype, lookup_service_type(stype), domain, siocgifname(interface), protocol)
93     print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, avahi.txt_array_to_string_array(txt))
94
95 def print_error(err):
96     print "Error:", str(err)
97
98 def new_service(interface, protocol, name, stype, domain, flags):
99     global server
100     
101     print "Found service '%s' of type '%s' (%s) in domain '%s' on %s.%i." % (name, stype, lookup_service_type(stype), domain, siocgifname(interface), protocol)
102
103     # Asynchronous resolving
104     server.ResolveService(interface, protocol, name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=service_resolved, error_handler=print_error)
105
106 def remove_service(interface, protocol, name, stype, domain, flags):
107     print "Service '%s' of type '%s' (%s) in domain '%s' on %s.%i disappeared." % (name, stype, lookup_service_type(stype), domain, siocgifname(interface), protocol)
108  
109 def new_service_type(interface, protocol, stype, domain, flags):
110     global server, service_browsers
111
112     # Are we already browsing this domain for this type? 
113     if service_browsers.has_key((interface, protocol, stype, domain)):
114         return
115
116     print "Browsing for services of type '%s' (%s) in domain '%s' on %s.%i ..." % (stype, lookup_service_type(stype), domain, siocgifname(interface), protocol)
117     
118     b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, stype, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
119     b.connect_to_signal('ItemNew', new_service)
120     b.connect_to_signal('ItemRemove', remove_service)
121
122     service_browsers[(interface, protocol, stype, domain)] = b
123
124 def browse_domain(interface, protocol, domain):
125     global server, service_type_browsers, stype
126
127     # Are we already browsing this domain?
128     if service_type_browsers.has_key((interface, protocol, domain)):
129         return
130
131     if stype is None:
132         print "Browsing domain '%s' on %s.%i ..." % (domain, siocgifname(interface), protocol)
133
134         b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain, dbus.UInt32(0))), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
135         b.connect_to_signal('ItemNew', new_service_type)
136
137         service_type_browsers[(interface, protocol, domain)] = b
138     else:
139         new_service_type(interface, protocol, stype, domain, dbus.UInt32(0))
140
141 def new_domain(interface, protocol, domain, flags):
142
143     # We browse for .local anyway...
144     if domain != "local":
145         browse_domain(interface, protocol, domain)
146
147
148 bus = dbus.SystemBus()
149 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
150
151 if domain is None:
152     # Explicitly browse .local
153     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
154
155     # Browse for other browsable domains
156     db = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
157     db.connect_to_signal('ItemNew', new_domain)
158
159 else:
160     # Just browse the domain the user wants us to browse
161     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
162
163 try:
164     gobject.MainLoop().run()
165 except KeyboardInterrupt, k:
166     pass