Search in sources :

Example 1 with ProtobufVarint32FrameDecoder

use of io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder in project intellij-community by JetBrains.

the class BuildManager method startListening.

private int startListening() throws Exception {
    EventLoopGroup group;
    BuiltInServer mainServer = StartupUtil.getServer();
    boolean isOwnEventLoopGroup = !Registry.is("compiler.shared.event.group", true) || mainServer == null || mainServer.getEventLoopGroup() instanceof OioEventLoopGroup;
    if (isOwnEventLoopGroup) {
        group = new NioEventLoopGroup(1, ConcurrencyUtil.newNamedThreadFactory("External compiler"));
    } else {
        group = mainServer.getEventLoopGroup();
    }
    final ServerBootstrap bootstrap = serverBootstrap(group);
    bootstrap.childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(@NotNull Channel channel) throws Exception {
            channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), myMessageDispatcher);
        }
    });
    Channel serverChannel = bootstrap.bind(InetAddress.getLoopbackAddress(), 0).syncUninterruptibly().channel();
    myChannelRegistrar.setServerChannel(serverChannel, isOwnEventLoopGroup);
    return ((InetSocketAddress) serverChannel.localAddress()).getPort();
}
Also used : ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) BuiltInServer(org.jetbrains.io.BuiltInServer) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) IOException(java.io.IOException) ExecutionException(com.intellij.execution.ExecutionException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) ChannelInitializer(io.netty.channel.ChannelInitializer) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with ProtobufVarint32FrameDecoder

use of io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder in project intellij-community by JetBrains.

the class BuildMain method main.

public static void main(String[] args) throws Throwable {
    try {
        final long processStart = System.currentTimeMillis();
        final String startMessage = "Build process started. Classpath: " + System.getProperty("java.class.path");
        System.out.println(startMessage);
        LOG.info(startMessage);
        final String host = args[HOST_ARG];
        final int port = Integer.parseInt(args[PORT_ARG]);
        final UUID sessionId = UUID.fromString(args[SESSION_ID_ARG]);
        @SuppressWarnings("ConstantConditions") final File systemDir = new File(FileUtil.toCanonicalPath(args[SYSTEM_DIR_ARG]));
        Utils.setSystemRoot(systemDir);
        final long connectStart = System.currentTimeMillis();
        // IDEA-123132, let's try again
        for (int attempt = 0; attempt < 3; attempt++) {
            try {
                ourEventLoopGroup = new NioEventLoopGroup(1, SharedThreadPool.getInstance());
                break;
            } catch (IllegalStateException e) {
                if (attempt == 2) {
                    printErrorAndExit(host, port, e);
                    return;
                } else {
                    LOG.warn("Cannot create event loop, attempt #" + attempt, e);
                    try {
                        //noinspection BusyWait
                        Thread.sleep(10 * (attempt + 1));
                    } catch (InterruptedException ignored) {
                    }
                }
            }
        }
        final Bootstrap bootstrap = new Bootstrap().group(ourEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer() {

            @Override
            protected void initChannel(Channel channel) throws Exception {
                channel.pipeline().addLast(new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(CmdlineRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), new MyMessageHandler(sessionId));
            }
        }).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
        final ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly();
        final boolean success = future.isSuccess();
        if (success) {
            LOG.info("Connection to IDE established in " + (System.currentTimeMillis() - connectStart) + " ms");
            final String projectPathToPreload = System.getProperty(PRELOAD_PROJECT_PATH, null);
            final String globalsPathToPreload = System.getProperty(PRELOAD_CONFIG_PATH, null);
            if (projectPathToPreload != null && globalsPathToPreload != null) {
                final PreloadedData data = new PreloadedData();
                ourPreloadedData = data;
                try {
                    // this will pre-load all FS optimizations
                    FileSystemUtil.getAttributes(projectPathToPreload);
                    final BuildRunner runner = new BuildRunner(new JpsModelLoaderImpl(projectPathToPreload, globalsPathToPreload, null));
                    data.setRunner(runner);
                    final File dataStorageRoot = Utils.getDataStorageRoot(projectPathToPreload);
                    final BuildFSState fsState = new BuildFSState(false);
                    final ProjectDescriptor pd = runner.load(new MessageHandler() {

                        @Override
                        public void processMessage(BuildMessage msg) {
                            data.addMessage(msg);
                        }
                    }, dataStorageRoot, fsState);
                    data.setProjectDescriptor(pd);
                    try {
                        final File fsStateFile = new File(dataStorageRoot, BuildSession.FS_STATE_FILE);
                        final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(fsStateFile)));
                        try {
                            final int version = in.readInt();
                            if (version == BuildFSState.VERSION) {
                                final long savedOrdinal = in.readLong();
                                // must skip "has-work-to-do" flag
                                final boolean hasWorkToDo = in.readBoolean();
                                fsState.load(in, pd.getModel(), pd.getBuildRootIndex());
                                data.setFsEventOrdinal(savedOrdinal);
                                data.setHasHasWorkToDo(hasWorkToDo);
                            }
                        } finally {
                            in.close();
                        }
                    } catch (FileNotFoundException ignored) {
                    } catch (IOException e) {
                        LOG.info("Error pre-loading FS state", e);
                        fsState.clearAll();
                    }
                    // preloading target configurations
                    final BuildTargetsState targetsState = pd.getTargetsState();
                    for (BuildTarget<?> target : pd.getBuildTargetIndex().getAllTargets()) {
                        targetsState.getTargetConfiguration(target);
                    }
                    BuilderRegistry.getInstance();
                    LOG.info("Pre-loaded process ready in " + (System.currentTimeMillis() - processStart) + " ms");
                } catch (Throwable e) {
                    LOG.info("Failed to pre-load project " + projectPathToPreload, e);
                // just failed to preload the project, the situation will be handled later, when real build starts
                }
            } else if (projectPathToPreload != null || globalsPathToPreload != null) {
                LOG.info("Skipping project pre-loading step: both paths to project configuration files and path to global settings must be specified");
            }
            future.channel().writeAndFlush(CmdlineProtoUtil.toMessage(sessionId, CmdlineProtoUtil.createParamRequest()));
        } else {
            printErrorAndExit(host, port, future.cause());
        }
    } catch (Throwable e) {
        LOG.error(e);
        throw e;
    }
}
Also used : MessageHandler(org.jetbrains.jps.incremental.MessageHandler) InetSocketAddress(java.net.InetSocketAddress) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) BuildTargetsState(org.jetbrains.jps.incremental.storage.BuildTargetsState) BuildFSState(org.jetbrains.jps.incremental.fs.BuildFSState) Bootstrap(io.netty.bootstrap.Bootstrap) UUID(java.util.UUID) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BuildMessage(org.jetbrains.jps.incremental.messages.BuildMessage)

Example 3 with ProtobufVarint32FrameDecoder

use of io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder in project intellij-community by JetBrains.

the class ExternalJavacManager method start.

