D-Bus Learning (8): Using XML to define the Server of D-Bus

Source: Internet
Author: User

Thanks to http://blog.chinaunix.net/u3/111961/showart_2187819.html first

. This is an excellent article.

In the last study, we wrote a small client example, which is very simple and the header file generated by using producer-binding-tool is very standard. Server is a little more complex than execution. It still uses the last xml file, but removes annotation, which is more primitive. The file wei. xml 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>

</Interface>

</Node>

The client applet was last learned and used for experiments. The following describes the procedure in detail.

Step 1: generate a header file

Export-binding-tool -- mode = glib-server -- prefix = com_wei wei. xml> wei_server.h

Note that -- prefix is an indispensable parameter, which is not mentioned in some articles. At least I need to test it in the Moblin operating system. Generally, a differentiated namespace is also required for a project. Whether required or not, we recommend that you add it. The "-- prefix" parameter defines the object prefix. If the object prefix is $ (prefix), the generated DBusGObjectInfo structure variable name is dbus_glib _ $ (prefix) _ object_info.

The header file wei_server.h is generated as follows:

/* Generated by using-binding-tool; do not edit! */

# Ifndef _ dbus_glib_marshal_com_wei_MARSHAL_H __

# Define _ dbus_glib_marshal_com_wei_MARSHAL_H __

# Include <glib-object.h>

G_BEGIN_DECLS

# Ifdef G_ENABLE_DEBUG

# Define g_marshal_value_peek_boolean (v) g_value_get_boolean (v)

# Define g_marshal_value_peek_char (v) g_value_get_char (v)

# Define g_marshal_value_peek_uchar (v) g_value_get_uchar (v)

# Define g_marshal_value_peek_int (v) g_value_get_int (v)

# Define g_marshal_value_peek_uint (v) g_value_get_uint (v)

# Define g_marshal_value_peek_long (v) g_value_get_long (v)

# Define g_marshal_value_peek_ulong (v) g_value_get_ulong (v)

# Define g_marshal_value_peek_int64 (v) g_value_get_int64 (v)

# Define g_marshal_value_peek_uint64 (v) g_value_get_uint64 (v)

# Define g_marshal_value_peek_enum (v) g_value_get_enum (v)

# Define g_marshal_value_peek_flags (v) g_value_get_flags (v)

# Define g_marshal_value_peek_float (v) g_value_get_float (v)

# Define g_marshal_value_peek_double (v) g_value_get_double (v)

# Define g_marshal_value_peek_string (v) (char *) g_value_get_string (v)

# Define g_marshal_value_peek_param (v) g_value_get_param (v)

# Define g_marshal_value_peek_boxed (v) g_value_get_boxed (v)

# Define g_marshal_value_peek_pointer (v) g_value_get_pointer (v)

# Define g_marshal_value_peek_object (v) g_value_get_object (v)

# Else /*! G_ENABLE_DEBUG */

/* WARNING: This code accesses GValues directly, which is unsupported api.

* Do not access GValues directly in your code. Instead, use

* G_value_get _ * () Functions

*/

# Define g_marshal_value_peek_boolean (V) (v)-> data [0]. v_int

# Define g_marshal_value_peek_char (V) (v)-> data [0]. v_int

# Define g_marshal_value_peek_uchar (V) (v)-> data [0]. v_uint

# Define g_marshal_value_peek_int (V) (v)-> data [0]. v_int

# Define g_marshal_value_peek_uint (V) (v)-> data [0]. v_uint

# Define g_marshal_value_peek_long (V) (v)-> data [0]. v_long

# Define g_marshal_value_peek_ulong (V) (v)-> data [0]. v_ulong

# Define g_marshal_value_peek_int64 (V) (v)-> data [0]. v_int64

# Define g_marshal_value_peek_uint64 (V) (v)-> data [0]. v_uint64

# Define g_1_al_value_peek_enum (V) (v)-> data [0]. v_long

# Define g_marshal_value_peek_flags (V) (v)-> data [0]. v_ulong

# Define g_marshal_value_peek_float (V) (v)-> data [0]. v_float

# Define g_marshal_value_peek_double (V) (v)-> data [0]. v_double

# Define g_marshal_value_peek_string (V) (v)-> data [0]. v_pointer

# Define g_marshal_value_peek_param (V) (v)-> data [0]. v_pointer

# Define g_marshal_value_peek_boxed (V) (v)-> data [0]. v_pointer

# Define g_marshal_value_peek_pointer (V) (v)-> data [0]. v_pointer

# Define g_marshal_value_peek_object (V) (v)-> data [0]. v_pointer

# Endif /*! G_enable_debug */

/* BOOLEAN: UINT, POINTER, POINTER (/tmp/dbus-binding-tool-c-marshallers.OI8HBV: 1 )*/

Extern void dbus_glib_marshal_com_wei_BOOLEAN _ UINT_POINTER_POINTER (GClosure * closure,

GValue * return_value,

Guint n_param_values,

Const GValue * param_values,

Gpointer invocation_hint,

Gpointer implements al_data );

Void

Dbus_glib_marshal_com_wei_boolean _ uint_pointer_pointer (gclosure * closure,

Gvalue * return_value g_gnuc_unused,

Guint n_param_values,

Const gvalue * param_values,

Gpointer invocation_hint g_gnuc_unused,

Gpointer implements al_data)

{

Typedef gboolean (* GMarshalFunc_BOOLEAN _ UINT_POINTER_POINTER) (gpointer data1,

Guint arg_1,

Gpointer arg_2,

Gpointer arg_3,

Gpointer data2 );

Register GMarshalFunc_BOOLEAN _ UINT_POINTER_POINTER callback;

Register GCClosure * cc = (GCClosure *) closure;

Register gpointer data1, data2;

Gboolean v_return;


G_return_if_fail (return_value! = NULL );

G_return_if_fail (n_param_values = 4 );


If (G_CCLOSURE_SWAP_DATA (closure ))

{

Data1 = closure-> data;

Data2 = g_value_peek_pointer (param_values + 0 );

}

Else

{

Data1 = g_value_peek_pointer (param_values + 0 );

Data2 = closure-> data;

}

Callback = (gexternalfunc_boolean _ UINT_POINTER_POINTER) (external_data? Marshal_data: cc-> callback );


V_return = callback (data1,

G_external_value_peek_uint (param_values + 1 ),

G_external_value_peek_pointer (param_values + 2 ),

G_external_value_peek_pointer (param_values + 3 ),

Data2 );


G_value_set_boolean (return_value, v_return );

}

G_end_decls

# Endif/* _ dbus_glib_marshal_com_wei_marshal_h __*/

# Include <alias/dbus-glib.h>

Static constDBusGMethodInfo

Dbus_glib_com_wei_methods [] = {

{(Gcallback)Com_wei_test

, Dbus_glib_marshal_com_wei_boolean _ uint_pointer_pointer, 0 },

};

ConstDBusGObjectInfo

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 ",

"/0 ",

"/0"

};

Step 2: Compile the Object file

Learning from D-Bus (5)

And learning (6) in progress

We provide an example of the underlying layer for direct communication operations without involving the concept of objects. We can generate objects using examples written by the Glib high-level interface, define methods and command operations in interfaces in objects. To be honest, I am not familiar with non-JAVA objects, and GObject is more ......, But just use the tool. Since we provide the Object Path in xml, We can name the object ComWeiMyObject. The header file com_wei_myobject.h is as follows:

# Ifndef COM_WEI_MYOBJECT_H

# Define COM_WEI_MYOBJECT_H

Typedef struct ComWeiMyObject;

Typedef struct ComWeiMyObjectClass;

Struct ComWeiMyObject

{

GObject parent;

};

Struct ComWeiMyObjectClass

{

GObjectClass parent;

};

# DefineCom_wei_myobject_type

(Com_wei_myobject_get_type ())

Gtype com_wei_myobject_get_type (void );
Gboolean com_wei_test (comweimyobject * OBJ, const guint in_x, gdouble * out_d_ret, gerror ** error );

