Exemplo n.º 1
0
  @Override
  public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Enable stream compression (you can remove these two if unnecessary)
    pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
    pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));

    // Add the number codec first,
    pipeline.addLast("decoder", new BigIntegerDecoder());
    pipeline.addLast("encoder", new NumberEncoder());

    // and then business logic.
    // Please note we create a handler for every new channel
    // because it has stateful properties.
    pipeline.addLast("handler", new FactorialServerHandler());
  }
  @Override
  public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Enable stream compression (you can remove these two if unnecessary)
    if (compress) {
      pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
      pipeline.addLast("inflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    /**
     * length (4 bytes).
     *
     * <p>Note: max message size is 64 Mb = 67108864 bytes this defines a framer with a max of 64 Mb
     * message, 4 bytes are the length, and strip 4 bytes
     */
    pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4));

    // pipeline.addLast("frameDecoder", new
    // DebugFrameDecoder(67108864, 0, 4, 0, 4));

    // decoder must be first
    Object[] messageTypes = {
      Management.getDefaultInstance(),
      Image.Request.getDefaultInstance(),
      App.Request.getDefaultInstance()
    };
    pipeline.addLast("protobufDecoder", new CustomRaftDecoder(messageTypes));
    // pipeline.addLast("protobufDecoder", new ProtobufDecoder(Management.getDefaultInstance()));
    // pipeline.addLast("protobufDecoder2", new ProtobufDecoder(Request.getDefaultInstance()));
    pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
    pipeline.addLast("protobufEncoder", new ProtobufEncoder());

    // our server processor (new instance for each connection)
    pipeline.addLast("handler", handler);
  }