File: DBusKit_2.html

package info (click to toggle)
dbuskit 0.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 3,552 kB
  • sloc: objc: 10,309; sh: 9,443; ansic: 200; makefile: 20
file content (501 lines) | stat: -rw-r--r-- 30,653 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- Created on April 14, 2013 by texi2html 1.82
texi2html was written by: 
            Lionel Cons <Lionel.Cons@cern.ch> (original author)
            Karl Berry  <karl@freefriends.org>
            Olaf Bachmann <obachman@mathematik.uni-kl.de>
            and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>GNUstep D-Bus Programming Manual: 2. Using D-Bus From Objective-C</title>

<meta name="description" content="GNUstep D-Bus Programming Manual: 2. Using D-Bus From Objective-C">
<meta name="keywords" content="GNUstep D-Bus Programming Manual: 2. Using D-Bus From Objective-C">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<a name="Using-D_002dBus-From-Objective_002dC"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="DBusKit_1.html#Comparison" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Generating-Protocol-Declarations-With-dk_005fmake_005fprotocol" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit_1.html#Introduction" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Using-D_002dBus-From-Objective_002dC-1"></a>
<h1 class="chapter">2. Using D-Bus From Objective-C</h1>
<p>In order to access D-Bus services from an Objective-C application, the
DBusKit framework is required. It provides infrastructure for
managing connections to D-Bus message buses and translating Objective-C
message sends to D-Bus method calls. This way, DBusKit can make
interacting with D-Bus objects appear quite similar to the way one
usually interacts with the DO system.
</p>
<hr size="6">
<a name="Generating-Protocol-Declarations-With-dk_005fmake_005fprotocol"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Obtaining-a-Proxy-to-a-D_002dBus-Object" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.1 Generating Protocol Declarations With dk_make_protocol</h2>

<p>If your application wants to invoke methods on D-Bus objects, some
preparations are required: As with all other code, you need to provide
declarations for the methods you want to invoke. You can either do this
by writing them manually or let the <b>dk_make_protocol</b> tool
generate them for you. This is possible if an .interface-file containing
the introspection data for the interface exists. Calling <b>dk_make_protocol</b> with the &ldquo;<b>-i</b>&rdquo;
switch and the name of the .interface-file will generate a header file
with an Objective-C protocol declaration for that interface. For the
hypothetical interface file for <em>org.freedesktop.Introspectable</em>,
<b>dk_make_protocol</b> might generate the following header file:
</p><table><tr><td>&nbsp;</td><td><pre class="example">#import &lt;Foundation/Foundation.h&gt;
/*
 * Objective-C protocol declaration for the D-Bus
 * org.freedesktop.Introspectable interface.
 */
@protocol org_freedesktop_Introspectable

- (NSString*)Introspect;

@end
</pre></td></tr></table>
<p>The generated header file does only contain method declarations with
arguments and return values that are Objective-C classes. The following
default mappings between Foundation classes and D-Bus types are defined:
</p>
<table>
<tr><td width="15%">NSNumber</td><td width="80%">booleans (b), integers (y, n, q, i, u, x, t), floating point values (d)</td></tr>
<tr><td width="15%">NSString</td><td width="80%">strings (s)</td></tr>
<tr><td width="15%">DKProxy</td><td width="80%">object paths (o)</td></tr>
<tr><td width="15%">NSArray</td><td width="80%">arrays (a?), structs ((?*))</td></tr>
<tr><td width="15%">NSDictionary</td><td width="80%">dictionaries (a{??})</td></tr>
<tr><td width="15%">id</td><td width="80%">variants (v)</td></tr>
</table>
<p>Here &ldquo;?&rdquo; denotes a single complete D-Bus type signature and &ldquo;*&rdquo;
denotes possible repetition. It is, however, possible to use the plain C
types corresponding to the D-Bus types, because DBusKit is capable of
determining all necessary conversions. Thus the following declarations
all specify valid ways to invoke <code>NameHasOwner()</code> method from
<em>org.freedesktop.DBus</em>:
</p><table><tr><td>&nbsp;</td><td><pre class="example">- (NSNumber*)NameHasOwner: (NSString*)name;
- (NSNumber*)NameHasOwner: (char*)name;
- (BOOL)NameHasOwner: (NSString*)name;
- (BOOL)NameHasOwner: (char*)name;
</pre></td></tr></table>

