#!/usr/bin/perl
#
# Sample D-Bus "caller". This shows how you can listen to various signals and
# call published methods on a given D-Bus message bus.
#
# Author:  Madison Kelly (mkelly@alteeve.ca)
# Updated: 2008/07/14
# Plug:    http://tle-bu.org
#
# Source code released under the GPLv3.
#  - http://www.gnu.org/licenses/gpl-3.0.txt
# Documentation released under Creative Commons
# Attribution-Noncommercial-Share Alike 3.0 License.
#  - http://creativecommons.org/licenses/by-nc-sa/3.0/
#
# This program file is not meant to be fast or event particularly sane. It's
# primary goal is to show as many possible methods and functions available in
# the Net::DBus* modules as possible in a format that is easy to follow for a
# new user. You will want to read up more on the best way to do various things
# one you have your own code working.
#
# The examples here are somewhat based on the example scripts provided by the
# Net::DBus module. Please check the scripts under the 'examples' directory if
# you would like to see the similarities.
#

# Being good little coder, I make things die early and loudly.
use strict;
use warnings;

# Load in the Net::DBus core module.
use Net::DBus;

# Create a connection to the Freedesktop.org 'session' bus, which is the bus
# accessible by the user invoking this module only.
my $bus=Net::DBus->session();

# Get a connection to the service with the well known name
# 'org.tle-bu.SampleService'. I know to use this service name because the
# author of the service made it clearly known in their always-excellent
# documentation. :) In our case, this is the service name used in the 'main'
# package in the 'service_01.pl' file's 'export_service' call.
my $service=$bus->get_service("org.tle-bu.SampleService");

# Now that I have a connection to the service, I need to connect to the methods
# exported under the object path '/org/tle_bu/DBusExampleObject' published
# under the interface 'org.tle_bu.DBusExampleInterface'. Technically, I could
# leave off the interface decleration *IF* I can be sure the
# 'DBusExampleObject' object is uniquly named within this service *AND* the
# remote object supports introspection. However, it is always safest to be
# specific.
#
# The object path is specified in the 'service_01.pl' file's 'new' method where
# '$self' was first invoked. The interface is specified in the same file when
# 'Net::DBus::Exporter' was called. Alternatively, if a different interface was
# defined when the 'dbus_method' call was made for the exported method I am
# interested in, I would use that here instead.
my $object=$service->get_object("/org/tle_bu/DBusExampleObject", "org.tle_bu.DBusExampleInterface");

# This will send a signal onto the message bus telling any interested parties
# that this script has started.
$object->emit_signal("signalMessage", "The 'caller_02.pl' script has started.");

# Let's do some math using the 'doMath' method.
my $num1=10;
my $num2=8;
my $symbol="/";
my ($result, $string)=$object->doMath($num1, $num2, $symbol);
if ($string)
{
	print "I asked what: [$num1 $symbol $num2] was, but I got an error: [$string]\n";
}
else
{
	print "I asked what: [$num1 $symbol $num2] is and I was told: [$result].\n";
}

# Let's brag to anyone who cares that I can do math. I'll do this by
# emitting a signal.
my $message=$object->emitMessage("I can do math, me! I think '$num1 $symbol $num2 = $result $string'.");
print "Called 'emitMessage' and got string: [$message]\n";

# This will send a signal onto the message bus telling any interested parties
# that this script has finished.
$object->emit_signal("signalMessage", "The 'caller_02.pl' script is done.");

# Exit cleanly.
exit 0;
