#include #include #include #include //#include #include #include //#include //#include //#include #include #include #include #include #define SNWDBUS_PACKET_NONE 0 #define SNWDBUS_PACKET_42 42 #define SNWDBUS_PACKET_43 43 #define SNWDBUS_PACKET_44 44 static const char *array = "BYTE AS STRING"; #define MATCH_RULE "type='signal'" static DBusMessageIter argsOut; static DBusConnection *connection = NULL; static DBusMessage *msgOut = NULL; static char busName[255]; static int32_t currentPacketOut = -1; // This piece of code sends signals of type 42 and receives packets of type 43 int32_t snwdbus_init(const char *bus_name) { DBusError err; unsigned int flags = DBUS_NAME_FLAG_ALLOW_REPLACEMENT | DBUS_NAME_FLAG_REPLACE_EXISTING | DBUS_NAME_FLAG_DO_NOT_QUEUE; if (connection != NULL) { return -1; } if (!dbus_threads_init_default()) { return -1; } // initialise the errors dbus_error_init(&err); connection = dbus_bus_get(DBUS_BUS_SESSION, &err); dbus_connection_set_exit_on_disconnect(connection, FALSE); // stop this insanity dbus_bus_request_name(connection, bus_name, flags, &err); strcpy(busName, bus_name); // the match rule to match the message going through the bus dbus_bus_add_match(connection, MATCH_RULE, &err); return 0; } #define MAX_LEN_PACKET_NUM 16 void snwdbus_signal_start(int32_t packet) { char packetnumber[MAX_LEN_PACKET_NUM]; if (connection == NULL) { return; // not initialized yet } if (msgOut) { dbus_message_unref(msgOut); msgOut = NULL; } _snprintf(packetnumber, MAX_LEN_PACKET_NUM, "sig%d", packet); // emit new signal, give it name of object, interface, and signal, in that order msgOut = dbus_message_new_signal("/", busName, packetnumber); // append arguments onto signal dbus_message_iter_init_append(msgOut, &argsOut); currentPacketOut = packet; } void snwdbus_signal_add_int(int32_t value) { dbus_message_iter_append_basic(&argsOut, DBUS_TYPE_INT32, (int32_t*)&value); } void snwdbus_signal_send(void) { //Adds a message to the outgoing message queue. dbus_connection_send(connection, msgOut, NULL); //flush the connection dbus_connection_flush(connection); //free the message dbus_message_unref(msgOut); msgOut = NULL; currentPacketOut = -1; } void *program2( void *arg) { DBusError err; DBusConnection *connectionT; DBusMessage *msgInT = NULL; static DBusMessageIter argsInT; dbus_error_init(&err); connectionT = dbus_bus_get(DBUS_BUS_SESSION, &err); // loop listening for signals being emmitted while ( 1 ) { const char *packetnumber; int32_t currentParamIn = 1; int32_t currentPacketIn = 0; dbus_connection_read_write(connectionT, 0); msgInT = dbus_connection_pop_message(connectionT); if (msgInT == NULL) { continue; } packetnumber = dbus_message_get_member(msgInT); if (packetnumber) { sscanf(packetnumber, "sig%d", ¤tPacketIn); } // Happily accept the fact that some messages are empty. (void) dbus_message_iter_init(msgInT, &argsInT); { int sigvalue = currentPacketIn; if ( sigvalue == SNWDBUS_PACKET_42 ) { int32_t sigvalue = 0; dbus_message_iter_get_basic(&argsInT, &sigvalue); dbus_message_iter_next(&argsInT); } else if ( sigvalue == SNWDBUS_PACKET_44 ) { return NULL; } } } } void send_iteration( int iteration ) { snwdbus_signal_start( SNWDBUS_PACKET_42 ); snwdbus_signal_add_int( iteration ); snwdbus_signal_send(); } #define MAX_THREADS 3 int main (int argc, char **argv) { int iterations = 0; DWORD id; HANDLE hThreadArray[MAX_THREADS]; int retval = snwdbus_init( "test.test" ); assert( retval == 0 ); CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)program2, 0, 0, &id); // SEND FIRST PING send_iteration( iterations ); while ( 1 ) { iterations++; send_iteration( iterations ); if ( iterations % 10 == 1 ) { fprintf(stderr, "gone %d iterations\n", iterations); } if ( iterations == 500000 ) { snwdbus_signal_start( SNWDBUS_PACKET_44 ); // ask child to quit snwdbus_signal_send(); Sleep( 10 ); exit( 0 ); } } return 0; }