Understanding of the use of Libdbus library functions

Source: Internet
Author: User
Tags message queue

1. Internal working methods of D-bus

typical d-bus system bus ), It starts at boot time. This bus is used by the operating system and the background process and is very secure so that any application cannot spoof system events. There will also be many session buses ( session buses can connect directly to the system bus -- But the message that it can send will be limited. only linux kernel, linux system bus to ensure system security and prevent malicious processes from impersonating linux send message .  

Once the application is connected to a bus, they mustdeclare which message they want to receive by adding a match (matchers). A match specifies a set of rules for messages that can be received based on interfaces, object paths, and methods. This allows applications to focus on what they want to work with to achieve efficient routing of messages and to keep the expected number of messages on the bus so that they do not degrade and slow down all applications because of these messages.

1.1 Object

Essentially,D-bus is a reciprocal (peer-to-peer) protocol--Each message has a source and a purpose. These addresses are specified as object paths. Conceptually, all applications that use d-bus include a set of objects that messages are sent to or sent from an object that is uniquely identified by the object path.

Each object can support one or more interfaces (interfaces). These interfaces look similar to interfaces in Java or pure virtual classes in C + + (pure virtual Classes). An interface is a collection of multiple methods and signals. Dbus uses a simple namespace string to represent an interface, such as org.freedesktop.Introspectable.

1.2 message

There are four types of messages in d-bus :

1. Method callto execute a method on an object

2. Methods return (methodreply) Returns the result of the method execution

3. Errors (error) Calling method-generated exceptions

4. signal (signal) notifies that the specified signal has occurred and can be imagined as " event ".

To execute the method of the D-bus object, you need to send an "methodcall" message to the object. It will complete some processing and return a "methodreply" message or an "error" message. The difference is that they do not return anything: there is no "methodreply" message, and there is no type of "error" message.

The message can also have arbitrary arguments. The arguments are strongly typed, and the range of the types is from the base non-derived type (Boolean (booleans), byte (bytes), Integer (integers ) to high-level data structures (strings (strings), arrays ( Arrays), and dictionaries (dictionaries )).

Use of the 2.d-bus low-level API 2.1 the process of establishing a service

Create aDbusafter connection--Dbus_bus_get (),Register the monitoring function for this connection--dbus_connection_set_watch_functions (), register the timeout function for this connection--dbus_connection_set_timeout_functions (),register the message handler for this connection--dbus_connection_add_filter (),For thisDbusConnection(dbusconnection)named--Dbus_bus_request_name(), this name will be the name of the service when we make a subsequent remote call, adding a match to the connectionDbus_bus_add_match(), register the object path to the bus, and then we go to the file descriptor read-write readiness State listening loop. In the loop, we take the message out of the bus--Dbus_watch_handle(), and then throughDbus_connection_dispatch() is distributed to the message handler function previously registered through Dbus_connection_add_filter (), and through the method interface name and method name in the comparison message--Dbus_message_is_method_call (), if consistent, then we jump to the appropriate processing to go. In the corresponding processing, we remove the parameters of the remote call from the message. and establish a pathway to the results of the backhaul.--Reply_to_method_call (). The callback action itself is equivalent to a remote call that does not need to wait for the result.

2.2 the process of sending signals:

After establishing a dbus Connection, for this dbus connection name, set up a channel to send the signal, note that in the function of establishing the channel, we need to fill in the signal interface and signal name -- Dbus_message_new_signal (). We then press the relevant parameters of the signal into -dbus_message_iter_init_append (); Dbus_message_iter_append_basic (). Then you can start sending --dbus_connection_send (); Dbus_connection_flush.

2.3 the process of making a remote call:

Establish a goodDbusafter connecting, for thisDbusconnection naming, requesting a remote call channel--Dbus_message_new_method_call (), note that when requesting a remote call channel, you need to fill in the server name, the interface name of this call, and the call name(Method Name). Parameters that are pressed into this call--Dbus_message_iter_init_append (); Dbus_message_iter_append_basic (), actually applied for a first address, we are the parameters we really want to pass, to this first address to send(after delivery, it is common to determine if memory is out of bounds.). Then it starts the send call and releases the message structure that is sent.--dbus_connection_send_with_reply (). This start function has a handle. We will immediately block waiting for this handle to send us back the message on the bus. When the handle is returned by the message, we separate the parameters from the message structure. WithDbusthe supplied function extracts the type and parameters of the parameter--Dbus_message_iter_init (); Dbus_message_iter_next (); Dbus_message_iter_get_arg_type (); dbus_message_iter_get _basic (). We have also achieved the purpose of this remote call.

2.4 Signal Reception Process:

After establishing a dbus Connection, name the dbus Connection and add a matching condition for the message loop we will be doing ( is the matching control by the signal name and the signal Interface name )--Dbus_bus_add_match (). After we enter the waiting loop, we only need to judge the signal name and signal interface name to handle various signals separately. On each processing branch. We can isolate the parameters in the message. The parameter type is judged and other processing is done.

3. D-bus Low-level public API the use of understanding 3.1 about the dbusconnection the Understanding

The dbusconnection represents a connection to a bus or another application, through which it can send and receive messages, and through the function Dbus_bus_get() function, to connect to a bus that has already been advertised, thus obtaining a Dbusconnection connection. Dbusconnection can be seen as a maintenance of two message queues for receiving and sending, which is based on the SOCKET for data transfer.

In the dbusconnection, through the Dbuswatch and Dbustimeout two objects to determine when to do message reception, message delivery and message distribution processing operations, through the Dbus_connection_dispatch () function, To process a message at the top of the receiving message queue, the Dbus_connection_dispatch () function executes a message handler function that has already been registered (in the ResourceManager program msg_filter () function). Delete this processed message from the receiving Message queue and return.

The Dbus_connection_get_dispatch_status () function indicates whether a message in the queue waits to be processed.

To achieve asynchronous processing of Message Queuing, we need two functions dbus_connection_set_watch_functions (), Dbus_connection_set_timeout_functions () To integrate dbusconnection into our function's main loop ( Bus_watch_daemonin the Resourcemanger program ). If asynchronous processing is not required, the object Dbuswatch, Dbustimeout, and Function Dbus_connection_dispatch () can be ignored. Then you need to use the function Dbus_connection_read_write_ Dispatch ().

When a message is sent using the function dbus_connection_send (), the message is added to the Dbusconnection send queue, and if the Dbus_connection_flush () function is executed, the process is blocked. Until the message in the Send queue is fully routed through the socket , if the function is not executed, it is routed through the socket the next time the main loop executes the Dbus_watch_handle .

When the connection is disconnected, a "disconnected" signal is received on the interface dbus_interface_local, Path dbus_path_local, before the connection is disconnected. You must keep a reference to dbusconnection.

3.2 about Dbusmessage the Understanding

Message Dbusmessage is sent and received through Dbusconnection.

The message has a message header, the properties of the message header are obtained through the function Dbus_message_get_type (), the message contains the message header, the destination method call or signal call that indicates the source message of the message, and other information, through the function dbus_message_get_ Member () to get. can also be obtained in a more convenient way, including

Dbus_message_is_method_call () to determine whether a method call

Dbus_message_is_signal () to determine if it is a signal message

Dbus_message_is_error () to determine if it is an error message

The message carries the parameter, obtains the signature through the Dbus_message_get_signature (), for the simple parameter through Dbus_message_get_args () to obtain, for the complex structure body parameter, obtained by Dbusmessageiter.

Understanding of the use of Libdbus library functions

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.