Example usage for io.netty.handler.codec.protobuf ProtobufVarint32LengthFieldPrepender ProtobufVarint32LengthFieldPrepender

List of usage examples for io.netty.handler.codec.protobuf ProtobufVarint32LengthFieldPrepender ProtobufVarint32LengthFieldPrepender

Introduction

In this page you can find the example usage for io.netty.handler.codec.protobuf ProtobufVarint32LengthFieldPrepender ProtobufVarint32LengthFieldPrepender.

Prototype

ProtobufVarint32LengthFieldPrepender

Source Link

Usage

From source file:WorldClockClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();/*from   w  w w . ja v  a  2  s .  c o m*/
    p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
    p.addLast("protobufDecoder", new ProtobufDecoder(NetworkMessage.PackageInformation.getDefaultInstance()));

    p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
    p.addLast("protobufEncoder", new ProtobufEncoder());

    p.addLast("handler", new WorldClockClientHandler());
}

From source file:WorldClockServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();// w ww. j a v  a2 s .com
    p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
    p.addLast("protobufDecoder", new ProtobufDecoder(NetworkMessage.PackageInformation.getDefaultInstance()));

    p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
    p.addLast("protobufEncoder", new ProtobufEncoder());

    p.addLast("handler", new WorldClockServerHandler());
}

From source file:books.netty.codec.protobuf.SubReqClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*  w  w w .ja  v  a2 s.  c om*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        ch.pipeline().addLast(
                                new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance()));
                        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        ch.pipeline().addLast(new ProtobufEncoder());
                        ch.pipeline().addLast(new SubReqClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:books.netty.codec.protobuf.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from  w  ww .j  a v a2s  .c o  m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        ch.pipeline().addLast(
                                new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
                        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        ch.pipeline().addLast(new ProtobufEncoder());
                        ch.pipeline().addLast(new SubReqServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:bridgempp.bot.wrapper.BotWrapper.java

private static void botInitialize(File botConfig) {
    try {//from   w  ww  .ja  v  a  2  s  .c o m
        Properties botProperties = new Properties();
        if (!botConfig.exists()) {
            botConfig.createNewFile();
        }
        botProperties.load(new FileInputStream(botConfig));
        String botClass = botProperties.getProperty("botClass");
        if (botClass == null) {
            writeDefaultConfig(botProperties);
            throw new UnsupportedOperationException(
                    "Bot Class is null, cannot execute BridgeMPP server commands");
        }
        Bot bot = (Bot) Class.forName(botClass).newInstance();
        bot.initializeBot();
        String serverAddress = botProperties.getProperty("serverAddress");
        int portNumber = Integer.parseInt(botProperties.getProperty("serverPort"));
        if (serverAddress == null) {
            writeDefaultConfig(botProperties);
            throw new UnsupportedOperationException(
                    "Server Address is null, cannot execute BridgeMPP server commands");
        }
        ChannelFuture channelFuture = bootstrap.connect(new InetSocketAddress(serverAddress, portNumber));
        byte[] protocol = new byte[1];
        protocol[0] = 0x32;
        channelFuture.await();
        channelFuture.channel().writeAndFlush(Unpooled.wrappedBuffer(protocol));
        channelFuture.channel().pipeline().addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
        channelFuture.channel().pipeline().addLast("protobufDecoder",
                new ProtobufDecoder(ProtoBuf.Message.getDefaultInstance()));
        channelFuture.channel().pipeline().addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
        channelFuture.channel().pipeline().addLast("protobufEncoder", new ProtobufEncoder());
        channelFuture.channel().pipeline().addLast(new IncommingMessageHandler(bot));
        bot.channelFuture = channelFuture;
        bot.setProperties(botProperties);

        String serverKey = botProperties.getProperty("serverKey");
        if (serverKey == null) {
            writeDefaultConfig(botProperties);
            throw new UnsupportedOperationException(
                    "Server Key is null, cannot execute BridgeMPP server commands");
        }
        printCommand("!usekey " + serverKey, bot);
        String botAlias = botProperties.getProperty("botname");
        if (botAlias != null) {
            printCommand("!createalias " + botAlias, bot);
        }
        bot.name = botAlias;
        String[] groups = botProperties.getProperty("groups").split("; ");
        for (int i = 0; i < groups.length; i++) {
            printCommand("!subscribegroup " + groups[i], bot);
        }
        System.out.println("Joined " + groups.length + " groups");

    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
            | InterruptedException ex) {
        Logger.getLogger(BotWrapper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:c5db.replication.ReplicatorService.java

License:Apache License

/**
 * ********** Service startup/registration and shutdown/termination **************
 *//*from   ww  w. j  a  v a 2 s  . co m*/
@Override
protected void doStart() {
    // must start the fiber up early.
    fiber = fiberSupplier.getNewFiber(this::failModule);
    setupEventChannelSubscription();
    fiber.start();

    C5Futures.addCallback(getDependedOnModules(), (ignore) -> {
        ChannelInitializer<SocketChannel> initer = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("frameDecode", new ProtobufVarint32FrameDecoder());
                p.addLast("pbufDecode", new ProtostuffDecoder<>(ReplicationWireMessage.getSchema()));

                p.addLast("frameEncode", new ProtobufVarint32LengthFieldPrepender());
                p.addLast("pbufEncoder", new ProtostuffEncoder<ReplicationWireMessage>());

                p.addLast(new MessageHandler());
            }
        };

        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 100)
                .childOption(ChannelOption.TCP_NODELAY, true).childHandler(initer);

        //noinspection RedundantCast
        serverBootstrap.bind(port).addListener((ChannelFutureListener) future -> {
            if (future.isSuccess()) {
                LOG.info("successfully bound node {} port {} ", nodeId, port);
                listenChannel = future.channel();
            } else {
                LOG.error("Unable to bind! ", future.cause());
                failModule(future.cause());
            }
        });

        outgoingBootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.TCP_NODELAY, true)
                .handler(initer);

        //noinspection Convert2MethodRef
        outgoingRequests.subscribe(fiber, message -> handleOutgoingMessage(message),
                // Clean up cancelled requests.
                message -> handleCancelledSession(message.getSession()));

        notifyStarted();

    }, (Throwable t) -> {
        LOG.error("ReplicatorService unable to retrieve modules!", t);
        failModule(t);
    }, fiber);
}

From source file:club.jmint.crossing.client.ClientChannelInitializer.java

License:Apache License

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    // TODO Auto-generated method stub
    ChannelPipeline p = ch.pipeline();/*from  w  w  w .j ava  2s . c  o  m*/
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }

    p.addLast(new ProtobufVarint32FrameDecoder());
    p.addLast(new ProtobufDecoder(CrossingRespProto.CrossingResp.getDefaultInstance()));
    p.addLast(new ProtobufVarint32LengthFieldPrepender());
    p.addLast(new ProtobufEncoder());

    p.addLast(handler);
}

