11package org.bread_experts_group.application_carpool.client
22
33import 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
1211import rmi.ApplicationNotFoundException
1312import java.lang.management.ManagementFactory
1413import java.nio.file.Path
1514import java.rmi.UnmarshalException
1615import java.rmi.registry.LocateRegistry
1716import java.util.logging.Level
17+ import java.util.logging.Logger
1818import kotlin.io.path.Path
1919import kotlin.io.path.absolutePathString
2020import 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
8382fun 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 }
0 commit comments