Help with simple example use of perl's Net::DBus

Madison Kelly linux at alteeve.com
Mon Mar 10 07:35:16 PDT 2008


Daniel P. Berrange wrote:
> On Mon, Mar 10, 2008 at 09:46:32AM -0400, Madison Kelly wrote:
>> Hi all,
>>
>>    I've been trying to use Net::DBus for a fairly simple purpose; 
>> connect to the system bus and listen for signals. Nothing more. However, 
>> I seem to be quite daft as this simple task escapes me. I had decided to 
>> give up on it for now and listed to a system call to 'dbus-monitor 
>> --system --profile', but it turns out that is not feasible because of 
>> the output being buffered causing unfeasible delays in the perl code 
>> seeing the output.
> 
> Take a look at the code in the  'examples' directory of the Net::DBus
> binding - it demonstrates how to listen for signals,
> 
> http://search.cpan.org/src/DANBERR/Net-DBus-0.33.6/examples/
> 
> eg   This code from 'example-signal-receive.pl'  will listen for 'HelloSignal' 
> and 'hello_signal_handler' will be called each time the signal is emitted:
> 
> 
>   my $bus = Net::DBus->session(); 
>   my $service = $bus->get_service("org.designfu.TestService");
>   my $object  = $service->get_object("/org/designfu/TestService/object",
>                                      "org.designfu.TestService");
> 
> 
>   sub hello_signal_handler {
>     my $greeting = shift;
>     print "Received hello signal with greeting '$greeting'\n";
>   }
> 
>   $object->connect_to_signal("HelloSignal", \&hello_signal_handler);
> 
>   my $reactor = Net::DBus::Reactor->main();
>   $reactor->run();
> 
> 
> The 'example-signal-emitter.pl' is the corresponding server side to test
> with.
> 
> Regards,
> Dan.

Thank you kindly, I've got what I wanted working (finally)!!

Madison

PS - To add the the web's collection of sample code, here is a rough-up 
that listens for storage devices to be added, removed or modified. I am 
sure it is flawed, but it works. :)

-=] Begin 'test.pl' [=-
#!/usr/bin/perl

use strict;
use warnings;
use Net::DBus;
use Net::DBus::Reactor;

my $bus = Net::DBus->system;
my $hal = $bus->get_service("org.freedesktop.Hal");

# Connect to the manager to hear device add and remove messages.
my $hal_manager = $hal->get_object("/org/freedesktop/Hal/Manager", 
"org.freedesktop.Hal.Manager");
my $hal_devices = $hal->get_object("/org/freedesktop/Hal/devices", 
"org.freedesktop.Hal.Devices");
$hal_manager->connect_to_signal('DeviceAdded', sub {
	my ($callid) = @_;
	print "Added: [$callid]\n";
	if ( $callid =~ /^\/org\/freedesktop\/Hal\/devices\/volume_/ )
	{
		print "Adding listener for object: [$callid]\n";
		my $hal_device = $hal->get_object("$callid", 
"org.freedesktop.Hal.Device");
		$hal_device->connect_to_signal('PropertyModified', sub {
			my ($callid) = @_;
			print "Modified: [$callid]\n";
		});
	}
});
$hal_manager->connect_to_signal('DeviceRemoved', sub {
	my ($callid) = @_;
	print "Removed: [$callid]\n";
});

my $reactor = Net::DBus::Reactor->main();
$reactor->run();

exit(0);




More information about the dbus mailing list