#!/usr/bin/env python3

from twisted.internet import reactor, defer

from txdbus           import client, objects, error
from txdbus.interface import DBusInterface, Signal


class SignalSender (objects.DBusObject):

    iface = DBusInterface( 'org.example.SignalSender',
                           Signal('tick', 'u')
                         )

    dbusInterfaces = [iface]
    
    def __init__(self, objectPath):
        super(SignalSender, self).__init__(objectPath)
        self.count = 0


    def sendTick(self):
        self.emitSignal('tick', self.count)
        self.count += 1
        reactor.callLater(1, self.sendTick)


@defer.inlineCallbacks
def main():
    try:
        conn = yield client.connect(reactor)

        s = SignalSender('/Signaller')
        
        conn.exportObject( s )

        yield conn.requestBusName('org.example')

        print 'Object exported on bus name "org.example" with path /Signaller'
        print 'Emitting "tick" signals every second'
        
        s.sendTick() # begin looping

    except error.DBusException, e:
        print 'Failed to export object: ', e
        reactor.stop()
        
    
reactor.callWhenRunning( main )
reactor.run()
