Skip to content

Instantly share code, notes, and snippets.

@jbarber
Created April 2, 2015 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbarber/66fb17f6f9f6a499ee86 to your computer and use it in GitHub Desktop.
Save jbarber/66fb17f6f9f6a499ee86 to your computer and use it in GitHub Desktop.
Example perl DBUS service with AnyEvent::DBUS
#!/usr/bin/env perl
# Simple (!) Net::DBus service example using AnyEvent::DBus to run the event
# loop. You can examine the service with the following command:
# gdbus introspect --session --dest=pt.up.fe.door -o /pt/up/fe/door
#
# And invoke the unlock method as follows:
# dbus-send --session --dest=pt.up.fe.door /pt/up/fe/door pt.up.fe.door.unlock
#
# Once the unlock method is invoked, the program terminates.
use 5.10.0;
use strict;
use warnings;
package OpenDoor;
use Net::DBus::Exporter qw(pt.up.fe.door);
use base qw(Net::DBus::Object);
dbus_method("unlock", [], [], { no_return => 1 });
sub unlock {
my ($self) = @_;
warn "method called\n";
$self->{_cv}->send;
}
sub new {
my ($class, $service, $cv) = @_;
my $self = $class->SUPER::new($service, "/pt/up/fe/door");
bless $self, $class;
$self->{_cv} = $cv;
return $self;
}
package main;
use strict;
use warnings;
use Net::DBus qw(1.0.0);
use AnyEvent::DBus 0.31;
use Net::DBus::Annotation qw(:call);
my $bus = Net::DBus->find();
my $service = $bus->export_service("pt.up.fe.door");
my $cv = AE::cv;
OpenDoor->new($service, $cv);
$cv->recv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment