dbus-glib: Signals and g_main_context_new

bartolome moreno bartolome_34_680 at hotmail.com
Fri Jul 30 06:07:32 PDT 2010


Hello "DBUS-People",
 
it is my first post about smth. new for me, so please be patient :)
 
1- I took an example from the web, and I began to experiment a little. And I found smth. that I do not understand, and I found nowhere help.
2- The following code is a client application ( the server source code I´m going to ignore for the moment). The problem is in the "g_main_context_new" line.
 
Problem:
If I comment this line, this client application receive the signals from the server. I mean, I work with the default context.
If I leave this line, this client application receive the signals no more. I mean, I define a new context.
 
Questions:
Why the proxy stop to receive the signals from the server, when a new context (NOT the dafault one) is created and attached to the main loop?
 
Thanks a lot in advance, and if this is the wrong place to post this... sorry and please tell me where can I find help about this topic.
 
Thanks again
Bart
 
#include <dbus/dbus-glib.h>
...
 
// Timer callback function to call every second the setvalue1 server´s method. Until localValue > 10.
gboolean timerCallback(gpointer data)
{
 static gint localValue = -80;
 GError* error = NULL;
 TimerData* timerData = (TimerData*)data;
 org_maemo_Value_setvalue1(timerData->pProxy, localValue , &error);
 .....
 
 localValue += 10;
 if(10 < localValue)
 {
  g_print("timerCallback quit:%d.\n", quit);
  quit = 1;
  return FALSE;
 }
 return TRUE;
}
 
// Signal handler that receive the signals from server with a string parameter.
static void valueChangedSignalHandler(DBusGProxy* proxy, const char* valueName, gpointer userData)
{
 g_print("valueChangedSignalHandler - changed (%s).\n", valueName);
 if(quit)
 {
  g_print("valueChangedSignalHandler quit:%d.\n", quit);
  g_main_loop_quit((GMainLoop*)userData);
 }
}
 
int main(int argc , char ** argv)
{
 GMainLoop* mainloop = NULL;
 GMainContext* context = NULL; 
 GError* gerror = NULL;
 DBusGConnection* pGConnection = NULL;
 DBusGProxy* pGProxy = NULL;
  GSource* source = NULL;
 int id;
 g_type_init();
 
 // Create connection / proxy.
 g_print("Creating a GConnection\n");
 pGConnection = dbus_g_bus_get(DBUS_BUS_SESSION, &gerror);
 if(gerror != NULL)
 {
  g_printerr("could not open connection: %s\n", gerror->message);
  return 1;
 }
 g_print("Creating a GProxy\n");
 pGProxy = dbus_g_proxy_new_for_name( pGConnection,
                    "org.maemo.Platdev_ex", // name
                    "/GlobalValue", // obj path
                    "org.maemo.Value"); // interface
 if(pGProxy == NULL)
 {
  g_printerr("Couldn ’t create the proxy object ");
  return NULL;
 }
 
 //create a new time-out source
 source = g_timeout_source_new(1000);
 
 //create a main loop with context
 context = g_main_context_new(); <=== Problematic line :):) Not Commented: Stop receive signals
// context = g_main_context_new(); <=== Problematic line :):) Commented : Receive signals
 
  //attach source to context
  id = g_source_attach(source, context);
  
 mainloop = g_main_loop_new(context, FALSE);
 if(mainloop == NULL)
 {
  g_printerr("Failed to create the mainloop.\n");
  return 0;
 }
  
 //set the callback for this source
 TimerData timerData;
 timerData.mainLoop = mainloop;
 timerData.pProxy = pGProxy;
 g_source_set_callback(source, timerCallback, (gpointer)&timerData, NULL);
 
 g_print("Registering signal handler signatures.\n");
 dbus_g_proxy_add_signal(pGProxy, SIGNAL_CHANGED_VALUE1, G_TYPE_STRING, G_TYPE_INVALID);
 
 g_print("Registering D-Bus signal handlers. Thd %p\n");
 dbus_g_proxy_connect_signal(pGProxy, SIGNAL_CHANGED_VALUE1,
        G_CALLBACK(valueChangedSignalHandler),
        (gpointer)mainloop, NULL);
 
 g_main_loop_run(mainloop);
 
 g_source_destroy(source);
 
 g_source_unref(source);
 
 g_print("Exiting main loop\n");
 g_main_loop_unref(mainloop);
 g_print("Bye ...\n");
 return 0;
}

  		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20100730/f7341f95/attachment.html>


More information about the dbus mailing list