pax_global_header00006660000000000000000000000064122226370750014520gustar00rootroot0000000000000052 comment=912084ff36ba9d7eda63b7497bbad18eb0cfd9c1 dbus_tutorial/000077500000000000000000000000001222263707500137445ustar00rootroot00000000000000dbus_tutorial/baked_strings.txt000066400000000000000000000001221222263707500173170ustar00rootroot00000000000000d-feet, notification: 'MyApp', 0, '', 'Summary', 'Notification body', '', '', -1 dbus_tutorial/listen_signal.pl000077500000000000000000000023241222263707500171400ustar00rootroot00000000000000#!/usr/bin/env perl use warnings; use strict; use threads; use Net::DBus; use Net::DBus::Reactor; sub signal { ############################################################################## #DBus stuff ############################################################################## # The usual... # Normally, dbus connection is shared. Private gives a whole new instance, # to avoid race conditions my $bus = Net::DBus->session(private => 1); my $service = $bus->get_service("org.freedesktop.Notifications"); my $object = $service->get_object("/org/freedesktop/Notifications", "org.freedesktop.Notifications"); # Register callback for a certain signal on our object $object->connect_to_signal("NotificationClosed", sub{print "Hey, a notification was closed\n";}); # Spent an hour wondering why I couldn't register a darned signal because of this bugger--^ # Start event loop my $reactor = Net::DBus::Reactor->main(); $reactor->run(); ############################################################################## } threads->create(\&signal); # Start your other stuff # Initially, I started mojolicious, but mojolicious doesn't play nice with # Net::DBus::Reactor (ithread, more specifically) # while(1){sleep 1}; dbus_tutorial/presentation.txt000066400000000000000000000103431222263707500172210ustar00rootroot00000000000000-------------------------------------------------------------------------------- 1. What is DBus -------------------------------------------------------------------------------- Same machine Inter-Process Communication (IPC) and Remote-Procedure Call (RPC) Bindings in some languages: c (glib), c++, perl, python, ? a. system and session buses -------------------------------------------------------------------- System bus: Allow communication between system processes (network manager, etc) and user sessions Session bus: Allow communication between applications User bus: Other busses can also be created b. Terminology -------------------------------------------------------------------- Bus name: An application end point. Generally, 1 application = 1 bus name, but an application could have many bus names and group similar objects Interface: Like a java interface, c++ abstract class, mostly a contract of the methods implemented implemented by the object Object: An object proper Properties: Variables in an object Methods: Function of an object Signals: Signals emitted by the object Slightly overzealous naming convention: org.freedesktop.Notifications service with a /org/freedesktop/Notifications with a org.freedesktop.Notifications interface c. Bus management: org.freedesktop.DBus -------------------------------------------------------------------- The DBus daemon provides this bus name, with the corresponding object and interface. Allows finding services (GetNameOwner) as well registering services (RequestName) and signal taps (AddMatch) d. Introspection: org.freedesktop.DBus.Instrospectable -------------------------------------------------------------------- An interface that most objects implement, that adds a method called Introspect, that return a description of the object's interfaces, its methods, properties, signals, as well as the types for all of the aboves (input and outputs for methods, params for signals, type for properties). * Show in qdbusviewer or d-feet -------------------------------------------------------------------------------- 2. The org.freedesktop.Notifications interface -------------------------------------------------------------------------------- Present the Notifications interface https://developer.gnome.org/notification-spec/ Use this string in d-feet 'MyApp', 0, '', 'Summary', 'Notification body', '', '', -1 -------------------------------------------------------------------------------- 3. Demo: Send notification: DBUs org.freedesktop.Notifications.Notify -------------------------------------------------------------------------------- cd ~/pres/web/; morbo webnotification.pl Url: localhost:3000 Watch the notification pop up look at ~/pres/web/webnotification.pl -------------------------------------------------------------------------------- 4. Demo: Catch signal: org.freedesktop.Notifications.NotificationClosed signal -------------------------------------------------------------------------------- ~/pres/listen_signal.pl Send a notification through web interface, and close it, check the message look at ~/pres/listen_signal.pl -------------------------------------------------------------------------------- 5. Demo: Export object: properties, methods and signals -------------------------------------------------------------------------------- The big thing: ~/pres/service/main.pl Fire up qdbusviewer to register on signal and to send commands look at ~/pres/service/main.pl -------------------------------------------------------------------------------- 6. Still more time? -------------------------------------------------------------------------------- Fire up dbus-monitor and plug in a usb key Look at the different interfaces -------------------------------------------------------------------------------- 0. Extra documentation -------------------------------------------------------------------------------- /usr/share/doc/libnet-dbus-perl/examples/ http://search.cpan.org/~danberr/Net-DBus-1.0.0/lib/Net/DBus.pm http://search.cpan.org/~danberr/Net-DBus-1.0.0/lib/Net/DBus/Tutorial.pod http://search.cpan.org/~danberr/Net-DBus-1.0.0/lib/Net/DBus/Tutorial/ExportingObjects.pod http://www.freedesktop.org/wiki/Software/dbus/#index4h1 http://dbus.freedesktop.org/doc/dbus-specification.html dbus_tutorial/service/000077500000000000000000000000001222263707500154045ustar00rootroot00000000000000dbus_tutorial/service/README000066400000000000000000000001461222263707500162650ustar00rootroot00000000000000Based off: http://search.cpan.org/~danberr/Net-DBus-0.33.1/lib/Net/DBus/Tutorial/ExportingObjects.pod dbus_tutorial/service/main.pl000077500000000000000000000004051222263707500166670ustar00rootroot00000000000000#!/usr/bin/env perl use warnings; use strict; use Net::DBus; use Net::DBus::Reactor; use pm::perlservice; my $bus = Net::DBus->session; my $service = pm::perlservice->new($bus); Net::DBus::Reactor->main->run; # Continue at ~/pres/service/pm/perlservice.pm dbus_tutorial/service/pm/000077500000000000000000000000001222263707500160205ustar00rootroot00000000000000dbus_tutorial/service/pm/perlobject.pm000077500000000000000000000053111222263707500205120ustar00rootroot00000000000000 # Funky package name to make sure none of these somehow cross over on the # public interface package pm::perlobject; # This is the interface name ===v use Net::DBus::Exporter qw(pm.exporter); # We extend Net::DBus::Object use base qw(Net::DBus::Object); # Only explicitly exported methods can be invoked dbus_strict_export; our ($intval, $stringval);#, @array, %dict); sub new { my $class = shift; my $service = shift; # This is the path of the object ===============v my $self = $class->SUPER::new($service, "/pm/objpath"); bless $self, $class; return $self; } # Simple method that prints to stdout the parameter in input # method name input args out anotations, here parameter name dbus_method("echo", ["string"], [], {param_names => ["message"]}); sub echo { my $self = shift; my $message = shift; print "$message\n"; } dbus_method("getString", [], ["string"]); sub getString { my $self = shift; print "getString called\n"; # die is "hijacked" and throws errors on caller die "val is not set\n" unless $stringval; return $stringval; } # Declare a signal for our interface # Signals don't have return parameters dbus_signal("StringChanged", ["string"]); dbus_method("setString", ["string"]); sub setString { my $self = shift; print "setString called\n"; my $newstring = shift; # Emit signal # Issue when $stringval is not set $self->emit_signal("StringChanged", "String changed from $stringval to $newstring"); $stringval = shift; } dbus_method("getInt", [], ["string"]); sub getInt { my $self = shift; print "geInt called\n"; die "val is not set\n" unless $intval; return $intval; } dbus_method("setInt", ["string"]); sub setInt { my $self = shift; print "setInt called\n"; $intval = shift; } dbus_method("complexSet", ["uint32", "string"]); sub complexSet { my $self = shift; print "complexSet called\n"; $intval = shift; $stringval = shift; } # dbus supports multiple return values, but not perl. Would have to see how # Net:DBus works around the problem # In the meantime, returning a single argument, a struct dbus_method("complexGet", [], [["struct","uint32", "string"]]); sub complexGet { my $self = shift; print "complexGet called\n"; die "val is not set\n" unless $intval && $stringval; return [$intval, $stringval]; } dbus_method("unset"); sub unset { my $self = shift; print "unset called\n"; $intval = undef; $stringval = undef; } # Errors when trying to use props: # Use of uninitialized value $key in hash element at /usr/lib/perl5/Net/DBus/Binding/Iterator.pm line 504 #dbus_property("testprop", "string", "readwrite"); #dbus_method("complexGet", ["uint32", "string", "array", "string", "dict", "string", "uint32"]); # 1; dbus_tutorial/service/pm/perlservice.pm000077500000000000000000000005331222263707500207050ustar00rootroot00000000000000package pm::perlservice; use pm::perlobject; use base qw(Net::DBus::Service); sub new { my $class = shift; my $bus = shift; my $self = $class->SUPER::new($bus, "pm.service"); bless $self, $class; # Create our object $self->{manager} = pm::perlobject->new($self); return $self; } 1; # Continue at ~/pres/service/pm/perlobject.pm dbus_tutorial/web/000077500000000000000000000000001222263707500145215ustar00rootroot00000000000000dbus_tutorial/web/fail_alertclosed.pl000077500000000000000000000035351222263707500203630ustar00rootroot00000000000000#!/usr/bin/env perl use threads; use threads::shared; use Net::DBus; use Try::Tiny; use Mojolicious::Lite; use Mojo::Reactor::Poll; #MOJO_REACTOR=Mojo::Reactor::Poll; # Documentation browser under "/perldoc" plugin 'PODRenderer'; our $client :shared; sub dbus_signal { my $bus = Net::DBus->session(private => 1); my $service = $bus->get_service("org.freedesktop.Notifications"); my $object = $service->get_object("/org/freedesktop/Notifications", "org.freedesktop.Notifications"); $object->connect_to_signal("NotificationClosed", sub{$client->send(text =>"Notification was closed") if $client;}); my $reactor = Net::DBus::Reactor->main(); $reactor->run(); } get '/' => sub { my $self = shift; $self->render('index'); }; websocket '/notifications' => sub { my $self = shift; $client = $self->tx; $self->on(finish => sub { $client = undef; }); }; threads->create(\&dbus_signal); # Mojolicious doesn not appreciate the presence of my other thread: # "EV does not work with ithread" # There doesn't seem to be any way around it :( app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Welcome'; Simple method invocation demonstration
Summary:
Body:
@@ layouts/default.html.ep <%= title %> <%= content %> @@ notify.html.ep % layout 'default'; % title 'Notified'; Notification sent, you should see something dbus_tutorial/web/webnotification.pl000077500000000000000000000043531222263707500202520ustar00rootroot00000000000000#!/usr/bin/env perl # # Simple application of Net::DBus's documentation # http://search.cpan.org/~danberr/Net-DBus-1.0.0/lib/Net/DBus.pm use Net::DBus; use Try::Tiny; use Mojolicious::Lite; # Documentation browser under "/perldoc" plugin 'PODRenderer'; ############################################################################## #DBus stuff ############################################################################## # Connecting to the dbus session bus my $bus = Net::DBus->session; # Connecting to the org.freedesktop.Notifications service my $service = $bus->get_service("org.freedesktop.Notifications"); # Connecting to the /org/freedesktop/Notifications object, our $notification = $service->get_object("/org/freedesktop/Notifications", # which implements the org.freedesktop.Notifications "org.freedesktop.Notifications"); ############################################################################## get '/' => sub { my $self = shift; $self->render('index'); }; post '/notify' => sub { my $self = shift; my $summary = $self->param('summary'); my $body = $self->param('body'); try { ############################################################################## #DBus stuff ############################################################################## # Call the Notify method on our $notification object # args: appname, replaceid, icon, summary, body, actions, hint, timeout # https://developer.gnome.org/notification-spec/ $notification->Notify('WebNotification', 0, '', $summary, $body, [], {}, -1); $self->render('notify'); } catch { $self->render(text => 'Error while calling Notify', status => 500); } ############################################################################## }; app->start; __DATA__ @@ index.html.ep % layout 'default'; % title 'Welcome'; Simple method invocation demonstration
Summary:
Body:
@@ layouts/default.html.ep <%= title %> <%= content %> @@ notify.html.ep % layout 'default'; % title 'Notified'; Notification sent, you should see something