calculator_server.cpp
calculator_client.cpp
calculator_watcher.cpp
This particular piece is the watcher that uses the generated ObjectProxy derived class to observe calculation signals from the server.
The client and watcher both use the same proxy generated from the XML document (see the calculator server example for the modified document).
The proxy is generated with this command:
dbus-cxx-xml2cpp --xml calculator.xml --proxy -f
Here is the generated proxy file:
#ifndef __DBUS_PROXY_DBUS_EXAMPLE_CALCULATOR_H #define __DBUS_PROXY_DBUS_EXAMPLE_CALCULATOR_H #include <dbus-cxx.h> namespace DBus { namespace Example { class CalculatorProxy : public ::DBus::ObjectProxy { protected: CalculatorProxy( ::DBus::Connection::pointer conn, const std::string& dest="dbuscxx.example.calculator.server", const std::string& path="/dbuscxx/example/Calculator"): ::DBus::ObjectProxy(conn, dest, path) { m_method_add = this->create_method<double,double,double>( "Calculator.Basic", "add" ); m_method_sub = this->create_method<double,double,double>( "Calculator.Basic", "sub" ); m_method_mul = this->create_method<double,double,double>( "Calculator.Basic", "mul" ); m_method_div = this->create_method<double,double,double>( "Calculator.Basic", "div" ); m_method_pi = this->create_method<double>( "Calculator.Basic", "pi" ); m_method_print_pi = this->create_method<void>( "Calculator.Basic", "print_pi" ); m_signal_calculation = this->create_signal<void,std::string,std::string,double,double,double>( "Calculator.Basic", "calculation" ); m_method_factorial = this->create_method<uint64_t,uint8_t>( "Calculator.Computed", "factorial" ); m_method_fibonacci = this->create_method<uint64_t,uint8_t>( "Calculator.Computed", "fibonacci" ); m_method_thue_morse = this->create_method<uint64_t,uint8_t>( "Calculator.Computed", "thue_morse" ); m_signal_computation = this->create_signal<void,std::string,uint64_t,uint8_t>( "Calculator.Computed", "computation" ); } public: typedef DBusCxxPointer<CalculatorProxy> pointer; static pointer create( ::DBus::Connection::pointer conn, const std::string& dest="dbuscxx.example.calculator.server", const std::string& path="/dbuscxx/example/Calculator" ) { return pointer( new CalculatorProxy(conn, dest, path)); } double add( double a, double b ) { return (*m_method_add)( a, b); } double sub( double a, double b ) { return (*m_method_sub)( a, b); } double mul( double a, double b ) { return (*m_method_mul)( a, b); } double div( double a, double b ) { return (*m_method_div)( a, b); } double pi( ) { return (*m_method_pi)(); } void print_pi( ) { return (*m_method_print_pi)(); } ::DBus::signal_proxy<void,std::string,std::string,double,double,double >& signal_calculation() { return *m_signal_calculation; } uint64_t factorial( uint8_t n ) { return (*m_method_factorial)( n); } uint64_t fibonacci( uint8_t n ) { return (*m_method_fibonacci)( n); } uint64_t thue_morse( uint8_t n ) { return (*m_method_thue_morse)( n); } ::DBus::signal_proxy<void,std::string,uint64_t,uint8_t >& signal_computation() { return *m_signal_computation; } protected: ::DBus::MethodProxy<double,double,double>::pointer m_method_add; ::DBus::MethodProxy<double,double,double>::pointer m_method_sub; ::DBus::MethodProxy<double,double,double>::pointer m_method_mul; ::DBus::MethodProxy<double,double,double>::pointer m_method_div; ::DBus::MethodProxy<double>::pointer m_method_pi; ::DBus::MethodProxy<void>::pointer m_method_print_pi; ::DBus::signal_proxy<void,std::string,std::string,double,double,double>::pointer m_signal_calculation; ::DBus::MethodProxy<uint64_t,uint8_t>::pointer m_method_factorial; ::DBus::MethodProxy<uint64_t,uint8_t>::pointer m_method_fibonacci; ::DBus::MethodProxy<uint64_t,uint8_t>::pointer m_method_thue_morse; ::DBus::signal_proxy<void,std::string,uint64_t,uint8_t>::pointer m_signal_computation; }; } } #endif
And here is the watcher application:
/*************************************************************************** * Copyright (C) 2009 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 "calculator_proxy.h" void print_calculation(std::string op, std::string opsym, double result, double param1, double param2); void print_computation(std::string op, uint64_t result, uint8_t n); int main() { DBus::init(); DBus::Dispatcher dispatcher; DBus::Connection::pointer connection = dispatcher.create_connection( DBus::BUS_SESSION ); DBus::Example::CalculatorProxy::pointer calculator = DBus::Example::CalculatorProxy::create(connection); calculator->signal_calculation().connect( sigc::ptr_fun(print_calculation) ); calculator->signal_computation().connect( sigc::ptr_fun(print_computation) ); dispatcher.start(); std::cout << "Running" << std::flush; for (int i=0; i < 10; i++) { std::cout << "." << std::flush; sleep(1); } dispatcher.stop(); std::cout << std::endl; return 0; } void print_calculation(std::string op, std::string opsym, double result, double param1, double param2) { std::cout << param1 << " " << opsym << " " << param2 << " = " << result << std::endl; } void print_computation(std::string op, uint64_t result, uint8_t n) { std::cout << op << "(" << (int)n << ") = " << result << std::endl; }