source: project/wiki/eggref/4/dbus @ 25876

Last change on this file since 25876 was 25876, checked in by Mario Domenech Goulart, 9 years ago

dbus (wiki): removed "Download" section

File size: 14.2 KB
1[[tags: egg]]
3== dbus
7=== Overview
9This is a binding for libdbus 1.x.  DBus is a popular IPC (inter-process communication) protocol which is often used between system components (for example hald informs applications when new hardware becomes available, gpsd informs apps when the location has changed, or an application requests gsmd to place a phone call).
11=== Goals & status
15<tr><td>send signals</td><td>yes</td></tr>
16<tr><td>call methods in other processes, and get the return values</td><td>yes</td></tr>
17<tr><td>call methods in other processes asynchronously, and the return values come back to a callback later</td><td></td></tr>
18<tr><td>register a procedure as a handler which will be called when a signal is received</td><td>yes</td></tr>
19<tr><td>register a procedure as a method which can be called from another process</td><td>yes</td></tr>
20<tr><td>assign a path to a TinyClos object and map applicable generic functions as dbus methods</td><td></td></tr>
21<tr><td>create proxy methods matching remote methods</td><td>yes</td></tr>
22<tr><td>create proxy objects matching remote objects (so that all the remote object's applicable methods are made into local proxy methods)</td><td></td></tr>
23<tr><td>discover services locally</td><td>yes</td></tr>
24<tr><td>discover services on nearby machines</td><td>impossible with dbus-daemon as-is</td></tr>
25<tr><td>discover methods and signals provided by services</td><td>XML only so far</td></tr>
26<tr><td>registered methods and signal-handlers are automatically included in the Introspectable interface implementation</td><td></td></tr>
27<tr><td>user code to do any of the above should be minimal: abstract away the orthogonal extra steps (open a connection, start a polling thread, etc.)</td><td>yes</td></tr>
28<tr><td>support all DBus data types for method parameters and return values</td><td></td></tr>
31=== Author
33[[Shawn Rutledge]]
35=== Requirements
37[[|libdbus and its headers]]; [[|SRFI-18]]
40=== License
42libdbus has historically had a GPL license.  Later they switched to AFL / GPL dual license.  This egg is released under the MIT license, which is compatible with AFL.
44=== Version
46As of version 0.8, it seems to be working for the limited use cases of sending and receiving method calls, but the interface should not be considered "frozen" yet.
48=== Terminology
50; bus : The aggregator and dispatcher of messages between processes.  The usual choices are system bus, session bus or an app-specific bus.  This egg uses the session bus by default.  It is the same bus used by desktop session services, like KDE components for example.  Accessing the system bus requires special permission.  An app-specific bus does not promote application interoperability.  So that leaves the session bus as generally being the most appropriate.
52; bus name : A unique identifier for a DBus client.  Every DBus client is automatically assigned a bus name.  These consist of a colon character ':' followed by some numbers.  You can see these in the DBus traffic in the output of dbus-monitor.  DBus clients that want to listen for method calls can request a second bus name, a so-called well-known bus name, which is a name by which other DBus clients can refer to them.  Well-known bus names are what you use in the 'service' field of a method call.
54; service : A well-known bus name giving the destination of a method call.  Conventionally, these look like reversed domain names.  Signals do not use services.
56; path : A DBus-exposed API can be conceptually organized into a hierarchy.  Paths index into this hierarchy with notation familiarly derived from file and URL paths, where path components are separated by slashes '/'.  The program might map path components to actual objects in memory, but this is not required.  The default path, when not given, is "/".
58; interface : a partitioned set of methods which is being offered, like a Java interface or an abstract class.  An interface is technically optional when registering a method or signal handler, but this egg currently requires it.
60; member name : also called method name, or the name of the signal or message.
62; signal : a one-to-many notification from one process to any others which happen to be listening for that particular notification.  A signal message does not include a service because it does not have a specific destination.  Signals are one way; there is no explicit reply from the receivers.
64; method call : a one-to-one message between DBus clients.  The caller can pass arguments, and the handler sends a method return message back with the result.
66; method return : the message type of the response to a method call.
68; method : a scheme function to be invoked when a particular method call message is received.  Its result will be sent back to the calling client as a method return.
70; message : the DBus-protocol representation of a signal or a method call passing across the DBus connection between two processes.
72And this egg introduces one more:
74; context : a combination of bus, service, interface and path.  That and a method name will get you a unique identifier for a callback (AKA method or message handler).
76This egg does not yet fully support making methods, signal handlers, and interfaces discoverable by other clients.
78=== DBus in general
80Use dbus-monitor to see all the dbus traffic (on the session bus by default).
82A many-to-many "bus" exists as a running dbus-daemon process.  Each application which is interested in using that bus connects to the daemon via Unix-domain sockets.  The daemon collects incoming messages and dispatches them to the connected processes which have expressed interest in receiving particular categories of messages.  Usually there is one running instance of dbus-daemon for the system bus, and one running instance for each session bus: each user who is logged in and running a desktop session is typically running one instance of the daemon.
84A one-to-one application-specific bus can bypass the daemon: one app connects directly to the other.  But this is not a flexible service-oriented approach to IPC, so it is not usually done, and this egg does not support it.
86=== Data types
88DBus protocol (unlike XML, for example) allows sending real native datatypes across the connection.  In fact it provides more data types than does Chicken.  So when you pass some parameters along with a method call, these are the default conversions:
91<tr><th>Chicken data type</th><th>DBus data type</th></tr>
96<tr><td>anything else</td><td>DBUS_TYPE_STRING</td></tr>
99When a DBus message is received, the parameters are converted similarly, but unsupported DBus types will be converted to #f.
101Watch out for cases when Chicken converts an integer to a flonum, because it was too large to fit in a fixnum.  It will go across the DBus connection as a double, which might not be what you wanted.
103It is planned to support requests for conversion to other DBUS types, in case you need to interface with an existing service that requires datatypes other than the typical ones above.  DBus supports variants and structs; these conversions are not done yet.
105=== Exported functions
107dbus:make-context : glom together a bus, service, interface and path into an object that you can conveniently hold for later use.  The choices for bus are dbus:session-bus (which is the default if you don't specify it), dbus:system-bus and dbus:starter-bus.  The service, interface and path can be given as strings or symbols.  Symbols would be preferred; if you provide strings, dbus:make-context will convert them to symbols.  The default for path is "/" so if you don't need a hierarchical organization of "objects", you don't need to specify it.
109dbus:send : send a signal.
111dbus:make-sender : wrap dbus:send in a lambda, which you can then use like any local function.  The return value from the function thus created is not yet defined.  (Maybe should be #f or an error message?  or is it better to use exceptions when something cannot be sent?)
113dbus:call : call a remote method, wait for the reply, and receive the return values from the remote method, as a list.  Note that since this is a blocking call (it waits for the reply), it's not suitable for implementing the [[|actor model]] or anything similarly lightweight/low-latency.
115dbus:make-method-proxy : wrap dbus:call in a lambda, which you can then use like any local function.  The return value from the function thus created will be the list of return values from the remote method.
117dbus:register-signal-handler : provide a handler to be called when the current process receives a DBus signal which matches the given context and the given signal name.  Start a SRFI-18 thread to poll for incoming signals (unless polling has been disabled on this bus).
119dbus:register-method : provide a handler (a method body) to be called when the current process receives a DBus message which matches the given context and the given method name.  Start a SRFI-18 thread to poll for incoming messages (unless polling has been disabled on this bus).
121dbus:enable-polling-thread! : enable or disable the polling thread for a particular bus, and set the polling interval in seconds
123dbus:poll-for-message : check once whether any incoming DBus message is waiting on a particular bus, and if so, dispatch it to the appropriate callback (which you have previously registered using dbus:register-method or dbus:register-signal-handler).  Returns #t if it received a message, #f if not.
125dbus:discover-services : get a list of service names available on a bus
127dbus:discover-api-xml : get the XML API "documentation" provided by the Introspectable interface of a service, if that interface is implemented
129=== Examples
131These are in the examples subdirectory in svn, and included with the egg too.
133==== Examples you can test with QT
135QT includes a DBUS remote-controlled car example.  E.g. it might be located in /usr/share/qt4/examples/qdbus/remotecontrolledcar/car depending on your distro.  If you run the car, you can cause the wheels of the car to turn to the right by doing this:
137<enscript highlight=scheme>
138(use dbus)
139(define rc-car-context (dbus:make-context
140        service: 'com.trolltech.CarExample
141        interface: 'com.trolltech.Examples.CarInterface
142        path: '/Car))
143(dbus:call rc-car-context "turnRight")
146That example called a method but it did not expect any return values.
148Now suppose you want to simulate the car, so you can use the above example to control your own car rather than the QT one.  And suppose you want to poll for messages manually rather than via the default SRFI-18 polling thread:
150<enscript highlight=scheme>
151(use dbus)
153(define (turn-right) (printf "car is turning to the right~%"))
154(define (turn-left) (printf "car is turning to the left~%"))
156(define rc-car-context (dbus:make-context
157        service: 'com.trolltech.CarExample
158        path: '/Car
159        interface: 'com.trolltech.Examples.CarInterface ))
161(dbus:enable-polling-thread enable: #f)
163(dbus:register-method rc-car-context "turnRight" turn-right)
164(dbus:register-method rc-car-context "turnLeft" turn-left)
166(let loop ()
167        (dbus:poll-for-message)
168        (loop))
171dbus:register-method starts a polling loop the first time that it is called with a context specifying a particular bus.  (And if you register methods on multiple buses, there must be a polling thread for each.)  So you can then run the program above which does dbus:send, and you will see the appropriate printf statement execute asynchronously when the message is received.  However the polling thread is subject to the usual limitations of Chicken threads: if there is any blocking I/O, which is waiting for something, then all threads are blocked.  That means you should not use the readline egg for example, because the polling thread will be blocked between each time that you type something and hit Enter.
173If the polling thread doesn't work, and you would prefer to poll manually, you can call (dbus:enable-polling-thread! enable: #f) to stop the thread (or to prevent it from being started when you register a new method), and then call dbus:poll-for-message periodically.  Both of those functions can take an optional bus: parameter (which defaults to dbus:session-bus, naturally).  dbus:poll-for-message can also take an optional timeout: parameter (which is 0 by default, so that it is a non-blocking call).
175==== Examples based on the DBus Tutorial
177The next example, taken from the [[|DBus tutorial]], shows how to deal with return values.  First the "listener" program which will answer the query:
179<enscript highlight=scheme>
180(use dbus)
181(define (query . params)
182        (printf "got a query; params: ~s~%" params)
183        ;; the response to the query:
184        `(#t 42))
185(define ctxt (dbus:make-context
186        service: 'test.method.server
187        interface: 'test.method.Type
188        path: '/test/method/Object))
189(dbus:register-method ctxt "Method" query)
192And now the program which sends a query and prints out the response:
194<enscript highlight=scheme>
195(use dbus)
196(define ctxt (dbus:make-context
197        service: 'test.method.server
198        interface: 'test.method.Type
199        path: '/test/method/Object))
200(let ([response (dbus:call ctxt "Method" "query"
201                "What is the meaning of life, the universe and everything?") ])
202(printf "sent a very important query with a known answer; got flippant response ~s~%" response)
203        (if (and (list? response) (eq? 42 (cadr response)))
204                (printf "bingo!~%")
205                (printf "and the answer is wrong too!  Bad supercomputer, bad!~%")))
208What do you get if you combine a remote-controlled mobile thingy with a supercomputer capable of deep thought?  A paranoid android of course!  This example in the test directory handles both kinds of messages from the two examples above (the query and the turnLeft/turnRight commands) and proves that one polling thread is enough to handle multiple, completely different contexts, on a single bus.  So registering more than one method is low-cost compared to registering the first one.
Note: See TracBrowser for help on using the repository browser.