summaryrefslogtreecommitdiffstats
path: root/samples/dbus_client.py
blob: f4c38deb1110297e624fa38045768d36d5267f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3

# This package is needed on Debian derived ditributions: python3-dbus

import dbus
import dbus.mainloop.glib
import signal
import time
from gi.repository import GLib

def sigint_handler(sig, frame):
    if sig == signal.SIGINT:
        loop.quit()
    else:
        raise ValueError("Undefined handler for '{}'".format(sig))

def sig_started(*args, **kwargs):
    print("Server started")

def sig_stopped(*args, **kwargs):
    print("Server stopped")

def sig_updated(*args, **kwargs):
    (zone, serial) = args
    print("Updated zone=%s to serial=%d" % (zone, serial))

def sig_keys_upd(*args, **kwargs):
    (zone) = args
    print("Keys updated for zone=%s" % (zone))

def sig_submission(*args, **kwargs):
    (zone, key_tag, kasp_id) = args
    print("Ready KSK for zone=%s keytag=%u keyid=%s" % (zone, key_tag, kasp_id))

def sig_invalid(*args, **kwargs):
    (zone) = args
    print("Invalid DNSSEC for zone=%s" % (zone))

if __name__ == '__main__':
    signal.signal(signal.SIGINT, sigint_handler)

    loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    bus = dbus.SystemBus()
    while True: # Wait until the service (knotd) is ready.
        try:
            knotd = bus.get_object('cz.nic.knotd', '/cz/nic/knotd',
                                   follow_name_owner_changes=True,
                                   introspect=False)
            break
        except:
            time.sleep(0.1)
    events_iface = dbus.Interface(knotd, dbus_interface='cz.nic.knotd.events')
    events_iface.connect_to_signal("started", sig_started)
    events_iface.connect_to_signal("stopped", sig_stopped)
    events_iface.connect_to_signal("zone_updated", sig_updated)
    events_iface.connect_to_signal("keys_updated", sig_keys_upd)
    events_iface.connect_to_signal("zone_ksk_submission", sig_submission)
    events_iface.connect_to_signal("zone_dnssec_invalid", sig_invalid)

    loop = GLib.MainLoop()
    loop.run()