<?xml version="1.0" encoding="iso-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd--> <html xmlns="http://www.w3.org/1999/xhtml" > <head><title>DBusConnection</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" /> <meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" /> <!-- xhtml,2,html --> <meta name="src" content="dbus-java.tex" /> <meta name="date" content="2016-03-02 10:34:00" /> <link rel="stylesheet" type="text/css" href="dbus-java.css" /> </head><body > <!--l. 119--><div class="crosslinks"><p class="noindent">[<a href="dbus-javase9.html" >next</a>] [<a href="dbus-javase1.html" >prev</a>] [<a href="dbus-javase1.html#taildbus-javase1.html" >prev-tail</a>] [<a href="#taildbus-javase2.html">tail</a>] [<a href="dbus-java.html#dbus-javase5.html" >up</a>] </p></div> <h3 class="sectionHead"><span class="titlemark">2 </span> <a id="x9-130002"></a>DBusConnection</h3> <!--l. 121--><p class="noindent" >The <span class="cmtt-12">DBusConnection</span><span class="footnote-mark"><a href="dbus-java10.html#fn4x0"><sup class="textsuperscript">4</sup></a></span><a id="x9-13001f4"></a> class provides methods for connecting to the bus, exporting objects, sending signals and getting references to remote objects. </p><!--l. 126--><p class="indent" > <span class="cmtt-12">DBusConnection </span>is a singleton class, multiple calls to <span class="cmtt-12">getConnection </span>will return the same bus connection. </p> <div class="verbatim" id="verbatim-1"> conn = DBusConnection.getConnection(DBusConnection.SESSION); </div> <!--l. 131--><p class="nopar" > </p><!--l. 133--><p class="indent" > This creates a connection to the session bus, or returns the existing connection. </p> <div class="verbatim" id="verbatim-2"> conn.addSigHandler(TestSignalInterface.TestSignal.class,  <br />                   new SignalHandler()); </div> <!--l. 139--><p class="nopar" > </p><!--l. 141--><p class="indent" > This sets up a signal handler for the given signal type. SignalHandler.handle will be called in a new thread with an instance of TestSignalInterface.TestSignal when that signal is recieved. </p> <div class="verbatim" id="verbatim-3"> conn.sendSignal(new TestSignalInterface.TestSignal(  <br />                     "/foo/bar/com/Wibble",  <br />                     "Bar",  <br />                     new UInt32(42))); </div> <!--l. 150--><p class="nopar" > </p><!--l. 152--><p class="indent" > This sends a signal of type <span class="cmtt-12">TestSignalInterface.TestSignal</span>, from the object <span class="cmti-12">“/foo/bar/com/Wibble” </span>with the arguments <span class="cmti-12">“Bar” </span>and <span class="cmtt-12">UInt32(42)</span>. </p> <div class="verbatim" id="verbatim-4"> conn.exportObject("/Test", new testclass()); </div> <!--l. 158--><p class="nopar" > </p><!--l. 160--><p class="indent" > This exports the <span class="cmtt-12">testclass </span>object on the path <span class="cmti-12">“/Test”</span> </p> <div class="verbatim" id="verbatim-5"> Introspectable intro = (Introspectable) conn.getRemoteObject(  <br />                              "foo.bar.Test", "/Test",  <br />                              Introspectable.class); </div> <!--l. 166--><p class="nopar" > </p><!--l. 168--><p class="indent" > This gets a reference to the <span class="cmti-12">“/Test” </span>object on the process with the name <span class="cmti-12">“foo.bar.Test” </span>. The object implements the <span class="cmtt-12">Introspectable </span>interface, and calls may be made to methods in that interface as if it was a local object. </p> <div class="verbatim" id="verbatim-6"> String data = intro.Introspect(); </div> <!--l. 175--><p class="nopar" > </p><!--l. 177--><p class="indent" > The Runtime Exception <span class="cmtt-12">DBusExecutionException </span>may be thrown by any remote method if any part of the execution fails. </p> <h4 class="subsectionHead"><span class="titlemark">2.1 </span> <a id="x9-140002.1"></a>Asynchronous Calls</h4> <!--l. 182--><p class="noindent" >Calling a method on a remote object is synchronous, that is the thread will block until it has a reply. If you do not want to block you can use an asynchronous call. </p><!--l. 186--><p class="indent" > There are two ways of making asynchronous calls. You can either call the <span class="cmtt-12">callMethodAsync </span>function on the connection object, in which case you are returned a <span class="cmtt-12">DBusAsyncReply</span><span class="footnote-mark"><a href="dbus-java11.html#fn5x0"><sup class="textsuperscript">5</sup></a></span><a id="x9-14001f5"></a> object which can be used to check for a reply and get the return value. This is demonstrated in figure <a href="#x9-140021">1<!--tex4ht:ref: fig:async --></a>. </p> <hr class="figure" /><div class="figure" > <a id="x9-140021"></a> <div class="center" > <!--l. 194--><p class="noindent" > </p> <div class="verbatim" id="verbatim-7"> DBusAsyncReply<Boolean> stuffreply =  <br />   conn.callMethodAsync(remoteObject, "methodname", arg1, arg2);  <br />...  <br />if (stuffreply.hasReply()) {  <br />   Boolean b = stuffreply.getReply();  <br />   ...  <br />} </div> <!--l. 203--><p class="nopar" ></p></div> <br /> <div class="caption" ><span class="id">Figure 1: </span><span class="content">Calling an asynchronous method</span></div><!--tex4ht:label?: x9-140021 --> </div><hr class="endfigure" /> <!--l. 209--><p class="indent" > Alternatively, you can register a callback with the connection using the <span class="cmtt-12">callWithCallback</span> function on the connection object. In this case, your callback class (implementing the <span class="cmtt-12">CallbackHandler</span><span class="footnote-mark"><a href="dbus-java12.html#fn6x0"><sup class="textsuperscript">6</sup></a></span><a id="x9-14003f6"></a> interface will be called when the reply is returned from the bus. </p> <!--l. 215--><div class="crosslinks"><p class="noindent">[<a href="dbus-javase9.html" >next</a>] [<a href="dbus-javase1.html" >prev</a>] [<a href="dbus-javase1.html#taildbus-javase1.html" >prev-tail</a>] [<a href="dbus-javase2.html" >front</a>] [<a href="dbus-java.html#dbus-javase5.html" >up</a>] </p></div> <!--l. 215--><p class="indent" > <a id="taildbus-javase2.html"></a> </p> </body></html>