Created
July 9, 2015 19:58
-
-
Save aspratyush/dc50878393e6d291d2f3 to your computer and use it in GitHub Desktop.
QDBus-based dbus-signal receiving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************* SERVER : INTERFACE CLASS **************/ | |
#ifndef ICALCULATOR_H | |
#define ICALCULATOR_H | |
#include <QObject> | |
class ICalculator : public QObject | |
{ | |
Q_OBJECT | |
//D-Bus interface name | |
Q_CLASSINFO("D-Bus Interface", "com.wp.asp.Calculator") | |
public: | |
//explicit ICalculator(QObject *parent = 0); | |
virtual ~ICalculator(){} | |
signals: | |
void calculationCompleted(QString str); | |
public slots: | |
/** | |
* @brief helloWorld hello world from QDBus | |
*/ | |
virtual QString helloWorld() = 0; | |
/** | |
* @brief addition add two nos. | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int addition(int a, int b) = 0; | |
/** | |
* @brief subtraction subtract two nos. | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int subtraction(int a, int b) = 0; | |
/** | |
* @brief multiple multiply two nos. | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int multiply(int a, int b) = 0; | |
/** | |
* @brief divide divide two nos. | |
* @param a int | |
* @param b int | |
* @return float | |
*/ | |
virtual double divide(int a, int b) = 0; | |
}; | |
#endif // ICALCULATOR_H | |
/************* SERVER : INTERFACE IMPLEMENTATION **************/ | |
#ifndef CCALCULATOR_H | |
#define CCALCULATOR_H | |
#include <QObject> | |
#include <iostream> | |
/**** project includes ****/ | |
#include "icalculator.h" | |
class CCalculator : public ICalculator | |
{ | |
Q_OBJECT | |
public: | |
explicit CCalculator(); | |
~CCalculator(){} | |
/** | |
* @brief helloWorld implements the inherited hello world | |
*/ | |
virtual QString helloWorld(); | |
/** | |
* @brief addition add two nos. | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int addition(int a, int b); | |
/** | |
* @brief subtraction implements the inherited subtraction | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int subtraction(int a, int b); | |
/** | |
* @brief multiple implements the inherited multiply | |
* @param a int | |
* @param b int | |
* @return int | |
*/ | |
virtual int multiply(int a, int b); | |
/** | |
* @brief divide implements the inherited divide | |
* @param a int | |
* @param b int | |
* @return float | |
*/ | |
virtual double divide(int a, int b); | |
signals: | |
public slots: | |
}; | |
#endif // CCALCULATOR_H | |
/************* SERVER : MAIN **************/ | |
/**** system includes****/ | |
#include <QCoreApplication> | |
#include <memory> | |
/**** project includes****/ | |
#include "icalculator.h" | |
#include "ccalculator.h" | |
#include "calculatorAdapter.h" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
//init Obj | |
ICalculator* cal = new CCalculator(); | |
//inject Obj to DbusAdapter | |
new CalculatorAdaptor(cal); | |
//create conn to D-Bus | |
QDBusConnection conn = QDBusConnection::sessionBus(); | |
if (!conn.isConnected()){ | |
std::cerr<<"Error connecting to D-Bus\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<std::endl; | |
return EXIT_FAILURE; | |
} | |
//register object using serverImpl | |
if ( !conn.registerObject("/serverEg", cal) ){ | |
std::cerr<<"Error creating object\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<std::endl; | |
return EXIT_FAILURE; | |
} | |
std::cout<<"Object registered...\n"; | |
//register service | |
if ( !conn.registerService("com.wp.asp.ServerTest") ){ | |
std::cerr<<"Error registering service\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<std::endl; | |
return EXIT_FAILURE; | |
} | |
std::cout<<"Service registered...\n"; | |
std::cout<<"Waiting...\n"; | |
return a.exec(); | |
} | |
/*********** CLIENT : SIGNAL HANDLER CLASS ************/ | |
#ifndef ONSIGNALRECEIVED_H | |
#define ONSIGNALRECEIVED_H | |
#include <QObject> | |
#include <QCoreApplication> | |
#include <iostream> | |
class onSignalReceived : public QObject | |
{ | |
Q_OBJECT | |
public: | |
explicit onSignalReceived(QObject *parent = 0); | |
~onSignalReceived(); | |
signals: | |
public slots: | |
void signalReceived(QString str); | |
}; | |
#endif // ONSIGNALRECEIVED_H | |
/*************** CLIENT : MAIN ****************/ | |
/**** system includes****/ | |
#include <QCoreApplication> | |
#include <iostream> | |
/**** project includes****/ | |
#include "calculatorProxy.h" | |
#include "onsignalreceived.h" | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
//connect to DBus session-bus | |
QDBusConnection conn = QDBusConnection::sessionBus(); | |
if (!conn.isConnected()){ | |
std::cerr<<"Error connecting to D-Bus session-bus\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<std::endl; | |
return EXIT_FAILURE; | |
} | |
std::cout<<"Connected to session-bus\n"; | |
//connect to the interface exposed by the service | |
com::wp::asp::Calculator* iFace = new com::wp::asp::Calculator | |
("com.wp.asp.ServerTest", "/serverEg", conn, 0); | |
if (!iFace->isValid()){ | |
std::cerr<<"Error connecting to interface provided\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<std::endl; | |
return EXIT_FAILURE; | |
} | |
std::cout<<"Connected to interface..\n"; | |
//subscribe to the signal | |
onSignalReceived signalObj; | |
if ( !QObject::connect(iFace, SIGNAL(calculationCompleted(QString)), | |
&signalObj, SLOT(signalReceived(QString))) ){ | |
std::cerr<<"Error subscribing to signal\n"; | |
std::cerr<<qPrintable(conn.lastError().message())<<"\n"; | |
} | |
std::cout<<"Subscribed to signal...\n"; | |
int ch = 0; | |
int res; | |
//loop over choices | |
do{ | |
std::cout<<"Enter choice:\n" | |
"1) helloWorld\n" | |
"2) Addition\n" | |
"3) Subtraction\n" | |
"4) Multiple\n" | |
"5) Divide\n" | |
"0) exit\n"; | |
std::cin>>ch; | |
switch(ch){ | |
case 0:{ | |
break; | |
} | |
case 1:{ | |
QString str = iFace->helloWorld(); | |
std::cout<<"Server says = "<<str.toStdString()<<"\n"; | |
break; | |
} | |
case 2:{ | |
res = iFace->addition(12,13); | |
std::cout<<"12+13 = "<<res<<std::endl; | |
break; | |
} | |
case 3:{ | |
res = iFace->subtraction(13,14); | |
std::cout<<"13-14 = "<<res<<std::endl; | |
break; | |
} | |
case 4:{ | |
res = iFace->multiply(14,15); | |
std::cout<<"14*15 = "<<res<<std::endl; | |
break; | |
} | |
case 5:{ | |
res = iFace->divide(15,4); | |
std::cout<<"15/4 = "<<res<<std::endl; | |
break; | |
} | |
default:{ | |
std::cout<<"Invalid choice.. enter again"; | |
break; | |
} | |
} | |
}while(ch!=0); | |
delete(iFace); | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment