D-Bus Learning (9): Using XML to define the Singal sending and receiving example of D-Bus

Source: Internet
Author: User
Tags signal handler xml example

We have learned how to send and receive methods using the Glib elevation Bundle method. Now we want to learn how to send and receive singal. The xml example is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Node name = "/com/wei/MyObject">

<Interface name = "com. wei. MyObject. Sample">

<Method name = "Test">

<Arg name = "x" type = "u" direction = "in"/>

<Arg name = "d_ret" type = "d" direction = "out"/>

</Method>

<Signal name = "Hello">

<Arg name = "w_dialog" type = "s"/>

</Signal>

</Interface>

</Node>

We have learned how to process the Test method. Now we add a Hello signal. In order to make the processing method more concise, this signal only brings a string called w_dialog. Similarly, we used the expose-binding-tool to generate the header file wei_server2.h for the server and wei_client2.h for the client respectively. (For operation methods, see the previous two learning ).

Server Compilation

View wei_server2.h. Compared with the method, there is only a slight change. In the info of the object, the Hello description is added. As follows:

......

Const DBusGObjectInfo dbus_glib_com_wei_object_info = {

0,

Dbus_glib_com_wei_methods,

1,

"Com. wei. MyObject. Sample/0 Test/0 S/0x/0I/0u/0d_ret/0O/0F/0N/0d/0/0/0 ",
"Com. Wei. myobject. Sample/0 Hello/0/0"

,

"/0"

};

We need to add relevant code to the object. The header file com_wei_myobject2.h is as follows:

# Ifndef WEI_COM_WEI_MYOBJECT2_H

# Define WEI_COM_WEI_MYOBJECT2_H

Typedef struct ComWeiMyObject2 ComWeiMyObject2;

Typedef struct ComWeiMyObject2Class ComWeiMyObject2Class;

Struct ComWeiMyObject2

{

GObject parent;

};

Struct ComWeiMyObject2Class

{

GObjectClass parent;

};

# Define COM_WEI_MYOBJECT2_TYPE (com_wei_myobject2_get_type ())

GType com_wei_myobject2_get_type (void );

Gboolean com_wei_test (ComWeiMyObject2 * obj, const guint IN_x, gdouble * OUT_d_ret, GError ** error );

Void com_wei_hello (ComWeiMyObject2 * obj, const char * w_dialog );

# Endif

Add a function to send the Hello signal. The source file com_wei_myobject2.c is as follows:

# Include "com_wei_myobject2.h"

# Include <alias/dbus-glib.h>

Enum

{

HELLO_MESSAGE,

LAST_SIGNAL

};

Static guint signals [LAST_SIGNAL];

G_DEFINE_TYPE (ComWeiMyObject2, com_wei_myobject2, G_TYPE_OBJECT)

Static void com_wei_myobject2_init (ComWeiMyObject2 * object)

{

}

Static void com_wei_myobject2_class_init (ComWeiMyObject2Class * klass)

{
Signals [HELLO_MESSAGE] = g_signal_new

(

"Hello ",

// The signal name. I used to write it as "Hello". The report does not find the "Hello" signal. Therefore, you may need to change it to lowercase.

G_OBJECT_CLASS_TYPE (klass ),

G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,

0,

NULL, NULL,

G_cclosure_marshal_VOID _ STRING,

// The function to translate arrays of parameter values to signal emissions into C language callback invocations. It is set in gsf-al. h. If the structure parameters are complex, you need to define them by yourself. Refer to a good blog

In our example, we provide a simple STRING mode.

G_TYPE_NONE,

 

// Return value. The value is NONE because the signal is not returned.

1,

// Number of parameters

G_TYPE_STRING );

 
// Parameter type

}

Gboolean com_wei_test (ComWeiMyObject2 * obj, const guint IN_x,

Gdouble * OUT_d_ret, GError ** error)

{

Printf ("com_wei_test () get input param: x = % d/n", IN_x );

* OUT_d_ret = 0.99;

Return TRUE;

}

Void com_wei_hello (ComWeiMyObject2 * obj, const char * w_dialog)

{

G_signal_emit

(

Obj, signals [HELLO_MESSAGE], 0, w_dialog

);

 
// Signal release

}

In an object, the key is to define the signal and release the model. For the source file wei_server2.c, it is very simple. Just like the previous method example, you can call the com_wei_hello function to release the signal.

......

 Com_wei_hello (obj, "Hello, World! ");

......

Client Compilation

Wei_client2.h generated by using the release-bind-tool is the same as that in the previous method-only example and is not described. Wei_client2.c is as follows. We will focus on the singal section. For other sections, see

......

Static voidHello_signal_callback

(DBusGProxy * proxy, const char * string, gpointer user_data)

{

Printf ("received Hello signal % s/n", string );

}

......

Int main (INT argc, char ** argv)

{

Dbusgconnection * conn;

Dbusgproxy * proxy;

... // We recommend that you connect

// Create remote OBJ: proxy

Proxy = dbus_g_proxy_new_for_name (Conn, "com. Wei. Test", "/COM/Wei/myobject", "com. Wei. myobject. sample ");

Dbus_g_proxy_add_signal

(Proxy, "hello", g_type_string, g_type_invalid );

Dbus_g_proxy_connect_signal

(Proxy, "hello", g_callback (hello_signal_callback), null, null );

 

......

}

We track through skip-monitor, dbus_g_proxy_add_singal [Specifies the argument signature of a signal;. only necessary if the remote object does not support introspection.]

And dbus_g_proxy_connect_singal [Connect a signal handler to a proxy for a remote interface. When the remote interface emits the specified signal, the proxy will emit a corresponding GLib signal.]

Register with voddaemo to listen to the Hello signal of this interface.

Method call sender =: 1.72-> DEST = org. freedesktop. Export serial = 2 Path =/org/freedesktop/platform; interface = org. freedesktop. Memory; member = addmatch

 

Link: Me
Linux related articles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.