* add new tool avahi-bookmarks
authorLennart Poettering <lennart@poettering.net>
Thu, 4 Aug 2005 22:33:07 +0000 (22:33 +0000)
committerLennart Poettering <lennart@poettering.net>
Thu, 4 Aug 2005 22:33:07 +0000 (22:33 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@234 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-utils/Makefile.am
avahi-utils/avahi-bookmarks.in [new file with mode: 0755]

index 0079795..90b908c 100644 (file)
@@ -23,13 +23,15 @@ pythonscripts = \
        avahi-publish-address \
        avahi-publish-service \
        avahi-dump-all \
-       avahi-discover 
+       avahi-discover \
+       avahi-bookmarks
 
 EXTRA_DIST = \
        avahi-publish-address.in \
        avahi-publish-service.in \
        avahi-dump-all.in \
-       avahi-discover.in 
+       avahi-discover.in \
+       avahi-bookmarks.in
 
 if HAVE_PYTHON
 bin_SCRIPTS = $(pythonscripts)
@@ -52,4 +54,8 @@ avahi-discover: avahi-discover.in
                -e 's,@interfacesdir\@,$(interfacesdir),g' $< > $@
        chmod +x $@
 
+avahi-bookmarks: avahi-bookmarks.in
+       sed -e 's,@PYTHON\@,$(PYTHON),g' $< > $@
+       chmod +x $@
+
 CLEANFILES = $(pythonscripts)
diff --git a/avahi-utils/avahi-bookmarks.in b/avahi-utils/avahi-bookmarks.in
new file mode 100755 (executable)
index 0000000..7504349
--- /dev/null
@@ -0,0 +1,111 @@
+#!@PYTHON@
+# -*-python-*-
+# $Id$ 
+
+# This file is part of avahi.
+#
+# avahi is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+#
+# avahi is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
+# License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with avahi; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA.
+
+import avahi, dbus, gobject, sys
+
+from twisted.internet import gtk2reactor
+gtk2reactor.install()
+from twisted.internet import reactor
+from twisted.web import server, resource
+
+try:
+    import dbus.glib
+except ImportError, e:
+    pass
+
+
+class AvahiBookmarks(resource.Resource):
+    isLeaf = True
+
+    services = {}
+
+    def __init__(self):
+        resource.Resource.__init__(self)
+
+
+        self.bus = dbus.SystemBus()
+        self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
+
+        self.version_string = self.server.GetVersionString()
+
+        self.browser = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.ServiceBrowserNew(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "_http._tcp", "local")), avahi.DBUS_INTERFACE_SERVICE_BROWSER)
+
+        self.browser.connect_to_signal('ItemNew', self.new_service)
+        self.browser.connect_to_signal('ItemRemove', self.remove_service)
+
+    def find_path(self, txt):
+
+        for k in txt:
+            if k[:5] == "path=":
+                return k[5:]
+
+        return "/"
+
+    def render_GET(self, request):
+
+        t = '<html><head><title>Zeroconf Bookmarks</title></head><body><h1>Zeroconf Bookmarks</h1>'
+
+        if len(self.services) == 0:
+            t += '<p>Sorry, no web services have been registered on the local LAN.</p>'
+        else:
+            t += '<ul style="padding: 0px; margin: 20px; list-style-type: none">'
+
+            for k, v in self.services.iteritems():
+            
+                if v[3] == 80:
+                    port = ''
+                else:
+                    port = ':%i' % v[3]
+
+                path = self.find_path(v[4])
+
+
+                t += '<li><a href="http://%s%s%s">%s</a></li>' % (v[2], port, path, k[2])
+                
+            t += '</ul>'
+        
+        t += '<hr noshade/><p style="font-size: 8; font-family: sans-serif">Served by %s</p></body></html>' % self.version_string
+
+        return t
+
+
+    def new_service(self, interface, protocol, name, type, domain):
+
+        interface, protocol, name, type, domain, host, aprotocol, address, port, txt = self.server.ResolveService(interface, protocol, name, type, domain, avahi.PROTO_UNSPEC)
+
+        self.services[(interface, protocol, name, type, domain)] = (host, aprotocol, address, port, txt)
+
+    def remove_service(self, interface, protocol, name, type, domain):
+        del self.services[(interface, protocol, name, type, domain)]
+
+
+port = 8080
+
+if __name__ == '__main__':
+    site = server.Site(AvahiBookmarks())
+    reactor.listenTCP(port, site)
+
+    print "Now point your web browser to http://localhost:%u/!" % port
+
+    try:
+        reactor.run()
+    except KeyboardInterrupt, k:
+        pass