@Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    logger.info("Handling message: " + msg);

    Message message = (Message) msg;
    long session = message.getSession();

    switch (message.getType()) {
      case CHUNK:
        {
          ChunkMessage chunk = (ChunkMessage) msg;
          sessions.onChunk(session, chunk.getData());
          break;
        }
      case CHOKE:
        {
          sessions.onCompleted(session);
          break;
        }
      case ERROR:
        {
          ErrorMessage error = (ErrorMessage) msg;
          sessions.onError(
              session, new ServiceErrorException(service, error.getMessage(), error.getCode()));
          break;
        }
      default:
        {
          sessions.onError(session, new UnexpectedServiceMessageException(service, message));
          break;
        }
    }
  }
 @Override
 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
   sessions.onCompleted();
 }