Redone properties example
This commit is contained in:
parent
fcf526f7f2
commit
96eb77ce7e
14 changed files with 102 additions and 206 deletions
|
@ -187,7 +187,6 @@ AC_OUTPUT(
|
||||||
doc/Doxyfile
|
doc/Doxyfile
|
||||||
examples/Makefile
|
examples/Makefile
|
||||||
examples/properties/Makefile
|
examples/properties/Makefile
|
||||||
examples/properties_get_set/Makefile
|
|
||||||
examples/echo/Makefile
|
examples/echo/Makefile
|
||||||
examples/hal/Makefile
|
examples/hal/Makefile
|
||||||
examples/glib/Makefile
|
examples/glib/Makefile
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
SUBDIRS = properties properties_get_set echo hal glib
|
SUBDIRS = properties echo hal glib
|
||||||
|
|
||||||
MAINTAINERCLEANFILES = \
|
MAINTAINERCLEANFILES = \
|
||||||
Makefile.in
|
Makefile.in
|
||||||
|
|
|
@ -1,16 +1,22 @@
|
||||||
EXTRA_DIST = README props-introspect.xml
|
EXTRA_DIST = propsget-introspect.xml
|
||||||
|
|
||||||
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
|
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
|
||||||
|
|
||||||
noinst_PROGRAMS = props-server
|
noinst_PROGRAMS = propsgs-server propsgs-client
|
||||||
|
|
||||||
props_server_SOURCES = props-glue.h props-server.h props-server.cpp
|
propsgs_server_SOURCES = propsgs-glue.h propsgs-server.h propsgs-server.cpp
|
||||||
props_server_LDADD = $(top_builddir)/src/libdbus-c++-1.la
|
propsgs_server_LDADD = $(top_builddir)/src/libdbus-c++-1.la
|
||||||
|
|
||||||
props-glue.h: props-introspect.xml
|
propsgs_client_SOURCES = propsgs-glue.h propsgs-client.h propsgs-client.cpp
|
||||||
$(top_builddir)/tools/dbusxx-xml2cpp $^ --adaptor=$@
|
propsgs_client_LDADD = $(top_builddir)/src/libdbus-c++-1.la -lpthread
|
||||||
|
|
||||||
BUILT_SOURCES = props-glue.h
|
propsgs-glue-adaptor.h: propsgs-introspect.xml
|
||||||
|
$(top_builddir)/tools/dbusxx-xml2cpp $^ --adaptor=$@ --
|
||||||
|
|
||||||
|
propsgs-glue-proxy.h: propsgs-introspect.xml
|
||||||
|
$(top_builddir)/tools/dbusxx-xml2cpp $^ --proxy=$@ --
|
||||||
|
|
||||||
|
BUILT_SOURCES = propsgs-glue-adaptor.h propsgs-glue-proxy.h
|
||||||
CLEANFILES = $(BUILT_SOURCES)
|
CLEANFILES = $(BUILT_SOURCES)
|
||||||
|
|
||||||
dist-hook:
|
dist-hook:
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
This very simple example shows how to export properties (from objects implementing the org.freedesktop.DBus.Properties interface)
|
|
||||||
|
|
||||||
To test, run `DBUSXX_VERBOSE=1 ./props-server` and try the following commands:
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Get string:"org.freedesktop.DBus.PropsDemo" string:"Version"
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Set string:"org.freedesktop.DBus.PropsDemo" string:"Version" int32:2
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Set string:"org.freedesktop.DBus.PropsDemo" string:"Message" variant:string:"Hello D-Bus"
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Set string:"org.freedesktop.DBus.PropsDemo" string:"Message" variant:int32:200
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Get string:"org.freedesktop.DBus.PropsDemo" string:"Message"
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Get string:"org.freedesktop.DBus.PropsDemo" string:"Something"
|
|
||||||
|
|
||||||
dbus-send --dest=org.freedesktop.DBus.Examples.Properties --type=method_call --print-reply /org/freedesktop/DBus/Examples/Properties org.freedesktop.DBus.Properties.Get string:"org.freedesktop.DBus.PropsDemo" int32:100
|
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
<?xml version="1.0" ?>
|
|
||||||
<node name="/org/freedesktop/DBus/Examples/Properties">
|
|
||||||
<interface name="org.freedesktop.DBus.PropsDemo">
|
|
||||||
<property name="Version" type="i" access="read"/>
|
|
||||||
<property name="Message" type="s" access="readwrite"/>
|
|
||||||
<signal name="MessageChanged">
|
|
||||||
<arg name="message" type="s"/>
|
|
||||||
</signal>
|
|
||||||
</interface>
|
|
||||||
</node>
|
|
|
@ -1,46 +0,0 @@
|
||||||
#include "props-server.h"
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
static const char *PROPS_SERVER_NAME = "org.freedesktop.DBus.Examples.Properties";
|
|
||||||
static const char *PROPS_SERVER_PATH = "/org/freedesktop/DBus/Examples/Properties";
|
|
||||||
|
|
||||||
PropsServer::PropsServer(DBus::Connection &connection)
|
|
||||||
: DBus::ObjectAdaptor(connection, PROPS_SERVER_PATH)
|
|
||||||
{
|
|
||||||
Version = 1;
|
|
||||||
Message = "default message";
|
|
||||||
}
|
|
||||||
|
|
||||||
void PropsServer::on_set_property
|
|
||||||
(DBus::InterfaceAdaptor &interface, const std::string &property, const DBus::Variant &value)
|
|
||||||
{
|
|
||||||
if (property == "Message")
|
|
||||||
{
|
|
||||||
std::string msg = value;
|
|
||||||
this->MessageChanged(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DBus::BusDispatcher dispatcher;
|
|
||||||
|
|
||||||
void niam(int sig)
|
|
||||||
{
|
|
||||||
dispatcher.leave();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
signal(SIGTERM, niam);
|
|
||||||
signal(SIGINT, niam);
|
|
||||||
|
|
||||||
DBus::default_dispatcher = &dispatcher;
|
|
||||||
|
|
||||||
DBus::Connection conn = DBus::Connection::SessionBus();
|
|
||||||
conn.request_name(PROPS_SERVER_NAME);
|
|
||||||
|
|
||||||
PropsServer server(conn);
|
|
||||||
|
|
||||||
dispatcher.enter();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
#ifndef __DEMO_PROPS_SERVER_H
|
|
||||||
#define __DEMO_PROPS_SERVER_H
|
|
||||||
|
|
||||||
#include <dbus-c++/dbus.h>
|
|
||||||
#include "props-glue.h"
|
|
||||||
|
|
||||||
class PropsServer
|
|
||||||
: public org::freedesktop::DBus::PropsDemo_adaptor,
|
|
||||||
public DBus::IntrospectableAdaptor,
|
|
||||||
public DBus::PropertiesAdaptor,
|
|
||||||
public DBus::ObjectAdaptor
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
PropsServer(DBus::Connection &connection);
|
|
||||||
|
|
||||||
void on_set_property
|
|
||||||
(DBus::InterfaceAdaptor &interface, const std::string &property, const DBus::Variant &value);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif//__DEMO_PROPS_SERVER_H
|
|
72
examples/properties/propsgs-client.cpp
Normal file
72
examples/properties/propsgs-client.cpp
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#include "propsgs-client.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
using namespace org::freedesktop::DBus;
|
||||||
|
|
||||||
|
static const char *PROPS_SERVER_NAME = "org.freedesktop.DBus.Examples.Properties";
|
||||||
|
static const char *PROPS_SERVER_PATH = "/org/freedesktop/DBus/Examples/Properties";
|
||||||
|
|
||||||
|
PropsClient::PropsClient(DBus::Connection &connection, const char *path, const char *name)
|
||||||
|
: DBus::ObjectProxy(connection, path, name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void PropsClient::MessageChanged(const std::string& message)
|
||||||
|
{
|
||||||
|
std::cout << "MessageChanged signal, new value: " << message << "\n";
|
||||||
|
};
|
||||||
|
|
||||||
|
void PropsClient::DataChanged(const double& data)
|
||||||
|
{
|
||||||
|
std::cout << "DataChanged signal, new value:" << data << "\n";
|
||||||
|
};
|
||||||
|
|
||||||
|
void *test_property_proxy(void * input)
|
||||||
|
{
|
||||||
|
PropsClient *client = static_cast<PropsClient*>(input);
|
||||||
|
|
||||||
|
std::cout << "read property 'Version', value:" << client->Version() << "\n";
|
||||||
|
|
||||||
|
std::cout << "read property 'Message', value:" << client->Message() << "\n";
|
||||||
|
|
||||||
|
client->Message("message set by property access");
|
||||||
|
std::cout << "wrote property 'Message'\n";
|
||||||
|
|
||||||
|
std::cout << "read property 'Message', value:" << client->Message() << "\n";
|
||||||
|
|
||||||
|
client->Data(1.1);
|
||||||
|
std::cout << "wrote property 'Data'\n";
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
DBus::BusDispatcher dispatcher;
|
||||||
|
|
||||||
|
void niam(int sig)
|
||||||
|
{
|
||||||
|
dispatcher.leave();
|
||||||
|
pthread_exit(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
signal(SIGTERM, niam);
|
||||||
|
signal(SIGINT, niam);
|
||||||
|
|
||||||
|
DBus::default_dispatcher = &dispatcher;
|
||||||
|
|
||||||
|
DBus::_init_threading();
|
||||||
|
|
||||||
|
DBus::Connection conn = DBus::Connection::SessionBus();
|
||||||
|
|
||||||
|
PropsClient client (conn, PROPS_SERVER_PATH, PROPS_SERVER_NAME);
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, NULL, test_property_proxy, &client);
|
||||||
|
|
||||||
|
dispatcher.enter();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -7,7 +7,7 @@
|
||||||
class PropsClient
|
class PropsClient
|
||||||
: public org::freedesktop::DBus::PropsGSDemo_proxy,
|
: public org::freedesktop::DBus::PropsGSDemo_proxy,
|
||||||
public DBus::IntrospectableProxy,
|
public DBus::IntrospectableProxy,
|
||||||
// public DBus::PropertiesProxy,
|
public DBus::PropertiesProxy,
|
||||||
public DBus::ObjectProxy
|
public DBus::ObjectProxy
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -15,6 +15,8 @@ public:
|
||||||
PropsClient(DBus::Connection &connection, const char *path, const char *name);
|
PropsClient(DBus::Connection &connection, const char *path, const char *name);
|
||||||
|
|
||||||
void MessageChanged(const std::string& message);
|
void MessageChanged(const std::string& message);
|
||||||
|
|
||||||
|
void DataChanged(const double& data);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif//__DEMO_PROPS_SERVER_H
|
#endif//__DEMO_PROPS_SERVER_H
|
|
@ -7,5 +7,8 @@
|
||||||
<signal name="MessageChanged">
|
<signal name="MessageChanged">
|
||||||
<arg name="message" type="s"/>
|
<arg name="message" type="s"/>
|
||||||
</signal>
|
</signal>
|
||||||
|
<signal name="DataChanged">
|
||||||
|
<arg name="Data" type="d"/>
|
||||||
|
</signal>
|
||||||
</interface>
|
</interface>
|
||||||
</node>
|
</node>
|
|
@ -1,4 +1,5 @@
|
||||||
#include "propsgs-server.h"
|
#include "propsgs-server.h"
|
||||||
|
#include <iostream>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
static const char *PROPS_SERVER_NAME = "org.freedesktop.DBus.Examples.Properties";
|
static const char *PROPS_SERVER_NAME = "org.freedesktop.DBus.Examples.Properties";
|
||||||
|
@ -16,9 +17,18 @@ void PropsServer::on_set_property
|
||||||
{
|
{
|
||||||
if (property == "Message")
|
if (property == "Message")
|
||||||
{
|
{
|
||||||
|
std::cout << "'Message' has been changed\n";
|
||||||
|
|
||||||
std::string msg = value;
|
std::string msg = value;
|
||||||
this->MessageChanged(msg);
|
this->MessageChanged(msg);
|
||||||
}
|
}
|
||||||
|
if (property == "Data")
|
||||||
|
{
|
||||||
|
std::cout << "'Data' has been changed\n";
|
||||||
|
|
||||||
|
double data = value;
|
||||||
|
this->DataChanged(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DBus::BusDispatcher dispatcher;
|
DBus::BusDispatcher dispatcher;
|
|
@ -1,26 +0,0 @@
|
||||||
EXTRA_DIST = propsget-introspect.xml
|
|
||||||
|
|
||||||
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
|
|
||||||
|
|
||||||
noinst_PROGRAMS = propsgs-server propsgs-client
|
|
||||||
|
|
||||||
propsgs_server_SOURCES = propsgs-glue.h propsgs-server.h propsgs-server.cpp
|
|
||||||
propsgs_server_LDADD = $(top_builddir)/src/libdbus-c++-1.la
|
|
||||||
|
|
||||||
propsgs_client_SOURCES = propsgs-glue.h propsgs-client.h propsgs-client.cpp
|
|
||||||
propsgs_client_LDADD = $(top_builddir)/src/libdbus-c++-1.la -lpthread
|
|
||||||
|
|
||||||
propsgs-glue-adaptor.h: propsgs-introspect.xml
|
|
||||||
$(top_builddir)/tools/dbusxx-xml2cpp $^ --adaptor=$@ --
|
|
||||||
|
|
||||||
propsgs-glue-proxy.h: propsgs-introspect.xml
|
|
||||||
$(top_builddir)/tools/dbusxx-xml2cpp $^ --proxy=$@ --
|
|
||||||
|
|
||||||
BUILT_SOURCES = propsgs-glue-adaptor.h propsgs-glue-proxy.h
|
|
||||||
CLEANFILES = $(BUILT_SOURCES)
|
|
||||||
|
|
||||||
dist-hook:
|
|
||||||
cd $(distdir); rm -f $(BUILT_SOURCES)
|
|
||||||
|
|
||||||
MAINTAINERCLEANFILES = \
|
|
||||||
Makefile.in
|
|
|
@ -1,75 +0,0 @@
|
||||||
#include "propsgs-client.h"
|
|
||||||
#include <signal.h>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace org::freedesktop::DBus;
|
|
||||||
|
|
||||||
#define P(x) std::cout << #x << " = " << x << std::endl;
|
|
||||||
|
|
||||||
static const char *PROPS_SERVER_NAME = "org.freedesktop.DBus.Examples.Properties";
|
|
||||||
static const char *PROPS_SERVER_PATH = "/org/freedesktop/DBus/Examples/Properties";
|
|
||||||
|
|
||||||
PropsClient::PropsClient(DBus::Connection &connection, const char *path, const char *name)
|
|
||||||
: DBus::ObjectProxy(connection, path, name)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void PropsClient::MessageChanged(const std::string& message){
|
|
||||||
std::cout << "message received: " << message << std::endl;
|
|
||||||
};
|
|
||||||
|
|
||||||
DBus::BusDispatcher dispatcher;
|
|
||||||
|
|
||||||
PropsClient * client;
|
|
||||||
|
|
||||||
void niam(int sig)
|
|
||||||
{
|
|
||||||
dispatcher.leave();
|
|
||||||
pthread_exit(NULL);
|
|
||||||
delete client;
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <pthread.h>
|
|
||||||
|
|
||||||
void * test_property_proxy(void * input){
|
|
||||||
P("1");
|
|
||||||
sleep(2);
|
|
||||||
P(client->Version());
|
|
||||||
|
|
||||||
P("2");
|
|
||||||
sleep(1);
|
|
||||||
P(client->Message());
|
|
||||||
|
|
||||||
P("3");
|
|
||||||
sleep(1);
|
|
||||||
client->Message( "message set by property access" );
|
|
||||||
|
|
||||||
P("4");
|
|
||||||
sleep(1);
|
|
||||||
P(client->Message());
|
|
||||||
|
|
||||||
P("5");
|
|
||||||
sleep(1);
|
|
||||||
client->Data( 1.1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
signal(SIGTERM, niam);
|
|
||||||
signal(SIGINT, niam);
|
|
||||||
|
|
||||||
DBus::default_dispatcher = &dispatcher;
|
|
||||||
|
|
||||||
DBus::Connection conn = DBus::Connection::SessionBus();
|
|
||||||
|
|
||||||
client = new PropsClient(conn, PROPS_SERVER_PATH, PROPS_SERVER_NAME );
|
|
||||||
|
|
||||||
pthread_t thread;
|
|
||||||
pthread_create(&thread, NULL, test_property_proxy, 0);
|
|
||||||
|
|
||||||
P("dispatcher.enter();");
|
|
||||||
|
|
||||||
dispatcher.enter();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue