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

Last change on this file since 33751 was 33751, checked in by retroj, 2 years ago

eggref/4/dbus: version history section

File size: 14.8 KB
Line 
1[[tags: egg]]
2
3== dbus
4
5[[toc:]]
6
7=== Overview
8
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).
10
11=== Goals & status
12
13<table>
14<tr><th>Goal</th><th>Achieved?</th></tr>
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>
29</table>
30
31=== Author
32
33[[Shawn Rutledge]]
34
35=== Requirements
36
37[[http://dbus.freedesktop.org/|libdbus and its headers]]
38
39
40=== License
41
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.
43
44=== Version
45
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.
47
48=== Terminology
49
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.
51
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.
53
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.
55
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 "/".
57
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.
59
60; member name : also called method name, or the name of the signal or message.
61
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.
63
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.
65
66; method return : the message type of the response to a method call.
67
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.
69
70; message : the DBus-protocol representation of a signal or a method call passing across the DBus connection between two processes.
71
72And this egg introduces one more:
73
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).
75
76This egg does not yet fully support making methods, signal handlers, and interfaces discoverable by other clients.
77
78=== DBus in general
79
80Use dbus-monitor to see all the dbus traffic (on the session bus by default).
81
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.
83
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.
85
86=== Data types
87
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:
89
90<table>
91<tr><th>Chicken data type</th><th>DBus data type</th></tr>
92<tr><td>fixnum</td><td>DBUS_TYPE_INT32</td></tr>
93<tr><td>flonum</td><td>DBUS_TYPE_DOUBLE</td></tr>
94<tr><td>boolean</td><td>DBUS_TYPE_BOOLEAN</td></tr>
95<tr><td>vector</td><td>DBUS_TYPE_ARRAY</td></tr>
96<tr><td>anything else</td><td>DBUS_TYPE_STRING</td></tr>
97</table>
98
99When a DBus message is received, the parameters are converted similarly, but unsupported DBus types will be converted to #f.
100
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.
102
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.
104
105=== Exported functions
106
107<procedure>(make-context #!key (bus session-bus) service interface (path "/"))</procedure>
108
109Glom together a bus, service, interface and path into an object that
110you can conveniently hold for later use.  The choices for bus are
111{{session-bus}} (which is the default if you don't specify it),
112{{system-bus}} and {{starter-bus}}.  The service, interface
113and path can be given as strings or symbols.  Symbols would be
114preferred; if you provide strings, {{make-context}} will convert
115them to symbols.  The default for path is "/" so if you don't need a
116hierarchical organization of "objects", you don't need to specify it.
117
118
119<procedure>(send context name . params)</procedure>
120
121Send a signal.
122
123
124<procedure>(call context name . params)</procedure>
125
126Call a remote method, wait for the reply, and receive the return
127values from the remote method, as a list.  Note that since this is a
128blocking call (it waits for the reply), it's not suitable for
129implementing the [[http://en.wikipedia.org/wiki/Actor_model|actor model]]
130or anything similarly lightweight/low-latency.
131
132
133<procedure>(make-method-proxy context name)</procedure>
134
135Wrap {{call}} in a lambda, which you can then use like any local
136function.  The return value from the function thus created will be the
137list of return values from the remote method.
138
139
140<procedure>(register-signal-handler context name msg-cb)</procedure>
141
142Provide a handler to be called when the current process receives a
143DBus signal which matches the given context and the given signal name.
144Start a SRFI-18 thread to poll for incoming signals (unless polling
145has been disabled on this bus).
146
147
148<procedure>(register-method context name msg-cb)</procedure>
149
150Provide a handler (a method body) to be called when the current
151process receives a DBus message which matches the given context and
152the given method name.  Start a SRFI-18 thread to poll for incoming
153messages (unless polling has been disabled on this bus).
154
155
156<procedure>(enable-polling-thread! #!key (bus session-bus) (enable #t) (interval default-polling-interval))</procedure>
157
158Enable or disable the polling thread for a particular bus, and set the
159polling interval in seconds.
160
161
162<procedure>(poll-for-message #!key (bus session-bus) (timeout 0))</procedure>
163
164Check once whether any incoming DBus message is waiting on a
165particular bus, and if so, dispatch it to the appropriate callback
166(which you have previously registered using {{register-method}}
167or {{register-signal-handler}}).  Returns {{#t}} if it received a
168message, {{#f}} if not.  The optional {{timeout}} parameter is an
169integer that indicates the maximum time in milliseconds to block waiting
170for messages.
171
172
173<procedure>(discover-services #!key (bus session-bus))</procedure>
174
175Get a list of service names available on a bus.
176
177
178<procedure>(discover-api-xml context)</procedure>
179
180Get the XML API "documentation" provided by the Introspectable
181interface of a service, if that interface is implemented.
182
183
184=== Exported constants
185
186<constant>session-bus</constant>
187
188Session bus.
189
190
191<constant>system-bus</constant>
192
193System bus.
194
195
196<constant>starter-bus</constant>
197
198Starter bus.
199
200
201=== Examples
202
203These are in the examples subdirectory in svn, and included with the egg too.
204
205==== Examples you can test with QT
206
207QT 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:
208
209<enscript highlight=scheme>
210(use dbus)
211
212(define rc-car-context
213  (make-context
214   service: 'com.trolltech.CarExample
215   interface: 'com.trolltech.Examples.CarInterface
216   path: '/Car))
217
218(call rc-car-context "turnRight")
219</enscript>
220
221That example called a method but it did not expect any return values.
222
223Now 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:
224
225<enscript highlight=scheme>
226(use dbus)
227
228(define (turn-right) (printf "car is turning to the right~%"))
229(define (turn-left) (printf "car is turning to the left~%"))
230
231(define rc-car-context
232  (make-context
233   service: 'com.trolltech.CarExample
234   path: '/Car
235   interface: 'com.trolltech.Examples.CarInterface ))
236
237(enable-polling-thread! enable: #f)
238
239(register-method rc-car-context "turnRight" turn-right)
240(register-method rc-car-context "turnLeft" turn-left)
241
242(let loop ()
243  (poll-for-message)
244  (loop))
245</enscript>
246
247{{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 {{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.
248
249If the polling thread doesn't work, and you would prefer to poll manually, you can call {{(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 {{poll-for-message periodically}}.  Both of those functions can take an optional bus: parameter (which defaults to {{session-bus}}, naturally).  {{poll-for-message}} can also take an optional timeout: parameter (which is 0 by default, so that it is a non-blocking call).
250
251==== Examples based on the DBus Tutorial
252
253The next example, taken from the [[http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html|DBus tutorial]], shows how to deal with return values.  First the "listener" program which will answer the query:
254
255<enscript highlight=scheme>
256(use dbus)
257(define (query . params)
258  (printf "got a query; params: ~s~%" params)
259  ;; the response to the query:
260  `(#t 42))
261
262(define ctxt
263  (make-context
264    service: 'test.method.server
265    interface: 'test.method.Type
266    path: '/test/method/Object))
267
268(register-method ctxt "Method" query)
269</enscript>
270
271And now the program which sends a query and prints out the response:
272
273<enscript highlight=scheme>
274(use dbus)
275
276(define ctxt
277  (make-context
278     service: 'test.method.server
279     interface: 'test.method.Type
280     path: '/test/method/Object))
281
282(let ([response
283       (call ctxt "Method" "query"
284             "What is the meaning of life, the universe and everything?") ])
285  (printf "sent a very important query with a known answer; got flippant response ~s~%" response)
286  (if (and (list? response) (eq? 42 (cadr response)))
287      (printf "bingo!~%")
288      (printf "and the answer is wrong too!  Bad supercomputer, bad!~%")))
289</enscript>
290
291What 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.
292
293=== Version History
294
295* 0.86
296* 0.87
297* 0.88
298* 0.89
299* 0.90
300* 0.91
301* 0.92
302* 0.93
303* 0.94 (2016-11-21) compatibility with CHICKEN 4.9.0.1, various fixes
304* 0.95 (2016-11-29) fix compiler warning, fix polling thread lag
Note: See TracBrowser for help on using the repository browser.