]> git.meshlink.io Git - catta/blob - avahi-python/avahi-discover.in
34bb4ce930a1e818724a926b9e609593f0d41300
[catta] / avahi-python / avahi-discover.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 os, sys
23
24 try:
25     import avahi, gtk, gobject, dbus, avahi.ServiceTypeDatabase
26     from avahi.SimpleGladeApp import SimpleGladeApp
27 except ImportError:
28     print "Sorry, to use this tool you need to install Avahi, pygtk and python-dbus."
29     sys.exit(1)
30
31 try:
32     import dbus.glib
33 except ImportError, e:
34     pass
35
36 service_type_browsers = {}
37 service_browsers = {}
38
39 glade_dir = "@interfacesdir@"
40
41 service_type_db = avahi.ServiceTypeDatabase.ServiceTypeDatabase()
42
43 class Main_window(SimpleGladeApp):
44     def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
45         path = os.path.join(glade_dir, path)
46         gtk.window_set_default_icon_name("gnome-networktool")
47         SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
48
49     def on_tree_view_cursor_changed(self, widget, *args):
50         (model, iter) = widget.get_selection().get_selected()
51         (name,interface,protocol,stype,domain) = self.treemodel.get(iter,1,2,3,4,5)
52         if stype == None:
53             self.info_label.set_markup("<i>No service currently selected.</i>")
54             return
55         #Asynchronous resolving
56         self.server.ResolveService( int(interface), int(protocol), name, stype, domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), reply_handler=self.service_resolved, error_handler=self.print_error)
57
58     def protoname(self,protocol):
59         if protocol == avahi.PROTO_INET:
60             return "IPv4"
61         if protocol == avahi.PROTO_INET6:
62             return "IPv6"
63         return "n/a"
64             
65     def siocgifname(self, interface):
66         if interface <= 0:
67             return "n/a"
68         else:
69             return self.server.GetNetworkInterfaceNameByIndex(interface)
70
71     def get_interface_name(self, interface, protocol):
72         if interface == avahi.IF_UNSPEC and protocol == avahi.PROTO_UNSPEC:
73             return "Wide Area"
74         else:
75             return str(self.siocgifname(interface)) + " " + str(self.protoname(protocol))
76                         
77     def service_resolved(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
78         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, stype, domain, interface, protocol)
79
80         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
81
82         self.update_label(interface, protocol, name, stype, domain, host, aprotocol, address, port, avahi.txt_array_to_string_array(txt))
83         
84     def print_error(self, err):
85         error_label = "<b>Error:</b> %s" % (err)
86         self.info_label.set_markup(error_label)
87         print "Error:", str(err)
88
89     def lookup_type(self, stype):
90         global service_type_db
91
92         try:
93             return service_type_db[stype]
94         except KeyError:
95             return stype
96             
97     def new_service(self, interface, protocol, name, stype, domain, flags):
98         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, stype, domain, interface, protocol)
99         if self.zc_ifaces.has_key((interface,protocol)) == False:
100
101             ifn = self.get_interface_name(interface, protocol)
102             
103             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn, None,interface,protocol,None,domain)
104         if self.zc_domains.has_key((interface,protocol,domain)) == False:
105             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
106         if self.zc_types.has_key((interface,protocol,stype,domain)) == False:
107             thisDomain = self.zc_domains[(interface,protocol,domain)]
108             self.zc_types[(interface,protocol,stype,domain)] = self.insert_row(self.treemodel, thisDomain, self.lookup_type(stype), name, interface,None,None,None)
109         treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,stype,domain)], name, name, interface,protocol,stype,domain)
110         self.services_browsed[(interface, protocol, name, stype, domain)] = treeiter
111         # expand the tree of this path
112         self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
113
114     def remove_service(self, interface, protocol, name, stype, domain, flags):
115         print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, stype, domain, interface, protocol)
116         self.info_label.set_markup("")
117         treeiter=self.services_browsed[(interface, protocol, name, stype, domain)]
118         parent = self.treemodel.iter_parent(treeiter)
119         self.treemodel.remove(treeiter)
120         del self.services_browsed[(interface, protocol, name, stype, domain)]
121         if self.treemodel.iter_has_child(parent) == False:
122             treeiter=self.zc_types[(interface,protocol,stype,domain)]
123             parent = self.treemodel.iter_parent(treeiter)
124             self.treemodel.remove(treeiter)
125             del self.zc_types[(interface,protocol,stype,domain)]
126             if self.treemodel.iter_has_child(parent) == False:
127                 treeiter=self.zc_domains[(interface,protocol,domain)]
128                 parent = self.treemodel.iter_parent(treeiter)
129                 self.treemodel.remove(treeiter)
130                 del self.zc_domains[(interface,protocol,domain)]
131                 if self.treemodel.iter_has_child(parent) == False:
132                     treeiter=self.zc_ifaces[(interface,protocol)]
133                     parent = self.treemodel.iter_parent(treeiter)
134                     self.treemodel.remove(treeiter)
135                     del self.zc_ifaces[(interface,protocol)]
136  
137     def new_service_type(self, interface, protocol, stype, domain, flags):
138         global service_browsers
139
140         # Are we already browsing this domain for this type? 
141         if service_browsers.has_key((interface, protocol, stype, domain)):
142             return
143         
144         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
145         
146         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, stype, domain, dbus.UInt32(0))),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
147         b.connect_to_signal('ItemNew', self.new_service)
148         b.connect_to_signal('ItemRemove', self.remove_service)
149
150         service_browsers[(interface, protocol, stype, domain)] = b
151
152     def browse_domain(self, interface, protocol, domain):
153         global service_type_browsers
154
155         # Are we already browsing this domain?
156         if service_type_browsers.has_key((interface, protocol, domain)):
157             return
158
159         if self.stype is None:
160             print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
161         
162             b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain, dbus.UInt32(0))),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
163             b.connect_to_signal('ItemNew', self.new_service_type)
164
165             service_type_browsers[(interface, protocol, domain)] = b
166         else:
167             new_service_type(interface, protocol, stype, domain)
168
169     def new_domain(self,interface, protocol, domain, flags):
170         if self.zc_ifaces.has_key((interface,protocol)) == False:
171             ifn = self.get_interface_name(interface, protocol)
172             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, ifn,None,interface,protocol,None,domain)
173         if self.zc_domains.has_key((interface,protocol,domain)) == False:
174             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
175         if domain != "local":
176             self.browse_domain(interface, protocol, domain)
177
178     def pair_to_dict(self, l):
179         res = dict()
180         for el in l:
181             if "=" not in el:
182                 res[el]=''
183             else:
184                 tmp = el.split('=',1)
185                 if len(tmp[0]) > 0:
186                     res[tmp[0]] = tmp[1]
187         return res
188                                                                             
189
190     def update_label(self,interface, protocol, name, stype, domain, host, aprotocol, address, port, txt):
191         if len(txt) != 0:
192             txts = ""
193             txtd = self.pair_to_dict(txt)
194             for k,v in txtd.items():
195                 txts+="<b>TXT <i>%s</i></b> = %s\n" % (k,v)
196         else:
197             txts = "<b>TXT Data:</b> <i>empty</i>"
198
199         infos = "<b>Service Type:</b> %s\n<b>Service Name:</b> %s\n<b>Domain Name:</b> %s\n<b>Interface:</b> %s %s\n<b>Address:</b> %s/%s:%i\n%s" % (stype, name, domain, self.siocgifname(interface), self.protoname(protocol), host, address, port, txts.strip())
200         self.info_label.set_markup(infos)
201
202     def insert_row(self, model,parent,
203                    content, name, interface,protocol,stype,domain):
204         myiter=model.insert_after(parent,None)
205         model.set(myiter,0,content,1,name,2,interface,3,protocol,4,stype,5,domain)
206         return myiter
207
208     def new(self):
209         print "A new main_window has been created"
210         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
211         self.tree_view.set_model(self.treemodel)
212
213         #creating the columns headers
214         self.tree_view.set_headers_visible(False)
215         renderer=gtk.CellRendererText()
216         column=gtk.TreeViewColumn("",renderer, text=0)
217         column.set_resizable(True)
218         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
219         column.set_expand(True);
220         self.tree_view.append_column(column)
221
222         self.domain = None
223         self.stype = None
224         self.zc_ifaces = {}
225         self.zc_domains = {}
226         self.zc_types = {}
227         self.services_browsed = {}
228         
229         self.bus = dbus.SystemBus()
230         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
231
232         if self.domain is None:
233             # Explicitly browse .local
234             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
235                         
236             # Browse for other browsable domains
237             db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE, dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
238             db.connect_to_signal('ItemNew', self.new_domain)
239         else:
240             # Just browse the domain the user wants us to browse
241             self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
242
243         
244 def main():
245     gtk.window_set_default_icon_name("gnome-networktool")
246     main_window = Main_window()
247
248     main_window.run()
249     
250 if __name__ == "__main__":
251     main()
252
253