<hr size="6">
<a name="Obtaining-a-Proxy-to-a-D_002dBus-Object"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Generating-Protocol-Declarations-With-dk_005fmake_005fprotocol" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Sending-Messages-to-D_002dBus-Objects" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.2 Obtaining a Proxy to a D-Bus Object</h2>
<p>With these provisions in place, it is quite easy to obtain a proxy to a
D-Bus object. The process is quite similar to creating a proxy to a
distant object using DO. First, you create the required ports:
</p><table><tr><td>&nbsp;</td><td><pre class="example">DKPort *sPort = [[DKPort alloc] initWithRemote: @&quot;org.freedesktop.DBus&quot;
                                         onBus: DKDBusSessionBus]
DKPort *rPort = [DKPort sessionBusPort];
</pre></td></tr></table>
<p>If a service on the system bus was the desired target, one could pass
<code>DKBusSystemBus</code> as the second argument of the <code>DKPort</code>
initialiser or use the <code>+systemBusPort</code> convenience method to
create a port object without remote.
</p> 
<p>Afterwards, a connection can be obtained to the
<em>org.freedesktop.DBus</em> service (which is bus itself) as follows:
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">NSConnection *c = [NSConnection connectionWithReceivePort: rPort
                                                 sendPort: sPort];
</pre></td></tr></table>
<p>Please note that this is exactly the way one would create a Distributed
Objects connection. Consequentially, on can obtain a proxy to an object
of this service by using <code>-rootProxy</code>:
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">id remoteObject = [c rootProxy];
</pre></td></tr></table>
<p>Unfortunately, a proxy to the root object of a D-Bus service is very
often not useful because services tend to install their primary object
at a path corresponding to the service name. DBusKit thus
extends <code>NSConnection</code> with a <code>-proxyAtPath:</code> method, which
can be used to obtain proxies to non-root object. It could be used to
obtain a proper proxy to <em>org.freedesktop.DBus</em> like this:
</p><table><tr><td>&nbsp;</td><td><pre class="example">id remoteObject = [c proxyAtPath: @&quot;/org/freedesktop/DBus&quot;];
</pre></td></tr></table>

<hr size="6">
<a name="Sending-Messages-to-D_002dBus-Objects"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Obtaining-a-Proxy-to-a-D_002dBus-Object" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Accessing-and-changing-D_002dBus-properties" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.3 Sending Messages to D-Bus Objects</h2>
<p>All further interactions with the remote object are indistinguishable
from interactions with an object in the local process. E.g. the
introspection data of the remote object could be obtained like this:
</p><table><tr><td>&nbsp;</td><td><pre class="example">NSString *introspectionData = [remoteObject Introspect];
</pre></td></tr></table>

<a name="index-interface_002c-D_002dBus-1"></a>
<a name="index-method_002c-D_002dBus"></a>
<a name="index-D_002dBus-interface-1"></a>
<a name="index-D_002dBus-method"></a>
<p>In some cases it is, however, necessary to treat D-Bus objects special:
Since D-Bus allows method names to be overloaded per interface, it might
be necessary to specify which method to call. DBusKit provides two
facilities to cope with this kind of situation. For one, it is possible
to embed the information about the required interface in the selector
string of the method to call. This is done by replacing all dots in the
interface string with underscores, placing it between <code>_DKIf_</code>
<code>_DKIfEnd_</code> marker and appending the method name.
</p>
<p>Assuming a D-Bus object  implements a <code>getBass()</code> method in the
interfaces <code>org.foo.Fish</code> and <code>org.bar.Instruments</code>, one could
distinguish between the methods by constructing the following selectors:
</p><ul>
<li> <code>-_DKIf_org_foo_Fish_DKIfEnd_getBass</code>
</li><li> <code>-_DKIf_org_bar_Instruments_DKIfEnd_getBass</code>
</li></ul>
<p>Since this is obviously quite clumsy, it will only be feasible for
simple cases. 
</p>
<p>The other facility provided by DBusKit is the
<code>-setPrimaryDBusInterface:</code> method, which instructs the proxy to
prefer the named interface when looking up methods. E.g. the following
statements would result in a call to the correct method:
</p><table><tr><td>&nbsp;</td><td><pre class="example">[remoteObject setPrimaryDBusInterface: @&quot;org.bar.Instruments&quot;];
id anInstrument = [remoteObject getBass];
</pre></td></tr></table>

