Development/Tutorials/D-Bus/Introduction (es): Difference between revisions

From KDE TechBase
No edit summary
No edit summary
Line 14: Line 14:
== Abstracto ==
== Abstracto ==


[http://www.freedesktop.org/wiki/Software/dbus '''D-Bus'''] es un software '''Libre''' y de '''Código Abierto''' especializado en el mecanismo de comunicación entre procesos '''I'''nter-'''P'''rocess '''C'''ommunication (IPC)) que es ampliamente utilizado hoy día para el software de código abierto para escritorio. Es parte del proyecto[http://freedesktop.org freedesktop.org] y se utiliza en amplio rango de aplicaciones. Ejemplos de su uso lo tenemos en las notificaciones hotplug de Linux, consultas de búsquedas de escritorio en strigi y también como medio primario de Comunicación Entre Procesos de KDE 4. Este tutorial sirve de introducción a la terminología de D-Bus y sus conceptos básicos, desde la perspectiva del programador de aplicaciones.
[http://www.freedesktop.org/wiki/Software/dbus '''D-Bus'''] es un software '''Libre''' y de '''Código Abierto''' especializado en el mecanismo de comunicación entre procesos '''I'''nter-'''P'''rocess '''C'''ommunication (IPC)) que es ampliamente utilizado hoy día para el software de código abierto para escritorio. Es parte del proyecto [http://freedesktop.org freedesktop.org] y se utiliza en amplio rango de aplicaciones. Ejemplos de su uso lo tenemos en las notificaciones hotplug de Linux, consultas de búsquedas de escritorio en strigi y también como medio primario de Comunicación Entre Procesos de KDE 4. Este tutorial sirve de introducción a la terminología de D-Bus y sus conceptos básicos, desde la perspectiva del programador de aplicaciones.


== ¿Que es IPC? ==
== ¿Que es IPC? ==
Line 68: Line 68:
Interfaces can be described for documentation and code re-use purposes using XML. Not only can users and programmers reference the XML description of the interface, but developers can use classes that are auto-generated from the XML — making using D-Bus much easier and less error-prone (e.g. the compiler can check the syntax of messages at compile time).
Interfaces can be described for documentation and code re-use purposes using XML. Not only can users and programmers reference the XML description of the interface, but developers can use classes that are auto-generated from the XML — making using D-Bus much easier and less error-prone (e.g. the compiler can check the syntax of messages at compile time).


=== Services ===
=== Servicios ===


A '''service''' represents an application connection to a bus.
A '''service''' represents an application connection to a bus.
Line 80: Line 80:
If an application has more than one connection to a bus, or if multiple instances of the same application may be active at once, it will need to use a unique service name for each connection. Often this is done by appending the process ID to the service name.
If an application has more than one connection to a bus, or if multiple instances of the same application may be active at once, it will need to use a unique service name for each connection. Often this is done by appending the process ID to the service name.


=== Objects ===
=== Objetos ===


Of course, an application is likely to advertise access to more than one object on the bus. This many-to-one relationship between objects and services is accommodated by providing a '''path''' component to the address. Each path associated with a service represents a different, unique object. An example might be <tt>/MainInterface</tt> or <tt>/Documents/Doc1</tt>. The actual path structure is completely arbitrary and is up to the application providing the service as to what the paths should be. These paths simply provide a way to identify and logically group objects for applications that send messages to the application.
Of course, an application is likely to advertise access to more than one object on the bus. This many-to-one relationship between objects and services is accommodated by providing a '''path''' component to the address. Each path associated with a service represents a different, unique object. An example might be <tt>/MainInterface</tt> or <tt>/Documents/Doc1</tt>. The actual path structure is completely arbitrary and is up to the application providing the service as to what the paths should be. These paths simply provide a way to identify and logically group objects for applications that send messages to the application.
Line 88: Line 88:
Objects provide access to interfaces. In fact, a given object can provide access to multiple interfaces at the same time.
Objects provide access to interfaces. In fact, a given object can provide access to multiple interfaces at the same time.


=== Putting It All Together ===
=== Poniendo todo esto junto ===


A D-Bus message contains an address made up of all the above components so that it can be routed to the correct application, object and method call. Such an address might look like this:
A D-Bus message contains an address made up of all the above components so that it can be routed to the correct application, object and method call. Such an address might look like this:
Line 96: Line 96:
</code>
</code>


In this case <tt>org.kde.krunner</tt> is the service, <tt>/ScreenSaver</tt> is the path to the object, <tt>org.kde.screensaver</tt> is the interface the object exports and <tt>setBlankOnly</tt> is a method in the interface. If the <tt>/ScreenSaver</tt> object only provides the <tt>org.kde.screensaver</tt> interface (or the <tt>setBlankOnly</tt> method is unique amongst the services it implements) then this would work equally well as an address:
En este caso <tt>org.kde.krunner</tt> es el servicio, <tt>/ScreenSaver</tt> es la ruta al objeto, <tt>org.kde.screensaver</tt> es el interface que el objeto exporta y <tt>setBlankOnly</tt> es un método en el interface. Si el objeto <tt>/ScreenSaver</tt> solamente aporta el interface <tt>org.kde.screensaver</tt> (o el método <tt>setBlankOnly</tt> es único entre los servicios que implementa) esto debería funcionar igualmente bien como una dirección:


<code>
<code>
Line 102: Line 102:
</code>
</code>


In this way each possible destination is uniquely and reliably addressable.
De esta forma cada destino posible es único y fiablemente direccionable.


== Calling and Being Called ==
== Llamando y siendo llamado ==


Now that we have a way to address any given end point on the bus, we can examine the possibilities when it comes to actually sending or receiving messages.
Ahora que tenemos una forma de direccionar cualquier punto final en el bus, podemos examinar las posibilidades a la hora de enviar y recibir mensajes.


=== Methods ===
=== Metodos ===


Methods are messages that are sent to cause code to be executed in the receiving application. If the method is not available because, for instance, the address was wrong or the requested application is not running, an error will be returned to the calling application. If the method is successfully called, an optional return value will be returned to the calling application. Even if there is no return value provided, a success message will be returned. This round trip does have overhead, and it is important to keep this in mind for performance critical code.
Los métodos son mensajes que se envian para producir la ejecución de código en la aplicación que lo recibe. Si el método no está disponible por, supongamos que la dirección no es correcta o la aplicación no está arrancada, entonces se retorna un error a la aplicación que lo envió. En caso de que el método se llame correctamente se retornará opcionalmente un valor a la aplicación llamante. Incluso si no se aporta un valor de retorno, se enviará un mensaje indicando que todo fue correcto.  


Este viaje de información de ida y vuelta causa sobrecarga y es importante recordar esto para conseguir un buen rendimiento con el código que se escribe.
Such method calls are always initiated by the calling application and the resulting messages have exactly one source and one destination address.
Such method calls are always initiated by the calling application and the resulting messages have exactly one source and one destination address.


=== Signals ===
=== Señales ===


Signals are like method calls except that they happen in the "reverse" direction and are not tied to a single destination. A signal is emitted by the application which is exporting the interface and is available to any application on the same bus. This allows an application to spontaneously advertise changes in state or other events, to any applications which may be interested in tracking those changes.
Signals are like method calls except that they happen in the "reverse" direction and are not tied to a single destination. A signal is emitted by the application which is exporting the interface and is available to any application on the same bus. This allows an application to spontaneously advertise changes in state or other events, to any applications which may be interested in tracking those changes.


If this sounds a lot like the signal and slots mechanism in Qt, that's because it is. For all intents and purposes it is a non-local version of the same functionality.
Si esto suena más al mecanismo de señales y slots en QT, esto es debido a que es así. Para todos los intentos y propósitos esta es una versión no-local de la misma funcionalidad.


== Useful Tools ==
== Utilidades de interés ==


There are several useful tools for exploring the D-Bus busses as well as developing applications that use D-Bus. We will now look briefly at end-user tools as the articles that follow cover the development tools in greater detail and context.
Hay varias utilidades de interés para explorar los buses D-Bus además de para desarrollar aplicaciones que hagan uso de D-Bus. Veamos brevemente estas utilidades de usuario final ya que en los artículos que siguen se explican en más detalle y contexto.


=== qdbus ===
=== qdbus ===


<tt>qdbus</tt> is a command line tool which can be used to list the services, objects and interfaces on a given bus as well as send messages to a given address on the bus. It can be used to explore both the system and the default session bus. If the <tt>--system</tt> switch is passed, <tt>qdbus</tt> will connect to the system bus, otherwise it uses the session bus.
<tt>qdbus</tt> es una utilidad de línea de mandatos que se puede utilizar para listar servicios, objetos e interfaces de un bus determinado además de para enviar mensajes a una determinada dirección en el bus. Se puede utilizar para explorar tanto el bus del sistema como el de la sesión por defecto. Si se le pasa el switch <tt>--system</tt>, <tt>qdbus</tt> conectará con el bus del sistema, en caso contrario utilizará el bus de sesión.


<tt>qdbus</tt> uses the rest of the supplied arguments on the command as an address and, if any, parameters to pass to a given object. If a full address is not supplied, then it lists all the objects available from that point on the bus. For instance, if no addresses are provided a list of available services is listed. If a service name is provided, object paths will be provided. If a path is also provided all methods in all interfaces will be listed. In this way one can quite easily explore and interact with objects on the bus, making <tt>qdbus</tt> very useful for testing, scripting and even idle exploration.
<tt>qdbus</tt> uses the rest of the supplied arguments on the command as an address and, if any, parameters to pass to a given object. If a full address is not supplied, then it lists all the objects available from that point on the bus. For instance, if no addresses are provided a list of available services is listed. If a service name is provided, object paths will be provided. If a path is also provided all methods in all interfaces will be listed. In this way one can quite easily explore and interact with objects on the bus, making <tt>qdbus</tt> very useful for testing, scripting and even idle exploration.
Line 132: Line 133:
=== qdbusviewer ===
=== qdbusviewer ===


<tt>qdbusviewer</tt> is a Qt application that provides a graphical interface to essentially the same set of features that <tt>qdbus</tt> provides on the command line, thus providing a more user friendly mechanism to interact with the bus. <tt>qdbusviewer</tt> ships with Qt4 itself and is easy for anyone who is familiar with the basic D-Bus concepts, such as object addresses, to use.
<tt>qdbusviewer</tt> es una aplicación Qt que aporta un interface gráfico a esenciamente las mismas características que <tt>qdbus</tt> tiene en la línea de mandatos, al tiempo que aporta un mecanismo más amigable para interactuar con el bus, <tt>qdbusviewer</tt> viene con Qt4 y es fácil de utilizar para cualquiera que esté familiarizado con los conceptos de D-Bus, tales como direcciones de objetos.

Revision as of 23:54, 9 September 2009


Development/Tutorials/D-Bus/Introduction


Introduction To D-BUS
Tutorial Series   D-Bus
Previous   None
What's Next   Accessing Interfaces
Further Reading   D-Bus documentation page

Abstracto

D-Bus es un software Libre y de Código Abierto especializado en el mecanismo de comunicación entre procesos Inter-Process Communication (IPC)) que es ampliamente utilizado hoy día para el software de código abierto para escritorio. Es parte del proyecto freedesktop.org y se utiliza en amplio rango de aplicaciones. Ejemplos de su uso lo tenemos en las notificaciones hotplug de Linux, consultas de búsquedas de escritorio en strigi y también como medio primario de Comunicación Entre Procesos de KDE 4. Este tutorial sirve de introducción a la terminología de D-Bus y sus conceptos básicos, desde la perspectiva del programador de aplicaciones.

¿Que es IPC?

El término "IPC" puede utilizarse para describir cualquier número de métodos para obtener información desde un proceso a otro. La información transmitida puede tener la forma de datos de aplicación, método de invocación o señales espontaneas. Actualmente la comunicación tiene lugar a través de sockets o pipes y algunas veces, como es el caso con D-Bus, utiliza un proceso para realizar el enrutamiento del mensaje, activación de servicio y control de acceso.

La utilización de IPC en el escritorio va desde el scripting (que permite utilizar un script (conjunto de sentencias) en un entorno común para interactuar o controlar varias aplicaciones) hasta para proveer acceso a servicios centralizados, permitiendo la coordinación entre múltiples instancias de aplicaciones cooperativas.

Como ejemplo concreto, el acceso a ficheros en KDE se realiza habitualmente a través del sistema de ficheros virtual (VFS:Virtual File System) KIO utilizando KIOSlaves. KIOSlaves consiste en una serie de aplicaciones que permiten el acceso a datos sobre un protocolo dado o especificación (e.g. http o APIs del sistema de ficheros local) mediante la implementación de funciones tales como lectura (read), escritura (write), etc Los esclavos (slaves) corren como procesos independientes e intercambian datos con la aplicación — con la cual el usuario está interactuando &mdash asíncronamente a través de IPC. Esto no solamente permite que múltiples aplicaciones utilicen el mismo esclavo (slave) sino que además permite al mismo esclavo mantenerse simple de forma que pueda realizar cosas como bloqueo de operaciones sin que ello cause el propio bloqueo de la aplicación gráfica. La aplicación gráfica nunca ve actualmente la ocurrencia IPC, como si esta tuviese lugar detrá de las escenas de la API KIO.

Otro ejemplo del uso de la IPC en KDE es el soporte de la aplicación única. Cuando se inicia una KUniqueApplication primeramente chequea si están corriendo otras instancias de la misma aplicación. Si es este el caso lo que hace es enviar un mensaje vía IPC para que dicha aplicación se muestre a sí misma.

El mecanismo primario de IPC para aplicaciones en KDE4 es D-BUS, el cual es un sistema muy popular utilizado por una variedad de software hoy día. QT4 aporta un conjunto de enlaces intuitivos y estables a D-Bus en la forma de clases C++. Esta serie de tutoriales introducen a D-Bus aparte de cubrir el uso de la API D-Bus en QT4. Este artículo en particular describe los conceptos de alto nivel en D-Bus y deja los detalles de como utilizar estas características y funcionalidad a los artículos que siguen.

Buses

D-Bus aporta múltiples "buses" de mensaje para que las aplicaciones se comuniquen entre sí. Cada bus aporta sus propias facilidades de conexión, la cual permite la separación de las diferentes categorias de mensajes: los mensajes enviados por un bus no pueden ser accedidos por otro bus, pero las aplicaciones conectadas al mismo bus pueden comunicarse entre ellas a trávés de él. De esta forma múltiples aplicaciones se pueden conectar al mismo bus en cualquier momento y también una aplicación se puede conectar a múltiples buses simultaneamente. Esto permite la aplicación de diferentes políticas de seguridad a buses diferentes al tiempo que se mantiene una eficiente compartición tanto de los mensajes locales como globales.


Se aporta por defecto un bus de sistema amplio para mensajes internos del kernel (núcleo) del sistema operativo y para otros servicios globales del sistema denominándose, apropiadamente, el bus del sistema (the system bus).

Este es el bus utilizado para cosas tales como señalizar cuando hay disponible nuevo hardware detectado en el sistema, tal como ocurre cuando una unidad flash se conecta o cuando se inserta un disco DVD en una unidad.

Cada sesión de escritorio (e.g. cada usuario logeado) tiene uno o más buses asociados. Estos reciben el nombre de buses de sesion. El bus por defecto de la actual sesión recibe el nombre El bus de sesión y es el que las aplicaciones de escritorio tenderán a usar con más frecuencia.

Adicionalmente, una aplicación puede crear su propio bus privados con otra aplicación en la modalidad de igual a igual (peer to peer).

No hay un límite práctico sobre el número de buses que pueden utilizar las aplicaciones asociadas a un usuario. Sin embargo es en la habilidad de compartir buses — y por tanto mensajes comunes — donde brillan realmente sistemas como D-Bus

Mensajes

La unidad base de comunicación en un bus es el mensaje. Toda la información pasada a través de bus se realiza en forma de mensajes, similar a la información transmitida utilizando TCP/IP vía paquetes.

A diferencia de los paquetes de red, sin embargo, cada mensaje D-Bus garantiza contener el conjunto completo de datos a enviar o recibir. En adición a los datos a enviar, los mensajes además registran quienes son tanto el que envía como el destinatario para permitir el enrutamiento adecuado.

Este sistema muestra muy bien como tienden a trabajar internamente las aplicaciones de escritorio: métodos de llamada method calls (messages) result in some action being taken con la posibilidad de retornar valores. Una de las característica primarias de los mensajes con D-Bus es que son asíncronos: se puede enviar un mensaje mientras que la respuesta no ha tenido lugar por un periodo de tiempo (se generan timeouts para prevenir 'esperas por siempre' ('wait forever') deadlocks). Por supuesto, hay en la API de D-Bus en Qt4 para hacer parecer síncronas las llamadas a la aplicación llamante.

Los mensajes pueden enviarse desde una aplicación a sí misma. Estos mensajes se cortocircuitan y por tanto se mantinen locales a la aplicación, por lo que no se hace necesario que el código de la aplicación se encargue de saber si está llamando a una aplicación local o remota. Esto es a menudo útil en aplicaciones con muchos componentes, permitiendo prevenir posibles deadlock.


Espacio de nombres y direcciones

Desde que múltiples aplicaciones pueden utilizar el mismo bus, y una aplicación puede tener múltiples objetos a los cuales los mensajes pueden llamar, se hace necesario establecer un medio para direccionar efectivamente y de forma inequívoca cualquier objeto en cualquier bus, similar al modo en que la dirección de una calle identifica de forma única cualquier residente u oficina. Hay tres trozos de información los cuales cuando se juntan crean una única dirección para un objeto dado en el bus: interface, servicio y nombre de objeto.


Interfaces

Un interface es un conjunto de métodos y señales que pueden ser llamadas publicitándolas en el bus. An interface provides a "contract" between the applications passing messages that defines the name, parameters (if any) and return values (if any) of the interface. These methods may not map directly in a one-to-one fashion to methods or API in the application that is advertising the interface, though they often do. This allows multiple applications to provide similar or the same interfaces, regardless of internal implementation, while allowing applications to use these interfaces without worrying about the internal design of the applications.

Interfaces can be described for documentation and code re-use purposes using XML. Not only can users and programmers reference the XML description of the interface, but developers can use classes that are auto-generated from the XML — making using D-Bus much easier and less error-prone (e.g. the compiler can check the syntax of messages at compile time).

Servicios

A service represents an application connection to a bus.

Service here corresponds to "well-known" Bus names in the D-Bus specification terminology. The term "Bus name" is a bit confusing. Regardless of how it sounds like, Bus names are the names of connections(!) on the bus, not names of buses(!). So here the term service will be used, as the Qt Documentation calls it.

These are kept unique by using a "reverse domain name" approach, as can be seen in many other systems that need to namespace for multiple components. Most services provided by applications from the KDE project itself use the org.kde prefix to their service name. So one may find "org.kde.screensaver" advertised on the session bus.

You should use the domain name for your organization or application for your service names. For example if your domain is awesomeapps.org and the name of your application is wickedwidget you would probably use org.awesomeapps.wickedwidget as the service name on the bus.

If an application has more than one connection to a bus, or if multiple instances of the same application may be active at once, it will need to use a unique service name for each connection. Often this is done by appending the process ID to the service name.

Objetos

Of course, an application is likely to advertise access to more than one object on the bus. This many-to-one relationship between objects and services is accommodated by providing a path component to the address. Each path associated with a service represents a different, unique object. An example might be /MainInterface or /Documents/Doc1. The actual path structure is completely arbitrary and is up to the application providing the service as to what the paths should be. These paths simply provide a way to identify and logically group objects for applications that send messages to the application.

Some libraries export object paths with their "reverse domain" prepended to it, so as to properly namespace their objects. This is quite common for libraries and plugins that join an arbitrary service and must therefore avoid all clashing with objects exported by the application and other components. However, this practice is not in use in KDE applications and libraries.

Objects provide access to interfaces. In fact, a given object can provide access to multiple interfaces at the same time.

Poniendo todo esto junto

A D-Bus message contains an address made up of all the above components so that it can be routed to the correct application, object and method call. Such an address might look like this:

org.kde.krunner /ScreenSaver org.kde.screensaver.setBlankOnly

En este caso org.kde.krunner es el servicio, /ScreenSaver es la ruta al objeto, org.kde.screensaver es el interface que el objeto exporta y setBlankOnly es un método en el interface. Si el objeto /ScreenSaver solamente aporta el interface org.kde.screensaver (o el método setBlankOnly es único entre los servicios que implementa) esto debería funcionar igualmente bien como una dirección:

org.kde.krunner /ScreenSaver setBlankOnly

De esta forma cada destino posible es único y fiablemente direccionable.

Llamando y siendo llamado

Ahora que tenemos una forma de direccionar cualquier punto final en el bus, podemos examinar las posibilidades a la hora de enviar y recibir mensajes.

Metodos

Los métodos son mensajes que se envian para producir la ejecución de código en la aplicación que lo recibe. Si el método no está disponible por, supongamos que la dirección no es correcta o la aplicación no está arrancada, entonces se retorna un error a la aplicación que lo envió. En caso de que el método se llame correctamente se retornará opcionalmente un valor a la aplicación llamante. Incluso si no se aporta un valor de retorno, se enviará un mensaje indicando que todo fue correcto.

Este viaje de información de ida y vuelta causa sobrecarga y es importante recordar esto para conseguir un buen rendimiento con el código que se escribe. Such method calls are always initiated by the calling application and the resulting messages have exactly one source and one destination address.

Señales

Signals are like method calls except that they happen in the "reverse" direction and are not tied to a single destination. A signal is emitted by the application which is exporting the interface and is available to any application on the same bus. This allows an application to spontaneously advertise changes in state or other events, to any applications which may be interested in tracking those changes.

Si esto suena más al mecanismo de señales y slots en QT, esto es debido a que es así. Para todos los intentos y propósitos esta es una versión no-local de la misma funcionalidad.

Utilidades de interés

Hay varias utilidades de interés para explorar los buses D-Bus además de para desarrollar aplicaciones que hagan uso de D-Bus. Veamos brevemente estas utilidades de usuario final ya que en los artículos que siguen se explican en más detalle y contexto.

qdbus

qdbus es una utilidad de línea de mandatos que se puede utilizar para listar servicios, objetos e interfaces de un bus determinado además de para enviar mensajes a una determinada dirección en el bus. Se puede utilizar para explorar tanto el bus del sistema como el de la sesión por defecto. Si se le pasa el switch --system, qdbus conectará con el bus del sistema, en caso contrario utilizará el bus de sesión.

qdbus uses the rest of the supplied arguments on the command as an address and, if any, parameters to pass to a given object. If a full address is not supplied, then it lists all the objects available from that point on the bus. For instance, if no addresses are provided a list of available services is listed. If a service name is provided, object paths will be provided. If a path is also provided all methods in all interfaces will be listed. In this way one can quite easily explore and interact with objects on the bus, making qdbus very useful for testing, scripting and even idle exploration.

qdbusviewer

qdbusviewer es una aplicación Qt que aporta un interface gráfico a esenciamente las mismas características que qdbus tiene en la línea de mandatos, al tiempo que aporta un mecanismo más amigable para interactuar con el bus, qdbusviewer viene con Qt4 y es fácil de utilizar para cualquiera que esté familiarizado con los conceptos de D-Bus, tales como direcciones de objetos.