* Added a wrapper for dbus_threads_init_default
* Added a wrapper for dbus_connection_set_exit_on_disconnect * Added a multithreaded test case for the echo example git-svn-id: http://dev.openwengo.org/svn/openwengo/wengophone-ng/branches/wengophone-dbus-api/libs/dbus@11874 30a43799-04e7-0310-8b2b-ea0d24f86d0e
This commit is contained in:
parent
b116f68663
commit
568ec7fc45
10 changed files with 137 additions and 5 deletions
|
@ -81,7 +81,7 @@ AM_CONDITIONAL(HAVE_GTKMM, test 0 = 1)
|
|||
fi
|
||||
|
||||
AC_CHECK_LIB([expat], XML_ParserCreate_MM,
|
||||
[ AC_CHECK_HEADERS(expat.h, have_expat=true, have_expat=false) ],
|
||||
[AC_CHECK_HEADERS(expat.h, have_expat=true, have_expat=false)],
|
||||
have_expat=false)
|
||||
|
||||
if ! $have_expat; then
|
||||
|
@ -95,6 +95,12 @@ xml_LIBS=-lexpat
|
|||
AC_SUBST(xml_CFLAGS)
|
||||
AC_SUBST(xml_LIBS)
|
||||
|
||||
AC_CHECK_LIB([pthread], pthread_create,
|
||||
[AC_CHECK_HEADERS(pthread.h, have_pthread=true, have_pthread=false)],
|
||||
have_pthread=false)
|
||||
|
||||
AM_CONDITIONAL(HAVE_PTHREAD, test x$have_pthread = xtrue)
|
||||
|
||||
if test x$enable_debug = xyes ; then
|
||||
CXXFLAGS="-Wall -ggdb -O0 -DDEBUG"
|
||||
else
|
||||
|
|
|
@ -4,13 +4,23 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
|
|||
|
||||
noinst_PROGRAMS = echo-server
|
||||
|
||||
echo_server_SOURCES = echo-glue.h echo-server.h echo-server.cpp
|
||||
echo_server_SOURCES = echo-server-glue.h echo-server.h echo-server.cpp
|
||||
echo_server_LDADD = $(top_builddir)/src/libdbus-c++-1.la
|
||||
|
||||
echo-glue.h: echo-introspect.xml
|
||||
echo-server-glue.h: echo-introspect.xml
|
||||
$(top_builddir)/tools/dbusxx-xml2cpp $^ --adaptor=$@
|
||||
|
||||
BUILT_SOURCES = echo-glue.h
|
||||
if HAVE_PTHREAD
|
||||
noinst_PROGRAMS += echo-client-mt
|
||||
endif
|
||||
|
||||
echo_client_mt_SOURCES = echo-client-glue.h echo-client.h echo-client.cpp
|
||||
echo_client_mt_LDADD = $(top_builddir)/src/libdbus-c++-1.la -lpthread
|
||||
|
||||
echo-client-glue.h: echo-introspect.xml
|
||||
$(top_builddir)/tools/dbusxx-xml2cpp $^ --proxy=$@
|
||||
|
||||
BUILT_SOURCES = echo-server-glue.h echo-client-glue.h
|
||||
CLEANFILES = $(BUILT_SOURCES)
|
||||
|
||||
dist-hook:
|
||||
|
|
80
examples/echo/echo-client.cpp
Normal file
80
examples/echo/echo-client.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#include "echo-client.h"
|
||||
#include <iostream>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
static const char* ECHO_SERVER_NAME = "org.freedesktop.DBus.Examples.Echo";
|
||||
static const char* ECHO_SERVER_PATH = "/org/freedesktop/DBus/Examples/Echo";
|
||||
|
||||
EchoClient::EchoClient( DBus::Connection& connection, const char* path, const char* name )
|
||||
: DBus::ObjectProxy(connection, path, name)
|
||||
{
|
||||
}
|
||||
|
||||
void EchoClient::Echoed( const DBus::Variant& value )
|
||||
{
|
||||
cout << "!";
|
||||
}
|
||||
|
||||
/* NOTE: using many threads is likely to trigger a race condition in the event loop
|
||||
* in fact access to the list of timeouts and watches inside the default dispatcher
|
||||
* implementation (DBus::BusDispatcher) is NOT serialized
|
||||
*
|
||||
* (yes, I'm well aware of how much this sucks)
|
||||
*/
|
||||
static const int THREADS = 1;
|
||||
|
||||
static bool spin = true;
|
||||
|
||||
void* greeter_thread( void* )
|
||||
{
|
||||
DBus::Connection conn = DBus::Connection::SessionBus();
|
||||
|
||||
EchoClient client(conn, ECHO_SERVER_PATH, ECHO_SERVER_NAME);
|
||||
|
||||
while(spin)
|
||||
{
|
||||
client.Hello("client");
|
||||
|
||||
cout << "*";
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DBus::BusDispatcher dispatcher;
|
||||
|
||||
void niam( int sig )
|
||||
{
|
||||
spin = false;
|
||||
|
||||
dispatcher.leave();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
signal(SIGTERM, niam);
|
||||
signal(SIGINT, niam);
|
||||
|
||||
DBus::_init_threading();
|
||||
|
||||
DBus::default_dispatcher = &dispatcher;
|
||||
|
||||
pthread_t threads[THREADS];
|
||||
|
||||
for(int i = 0; i < THREADS; ++i)
|
||||
{
|
||||
pthread_create(threads+i, NULL, greeter_thread, NULL);
|
||||
}
|
||||
|
||||
dispatcher.enter();
|
||||
|
||||
for(int i = 0; i < THREADS; ++i)
|
||||
{
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
19
examples/echo/echo-client.h
Normal file
19
examples/echo/echo-client.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#ifndef __DEMO_ECHO_CLIENT_H
|
||||
#define __DEMO_ECHO_CLIENT_H
|
||||
|
||||
#include <dbus-c++/dbus.h>
|
||||
#include "echo-client-glue.h"
|
||||
|
||||
class EchoClient
|
||||
: public org::freedesktop::DBus::EchoDemo,
|
||||
public DBus::IntrospectableProxy,
|
||||
public DBus::ObjectProxy
|
||||
{
|
||||
public:
|
||||
|
||||
EchoClient( DBus::Connection& connection, const char* path, const char* name );
|
||||
|
||||
void Echoed( const DBus::Variant& value );
|
||||
};
|
||||
|
||||
#endif//__DEMO_ECHO_CLIENT_H
|
|
@ -2,7 +2,7 @@
|
|||
#define __DEMO_ECHO_SERVER_H
|
||||
|
||||
#include <dbus-c++/dbus.h>
|
||||
#include "echo-glue.h"
|
||||
#include "echo-server-glue.h"
|
||||
|
||||
class EchoServer
|
||||
: public org::freedesktop::DBus::EchoDemo,
|
||||
|
|
|
@ -18,6 +18,9 @@
|
|||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#undef HAVE_MEMORY_H
|
||||
|
||||
/* Define to 1 if you have the <pthread.h> header file. */
|
||||
#undef HAVE_PTHREAD_H
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#undef HAVE_STDINT_H
|
||||
|
||||
|
|
|
@ -91,6 +91,8 @@ public:
|
|||
|
||||
void disconnect();
|
||||
|
||||
void exit_on_disconnect( bool exit );
|
||||
|
||||
void flush();
|
||||
|
||||
bool send( const Message&, unsigned int* serial = NULL );
|
||||
|
|
|
@ -181,6 +181,8 @@ typedef bool (*CondVarWaitTimeoutFn)( CondVar* cv, Mutex* mx, int timeout );
|
|||
typedef void (*CondVarWakeOneFn)( CondVar* cv );
|
||||
typedef void (*CondVarWakeAllFn)( CondVar* cv );
|
||||
|
||||
void _init_threading();
|
||||
|
||||
void _init_threading(
|
||||
MutexNewFn, MutexFreeFn, MutexLockFn, MutexUnlockFn,
|
||||
CondVarNewFn, CondVarFreeFn, CondVarWaitFn, CondVarWaitTimeoutFn, CondVarWakeOneFn, CondVarWakeAllFn
|
||||
|
|
|
@ -282,6 +282,11 @@ void Connection::disconnect()
|
|||
dbus_connection_close(_pvt->conn);
|
||||
}
|
||||
|
||||
void Connection::exit_on_disconnect( bool exit )
|
||||
{
|
||||
dbus_connection_set_exit_on_disconnect(_pvt->conn, exit);
|
||||
}
|
||||
|
||||
bool Connection::unique_name( const char* n )
|
||||
{
|
||||
return dbus_bus_set_unique_name(_pvt->conn, n);
|
||||
|
|
|
@ -168,6 +168,11 @@ void Dispatcher::dispatch_pending()
|
|||
}
|
||||
}
|
||||
|
||||
void DBus::_init_threading()
|
||||
{
|
||||
dbus_threads_init_default();
|
||||
}
|
||||
|
||||
void DBus::_init_threading(
|
||||
MutexNewFn m1,
|
||||
MutexFreeFn m2,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue