dbus-cxx logo

calculator_client.cpp

This example is one part of three example applications that demonstrate client, server and watcher applications that use adapters and proxies generated by dbus-cxx-xml2cpp from a modified dbus introspection XML document. These three examples are:

This particular piece is the client that uses the generated ObjectProxy derived class to call upon the calculator server to perform calculations.

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>

#include "calculator.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)
        {
          ::DBus::Path child_path;

          m_method_add_ddd = this->create_method< double,double,double >( "Calculator.Basic", "add" );
          m_method_sub_ddd = this->create_method< double,double,double >( "Calculator.Basic", "sub" );
          m_method_mul_ddd = this->create_method< double,double,double >( "Calculator.Basic", "mul" );
          m_method_div_ddd = this->create_method< double,double,double >( "Calculator.Basic", "div" );
          m_method_pi_d = this->create_method< double >( "Calculator.Basic", "pi" );
          m_method_print_pi_v = 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_ty = this->create_method< uint64_t,uint8_t >( "Calculator.Computed", "factorial" );
          m_method_fibonacci_ty = this->create_method< uint64_t,uint8_t >( "Calculator.Computed", "fibonacci" );
          m_method_thue_morse_ty = 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_ddd)( a, b); }

        double subtract( double a, double b ) { return (*m_method_sub_ddd)( a, b); }

        double multiply( double a, double b ) { return (*m_method_mul_ddd)( a, b); }

        double divide( double a, double b ) { return (*m_method_div_ddd)( a, b); }

        double pi( ) { return (*m_method_pi_d)(); }

        void print_pi( ) { return (*m_method_print_pi_v)(); }

        ::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_ty)( n); }

        uint64_t fibonacci( uint8_t n ) { return (*m_method_fibonacci_ty)( n); }

        uint64_t thue_morse( uint8_t n ) { return (*m_method_thue_morse_ty)( 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_ddd;
        ::DBus::MethodProxy<double,double,double>::pointer m_method_sub_ddd;
        ::DBus::MethodProxy<double,double,double>::pointer m_method_mul_ddd;
        ::DBus::MethodProxy<double,double,double>::pointer m_method_div_ddd;
        ::DBus::MethodProxy<double>::pointer m_method_pi_d;
        ::DBus::MethodProxy<void>::pointer m_method_print_pi_v;
        ::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_ty;
        ::DBus::MethodProxy<uint64_t,uint8_t>::pointer m_method_fibonacci_ty;
        ::DBus::MethodProxy<uint64_t,uint8_t>::pointer m_method_thue_morse_ty;
        ::DBus::signal_proxy<void,std::string,uint64_t,uint8_t>::pointer m_signal_computation;


    };

  }
}

#endif

And here is the client 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"

#include "client_popt.h"

int main(int argc, const char** argv)
{
  // Useful symbols declared:
  //   op: C string containing the op to perform
  //   opsym: C char containing the mathmatical symbol of the op
  //   param1: double containing parameter 1
  //   param2: double containing parameter 2
  CLIENT_PARSE_ARGS(argc, argv);

  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);

  double answer=NAN;
  uint64_t result;

  if      ( strcmp(op,"add")         == 0 ) { answer = calculator->add(param1,param2); std::cout << param1 << " + " << param2 << " = " << answer << std::endl; }
  else if ( strcmp(op,"sub")         == 0 ) { answer = calculator->subtract(param1,param2); std::cout << param1 << " - " << param2 << " = " << answer << std::endl; }
  else if ( strcmp(op,"mul")         == 0 ) { answer = calculator->multiply(param1,param2); std::cout << param1 << " * " << param2 << " = " << answer << std::endl; }
  else if ( strcmp(op,"div")         == 0 ) { answer = calculator->divide(param1,param2); std::cout << param1 << " / " << param2 << " = " << answer << std::endl; }
  else if ( strcmp(op,"pi")          == 0 ) { answer = calculator->pi();               std::cout << "pi = " << answer << std::endl; }
  else if ( strcmp(op,"print-pi")    == 0 ) { calculator->print_pi();                  std::cout << "printed pi on server" << std::endl; }
  else if ( strcmp(op,"factorial")   == 0 ) { result = calculator->factorial(param1);  std::cout << (uint64_t)param1 << "! = " << result << std::endl; }
  else if ( strcmp(op,"fibonacci")   == 0 ) { result = calculator->fibonacci(param1);  std::cout << "Fibonacci_" << (uint64_t)(param1) << " = " << result << std::endl; }
  else if ( strcmp(op,"thue-morse")  == 0 )
  {
    if ( param1 > 6.0 )
    {
      std::cout << "Thue-Morse limit is 6" << std::endl;
    }
    else
    {
      result = calculator->thue_morse(param1);
      std::cout << "Thue-Morse_" << (uint64_t)(param1) << " = " << result << std::endl;
      std::cout << "Thue-Morse_" << (uint64_t)(param1) << " = ";
      uint64_t mask = 0x0001;
      std::string binary;
      for (int i = 0; i < pow(2,param1); i++)
      {
        binary += (result&mask)?'1':'0';
        result = result >> 1;
      }
      for ( std::string::reverse_iterator i = binary.rbegin(); i != binary.rend(); i++ )
        std::cout << *i;
      std::cout << std::endl;
    }
  }

  return 0;
}

Generated on Fri Jun 26 15:50:37 2009 for dbus-cxx by doxygen 1.5.8