Skip to content

Commit 31372f1

Browse files
committed
Upgrade to bread_server_lib 2.32.1
1 parent 538bf2c commit 31372f1

File tree

6 files changed

+47
-68
lines changed

6 files changed

+47
-68
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ repositories {
1212

1313
dependencies {
1414
testImplementation(kotlin("test"))
15-
implementation("org.bread_experts_group:bread_server_lib-code:2.22.2")
15+
implementation("org.bread_experts_group:bread_server_lib-code:2.32.1")
1616
}
1717

1818
tasks.test {

src/main/kotlin/client/CarpoolCLIMain.kt

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package org.bread_experts_group.application_carpool.client
22

33
import org.bread_experts_group.application_carpool.rmi.Supervisor
4-
import org.bread_experts_group.Flag
5-
import org.bread_experts_group.MultipleArgs
6-
import org.bread_experts_group.SingleArgs
7-
import org.bread_experts_group.readArgs
8-
import org.bread_experts_group.logging.ColoredLogger
9-
import org.bread_experts_group.stringToBoolean
10-
import org.bread_experts_group.stringToInt
11-
import org.bread_experts_group.stringToLong
4+
import org.bread_experts_group.command_line.ArgumentContainer
5+
import org.bread_experts_group.command_line.Flag
6+
import org.bread_experts_group.command_line.readArgs
7+
import org.bread_experts_group.logging.ColoredHandler
8+
import org.bread_experts_group.command_line.stringToBoolean
9+
import org.bread_experts_group.command_line.stringToInt
10+
import org.bread_experts_group.command_line.stringToLong
1211
import rmi.ApplicationNotFoundException
1312
import java.lang.management.ManagementFactory
1413
import java.nio.file.Path
1514
import java.rmi.UnmarshalException
1615
import java.rmi.registry.LocateRegistry
1716
import java.util.logging.Level
17+
import java.util.logging.Logger
1818
import kotlin.io.path.Path
1919
import kotlin.io.path.absolutePathString
2020
import kotlin.io.path.createDirectories
@@ -31,7 +31,7 @@ val FLAGS = listOf(
3131
"port",
3232
"The port to use for the RMI registry.",
3333
default = 1099,
34-
conv = ::stringToInt
34+
conv = stringToInt()
3535
),
3636
Flag(
3737
"log_directory",
@@ -74,66 +74,68 @@ val FLAGS = listOf(
7474
"Command to remove a managed application.",
7575
default = -1,
7676
repeatable = true,
77-
conv = ::stringToLong
77+
conv = stringToLong()
7878
)
7979
)
80-
private val LOGGER = ColoredLogger.newLogger("Application Carpool CLI")
81-
val SINGLE_COMMANDS = listOf("list_applications", "status", "stop")
80+
private val LOGGER = Logger.getLogger("Application Carpool CLI")
8281

8382
fun main(args: Array<String>) {
84-
val (singleArgs, multipleArgs) = readArgs(
83+
LOGGER.useParentHandlers = false
84+
LOGGER.addHandler(ColoredHandler())
85+
86+
val args = readArgs(
8587
args,
8688
FLAGS,
8789
"Application Carpool",
8890
"A program for running your applications in the background."
8991
)
90-
LOGGER.level = singleArgs["log_level"] as Level
92+
LOGGER.level = args.getRequired<Level>("log_level")
9193
LOGGER.fine("Reading arguments")
9294

93-
(singleArgs["log_directory"] as Path).createDirectories()
95+
args.getRequired<Path>("log_directory").createDirectories()
9496

95-
connectToSupervisor(singleArgs, multipleArgs, false)
97+
connectToSupervisor(args, false)
9698
}
9799

98-
private fun connectToSupervisor(singleArgs: SingleArgs, multipleArgs: MultipleArgs, started: Boolean) {
100+
private fun connectToSupervisor(args: ArgumentContainer, started: Boolean) {
99101
LOGGER.fine("Connecting to supervisor (reconnection?: $started)")
100102

101-
val port = singleArgs["port"] as Int
103+
val port = args.getRequired<Int>("port")
102104
val trySupervisor: Result<Supervisor> = runCatching {
103105
val registry = LocateRegistry.getRegistry(port)
104106
registry.lookup("CarpoolSupervisor") as Supervisor
105107
}
106108

107-
if (singleArgs["start"] as Boolean && !started) {
108-
if (singleArgs["stop"] as Boolean) {
109+
if (args.getRequired<Boolean>("start") && !started) {
110+
if (args.getRequired<Boolean>("stop")) {
109111
LOGGER.severe("Please only use EITHER -start or -stop.")
110112
exitProcess(1)
111113
}
112114

113115
trySupervisor.onSuccess {
114-
val supervisorPid = it.status().pid
116+
val supervisorPid = it.status()
115117
LOGGER.severe("You have asked to start the supervisor daemon, but it appears to already be running (PID $supervisorPid).")
116118
exitProcess(1)
117119
}
118120

119-
spawnSupervisor(LOGGER.level, port, singleArgs["log_directory"] as Path)
121+
spawnSupervisor(LOGGER.level, port, args.getRequired<Path>("log_directory"))
120122
LOGGER.info("Giving the supervisor time to wake up...")
121123
Thread.sleep(500)
122124
LOGGER.fine("Attempting to connect to newly-started supervisor")
123-
connectToSupervisor(singleArgs, multipleArgs, true)
125+
connectToSupervisor(args, true)
124126
return
125127
}
126128

127129
trySupervisor.onSuccess { supervisor ->
128-
handleCommands(singleArgs.filterKeys { SINGLE_COMMANDS.contains(it) }, multipleArgs, supervisor)
130+
handleCommands(args, supervisor)
129131
}.onFailure { e ->
130132
LOGGER.log(Level.SEVERE, e) { "The supervisor daemon does not appear to be running. Please start it with -start." }
131133
exitProcess(1)
132134
}
133135
}
134136

135-
private fun handleCommands(singleArgs: SingleArgs, multipleArgs: MultipleArgs, supervisor: Supervisor) {
136-
if (singleArgs["stop"] as Boolean)
137+
private fun handleCommands(args: ArgumentContainer, supervisor: Supervisor) {
138+
if (args.getRequired<Boolean>("stop"))
137139
try {
138140
supervisor.stop()
139141
} catch (_: UnmarshalException) { // expected, so ignore
@@ -142,49 +144,32 @@ private fun handleCommands(singleArgs: SingleArgs, multipleArgs: MultipleArgs, s
142144
exitProcess(0)
143145
}
144146

145-
for (arg in singleArgs)
146-
when (arg.key) {
147-
"status" -> if (singleArgs["status"] as Boolean) {
148-
val status = try {
149-
supervisor.status()
150-
} catch (e: Exception) {
151-
LOGGER.info("There was an exception getting the supervisor's status -- is it online?")
152-
LOGGER.log(Level.FINE, e) { "The produced exception was" }
153-
continue
154-
}
155-
156-
if (status.status)
157-
LOGGER.info("Supervisor online, PID ${status.pid}")
158-
else
159-
LOGGER.info("Supervisor OFFLINE -- please check your configuration")
147+
for (arg in args.of.keys)
148+
when (arg) {
149+
"status" -> if (args.getRequired<Boolean>("status")) {
150+
LOGGER.info { "Supervisor online, PID: ${supervisor.status()}" }
160151
}
161-
"list_applications" -> if (singleArgs["list_applications"] as Boolean) {
152+
"list_applications" -> if (args.getRequired<Boolean>("list_applications")) {
162153
val applications = supervisor.listApplications()
163154
LOGGER.info("Currently ${applications.size} application(s)")
164155
for (app in applications)
165156
LOGGER.info("${app.commandString}\n -PID: ${app.pid}\n -Alive?: ${app.isRunning}")
166157
}
167-
}
168-
169-
for (arg in multipleArgs)
170-
when (arg.key) {
171-
"add_application" -> for (app in arg.value) {
172-
val asString = app as String
173-
if (asString.isEmpty()) continue
158+
"add_application" -> for (app in args.getsRequired<String>("add_application")) {
159+
if (app.isEmpty()) continue
174160

175-
val commandString = asString.split(" ").toTypedArray()
161+
val commandString = app.split(" ").toTypedArray()
176162
val appPid = supervisor.addApplication(commandString)
177-
LOGGER.info("Started application [$asString] -- PID $appPid")
163+
LOGGER.info("Started application [$app] -- PID $appPid")
178164
}
179-
"remove_application" -> for (app in arg.value) {
180-
val asLong = app as Long
181-
if (asLong == -1L) continue
165+
"remove_application" -> for (appPid in args.getsRequired<Long>("remove_application")) {
166+
if (appPid == -1L) continue
182167

183168
try {
184-
supervisor.removeApplication(asLong)
185-
LOGGER.info("Removed application with PID $asLong")
169+
supervisor.removeApplication(appPid)
170+
LOGGER.info("Removed application with PID $appPid")
186171
} catch (anfe: ApplicationNotFoundException) {
187-
LOGGER.warning("There is no application with PID $asLong")
172+
LOGGER.warning("There is no application with PID $appPid")
188173
LOGGER.log(Level.FINE, anfe) { "Exception info:" }
189174
}
190175
}

src/main/kotlin/rmi/StatusResult.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/main/kotlin/rmi/Supervisor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.rmi.RemoteException
77

88
interface Supervisor : Remote {
99
@Throws(RemoteException::class)
10-
fun status(): StatusResult
10+
fun status(): Long
1111

1212
@Throws(RemoteException::class)
1313
fun stop()

src/main/kotlin/supervisor/CarpoolSupervisor.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.bread_experts_group.application_carpool.supervisor
22

3-
import org.bread_experts_group.application_carpool.rmi.StatusResult
43
import org.bread_experts_group.application_carpool.rmi.Supervisor
54
import rmi.ApplicationInfo
65
import rmi.ApplicationNotFoundException
@@ -11,7 +10,7 @@ import kotlin.system.exitProcess
1110
class CarpoolSupervisor(val pid: Long, val logger: Logger) : UnicastRemoteObject(0), Supervisor {
1211
val applications = mutableMapOf<Long, ApplicationEntry>()
1312

14-
override fun status() = StatusResult(true, pid)
13+
override fun status() = pid
1514

1615
override fun stop() {
1716
logger.info("Stop request received, shutting down applications")

src/main/kotlin/supervisor/CarpoolSupervisorMain.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package org.bread_experts_group.application_carpool.supervisor
22

3-
import org.bread_experts_group.logging.ColoredLogger
43
import java.io.PrintStream
54
import java.lang.Integer.parseInt
65
import java.nio.file.StandardOpenOption
76
import java.rmi.registry.LocateRegistry
87
import java.util.logging.Level
8+
import java.util.logging.Logger
99
import kotlin.io.path.Path
1010
import kotlin.io.path.createFile
1111
import kotlin.io.path.notExists
1212
import kotlin.io.path.outputStream
1313
import kotlin.system.exitProcess
1414

15-
private val LOGGER = ColoredLogger.newLogger("Application Carpool Supervisor")
15+
private val LOGGER = Logger.getLogger("Application Carpool Supervisor")
1616

1717
fun main(args: Array<String>) {
1818
val pid = ProcessHandle.current().pid()

0 commit comments

Comments
 (0)