60 lines
1.9 KiB
Kotlin
60 lines
1.9 KiB
Kotlin
import ch.qos.logback.core.net.server.Client
|
|
import commands.ExecuteScriptCommand
|
|
import commands.ExitCommand
|
|
import commands.HelpCommand
|
|
import commands.SyncCommand
|
|
import commands.UndefinedCommand
|
|
import network.NetworkManager
|
|
import input.IOManager
|
|
import network.Request
|
|
import runner.CommandInvoker
|
|
|
|
fun main(args: Array<String>) {
|
|
val host = if (args.isNotEmpty()) args[0] else "localhost"
|
|
val port = if (args.size >= 2) args[1].toIntOrNull() ?: 8080 else 8080
|
|
|
|
val io = IOManager() // Name by Boris Bosenko
|
|
val client = NetworkManager(host, port)
|
|
val invoker = CommandInvoker(io)
|
|
|
|
// Shutdown hook
|
|
Runtime.getRuntime().addShutdownHook(Thread {
|
|
client.close()
|
|
})
|
|
|
|
io.print("""
|
|
|════════════════════════════════════════════
|
|
|Lab6 Client — подключение к $host:$port
|
|
|════════════════════════════════════════════
|
|
""".trimMargin())
|
|
|
|
registerBasicCommands(invoker, client, io)
|
|
runClientRepl(io, invoker)
|
|
}
|
|
|
|
fun registerBasicCommands(invoker: CommandInvoker,client: NetworkManager, io: IOManager) {
|
|
listOf(
|
|
HelpCommand(invoker),
|
|
ExecuteScriptCommand(io),
|
|
SyncCommand(invoker, client)
|
|
).forEach { invoker.register(it)}
|
|
}
|
|
|
|
fun runClientRepl(io: IOManager, invoker : CommandInvoker) {
|
|
// sync commands w server
|
|
invoker.execute("sync")
|
|
|
|
var isRunning = true
|
|
invoker.register(ExitCommand {isRunning = false})
|
|
|
|
while (isRunning) {
|
|
val line = io.readLine(" ☭ ") ?: break
|
|
if (line.isBlank()) continue
|
|
invoker.execute(line)
|
|
}
|
|
|
|
// and here we exit that crazy circus of code and tears
|
|
io.print("До свидания!")
|
|
|
|
}
|