1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 00:38:11 -07:00

Log stuff.

This commit is contained in:
Cedric Beust 2015-10-23 02:50:20 -07:00
parent cbf7ccffbb
commit db40768ea0

View file

@ -51,7 +51,7 @@ interface ICommandSender {
* it into a Kotlin *Data class. The downside of this approach is a double parsing, * it into a Kotlin *Data class. The downside of this approach is a double parsing,
* but since the data part is parsed as a string first, this is probably not a huge deal. * but since the data part is parsed as a string first, this is probably not a huge deal.
*/ */
class CommandData(val commandName: String, val data: String) class CommandData(val name: String, val data: String)
@Singleton @Singleton
public class KobaltServer @Inject constructor(val args: Args) : Runnable, ICommandSender { public class KobaltServer @Inject constructor(val args: Args) : Runnable, ICommandSender {
@ -66,14 +66,14 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
override fun run() { override fun run() {
val portNumber = args.port val portNumber = args.port
log1("Listening to port $portNumber") log(1, "Listening to port $portNumber")
var quit = false var quit = false
val serverSocket = ServerSocket(portNumber) val serverSocket = ServerSocket(portNumber)
while (! quit) { while (! quit) {
val clientSocket = serverSocket.accept() val clientSocket = serverSocket.accept()
outgoing = PrintWriter(clientSocket.outputStream, true) outgoing = PrintWriter(clientSocket.outputStream, true)
if (pending.size() > 0) { if (pending.size() > 0) {
log1("Emptying the queue, size $pending.size()") log(1, "Emptying the queue, size $pending.size()")
synchronized(pending) { synchronized(pending) {
pending.forEach { sendData(it) } pending.forEach { sendData(it) }
pending.clear() pending.clear()
@ -83,10 +83,10 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
try { try {
var line = ins.readLine() var line = ins.readLine()
while (!quit && line != null) { while (!quit && line != null) {
log1("Received from client $line") log(1, "Received from client $line")
val jo = JsonParser().parse(line) as JsonObject val jo = JsonParser().parse(line) as JsonObject
if ("quit" == jo.get("name").asString) { if ("quit" == jo.get("name").asString) {
log1("Quitting") log(1, "Quitting")
quit = true quit = true
} else { } else {
runCommand(jo) runCommand(jo)
@ -98,7 +98,7 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
} }
} }
} catch(ex: SocketException) { } catch(ex: SocketException) {
log1("Client disconnected, resetting") log(1, "Client disconnected, resetting")
} }
} }
} }
@ -117,20 +117,12 @@ public class KobaltServer @Inject constructor(val args: Args) : Runnable, IComma
if (outgoing != null) { if (outgoing != null) {
outgoing!!.println(content) outgoing!!.println(content)
} else { } else {
log1("Queuing $content") log(1, "Queuing $content")
synchronized(pending) { synchronized(pending) {
pending.add(commandData) pending.add(commandData)
} }
} }
} }
private fun log1(s: String) {
log(1, "[KobaltServer] $s")
}
private fun log2(s: String) {
log(2, "[KobaltServer] $s")
}
} }