dbus-cxx logo
Quick start example 0: A simple server and client
This example will demonstrate a simple server and client pair.
The server will only offer one method named add that will take two double parameters and return the sum as a double.
The client will use a proxy object for the server as well as a proxy for the add method.

A simple server

This section provides an example of a simple server. The server will run for 10 seconds and then automatically shut-down.
Summary
This is an overview of the code we will need in our example server:
  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. Request a name for our application on the bus
  6. Create an object on the bus
  7. Add a method named add to our object
  8. Wait for incoming calls

Simple server 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 <unistd.h>
double add( double param1, double param2 ) { return param1 + param2; }
int main()
{
int ret;
DBus::Connection::pointer conn = dispatcher->create_connection(DBus::BUS_SESSION);
ret = conn->request_name( "dbuscxx.quickstart_0.server", DBUS_NAME_FLAG_REPLACE_EXISTING );
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) return 1;
//create an object on us
DBus::Object::pointer object = conn->create_object("/dbuscxx/quickstart_0");
//add a method that can be called over the dbus
object->create_method<double,double,double>("dbuscxx.Quickstart", "add", sigc::ptr_fun(add) );
sleep(10);
return 0;
}

Simple server 0 code discussion

Include headers
First, we need to include the main dbus-cxx library header:
#include <dbus-cxx.h>
Then we will define a function that will be our workhorse on the server side. This function will be the actual function that will be called when the dbus add method is invoked, so we will name our function similarly. But, note that the actual name of the function in our program and the name in our dbus server do not have to match.
double add( double param1, double param2 ) { return param1 + param2; }
And now for the beginning of our main() function. We'll also declare a variable ret that can be used to check the return value of functions.
int main()
{
int ret;
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 an object on the bus
Now that our application has a name on the bus we need to add an object with it's path.
DBus::Object::pointer object = conn->create_object("/dbuscxx/quickstart_0");
Add a method named add to our object
We will now create a method named add for our object. The functionality of the method will be provided by the function we declared above also named add(). We must add this to an interface, and the D-Bus specification required that interface names must contain at least one . (period) character so we will use the interface name "dbuscxx.Quickstart".
Note that our object's method named add has nothing to do with C++ class methods. In essence we are creating virtual objects on the D-Bus and can choose to provide their functionality with either C++ class methods and/or plain functions.
object->create_method<double,double,double>("dbuscxx.Quickstart", "add", sigc::ptr_fun(add) );
Wait for incoming calls
Finally, we need to wait for incoming calls. We will sleep for 10 seconds in our main() function and the calls will be handled in the dispatcher's threads.
sleep(10);
return 0;
}

Continue On: Quick Start Client 0

Go Back: Quick Start Guide to dbus-cxx


Generated on Sun Mar 19 2017 14:54:27 for dbus-cxx by doxygen 1.8.8