Compare commits
2 Commits
596dddd630
...
d433179db4
| Author | SHA1 | Date | |
|---|---|---|---|
| d433179db4 | |||
| 70a7f3768e |
@@ -3,6 +3,7 @@ package net.eksb.obsdc
|
|||||||
import org.freedesktop.dbus.annotations.DBusInterfaceName
|
import org.freedesktop.dbus.annotations.DBusInterfaceName
|
||||||
import org.freedesktop.dbus.connections.impl.DBusConnection
|
import org.freedesktop.dbus.connections.impl.DBusConnection
|
||||||
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder
|
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder
|
||||||
|
import org.freedesktop.dbus.exceptions.DBusException
|
||||||
import org.freedesktop.dbus.interfaces.DBusInterface
|
import org.freedesktop.dbus.interfaces.DBusInterface
|
||||||
import org.freedesktop.dbus.interfaces.DBusSigHandler
|
import org.freedesktop.dbus.interfaces.DBusSigHandler
|
||||||
import org.freedesktop.dbus.messages.DBusSignal
|
import org.freedesktop.dbus.messages.DBusSignal
|
||||||
@@ -26,23 +27,32 @@ class DBus(private val opRunner:OpRunner): AutoCloseable {
|
|||||||
val dbus = DBusConnectionBuilder.forSessionBus().build()
|
val dbus = DBusConnectionBuilder.forSessionBus().build()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
// These lines are not necessary to handle signals, but are necessary to register methods.
|
// These lines are not necessary to handle signals, but are necessary to register methods.
|
||||||
dbus.requestBusName("net.eksb.obsdc")
|
try {
|
||||||
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
|
dbus.requestBusName(BUS_NAME)
|
||||||
|
} catch (e:DBusException) {
|
||||||
|
error("Error requesting bus. Already running?")
|
||||||
|
}
|
||||||
|
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
|
||||||
|
|
||||||
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal ->
|
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal ->
|
||||||
log.debug("signal: ${signal.op}")
|
log.debug("signal: ${signal.op}")
|
||||||
val op = Op.valueOf(signal.op)
|
val op = Op.valueOf(signal.op)
|
||||||
log.debug("op: ${op}")
|
log.debug("op: ${op}")
|
||||||
opRunner.run(op)
|
opRunner.run(op)
|
||||||
}
|
}
|
||||||
|
log.debug("DBUS initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
|
dbus.releaseBusName(BUS_NAME)
|
||||||
dbus.close()
|
dbus.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
private val log = LoggerFactory.getLogger(DBus::class.java)
|
companion object {
|
||||||
|
private const val BUS_NAME = "net.eksb.obsdc"
|
||||||
|
private val log = LoggerFactory.getLogger(DBus::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <reified T: DBusSignal> DBusConnection.addSigHandler(handler: DBusSigHandler<T>) {
|
inline fun <reified T: DBusSignal> DBusConnection.addSigHandler(handler: DBusSigHandler<T>) {
|
||||||
|
|||||||
@@ -1,18 +1,26 @@
|
|||||||
package net.eksb.obsdc
|
package net.eksb.obsdc
|
||||||
|
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
object Main {
|
object Main {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val config = CONFIG_FILE.properties()
|
try {
|
||||||
Obs(
|
val config = CONFIG_FILE.properties()
|
||||||
host = config.getProperty("host") ?: "localhost",
|
Obs(
|
||||||
port = config.getProperty("port")?.toInt() ?: 4455,
|
host = config.getProperty("host") ?: "localhost",
|
||||||
password = config.getProperty("password") ?: error("config missing \"password\""),
|
port = config.getProperty("port")?.toInt() ?: 4455,
|
||||||
connectionTimeout = config.getProperty("connectionTimeout")?.toInt() ?: 5
|
password = config.getProperty("password") ?: error("config missing \"password\""),
|
||||||
).use { obs ->
|
connectionTimeout = config.getProperty("connectionTimeout")?.toInt() ?: 5
|
||||||
DBus(OpRunner(obs)).use { dbus ->
|
).use { obs ->
|
||||||
waitForShutdown()
|
DBus(OpRunner(obs)).use { dbus ->
|
||||||
|
waitForShutdown()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e:Exception) {
|
||||||
|
log.error(e.message, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val log = LoggerFactory.getLogger(Main::class.java)
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ class Obs(
|
|||||||
.host(host)
|
.host(host)
|
||||||
.port(port)
|
.port(port)
|
||||||
.password(password)
|
.password(password)
|
||||||
.autoConnect(true)
|
.autoConnect(false)
|
||||||
.connectionTimeout(connectionTimeout)
|
.connectionTimeout(connectionTimeout)
|
||||||
.lifecycle()
|
.lifecycle()
|
||||||
.onReady(::onReady)
|
.onReady(::onReady)
|
||||||
@@ -67,7 +67,11 @@ class Obs(
|
|||||||
// OBSRemoteController starts a non-daemon thread. It probably should not do that.
|
// OBSRemoteController starts a non-daemon thread. It probably should not do that.
|
||||||
// Kill it on shutdown.
|
// Kill it on shutdown.
|
||||||
addShutdownHook {
|
addShutdownHook {
|
||||||
controller.stop()
|
close()
|
||||||
|
}
|
||||||
|
// connect() blocks until OBS is up, so fork it.
|
||||||
|
thread(name="obs-init-connect", isDaemon=true, start=true) {
|
||||||
|
controller.connect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,22 +116,24 @@ class Obs(
|
|||||||
/**
|
/**
|
||||||
* Thread that runs submitted requests from [q] when [ready].
|
* Thread that runs submitted requests from [q] when [ready].
|
||||||
*/
|
*/
|
||||||
private val opThread = thread(name="obs-op", isDaemon=true, start=true) {
|
private val opThread = thread(name="obs-op", isDaemon=false, start=true) {
|
||||||
while(!closed.get()) {
|
while(!closed.get()) {
|
||||||
val req = q.take()
|
val req = try {
|
||||||
|
q.take()
|
||||||
|
} catch (e:InterruptedException) {
|
||||||
|
log.debug("interrupted taking req")
|
||||||
|
continue
|
||||||
|
}
|
||||||
log.debug("got req: ${req}, wait for ready")
|
log.debug("got req: ${req}, wait for ready")
|
||||||
ready.enter {
|
try {
|
||||||
log.debug("ready")
|
ready.enter {
|
||||||
if (!req.expired()) {
|
log.debug("ready")
|
||||||
try {
|
if (!req.expired()) {
|
||||||
req.block.invoke(controller)
|
req.block.invoke(controller)
|
||||||
} catch (e:InterruptedException) {
|
|
||||||
log.debug("interrupted")
|
|
||||||
throw e
|
|
||||||
} catch (e:Exception) {
|
|
||||||
log.error("req ${req} failed", e )
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e:Exception) {
|
||||||
|
log.error("req ${req} failed", e )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.debug("done")
|
log.debug("done")
|
||||||
@@ -147,7 +153,6 @@ class Obs(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
log.debug("close")
|
|
||||||
closed.set(true)
|
closed.set(true)
|
||||||
opThread.interrupt()
|
opThread.interrupt()
|
||||||
controller.disconnect()
|
controller.disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user