Skip to content

Commit

Permalink
Updated site doc
Browse files Browse the repository at this point in the history
  • Loading branch information
hypfvieh committed Oct 4, 2023
1 parent e69b382 commit 92da3ab
Showing 1 changed file with 70 additions and 42 deletions.
112 changes: 70 additions & 42 deletions docs/exporting-objects.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,46 +76,61 @@
</header>
<main id="bodyColumn" class="span10" >
<h1>Exporting Objects</h1>
<p><i><a class="externalLink" href="https://github.com/hypfvieh/dbus-java/tree/master/dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/export">Full Source</a></i></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>

<div class="source"><pre class="prettyprint"><code>package com.foo;
<div class="source"><pre class="prettyprint"><code class="language-java">package com.github.hypfvieh.dbus.examples.export;

import org.freedesktop.dbus.interfaces.DBusInterface;

public interface IntInterface extends DBusInterface {
public interface ISampleExport extends DBusInterface {
int add(int _a, int _b);

public int add( int a, int b );
void terminateApp();
}
</code></pre></div>
<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>

<div class="source"><pre class="prettyprint"><code>package com.foo;
<div class="source"><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;

public class ExportExample implements IntInterface {

private DBusConnection m_conn;
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(&quot;test.dbusjava.export&quot;);
// 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(&quot;bye bye&quot;);
}

ExportExample() throws DBusException {
/* Get a connection to the session bus so we can request a bus name */
m_conn = DBusConnectionBuilder.forSessionBus().build();
/* Request a unique bus name */
m_conn.requestBusName( &quot;test.dbusjava.export&quot; );
/* Export this object onto the bus using the path '/' */
m_conn.exportObject( getObjectPath(), this );
@Override
public int add(int _a, int _b) {
return _a + _b;
}

@Override
public int add(int a, int b) {
return a + b;
public void terminateApp() {
waitClose.countDown();
}

@Override
Expand All @@ -126,51 +141,64 @@ <h1>Exporting Objects</h1>

@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
/*
* 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 &quot;/&quot;;
}


public static void main(String[] args) throws DBusException {
public static void main(String[] _args) throws Exception {
new ExportExample();
}

}
</code></pre></div>
<p>If you compile and run this program, you will be able to do two things at this
point: view the introspection data, and get the result of an addition.</p>
<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
<i>automatically</i> generated introspection XML document. This means that we can
define an entire interface without touching XML at all.</p>

<div class="source"><pre class="prettyprint"><code>$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / org.freedesktop.DBus.Introspectable.Introspect
&lt;!DOCTYPE node PUBLIC &quot;-//freedesktop//DTD D-BUS Object Introspection 1.0//EN&quot; &quot;http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd&quot;&gt;
&lt;node name=&quot;/&quot;&gt;
&lt;interface name=&quot;com.foo.IntInterface&quot;&gt;
&lt;method name=&quot;add&quot; &gt;
&lt;arg type=&quot;i&quot; direction=&quot;in&quot;/&gt;
&lt;arg type=&quot;i&quot; direction=&quot;in&quot;/&gt;
&lt;arg type=&quot;i&quot; direction=&quot;out&quot;/&gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;interface name=&quot;org.freedesktop.DBus.Introspectable&quot;&gt;
&lt;method name=&quot;Introspect&quot;&gt;
&lt;arg type=&quot;s&quot; direction=&quot;out&quot;/&gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;interface name=&quot;org.freedesktop.DBus.Peer&quot;&gt;
&lt;method name=&quot;Ping&quot;&gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;/node&gt;

&lt;!DOCTYPE node PUBLIC &quot;-//freedesktop//DTD D-BUS Object Introspection
1.0//EN&quot; &quot;http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd&quot;&gt;
&lt;node name=&quot;/&quot;&gt;
&lt;interface name=&quot;com.github.hypfvieh.dbus.examples.export.ISampleExport&quot;&gt;
&lt;method name=&quot;add&quot; &gt;
&lt;arg type=&quot;i&quot; direction=&quot;in&quot;/&gt;
&lt;arg type=&quot;i&quot; direction=&quot;in&quot;/&gt;
&lt;arg type=&quot;i&quot; direction=&quot;out&quot;/&gt;
&lt;/method&gt;
&lt;method name=&quot;terminateApp&quot; &gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;interface name=&quot;org.freedesktop.DBus.Introspectable&quot;&gt;
&lt;method name=&quot;Introspect&quot; &gt;
&lt;arg type=&quot;s&quot; direction=&quot;out&quot;/&gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;interface name=&quot;org.freedesktop.DBus.Peer&quot;&gt;
&lt;method name=&quot;Ping&quot; &gt;
&lt;/method&gt;
&lt;method name=&quot;GetMachineId&quot; &gt;
&lt;arg type=&quot;s&quot; direction=&quot;out&quot;/&gt;
&lt;/method&gt;
&lt;/interface&gt;
&lt;/node&gt;
</code></pre></div>
<p>Next, we can go call the remote method using dbus-send and get the result of
the addition back:</p>

<div class="source"><pre class="prettyprint"><code>$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / com.foo.IntInterface.add int32:5 int32:7
int32 12
</code></pre></div>
<p>Or we can use the <code>terminateApp()</code> method to stop our application:</p>

<div class="source"><pre class="prettyprint"><code>$ dbus-send --print-reply=literal --type=method_call --dest=test.dbusjava.export / com.foo.IntInterface.terminateApp
</code></pre></div>
</main>
</div>
Expand Down

0 comments on commit 92da3ab

Please sign in to comment.