add
that will take two double
parameters and return the sum as a double
.add
method.add
to our object/*************************************************************************** * 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> double add( double param1, double param2 ) { return param1 + param2; } int main() { int ret; DBus::init(); DBus::Dispatcher::pointer dispatcher = DBus::Dispatcher::create(); 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; DBus::Object::pointer object = conn->create_object("/dbuscxx/quickstart_0"); object->create_method<double,double,double>("dbuscxx.Quickstart", "add", sigc::ptr_fun(add) ); sleep(10); return 0; }
#include <dbus-cxx.h>
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; }
main()
function. We'll also declare a variable ret
that can be used to check the return value of functions. int main() { int ret;
init()
method which will take care of various initializations including the threading system. DBus::init();
DBus::Dispatcher::pointer dispatcher = DBus::Dispatcher::create();
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;
DBus::Object::pointer object = conn->create_object("/dbuscxx/quickstart_0");
add
to our objectadd
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"
.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) );
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