To send an event, require Writes not Format

Format is invariant, so it can impose extra
restrictions on how we can pass the Format around
while Writes is more flexible.
Add "ImpliedState" object tracking state implied by events
Sync new clients to current execution state

This involves moving eventListeners to an eventSink object
because we need to synchronize adding listeners and sending
current state, to avoid sending events twice or missing events.

A small bugfix in here is that we did not send ExecutionWaiting
for synthetic execution requests previously. This bug was revealed
because the event state tracker throws an exception if events are
incorrect, so had to fix the bug to make the tests pass.
SbtEventSink: change to JsonSink[T], always use Writes instances

This patch has several changes which were somewhat tricky to separate.

 * SbtEventSink changed to have a type parameter for what can be
   sent; now an event sink is JsonSink[Event] and a message sink
   JsonSink[Message] and so on.
 * SbtClientHandler has to be a JsonSink[Any] because UIContext
   allows sending a JsValue which will not be a Message
 * EventLogger can use a JsonSink[LogEvent]
 * for now we only have one sink but pass it around as the most
   specific type we can in various places.
 * We were not using the provided Writes instances we were passing
   around; instead ipc.Peer was hardcoded to always use a global
   universal Message serializer.
   * Changed ipc.Peer to accept a JSON filter when constructed,
     which avoids importing sbt.protocol down into the IPC module
   * set a filter when constructing ipc.Client and ipc.Server
     which calls the provided writes and then inserts a "type"
     field, instead of ignoring provided writes entirely
 * We were also ignoring provided Writes instances in some cases
   where we matched on message type and then re-sent the message;
     send[T:Writes](msg: T) = msg match {
       case x: LogEvent => client.send(x)
     }
   This is wrong, we need to always keep the Writes with the
   message.
 * We pass separate jsonSink, eventSink, and logSink to
   ServerEngine, though for now they are all really the
   same object. They may always have to be to provide
   proper ordering guarantees.
 * UIContext updated to use Writes rather than Format for sending
 * some minor reformatting in here that Eclipse is dying to
   do that I'm tired of reverting every time...
Change Format to Writes in SimpleClient

We only need Writes to send things.
Add ExecutionEngineEvent trait extending protocol.Event

This allows us to check via type system that ServerEngine
only sends certain events.
Split custom events out of Event hierarchy

When UIContext sends an event, it will now be contained in a
TaskEvent rather than being an event that extends Event.

Fixes #167 by limiting what UIContext can send
Fixes #169 (mostly) by making DynamicSerialization only deal with build values

Implications:

 - DynamicSerialization does not need to know about events
 - CompilationFailure and TestEvent do not extend Event
 - our ipc.Peer uses do not allow anything but Message to
   go over them
 - taskId moves out of CompilationFailure, TestEvent into
   the enclosing TaskEvent
 - UIContext only allows sending TaskEvent, not anything else
 - no need for UIContext.taskId (all contexts have fixed taskId)
 - SbtClient only allows sending Message
 - no need for JsonSink[Any]
 - no sendGenericEvent or sendRawEvent

Some possible discussion points / future improvements:

 - it's quite awkward to deserialize TaskEvent because we
   can't use unapply() because a custom unapply() on
   either TaskEvent or anything we want to stick inside
   TaskEvent will confuse play's Json.format macro.
   So to get a nice unapply() we have to hand-implement
   serialization - yuck!
 - we could strip the play JSON dependency out of UIContext
   and TaskEvent's companion object pretty trivially,
   just moving that code out a layer. Should we?
rename SbtClient.dynamicSerialization to .buildValueSerialization

This clarifies the purpose of this registry.

Fixes #169 (the rest of the way)
Add unapply() to CompilationFailure and TestEvent

This requires handcoding the formatter since Json.format gets
confused by custom unapply.

unapply() takes an Event
Let scalariform reformat protocol/package.scala

Every time I touch this file it makes a mess, so let's just
get this out of the way.
Split up giant handleNextEvent method in SimpleClient

By breaking it into request, response, and event it's
much easier to follow and modify.
Make withSbt test util synchronously block until the client closes

This reduces potential side effects where one test affects another.
Fix execution event tracker sending two ExecutionWaiting for same execution

Also add an exception when two ExecutionWaiting are processed, which
would make the tests fail if we regressed this again.
Track execution state in SimpleClient and zero it on disconnect

The idea here is that a user of SbtClient can rely on all
execution/task starts getting a corresponding completion event,
rather than having to track disconnection themselves as a
special case.
Remove asInstanceOf usage sending an EventWithWrites
Put JsValue in TaskEvent rather than String; add TaskEventUnapply trait

TaskEventUnapply auto-implements unapply() for events that are intended
to go in a TaskEvent.

The JsValue lets us serialize TaskEvent more cleanly with a nested
JSON tree instead of JSON-inside-a-JSON-string.
Merge pull request #168 from sbt/wip/havocp-sync-execution-state

Sync execution queue AND active execution for new clients