<hr size="6">
<a name="Accessing-and-changing-D_002dBus-properties"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Sending-Messages-to-D_002dBus-Objects" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Watching-D_002dBus-Signals" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.4 Accessing and changing D-Bus properties</h2>
<a name="index-property_002c-D_002dBus"></a>
<a name="index-D_002dBus-property"></a>
<p>DBusKit will automatically generate getters and setters for D-Bus properties. A
D-Bus interface might, for example, specifythe following property in its
introspection data:
</p><table><tr><td>&nbsp;</td><td><pre class="example">&lt;property name=&quot;address&quot; type=&quot;s&quot; access=&quot;readwrite&quot;/&gt;
</pre></td></tr></table>
<p>This property can then be accessed by calling <code>-address</code> and changed by
calling <code>-setaddress:</code> on the proxy object. Just like with other methods,
both the plain C types and the corresponding Foundation classes are valid as
parameters to the getter and setter methods:
</p><table><tr><td>&nbsp;</td><td><pre class="example">- (NSString*)address;
- (char*)address;
- (void)setaddress: (NSString*)address;
- (void)setaddress: (char*)address;
</pre></td></tr></table>
<p>If other methods with the same names exist within the same interface of the
remote object, those will take precedence over the generated getter and setter
methods.
</p>
<hr size="6">
<a name="Watching-D_002dBus-Signals"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Accessing-and-changing-D_002dBus-properties" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Recovering-from-Failure" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.5 Watching D-Bus Signals</h2>
<a name="index-signal_002c-D_002dBus"></a>
<a name="index-D_002dBus-signal"></a>
<p>Besides responding to method calls, D-Bus objects can also actively
inform remote objects about events or state changes by the use of
<em>signals</em>. These signals are published to the bus and the bus will
re-broadcast them to all connected entities that subscribe to the
signals. DBusKit includes support for receiving D-Bus signals through
the <code>DKNotificationCenter</code> class. <code>DKNotificationCenter</code> keeps
to OpenStep conventions in that it delivers the signals it receives from
D-Bus in the form of <code>NSNotification</code>s and is thus similar to the
notification center classes provided by the Foundation library
(gnustep-base).
</p>
<p>To make use of the notification feature, it is sometimes not even
necessary to create any explicit proxies. It is enough to just obtain a
reference to one of the notification centers:
</p>
<table><tr><td>&nbsp;</td><td><pre class="example">DKNotificationCenter *center = [DKNotificationCenter sessionBusCenter];
</pre></td></tr></table>
<p>(Again, a reference to the notification center for the system bus can be
obtained similarly by using <code>+systemBusCenter</code>.) In a very simple
case, one would simply use the center to add an object as an observer of
the <em>NameAcquired</em> signal from the <em>org.freedesktop.DBus</em>
interface.
</p><table><tr><td>&nbsp;</td><td><pre class="example">[center addObserver: myObject
           selector: @selector(didReceiveNotification:)
               name: @&quot;DKSignal_org.freedesktop.DBus_NameAquired&quot;
             object: nil];
</pre></td></tr></table>
<p>This example also illustrates the naming convention for signals: They
start with the &ldquo;<code>DKSignal</code>&rdquo;-identifier and continue with the
interface name and the signal name separated by underscores
(&ldquo;<code>_</code>&rdquo;). Additionally, it is possible to register a custom
notification name for a signal:
</p><table><tr><td>&nbsp;</td><td><pre class="example">[center registerNotificationName: @&quot;DKNameAquired&quot;
                        asSignal: @&quot;NameAquired&quot;
                     inInterface: @&quot;org.freedesktop.DBus&quot;];
</pre></td></tr></table>
<p>If this method returns YES, it will be possible to register observers for the
<code>DKNameAquired</code> notification (it might fail if the signal was
already registered under another name).
</p>
<p>Since D-Bus provides a fine-grained matching mechanism for signals,
Objective-C applications can specify in great detail what kind of signal
they want to receive. The full-blown version of the registration method
could be called as follows:
</p><table><tr><td>&nbsp;</td><td><pre class="example">[center addObserver: myObject
           selector: @selector(didReceiveNotification:)
             signal: @&quot;NameOwnerChanged&quot;
          interface: @&quot;org.freedesktop.DBus&quot;
             sender: theBus
        destination: nil
             filter: @&quot;org.gnustep.TextEditor&quot;
            atIndex: 0];
