How to get the name of the caller of a method in glib-dbus

Till Wimmer g4-lisz at tonarchiv.ch
Fri Aug 28 07:52:09 PDT 2009


Till Wimmer schrieb:
> Ali Abdallah schrieb:
>   
>> Till Wimmer wrote:
>>     
>>> Hello,
>>>
>>> i wrote a daemon which exposes a method ("invoke") to the bus:
>>>
>>>   dbus_g_object_type_install_info(...)
>>>   dbus_g_connection_register_g_object(...)
>>>
>>> This works fine.
>>>
>>> Now i'd like to find the unique connection name of the client who 
>>> calls the exposed method.
>>>   
>>>       
>> <method name="YourMethod">
>> <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
>>  <arg....... >
>> </method>
>>
>> Then the exposed method in your GObject can be like:
>>
>> method_name (YourObject, args, DBusGMethodInvocation *context)
>>
>> And from the context you get
>> dbus_g_method_get_sender (context); /* The unique name */
>>
>> Don't forget to return something with dbus_g_method_return at the end.
>>
>> Hope this is what you want.
>> Cheers,
>> Ali.
>>     
> Hi Ali,
>
> thanks four your help!
>
> The DBusGMethodInvocation * argument was the missing link because my 
> method looked like this before:
>
> gboolean skypeapi_dbusobj_invoke(SkypeAPIDBusObj *obj, const char 
> *message, char **ret, GError **error) {
>
>    g_assert(obj != NULL);
>   
>    obj->str_ret = [ some code here to get the response ];
>    *ret = obj->str_ret;
>
>    return TRUE;
> }
>
> Now i changed the methods XML to:
> <node>
>   <interface name="com.Skype.API">
>     <!-- Method definition -->
>     <!-- char *Invoke(char *message) -->
>     <method name="Invoke">
>       <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
>       <arg type="s" name="message" direction="in"/>
>       <arg type="s" name="str_ret" direction="out"/>
>     </method>
>   </interface>
> </node>
>
> How do i rewrite the skypeapi_dbusobj_invoke function with 
> dbus_g_method_return at the end?
>   
I got the solution thanks to the howto here:
http://live.gnome.org/DBusGlibBindings:

void skypeapi_dbusobj_invoke(SkypeAPIDBusObj *obj, const char *message,
DBusGMethodInvocation *context) {
   g_assert(obj != NULL);

   printf("Sender = %s\n", (char *)dbus_g_method_get_sender(context));

   char *ret = [ some code here to get the response ];

   dbus_g_method_return(context, ret);
}

Now everything works like expected!
Thanks again,
TW



More information about the dbus mailing list