public void start(int listenPort) {
    final ServerBootstrap bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1, SharedThreadPool.getInstance())).channel(NioServerSocketChannel.class);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
    final ChannelHandler compilationRequestsHandler = new CompilationRequestsHandler();
    bootstrap.childHandler(new ChannelInitializer() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline().addLast(myChannelRegistrar, new ProtobufVarint32FrameDecoder(), new ProtobufDecoder(JavacRemoteProto.Message.getDefaultInstance()), new ProtobufVarint32LengthFieldPrepender(), new ProtobufEncoder(), compilationRequestsHandler);
        }
    });
    try {
        final InetAddress loopback = InetAddress.getByName(null);
        myChannelRegistrar.add(bootstrap.bind(loopback, listenPort).syncUninterruptibly().channel());
        myListenPort = listenPort;
    } catch (UnknownHostException e) {
        throw new RuntimeException(e);
    }
}
Also used : ProtobufEncoder(io.netty.handler.codec.protobuf.ProtobufEncoder) UnknownHostException(java.net.UnknownHostException) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ProtobufDecoder(io.netty.handler.codec.protobuf.ProtobufDecoder) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) UnknownHostException(java.net.UnknownHostException) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) InetAddress(java.net.InetAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 4 with ProtobufVarint32FrameDecoder

use of io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder in project java-tron by tronprotocol.

the class UDPListener method start.

public void start() throws Exception {
    NioEventLoopGroup group = new NioEventLoopGroup(1);
    try {
        discoveryExecutor = new DiscoveryExecutor(nodeManager);
        discoveryExecutor.start();
        while (!shutdown) {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<NioDatagramChannel>() {

                @Override
                public void initChannel(NioDatagramChannel ch) throws Exception {
                    ch.pipeline().addLast(stats.udp);
                    ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                    ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                    ch.pipeline().addLast(new PacketDecoder());
                    MessageHandler messageHandler = new MessageHandler(ch, nodeManager);
                    nodeManager.setMessageSender(messageHandler);
                    ch.pipeline().addLast(messageHandler);
                }
            });
            channel = b.bind(port).sync().channel();
            logger.info("Discovery UDPListener started, bind port {}", port);
            channel.closeFuture().sync();
            if (shutdown) {
                logger.info("Shutdown discovery UDPListener");
                break;
            }
            logger.warn("UDP channel closed. Recreating after 5 sec pause...");
            Thread.sleep(5000);
        }
    } catch (Exception e) {
        if (e instanceof BindException && e.getMessage().contains("Address already in use")) {
            logger.error("Port " + port + " is busy. Check if another instance is running with the same port.");
        } else {
            logger.error("Can't start discover: ", e);
        }
    } finally {
        group.shutdownGracefully().sync();
    }
}
Also used : NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) Bootstrap(io.netty.bootstrap.Bootstrap) BindException(java.net.BindException) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) BindException(java.net.BindException)

Example 5 with ProtobufVarint32FrameDecoder

use of io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder in project java-tron by tronprotocol.

the class Channel method init.

public void init(ChannelPipeline pipeline, String remoteId, boolean discoveryMode, ChannelManager channelManager, PeerConnectionDelegate peerDel) {
    this.channelManager = channelManager;
    this.remoteId = remoteId;
    isActive = remoteId != null && !remoteId.isEmpty();
    // TODO: use config here
    pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(60, TimeUnit.SECONDS));
    pipeline.addLast(stats.tcp);
    pipeline.addLast("protoPender", new ProtobufVarint32LengthFieldPrepender());
    pipeline.addLast("lengthDecode", new ProtobufVarint32FrameDecoder());
    // handshake first
    pipeline.addLast("handshakeHandler", handshakeHandler);
    this.discoveryMode = discoveryMode;
    this.peerDel = peerDel;
    messageCodec.setChannel(this);
    msgQueue.setChannel(this);
    handshakeHandler.setChannel(this, remoteId);
    p2pHandler.setChannel(this);
    tronHandler.setChannel(this);
    p2pHandler.setMsgQueue(msgQueue);
    tronHandler.setMsgQueue(msgQueue);
    tronHandler.setPeerDel(peerDel);
    logger.info("Channel init finished");
}
Also used : ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder)

Aggregations

ProtobufVarint32FrameDecoder (io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder)7 ProtobufVarint32LengthFieldPrepender (io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender)7 ProtobufDecoder (io.netty.handler.codec.protobuf.ProtobufDecoder)5 ProtobufEncoder (io.netty.handler.codec.protobuf.ProtobufEncoder)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 InetSocketAddress (java.net.InetSocketAddress)2 ExecutionException (com.intellij.execution.ExecutionException)1 Channel (io.netty.channel.Channel)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 OioEventLoopGroup (io.netty.channel.oio.OioEventLoopGroup)1 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)1 IOException (java.io.IOException)1 BindException (java.net.BindException)1