dbus-cxx logo
Quick Start Client 0

A simple client

This section provides an example of a simple client for our simple server.
Summary
This is an overview of the code we will need in our example client:
  1. Include headers
  2. Initialize the dbus-cxx library
  3. Create a dispatcher to manage threads, timeouts and I/O watches
  4. Create a connection to the D-Bus session bus
  5. Create a proxy object for the server's quickstart_0 object
  6. Create a proxy add method for our proxy object
  7. Call our proxy method
  8. Print out the results

Simple client 0 code

Here is the code in full, followed by a section-by-section discussion.
/***************************************************************************
* Copyright (C) 2007,2010 by Rick L. Vinyard, Jr. *
* rvinyard@cs.nmsu.edu *
* *
* This file is part of the dbus-cxx library. *
* *
* The dbus-cxx library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* version 3 as published by the Free Software Foundation. *
* *
* The dbus-cxx library is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty *
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this software. If not see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include <dbus-cxx.h>
#include <iostream>
int main()
{
dispatcher = DBus::Dispatcher::create();
connection = dispatcher->create_connection( DBus::BUS_SESSION );
//create an object proxy, which stands in for a real object.
//a proxy exists over the dbus
object = connection->create_object_proxy("dbuscxx.quickstart_0.server", "/dbuscxx/quickstart_0");
//a method proxy acts like a real method, but will go over the dbus
//to do its work.
= *(object->create_method<double,double,double>("dbuscxx.Quickstart","add"));
double answer;
answer = add_proxy( 1.1, 2.2 );
std::cout << "1.1 + 2.2 = " << answer << std::endl;
return 0;
}

Simple client 0 code discussion

Include headers
First, we need to include the main dbus-cxx library header. We'll also include the iostream header since we'll print out the results of our addition.
#include <dbus-cxx.h>
#include <iostream>
And now for the beginning of our main() function.
int main()
{
Initialize the dbus-cxx library
Before we do anything in dbus-cxx we must call the library's init() method which will take care of various initializations including the threading system.
Create a dispatcher to manage threads, timeouts and I/O watches
Now, we will create a dispatcher to handle incoming and outgoing messages. Handling incoming and outgoing messages can be messy and the Dispatcher class handles much of this for us.
Create a connection to the D-Bus session bus
Now that we have a dispatcher we need to create a connection to the session bus.
DBus::Connection::pointer conn = dispatcher->create_connection(DBus::BUS_SESSION);
Request a name for our application on the bus
Next, we need to request a name that will identify our application on the session bus.
ret = conn->request_name( "dbuscxx.quickstart_0.server", DBUS_NAME_FLAG_REPLACE_EXISTING );
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) return 1;
Create a proxy object for the server's quickstart_0 object
You'll notice that up to this point the code for the client and the server has been nearly identical. This is the point at which they will diverge. We will now create a proxy object for the object on the server.
object = connection->create_object_proxy("dbuscxx.quickstart_0.server", "/dbuscxx/quickstart_0");
Note that we must use the name the server requested as well as the object path the server used when it created it's object when we create the proxy object.
Create a proxy add method for our proxy object
Now we create a proxy add method for the server object's add method. This will allow us to use function notation to call the method. When creating the proxy method we must use the same interface ("dbuscxx.Quickstart" ) and method name ( "add" ) as the server used.
= *(object->create_method<double,double,double>("dbuscxx.Quickstart","add"));
Call our proxy method
Now we can use our proxy method to call the method on the server using function notation. We'll keep things simple and just add 1.1 + 2.2 .
double answer;
answer = add_proxy( 1.1, 2.2 );
Print out the results
Finally, we can print out the results.
std::cout << "1.1 + 2.2 = " << answer << std::endl;
return 0;
}

Go Back: Quick Start Guide to dbus-cxx


Generated on Sat Sep 21 2013 17:22:40 for dbus-cxx by doxygen 1.8.1.2