init. dbus registration works

This commit is contained in:
2023-10-20 09:26:38 -04:00
commit 9c598a42bb
4 changed files with 104 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
# Ignore Gradle project-specific cache directory
.gradle
gradle
gradlew*
# Ignore Gradle build output directory
build

28
build.gradle.kts Normal file
View File

@@ -0,0 +1,28 @@
plugins {
id("org.jetbrains.kotlin.jvm") version "1.9.0"
application
}
dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.3")
implementation("com.github.hypfvieh:dbus-java-core:4.3.1")
runtimeOnly("com.github.hypfvieh:dbus-java-transport-native-unixsocket:4.3.1")
implementation("org.slf4j:slf4j-api:2.0.9")
runtimeOnly("org.slf4j:slf4j-simple:2.0.9")
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}
application {
mainClass.set("net.eksb.obsdc.Main")
}
tasks.named<Test>("test") {
useJUnitPlatform()
}

1
settings.gradle.kts Normal file
View File

@@ -0,0 +1 @@
rootProject.name = "obsdc"

View File

@@ -0,0 +1,68 @@
package net.eksb.obsdc
import org.freedesktop.dbus.annotations.DBusInterfaceName
import org.freedesktop.dbus.connections.impl.DBusConnection
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder
import org.freedesktop.dbus.interfaces.DBusInterface
import org.freedesktop.dbus.interfaces.DBusSigHandler
import org.freedesktop.dbus.messages.DBusSignal
import org.slf4j.LoggerFactory
object Main {
@JvmStatic
fun main(args:Array<String>) {
DBusConnectionBuilder.forSessionBus().build().use { dbus ->
// These lines are not necessary to handle signals, but are necessary to register methods.
dbus.requestBusName("net.eksb.obsdc")
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal -> log.info("signal: ${signal.message}") }
while (true) {
try {
Thread.sleep(60_000)
} catch (e:InterruptedException) {
log.info("interrupted")
break
}
}
}
log.info("done")
}
}
inline fun <reified T:DBusSignal> DBusConnection.addSigHandler(handler:DBusSigHandler<T>) {
addSigHandler(T::class.java, handler)
}
private val log = LoggerFactory.getLogger(Main::class.java)
@DBusInterfaceName("net.eksb.Obsdc")
interface ObsdcDBusInterface: DBusInterface {
fun echo(message:String): String
class Signal(path:String, val message:String): DBusSignal(path, message)
}
class ObsdcDBusInterfaceImpl: ObsdcDBusInterface {
override fun echo(message: String):String {
log.info("echo: ${message}")
return message
}
override fun getObjectPath(): String = "/"
}
/*
Monitor:
`dbus-monitor`
See what is registered:
`qdbus net.eksb.obsdc /`
Send signal:
`dbus-send --session --type=signal --dest=net.eksb.obsdc / net.eksb.Obsdc.Signal string:a`
Call method:
`dbus-send --session --type=method_call --print-reply --dest=net.eksb.obsdc / net.eksb.Obsdc.echo string:b`
`qdbus net.eksb.obsdc / net.eksb.Obsdc.echo hello`
*/