]> git.meshlink.io Git - catta/blob - avahi-utils/avahi-discover.in
fc1b4765cc53f15f7fdf18d26106dc440cc4d0cf
[catta] / avahi-utils / 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
23 import gtk
24 import gobject
25
26 import avahi, dbus, gobject, sys
27 from avahi.SimpleGladeApp import SimpleGladeApp
28
29 try:
30     import dbus.glib
31 except ImportError, e:
32     pass
33
34 service_type_browsers = {}
35 service_browsers = {}
36
37
38 glade_dir = "@interfacesdir@"
39
40 class Main_window(SimpleGladeApp):
41     def __init__(self, path="avahi-discover.glade", root="main_window", domain=None, **kwargs):
42         path = os.path.join(glade_dir, path)
43         SimpleGladeApp.__init__(self, path, root, domain, **kwargs)
44
45     def on_tree_view_cursor_changed(self, widget, *args):
46         (model, iter) = widget.get_selection().get_selected()
47         (name,interface,protocol,type,domain) = self.treemodel.get(iter,1,2,3,4,5)
48         if type == None:
49             self.info_label.set_markup("<i>No service currently selected.</i>")
50             return
51         #Asynchronous resolving
52         self.server.ResolveService( int(interface), int(protocol), name, type, domain, avahi.PROTO_UNSPEC, reply_handler=self.service_resolved, error_handler=self.print_error)
53
54
55     def protoname(self,protocol):
56         if protocol == avahi.PROTO_INET:
57             return "IPv4"
58         if protocol == avahi.PROTO_INET6:
59             return "IPv6"
60             
61             
62     def siocgifname(self, interface):
63         if interface <= 0:
64             return "any"
65         else:
66             return self.server.GetNetworkInterfaceNameByIndex(interface)
67                         
68     def service_resolved(self, interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
69         print "Service data for service '%s' of type '%s' in domain '%s' on %i.%i:" % (name, type, domain, interface, protocol)
70         print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(txt))
71         self.update_label(interface, protocol, name, type, domain, host, aprotocol, address, port, str(txt))
72         
73     def print_error(err):
74         print "Error:", str(err)
75             
76     def new_service(self, interface, protocol, name, type, domain):
77         print "Found service '%s' of type '%s' in domain '%s' on %i.%i." % (name, type, domain, interface, protocol)
78         if self.zc_types.has_key((interface,protocol,type,domain)) == False:
79             self.zc_types[(interface,protocol,type,domain)] = self.insert_row(self.treemodel,self.zc_domains[(interface,protocol,domain)], type, name, interface,None,None,None)
80         treeiter = self.insert_row(self.treemodel,self.zc_types[(interface,protocol,type,domain)], name, name, interface,protocol,type,domain)
81         self.services_browsed[(interface, protocol, name, type, domain)] = treeiter
82         # expand the tree of this path
83         self.tree_view.expand_to_path(self.treemodel.get_path(treeiter))
84
85
86     def remove_service(self, interface, protocol, name, type, domain):
87         print "Service '%s' of type '%s' in domain '%s' on %i.%i disappeared." % (name, type, domain, interface, protocol)
88         self.info_label.set_markup("")
89         treeiter=self.services_browsed[(interface, protocol, name, type, domain)]
90         parent = self.treemodel.iter_parent(treeiter)
91         self.treemodel.remove(treeiter)
92         del self.services_browsed[(interface, protocol, name, type, domain)]
93         if self.treemodel.iter_has_child(parent) == False:
94             treeiter=self.zc_types[(interface,protocol,type,domain)]
95             parent = self.treemodel.iter_parent(treeiter)
96             self.treemodel.remove(treeiter)
97             del self.zc_types[(interface,protocol,type,domain)]
98             if self.treemodel.iter_has_child(parent) == False:
99                 treeiter=self.zc_domains[(interface,protocol,domain)]
100                 parent = self.treemodel.iter_parent(treeiter)
101                 self.treemodel.remove(treeiter)
102                 del self.zc_domains[(interface,protocol,domain)]
103                 if self.treemodel.iter_has_child(parent) == False:
104                     treeiter=self.zc_ifaces[(interface,protocol)]
105                     parent = self.treemodel.iter_parent(treeiter)
106                     self.treemodel.remove(treeiter)
107                     del self.zc_ifaces[(interface,protocol)]
108
109  
110     def new_service_type(self, interface, protocol, type, domain):
111         global service_browsers
112
113         # Are we already browsing this domain for this type? 
114         if service_browsers.has_key((interface, protocol, type, domain)):
115             return
116         
117         print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (type, domain, interface, protocol)
118         
119         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(interface, protocol, type, domain)),  avahi.DBUS_INTERFACE_SERVICE_BROWSER)
120         b.connect_to_signal('ItemNew', self.new_service)
121         b.connect_to_signal('ItemRemove', self.remove_service)
122
123         service_browsers[(interface, protocol, type, domain)] = b
124
125     def browse_domain(self, interface, protocol, domain):
126         global service_type_browsers
127
128         # Are we already browsing this domain?
129         if service_type_browsers.has_key((interface, protocol, domain)):
130             return
131             
132         print "Browsing domain '%s' on %i.%i ..." % (domain, interface, protocol)
133         
134         b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceTypeBrowserNew(interface, protocol, domain)),  avahi.DBUS_INTERFACE_SERVICE_TYPE_BROWSER)
135         b.connect_to_signal('ItemNew', self.new_service_type)
136
137         service_type_browsers[(interface, protocol, domain)] = b
138
139     def new_domain(self,interface, protocol, domain):
140         self.browse_domain(interface, protocol, domain)
141         if self.zc_ifaces.has_key((interface,protocol)) == False:
142             self.zc_ifaces[(interface,protocol)] = self.insert_row(self.treemodel, None, str(self.siocgifname(interface))+" "+str(self.protoname(protocol)),None,interface,protocol,None,domain)
143         if self.zc_domains.has_key((interface,protocol,domain)) == False:
144             self.zc_domains[(interface,protocol,domain)] = self.insert_row(self.treemodel, self.zc_ifaces[(interface,protocol)], domain,None,interface,protocol,None,domain)
145
146     def update_label(self,interface, protocol, name, type, domain, host, aprotocol, address, port, txt):
147         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<b>TXT Data:</b> %s" % (type, name, domain, self.siocgifname(interface), self.protoname(protocol), host, address, port, str(txt))
148         self.info_label.set_markup(infos)
149
150     def insert_row(self, model,parent,
151                    content, name, interface,protocol,type,domain):
152         myiter=model.insert_after(parent,None)
153         model.set(myiter,0,content,1,name,2,interface,3,protocol,4,type,5,domain)
154         return myiter
155     
156     def new(self):
157         print "A new main_window has been created"
158         self.treemodel=gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING)
159         self.tree_view.set_model(self.treemodel)
160
161         #creating the columns headers
162         self.tree_view.set_headers_visible(False)
163         renderer=gtk.CellRendererText()
164         column=gtk.TreeViewColumn("",renderer, text=0)
165         column.set_resizable(True)
166         column.set_sizing("GTK_TREE_VIEW_COLUMN_GROW_ONLY");
167         column.set_expand(True);
168         self.tree_view.append_column(column)
169
170         # 
171         self.zc_ifaces = {}
172         self.zc_domains = {}
173         self.zc_types = {}
174         self.services_browsed = {}
175         
176         self.bus = dbus.SystemBus()
177         self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
178
179         # browse all domains
180         db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.DomainBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "", avahi.DOMAIN_BROWSER_BROWSE)), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
181         db.connect_to_signal('ItemNew', self.new_domain)
182
183         # Just browse the domain the user wants us to browse
184         #self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
185
186
187         
188 def main():
189     main_window = Main_window()
190
191     main_window.run()
192     
193 if __name__ == "__main__":
194     main()
195
196