better error handling and log messages
This commit is contained in:
@@ -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
|
||||||
@@ -27,7 +28,11 @@ class DBus(private val opRunner:OpRunner): AutoCloseable {
|
|||||||
|
|
||||||
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.requestBusName(BUS_NAME)
|
||||||
|
} catch (e:DBusException) {
|
||||||
|
error("Error requesting bus. Already running?")
|
||||||
|
}
|
||||||
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
|
dbus.exportObject("/", ObsdcDBusInterfaceImpl())
|
||||||
|
|
||||||
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal ->
|
dbus.addSigHandler<ObsdcDBusInterface.Signal> { signal ->
|
||||||
@@ -36,13 +41,18 @@ class DBus(private val opRunner:OpRunner): AutoCloseable {
|
|||||||
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val BUS_NAME = "net.eksb.obsdc"
|
||||||
private val log = LoggerFactory.getLogger(DBus::class.java)
|
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,8 +1,11 @@
|
|||||||
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>) {
|
||||||
|
try {
|
||||||
val config = CONFIG_FILE.properties()
|
val config = CONFIG_FILE.properties()
|
||||||
Obs(
|
Obs(
|
||||||
host = config.getProperty("host") ?: "localhost",
|
host = config.getProperty("host") ?: "localhost",
|
||||||
@@ -14,5 +17,10 @@ object Main {
|
|||||||
waitForShutdown()
|
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)
|
||||||
@@ -69,6 +69,10 @@ class Obs(
|
|||||||
addShutdownHook {
|
addShutdownHook {
|
||||||
close()
|
close()
|
||||||
}
|
}
|
||||||
|
// connect() blocks until OBS is up, so fork it.
|
||||||
|
thread(name="obs-init-connect", isDaemon=true, start=true) {
|
||||||
|
controller.connect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onClose(e:WebSocketCloseCode) {
|
private fun onClose(e:WebSocketCloseCode) {
|
||||||
@@ -112,24 +116,26 @@ 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")
|
||||||
|
try {
|
||||||
ready.enter {
|
ready.enter {
|
||||||
log.debug("ready")
|
log.debug("ready")
|
||||||
if (!req.expired()) {
|
if (!req.expired()) {
|
||||||
try {
|
|
||||||
req.block.invoke(controller)
|
req.block.invoke(controller)
|
||||||
} catch (e:InterruptedException) {
|
}
|
||||||
log.debug("interrupted")
|
}
|
||||||
throw e
|
|
||||||
} catch (e:Exception) {
|
} catch (e:Exception) {
|
||||||
log.error("req ${req} failed", e )
|
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