init. dbus registration works
This commit is contained in:
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal 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
28
build.gradle.kts
Normal 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
1
settings.gradle.kts
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = "obsdc"
|
||||
68
src/main/kotlin/net/eksb/obsdc/Main.kt
Normal file
68
src/main/kotlin/net/eksb/obsdc/Main.kt
Normal 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`
|
||||
*/
|
||||
Reference in New Issue
Block a user