Split using newline. Also more error handling.

This commit is contained in:
Eugene Yokota 2016-03-01 20:58:48 +01:00
parent 9557107c97
commit ec0fe7bb21
3 changed files with 15 additions and 10 deletions

View File

@ -10,7 +10,7 @@ abstract class ClientConnection(connection: Socket) {
// TODO handle client disconnect
private val running = new AtomicBoolean(true)
private val delimiter = '\0'.toByte
private val delimiter: Byte = '\n'.toByte
private val out = connection.getOutputStream

View File

@ -7,6 +7,7 @@ import org.json4s.JsonAST.{ JArray, JString }
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.native.JsonMethods._
import org.json4s.ParserUtil.ParseException
object Serialization {
@ -40,15 +41,18 @@ object Serialization {
/**
* @return A command or an invalid input description
*/
def deserialize(bytes: Seq[Byte]): Either[String, Command] = {
val json = parse(new String(bytes.toArray, "UTF-8"))
def deserialize(bytes: Seq[Byte]): Either[String, Command] =
try {
val json = parse(new String(bytes.toArray, "UTF-8"))
implicit val formats = DefaultFormats
implicit val formats = DefaultFormats
(json \ "type").extract[String] match {
case "command" => Right(Execution((json \ "command_line").extract[String]))
case cmd => Left(s"Unknown command type $cmd")
// TODO: is using extract safe?
(json \ "type").extract[String] match {
case "execution" => Right(Execution((json \ "command_line").extract[String]))
case cmd => Left(s"Unknown command type $cmd")
}
} catch {
case e: ParseException => Left(s"Parse error: ${e.getMessage}")
case e: MappingException => Left(s"Missing type field")
}
}
}

View File

@ -36,6 +36,7 @@ object Server {
val connection = new ClientConnection(socket) {
override def onCommand(command: Command): Unit = {
println(s"onCommand $command")
commandQueue.add(command)
}
}