diff options
author | Jan Hák <jan.hak@nic.cz> | 2022-01-11 14:20:01 +0100 |
---|---|---|
committer | Daniel Salzman <daniel.salzman@nic.cz> | 2022-01-18 19:06:41 +0100 |
commit | faf079ff3435deb95692e2856def3a1c0205ccb3 (patch) | |
tree | 2710e8078cda1324010d064174b34b4ee88bcd21 | |
parent | dbus: simplify config file (diff) | |
download | knot-faf079ff3435deb95692e2856def3a1c0205ccb3.tar.xz knot-faf079ff3435deb95692e2856def3a1c0205ccb3.zip |
samples: add client for python, perl, and bash
-rwxr-xr-x | samples/dbus_client.pl | 23 | ||||
-rwxr-xr-x | samples/dbus_client.py | 27 | ||||
-rwxr-xr-x | samples/dbus_client.sh | 22 |
3 files changed, 72 insertions, 0 deletions
diff --git a/samples/dbus_client.pl b/samples/dbus_client.pl new file mode 100755 index 000000000..5023eccd6 --- /dev/null +++ b/samples/dbus_client.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use Net::DBus; +use Net::DBus::Reactor; + +my $bus = Net::DBus->system; + +# Get a handle to the 'knotd' service +my $knotd = $bus->get_service("cz.nic.knotd"); + +# Get the device manager +my $knotd_interface = $knotd->get_object("/cz/nic/knotd", "cz.nic.knotd.events"); + +$knotd_interface->connect_to_signal('zone_updated', sub +{ + my ($zone, $serial) = @_; + print "Zone $zone updated, SOA serial $serial\n"; +}); + +# Main loop +Net::DBus::Reactor->main->run(); + +exit 0 diff --git a/samples/dbus_client.py b/samples/dbus_client.py new file mode 100755 index 000000000..0396048ea --- /dev/null +++ b/samples/dbus_client.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import dbus +import dbus.mainloop.glib +import signal +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 updated(*args, **kwargs): + (zone, serial) = args + print("Zone %s updated, SOA serial %d" % (zone, serial)) + +if __name__ == '__main__': + signal.signal(signal.SIGINT, sigint_handler) + + loop = dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) + bus = dbus.SystemBus() + knotd = bus.get_object('cz.nic.knotd', '/cz/nic/knotd') + events_iface = dbus.Interface(knotd, dbus_interface='cz.nic.knotd.events') + events_iface.connect_to_signal("zone_updated", updated) + loop = GLib.MainLoop() + loop.run() diff --git a/samples/dbus_client.sh b/samples/dbus_client.sh new file mode 100755 index 000000000..be23a97b2 --- /dev/null +++ b/samples/dbus_client.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +cb() { + echo "$1 [${@:2}]" +} + +gdbus monitor --system --dest cz.nic.knotd --object-path /cz/nic/knotd \ + | awk '/^\/cz\/nic\/knotd/ { + gsub("cz.nic.knotd.events.", "", $2); + tmp=""; + for(i=3;i<=NF;++i) { + if( $i ~ /[\),]$/ ) tmp=tmp$i; + } + gsub(/(^\()|(\)$)/, "", tmp); + split(tmp, args, ","); + printf "%s ", $2; + for (i in args) printf "%s ", args[i]; + print ""; + fflush(stdout); }' \ + | while read line; do \ + cb ${line[@]}; \ + done |