-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #236: Updated documentation for exporting objects; Added exampl…
…e code
- Loading branch information
Showing
4 changed files
with
165 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/export/ExportExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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; | ||
|
||
/** | ||
* Sample code to demonstrate exporting of an object.<br> | ||
* This is the code also found in {@code export-objects.md} | ||
* | ||
* @author hypfvieh | ||
* @since 5.0.0 - 2023-10-04 | ||
*/ | ||
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(); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
dbus-java-examples/src/main/java/com/github/hypfvieh/dbus/examples/export/ISampleExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.hypfvieh.dbus.examples.export; | ||
|
||
import org.freedesktop.dbus.interfaces.DBusInterface; | ||
|
||
/** | ||
* Example interface used to demonstrate exporting of an object. | ||
* | ||
* @author hypfvieh | ||
* @since 5.0.0 - 2023-10-04 | ||
*/ | ||
public interface ISampleExport extends DBusInterface { | ||
/** | ||
* Adds a to b and returns the result. | ||
* | ||
* @param _a first number | ||
* @param _b second number | ||
* @return sum of both numbers | ||
*/ | ||
int add(int _a, int _b); | ||
|
||
/** | ||
* Terminate the running application remotely. | ||
*/ | ||
void terminateApp(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters