forked from hypfvieh/dbus-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexporting-objects.html
More file actions
217 lines (194 loc) · 9.12 KB
/
Copy pathexporting-objects.html
File metadata and controls
217 lines (194 loc) · 9.12 KB
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
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from src/site/markdown/exporting-objects.md at 2025-12-23
| Rendered using Apache Maven Fluido Skin 2.1.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<title>Exporting Objects – dbus-java</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-2.1.0.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-2.1.0.min.js"></script>
</head>
<body>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2025-12-21</li>
<li id="projectVersion" class="pull-right">Version: 5.2.1-SNAPSHOT</li>
<li class="pull-right"><span class="divider">|</span>
<a href="https://search.maven.org/search?q=g:com.github.hypfvieh%20dbus-java">Maven Central</a></li>
<li class="pull-right"><span class="divider">|</span>
<a href="https://www.github.com/hypfvieh/dbus-java/wiki">GitHub Wiki</a></li>
<li class="pull-right"><a href="https://www.github.com/hypfvieh/dbus-java">GitHub Repository</a></li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Overview</li>
<li><a href="index.html">Home</a></li>
<li><a href="quick-start.html">Quickstart</a></li>
<li><a href="code-generation.html">Code Generation</a></li>
<li class="active"><a>Exporting Objects</a></li>
<li><a href="remote-objects.html">Calling Remote Objects</a></li>
<li><a href="properties.html">Properties</a></li>
<li><a href="variant-handling.html">Variant</a></li>
<li><a href="using-signals.html">Signals</a></li>
<li><a href="howto.html">Howto...</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html"><span class="icon-chevron-right"></span>Project Information</a></li>
<li><a href="project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
<li class="nav-header">Modules</li>
<li><a href="dbus-java-core/index.html">dbus-java-core</a></li>
<li><a href="dbus-java-osgi/index.html">dbus-java-osgi</a></li>
<li><a href="dbus-java-utils/index.html">dbus-java-utils</a></li>
<li><a href="dbus-java-transport-jnr-unixsocket/index.html">dbus-java-transport-jnr-unixsocket</a></li>
<li><a href="dbus-java-transport-junixsocket/index.html">dbus-java-transport-junixsocket</a></li>
<li><a href="dbus-java-transport-native-unixsocket/index.html">dbus-java-transport-native-unixsocket</a></li>
<li><a href="dbus-java-transport-tcp/index.html">dbus-java-transport-tcp</a></li>
<li><a href="dbus-java-bom/index.html">dbus-java-bom</a></li>
<li><a href="dbus-java-tests/index.html">dbus-java-tests</a></li>
<li><a href="dbus-java-examples/index.html">dbus-java-examples</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Exporting_Objects"></a>
<h1>Exporting Objects</h1>
<p><em><a href="https://github.com/hypfvieh/dbus-java/tree/master/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/export" class="externalLink">Full Source</a></em></p>
<p>In order for other programs to call a method, you must first export the object
onto the bus in order for other programs to see it. The object that is exported
must implement an interface which extends <code>DBusInterface</code>.</p>
<p>Here's a sample interface:</p>
<pre class="prettyprint"><code class="language-java">package com.github.hypfvieh.dbus.examples.export;
import org.freedesktop.dbus.interfaces.DBusInterface;
public interface ISampleExport extends DBusInterface {
int add(int _a, int _b);
void terminateApp();
}
</code></pre>
<p>In order for other applications to call this interface, we need to create an
object that implements this interface and export it onto the bus. Here's the
full code that does that:</p>
<pre class="prettyprint"><code class="language-java">package com.github.hypfvieh.dbus.examples.export;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import java.util.concurrent.CountDownLatch;
public class ExportExample implements ISampleExport {
private DBusConnection dbusConn;
private CountDownLatch waitClose;
ExportExample() throws DBusException, InterruptedException {
waitClose = new CountDownLatch(1);
// Get a connection to the session bus so we can request a bus name
dbusConn = DBusConnectionBuilder.forSessionBus().build();
// Request a unique bus name
dbusConn.requestBusName("test.dbusjava.export");
// Export this object onto the bus using the path '/'
dbusConn.exportObject(getObjectPath(), this);
// this will cause the countdown latch to wait until terminateApp() was called
// you probably want to do something more useful
waitClose.await();
System.out.println("bye bye");
}
@Override
public int add(int _a, int _b) {
return _a + _b;
}
@Override
public void terminateApp() {
waitClose.countDown();
}
@Override
public boolean isRemote() {
/* Whenever you are implementing an object, always return false */
return false;
}
@Override
public String getObjectPath() {
/*
* This is not strictly needed;
* it is a convenience method for housekeeping on your application side if you will
* be exporting and unexporting many times
*/
return "/";
}
public static void main(String[] _args) throws Exception {
new ExportExample();
}
}
</code></pre>
<p>If you compile and run this program, you will be able to do three things at this
point: view the introspection data get the result of an addition and terminate our application by
calling <code>terminateApp()</code>.</p>
<p>If we introspect our application like the following, we can see the
<em>automatically</em> generated introspection XML document. This means that we can
define an entire interface without touching XML at all.</p>
<pre><code class="nohighlight nocode">$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / org.freedesktop.DBus.Introspectable.Introspect
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection
1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/">
<interface name="com.github.hypfvieh.dbus.examples.export.ISampleExport">
<method name="add" >
<arg type="i" direction="in"/>
<arg type="i" direction="in"/>
<arg type="i" direction="out"/>
</method>
<method name="terminateApp" >
</method>
</interface>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect" >
<arg type="s" direction="out"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Peer">
<method name="Ping" >
</method>
<method name="GetMachineId" >
<arg type="s" direction="out"/>
</method>
</interface>
</node>
</code></pre>
<p>Next, we can go call the remote method using dbus-send and get the result of
the addition back:</p>
<pre><code class="nohighlight nocode">$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / com.foo.IntInterface.add int32:5 int32:7
int32 12
</code></pre>
<p>Or we can use the <code>terminateApp()</code> method to stop our application:</p>
<pre><code class="nohighlight nocode">$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / com.foo.IntInterface.terminateApp
</code></pre></section> </main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2025
</p>
</div>
</div>
</footer>
</body>
</html>