Start lightweight client

This is the beginning of a lightweight client, which talks to the
server over Contraband-generated JSON API. Given that the server is
started on port 5173:

```
$ cd /tmp/bogus
$ sbt client localhost:5173
> compile
StatusEvent(Processing, Vector(compile, server))
StatusEvent(Ready, Vector())
StatusEvent(Processing, Vector(, server))
StatusEvent(Ready, Vector())
```
This commit is contained in:
Eugene Yokota
2017-01-06 11:27:06 -05:00
parent 272e733b87
commit fa7253ece3
8 changed files with 224 additions and 17 deletions
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2016 Lightbend Inc. <http://www.typesafe.com>
*/
package sbt
package protocol
import sjsonnew.support.scalajson.unsafe.{ Converter, CompactPrinter }
import scala.json.ast.unsafe.JValue
import sjsonnew.support.scalajson.unsafe.Parser
import java.nio.ByteBuffer
import scala.util.{ Success, Failure }
object Serialization {
def serializeCommand(command: CommandMessage): Array[Byte] =
{
import codec.JsonProtocol._
val json: JValue = Converter.toJson[CommandMessage](command).get
CompactPrinter(json).getBytes("UTF-8")
}
def serializeEvent(event: EventMessage): Array[Byte] =
{
import codec.JsonProtocol._
val json: JValue = Converter.toJson[EventMessage](event).get
CompactPrinter(json).getBytes("UTF-8")
}
/**
* @return A command or an invalid input description
*/
def deserializeCommand(bytes: Seq[Byte]): Either[String, CommandMessage] =
{
val buffer = ByteBuffer.wrap(bytes.toArray)
Parser.parseFromByteBuffer(buffer) match {
case Success(json) =>
import codec.JsonProtocol._
Converter.fromJson[CommandMessage](json) match {
case Success(command) => Right(command)
case Failure(e) => Left(e.getMessage)
}
case Failure(e) =>
Left(s"Parse error: ${e.getMessage}")
}
}
/**
* @return A command or an invalid input description
*/
def deserializeEvent(bytes: Seq[Byte]): Either[String, EventMessage] =
{
val buffer = ByteBuffer.wrap(bytes.toArray)
Parser.parseFromByteBuffer(buffer) match {
case Success(json) =>
import codec.JsonProtocol._
Converter.fromJson[EventMessage](json) match {
case Success(event) => Right(event)
case Failure(e) => Left(e.getMessage)
}
case Failure(e) =>
Left(s"Parse error: ${e.getMessage}")
}
}
}