</pre></td></tr></table>
<p>If registered as an observer this way, <code>myObject</code> would only
receive a notification if a new application took ownership of the name
<em>org.gnustep.TextEditor</em>. 
</p>
<p>When delivering annotification to the observer, the notification center
will create a <code>NSNotification</code> with a userInfo dictionary that
follows a specific format to allow the receiver to process the
notification:
</p><dl compact="compact">
<dt> <em>member</em></dt>
<dd><p>The name of the signal being emitted.
</p>
</dd>
<dt> <em>interface</em></dt>
<dd><p>The name of the interface the signal belongs to.
</p>
</dd>
<dt> <em>sender</em></dt>
<dd><p>The <em>unique</em> name of the service emitting the signal.
</p>
</dd>
<dt> <em>path</em></dt>
<dd><p>The path to the object of the service that emitted the signal.
</p>
</dd>
<dt> <em>destination</em></dt>
<dd><p>The intended receiver of the signal; might be empty if the signal was
broadcast, which is usually the case.
</p>
</dd>
<dt> <em>arg0, ..., n</em></dt>
<dd><p>If the signal did specify any values to be send alongside the signal,
these values will be present in keys called <em>arg0</em>, <em>arg1</em>,
..., <em>argn</em>. 
</p></dd>
</dl>

<p>Additionally, calling <code>-object</code> on the notification will return a
proxy to the object that emitted the signal.
</p>
<hr size="6">
<a name="Recovering-from-Failure"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Watching-D_002dBus-Signals" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Multi_002dThreading-Considerations" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.6 Recovering from Failure</h2>
<p>There are two common reasons for failure when communicating with objects on
D-Bus. One is that the service your application is accessing is going away. In
that case, DBusKit will notify you in a way similar to Distributed Objects. This
means that when the service disappears from the bus, the <code>DKPort</code> used will
post a <code>NSPortDidBecomeInvalidNotification</code> to the default notification
center. You can watch for this notification and attempt recovery afterwards. 
</p>
<p>A more critical reason for failure is a malfunction or restart of the D-Bus
daemon. In that case, all affected ports will issue a
<code>NSPortDidBecomeInvalidNotification</code> and additionally the <code>DKDBus</code>
object for the bus will post a <code>DKBusDisconnectedNotification</code> with the
<code>DKDBusBusType</code> identifier at the <code>busType</code> key of the userInfo
dictionary. Afterwards, DBusKit will attempt to recover from the failure in the
background and you cannot use D-Bus services until you receive a
<code>DKBusReconnectedNotification</code>. After receiving the notification, you can
perform recovery as your application requires.
</p>
<p>Please note that usually, such recovery from bus failures will only be
successful for the system bus, for which one connects to a socket
address that is persistent across restarts. For the session bus the socket
address is not persistent, but stored in the <code>DBUS_SESSION_BUS_ADDRESS</code>
environment variable. Hence your application should assume that the user session
died when it looses connection to the session bus. 
</p>
<hr size="6">
<a name="Multi_002dThreading-Considerations"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Recovering-from-Failure" title="Previous section in reading order"> &lt; </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next section in reading order"> &gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<h2 class="section">2.7 Multi-Threading Considerations</h2>
<p>By default, DBusKit runs in single-threaded mode. This means that all
interaction with the D-Bus daemon happens on the runloop of the calling thread.
If multiple threads try to send messages D-Bus objects, this model of execution
cannot guarantee that message delivery from and to the bus daemon are
successful. The framework should still be thread-safe in the sense that it will
continue functioning after raising an exception due to timeouts, but the desired
behaviour can only be acheived by putting DBusKit in multi-threaded mode.
</p>
<p>In multi-threaded mode, DBusKit will exchange messages with the D-Bus daemons
via a dedicated worker-thread. To enable this behaviour the
<code>+enableWorkerThread</code> method must be called on <code>DKPort</code>. All
processing will then be taking place on the worker thread. Developers should
note that after doing so, it is no longer safe to call into DBusKit from
<code>+initialize</code>-methods. The reason for this is that with present
Objective-C runtimes, <code>+initialize</code> will obtain a global lock and
subsequent initialisations of classes on the worker thread might cause a
deadlock. This is also the reason multi-threaded operation is not the default.
But developers are encouraged to use this feature if it does not interfere with
their application design.
</p>

<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Using-D_002dBus-From-Objective_002dC" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_3.html#Exposing-Objects-on-D_002dBus" title="Next chapter"> &gt;&gt; </a>]</td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left"> &nbsp; </td>
<td valign="middle" align="left">[<a href="DBusKit.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_5.html#Concept-Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="DBusKit_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<p>
 <font size="-1">
  This document was generated by <em>Niels Grewe</em> on <em>April 14, 2013</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
 </font>
 <br>

</p>
</body>
</html>