#!/usr/bin/env python3

from twisted.internet import reactor, defer
from txdbus import error, client

def resolved(*args):
    print args

def error(*args):
    print args


class AvahiBrowser (object):

    avahi_bus_name    = 'org.freedesktop.Avahi'
    avahi_object_path = '/'
    
    @defer.inlineCallbacks
    def start(self, service_type, domain=''):
        
        con = yield client.connect(reactor, 'system')
        
        # Get a handle to the server
        self.server = yield con.getRemoteObject( self.avahi_bus_name,
                                                 self.avahi_object_path )
        
        # Create new service browser -> Get back path to object
        browser_path = yield self.server.callRemote( 'ServiceBrowserNew', 
                                                     -1, # interface: -1 is IF_UNSPEC
                                                     -1, # protocol: -1 is PROTO_UNSPEC 
                                                     service_type,
                                                     domain, 
                                                     0 )
        
        # Get service browser object
        self.browser = yield con.getRemoteObject( self.avahi_bus_name,
                                                  browser_path )
        
        # Setup listeners
        self.browser.notifyOnSignal('ItemNew',    self.new_service)
        self.browser.notifyOnSignal('ItemRemove', self.remove_service)
        
        print "Now browsing for avahi services ..."
    
    
    def new_service(self, interface, protocol, name, stype, domain, flags):
        """Add servivce"""
        
        print "New service found:"
        print interface, protocol, name, stype, domain, flags
        
        print "Browsing new-found service:"
        self.server.callRemote( "ResolveService", 
                                interface, 
                                protocol, 
                                name, 
                                stype, 
                                domain, 
                                -1, 
                                0 ).addCallbacks( resolved, 
                                                  error )
    
    def remove_service(self, interface, protocol, name, stype, domain, flags):
        """Remove service"""
        print "Service was removed..."
        print interface, protocol, name, stype, domain, flags
    
    
ab = AvahiBrowser()
reactor.callWhenRunning(ab.start, '_avahidemo._tcp')
reactor.run()
