Search in sources :

Example 16 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project alluxio by Alluxio.

the class RPCMessageIntegrationTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    sEventClient = new NioEventLoopGroup(1);
    sEventServer = new NioEventLoopGroup(1);
    sIncomingHandler = new MessageSavingHandler();
    // Setup the server.
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(sEventServer);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.childHandler(new PipelineInitializer(sIncomingHandler));
    InetSocketAddress address = new InetSocketAddress(NetworkAddressUtils.getLocalHostName(100), Integer.parseInt(PropertyKey.MASTER_RPC_PORT.getDefaultValue()));
    ChannelFuture cf = bootstrap.bind(address).syncUninterruptibly();
    sLocalAddress = cf.channel().localAddress();
    // Setup the client.
    sBootstrapClient = new Bootstrap();
    sBootstrapClient.group(sEventClient);
    sBootstrapClient.channel(NioSocketChannel.class);
    sBootstrapClient.handler(new PipelineInitializer(new MessageSavingHandler()));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BeforeClass(org.junit.BeforeClass)

Example 17 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project intellij-community by JetBrains.

the class ExternalJavacProcess method connect.

private boolean connect(final String host, final int port) throws Throwable {
    final Bootstrap bootstrap = new Bootstrap().group(myEventLoopGroup).channel(NioSocketChannel.class).handler(myChannelInitializer);
    bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
    final ChannelFuture future = bootstrap.connect(host, port).syncUninterruptibly();
    if (future.isSuccess()) {
        myConnectFuture = future;
        return true;
    }
    return false;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 18 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project intellij-community by JetBrains.

the class SimpleProtobufClient method connect.

public final boolean connect(final String host, final int port) throws Throwable {
    if (myState.compareAndSet(State.DISCONNECTED, State.CONNECTING)) {
        boolean success = false;
        try {
            final Bootstrap bootstrap = new Bootstrap().group(myEventLoopGroup).channel(NioSocketChannel.class).handler(myChannelInitializer);
            bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);
            final ChannelFuture future = bootstrap.connect(host, port).syncUninterruptibly();
            success = future.isSuccess();
            if (success) {
                myConnectFuture = future;
                try {
                    onConnect();
                } catch (Throwable e) {
                    LOG.error(e);
                }
            }
            return success;
        } finally {
            myState.compareAndSet(State.CONNECTING, success ? State.CONNECTED : State.DISCONNECTED);
        }
    }
    // already connected
    return true;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 19 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap 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 20 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project carbondata by apache.

the class DictionaryClient method startClient.

/**
   * start dictionary client
   *
   * @param address
   * @param port
   */
public void startClient(String address, int port) {
    LOGGER.audit("Starting client on " + address + " " + port);
    long start = System.currentTimeMillis();
    // Create an Event with 1 thread.
    workerGroup = new NioEventLoopGroup(1);
    Bootstrap clientBootstrap = new Bootstrap();
    clientBootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            // Based on length provided at header, it collects all packets
            pipeline.addLast("LengthDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 2, 0, 2));
            pipeline.addLast("DictionaryClientHandler", dictionaryClientHandler);
        }
    });
    clientBootstrap.connect(new InetSocketAddress(address, port));
    LOGGER.info("Dictionary client Started, Total time spent : " + (System.currentTimeMillis() - start));
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)435 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)209 Channel (io.netty.channel.Channel)197 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)191 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)171 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)141 SocketChannel (io.netty.channel.socket.SocketChannel)125 InetSocketAddress (java.net.InetSocketAddress)121 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)117 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)109 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)57 ChannelInitializer (io.netty.channel.ChannelInitializer)46