dbus-cplusplus/examples/echo/echo-client.cpp
pdurante 7c420f87cd * Enabled the symbol visibility feature from gcc 4, reduces binary size and dynamic loading speed
* A lot of fixes to keep compatibility with older (0.6x) versions of libdbus
* Moved the xml handling code from the library to the code generator
* Rewrote the routine to generate introspection data
* Autojunk cleanup



git-svn-id: http://dev.openwengo.org/svn/openwengo/wengophone-ng/branches/wengophone-dbus-api/libs/dbus@12019 30a43799-04e7-0310-8b2b-ea0d24f86d0e
2007-07-23 18:31:49 +00:00

88 lines
1.7 KiB
C++

#include "echo-client.h"
#include <iostream>
#include <pthread.h>
#include <signal.h>
#ifdef HAVE_CONFIG_H
#include <dbus-c++/config.h>
#endif
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);
#ifdef DBUS_HAS_THREADS_INIT_DEFAULT
DBus::_init_threading();
#else
cerr << "Thread support is not enabled! your D-Bus version is too old" << endl;
#endif
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;
}