]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-dump-all.in
* Show some messages when pygtk or twisted is missing for the execution of the python...
[catta] / avahi-utils / avahi-dump-all.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
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 service_type_browsers = {}
36 service_browsers = {}
37
38 def siocgifname(interface):
39     global server
40     
41     if interface <= 0:
42         return "any"
43     else:
44         return server.GetNetworkInterfaceNameByIndex(interface)
45
46 def service_resolved(interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
47     print "Service data for service '%s' of type '%s' in domain '%s' on %s.%i:" % (name, type, domain, siocgifname(interface), protocol)
48     print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
49
50 def print_error(err):
51     print "Error:", str(err)
52
53 def new_service(interface, protocol, name, type, domain):
54     global server
55     
56     print "Found service '%s' of type '%s' in domain '%s' on %s.%i." % (name, type, domain, siocgifname(interface), protocol)
57
58     # Asynchronous resolving
59     server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC, reply_handler=service_resolved, error_handler=print_error)
60
61 def remove_service(interface, protocol, name, type, domain):
62     print "Service '%s' of type '%s' in domain '%s' on %s.%i disappeared." % (name, type, domain, siocgifname(interface), protocol)
63  
64 def new_service_type(interface, protocol, type, domain):
65     global server, service_browsers
66
67     # Are we already browsing this domain for this type? 
68     if service_browsers.has_key((interface, protocol, type, domain)):
69         return
70
71     print "Browsing for services of type '%s' in domain '%s' on %s.%i ..." % (type, domain, siocgifname(interface), protocol)
72     
73     b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceBrowserNew(interface, protocol, type, domain)), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
74     b.connect_to_signal('ItemNew', new_service)
75     b.connect_to_signal('ItemRemove', remove_service)
76
77     service_browsers[(interface, protocol, type, domain)] = b
78
79 def browse_domain(interface, protocol, domain):
80     global server, service_type_browsers
81
82     # Are we already browsing this domain?
83     if service_type_browsers.has_key((interface, protocol, domain)):
84         return
85
86     print "Browsing domain '%s' on %s.%i ..." % (domain, siocgifname(interface), protocol)
87     
88     b = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.ServiceTypeBrowserNew(interface, protocol, domain)), avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
89     b.connect_to_signal('ItemNew', new_service_type)
90
91     service_type_browsers[(interface, protocol, domain)] = b
92
93 def new_domain(interface, protocol, domain):
94
95     # We browse for .local anyway...
96     if domain != "local":
97         browse_domain(interface, protocol, domain)
98
99
100 domain = None
101
102 if len(sys.argv) > 1:
103     domain = sys.argv[1]
104         
105 bus = dbus.SystemBus()
106 server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
107
108 if domain is None:
109     # Explicitly browse .local
110     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
111
112     # Browse for other browsable domains
113     db = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
114     db.connect_to_signal('ItemNew', new_domain)
115
116 else:
117     # Just browse the domain the user wants us to browse
118     browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
119
120 try:
121     gobject.MainLoop().run()
122 except KeyboardInterrupt, k:
123     pass