* 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:
pdurante 2007-07-07 01:23:13 +00:00
parent b116f68663
commit 568ec7fc45
10 changed files with 137 additions and 5 deletions

View 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;
}