# Endif

In the header file, we define a method processing function com_wei_test, Which is com_wei_test in wei_server.h. Its first parameter is the object, followed by the parameters of this method, including input and output. The following is the com_wei_myobject.c file:

# Include "com_wei_myobject.h"

G_define_type (comweimyobject, com_wei_myobject, g_type_object)

Static void com_wei_myobject_init (comweimyobject * object)

{// The two init functions are basically gobject routines. In this simple example, there is no special initialization process.

}

Static void com_wei_myobject_class_init (comweimyobjectclass * Klass)

{

}

GbooleanCom_wei_test

(Comweimyobject * OBJ, const guint in_x, gdouble * out_d_ret, gerror ** error)

{// We only perform the test, simply check the input parameters, and directly reply to the output results.

Printf ("com_wei_test () Get input Param: x = % d/N", in_x );

* Out_d_ret = 0.99;

Return true;

}

The experiment example is as simple as possible. The test method of COM. Wei. myobject. Sample interface is implemented in the comweimyobject object: com_wei_test.

Step 3: Write the server program

OK. We are ready to write the server program. Because the server needs to listen for a long time, we need to add a loop. Loop has learned 4 in D-bus.

Asynchronous examples. We noticed the difference between using the function in glib and the underlying libruntime API. For example, the underlying API uses dbus_bus_get, and the high-level glib uses dbus_g_bus_get. This naming method can be used for reference when we provide higher-level interfaces. Glib's D-bus api reference for http://dbus.freedesktop.org/doc/api/html/group__DBusGLib.html

# Include "com_wei_myobject.h"

# Include "wei_server.h" // pay attention to the order of the two files

Int main (INT argc, char ** argv)

{
DBusGConnection * conn;

GMainLoop * main_loop = NULL;

ComWeiMyObject * obj;

GError * error = NULL;

DBusGProxy * bus_proxy;

Int request_name_result;

G_type_init ();

Main_loop = g_main_loop_new (NULL, FALSE );

 
// DBusGObjectInfo dbus_glib_com_wei_object_info is defined in the automatically generated wei_server.h,
Install introspection information about the given object GType
Sufficient to allow methods on the object to be invoked by name.

In this way, you can trigger the call method when receiving the information.

Dbus_g_object_type_install_info (COM_WEI_MYOBJECT_TYPE

, & Dbus_glib_com_wei_object_info );

// We recommend that you connect to session persistence. You have learned this before.

Conn = dbus_g_bus_get (DBUS_BUS_SESSION, & error );

If (conn = NULL ){

G_printerr ("Failed to connect D-Bus session Daemon! ");

G_error_free (error );

Return 1;

}



// Create a name for the connection. For GLib, this process is more complex than the underlying API interface and needs to be directed to org. freedesktop. the method "RequestName" to call its interface. It's more complicated and strange.

Bus_proxy = dbus_g_proxy_new_for_name (conn, "org. freedesktop. Large", "/", "org. freedesktop. Large ");

If (! Dbus_g_proxy_call (bus_proxy ,"RequestName

", & Error,

G_TYPE_STRING, "com. wei. test", G_TYPE_UINT, 0, G_TYPE_INVALID,

G_TYPE_UINT, & request_name_result, G_TYPE_INVALID )){

G_printerr ("Request Name error: % s/n", error-> message );

Return 1;

}


Obj = g_object_new (COM_WEI_MYOBJECT_TYPE, NULL );

// Register the object in the specified path to the connection.

Registers a GObject at the given path.

When a message is connected and the object is located in the path, the corresponding processing method can be called Based on dbus_g_object_type_insall_info.


 Dbus_g_connection_register_g_object (conn, "/com/wei/MyObject", G_OBJECT (obj ));

G_main_loop_run (main_loop );

Return 0;

}

OK. If we add <Annotation
Name = "org. freedesktop. Examples. glib. csymbol" value = "wei_response"/>, the call function of this method is not automatically generated com_wei_test, but wei_response. In this way, we can specify the name we want to use.

.

 

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.