From source file:club.jmint.crossing.server.ServerChannelInitializer.java

License:Apache License

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    // TODO Auto-generated method stub

    ChannelPipeline p = ch.pipeline();// w w w.j ava2  s. c  o m
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }

    p.addLast(new ProtobufVarint32FrameDecoder());
    p.addLast(new ProtobufDecoder(CrossingReqProto.CrossingReq.getDefaultInstance()));
    p.addLast(new ProtobufVarint32LengthFieldPrepender());
    p.addLast(new ProtobufEncoder());

    p.addLast(new ServerHandler());
}

From source file:com.bdtools.doxecute.WorldClockClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();/*from  ww  w .j  a  va  2 s .c om*/
    p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
    p.addLast("protobufDecoder", new ProtobufDecoder(WorldClockProtocol.LocalTimes.getDefaultInstance()));

    p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
    p.addLast("protobufEncoder", new ProtobufEncoder());

    p.addLast("handler", new WorldClockClientHandler());
}

From source file:com.bdtools.doxecute.WorldClockServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();// w ww.ja  va2  s  .  c o m
    p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
    p.addLast("protobufDecoder", new ProtobufDecoder(WorldClockProtocol.Locations.getDefaultInstance()));

    p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
    p.addLast("protobufEncoder", new ProtobufEncoder());

    p.addLast("handler", new WorldClockServerHandler());
}