Search in sources :

Example 16 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project sockjs-netty by cgbystrom.

the class SockJsClient method newClient.

public static SockJsClient newClient(URI url, Protocol protocol, final SessionCallback callback) {
    ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
    switch(protocol) {
        case WEBSOCKET:
            final WebSocketClient clientHandler = new WebSocketClient(bootstrap, url, callback);
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

                public ChannelPipeline getPipeline() throws Exception {
                    ChannelPipeline pipeline = Channels.pipeline();
                    pipeline.addLast("decoder", new HttpResponseDecoder());
                    pipeline.addLast("encoder", new HttpRequestEncoder());
                    pipeline.addLast("ws-handler", clientHandler);
                    return pipeline;
                }
            });
            return clientHandler;
    }
    throw new IllegalArgumentException("Invalid protocol specified");
}
Also used : ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) HttpRequestEncoder(org.jboss.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(org.jboss.netty.handler.codec.http.HttpResponseDecoder) URISyntaxException(java.net.URISyntaxException)

Example 17 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project sockjs-netty by cgbystrom.

the class SockJsClient method newLocalClient.

public static SockJsClient newLocalClient(URI url, Protocol protocol, final SessionCallback callback) {
    ClientBootstrap bootstrap = new ClientBootstrap(localSocketChannelFactory);
    switch(protocol) {
        case WEBSOCKET:
            final WebSocketClient clientHandler = new WebSocketClient(bootstrap, url, callback);
            bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

                public ChannelPipeline getPipeline() throws Exception {
                    ChannelPipeline pipeline = Channels.pipeline();
                    pipeline.addLast("decoder", new HttpResponseDecoder());
                    pipeline.addLast("encoder", new HttpRequestEncoder());
                    pipeline.addLast("ws-handler", clientHandler);
                    return pipeline;
                }
            });
            return clientHandler;
    }
    throw new IllegalArgumentException("Invalid protocol specified");
}
Also used : ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) HttpRequestEncoder(org.jboss.netty.handler.codec.http.HttpRequestEncoder) HttpResponseDecoder(org.jboss.netty.handler.codec.http.HttpResponseDecoder) URISyntaxException(java.net.URISyntaxException)

Example 18 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project databus by linkedin.

the class TestDatabusRelayMain method testClient.

private void testClient(int relayPort, int fetchSize, long scn, HttpResponseHandler handler) throws Exception {
    Checkpoint ckpt = Checkpoint.createOnlineConsumptionCheckpoint(scn);
    //TODO why is this needed
    //ckpt.setCatchupSource("foo");
    String uristr = "/stream?sources=105&output=json&size=" + fetchSize + "&streamFromLatestScn=false&checkPoint=" + ckpt.toString();
    ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    bootstrap.setPipelineFactory(new HttpClientPipelineFactory(handler));
    ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", relayPort));
    Channel channel = future.awaitUninterruptibly().getChannel();
    Assert.assertTrue(future.isSuccess(), "Cannot connect to relay at localhost:" + relayPort);
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uristr);
    request.setHeader(HttpHeaders.Names.HOST, "localhost");
    channel.write(request);
    channel.getCloseFuture().awaitUninterruptibly();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Checkpoint(com.linkedin.databus.core.Checkpoint) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) Channel(org.jboss.netty.channel.Channel)

Example 19 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project adbcj by mheath.

the class Encoder method initBootstrap.

private ClientBootstrap initBootstrap(ChannelFactory channelFactory, String host, int port) {
    ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
    bootstrap.setPipelineFactory(Channels.pipelineFactory(Channels.pipeline()));
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("remoteAddress", new InetSocketAddress(host, port));
    return bootstrap;
}
Also used : ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress)

Example 20 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project neo4j by neo4j.

the class Client method start.

@Override
public void start() {
    bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(newCachedThreadPool(daemon(getClass().getSimpleName() + "-boss@" + destination)), newCachedThreadPool(daemon(getClass().getSimpleName() + "-worker@" + destination))));
    bootstrap.setPipelineFactory(this);
    channelPool = new ResourcePool<ChannelContext>(maxUnusedChannels, new ResourcePool.CheckStrategy.TimeoutCheckStrategy(DEFAULT_CHECK_INTERVAL, Clocks.systemClock()), new LoggingResourcePoolMonitor(msgLog)) {

        @Override
        protected ChannelContext create() {
            msgLog.info(threadInfo() + "Trying to open a new channel from " + origin + " to " + destination, true);
            // We must specify the origin address in case the server has multiple IPs per interface
            ChannelFuture channelFuture = bootstrap.connect(destination, origin);
            channelFuture.awaitUninterruptibly(5, TimeUnit.SECONDS);
            if (channelFuture.isSuccess()) {
                msgLog.info(threadInfo() + "Opened a new channel from " + channelFuture.getChannel().getLocalAddress() + " to " + channelFuture.getChannel().getRemoteAddress());
                return new ChannelContext(channelFuture.getChannel(), ChannelBuffers.dynamicBuffer(), ByteBuffer.allocate(1024 * 1024));
            }
            Throwable cause = channelFuture.getCause();
            String msg = Client.this.getClass().getSimpleName() + " could not connect from " + origin + " to " + destination;
            msgLog.debug(msg, true);
            throw traceComException(new ComException(msg, cause), "Client.start");
        }

        @Override
        protected boolean isAlive(ChannelContext context) {
            return context.channel().isConnected();
        }

        @Override
        protected void dispose(ChannelContext context) {
            Channel channel = context.channel();
            if (channel.isConnected()) {
                msgLog.info(threadInfo() + "Closing: " + context + ". " + "Channel pool size is now " + currentSize());
                channel.close();
            }
        }

        private String threadInfo() {
            return "Thread[" + Thread.currentThread().getId() + ", " + Thread.currentThread().getName() + "] ";
        }
    };
    /*
         * This is here to couple the channel releasing to Response.close() itself and not
         * to TransactionStream.close() as it is implemented here. The reason is that a Response
         * that is returned without a TransactionStream will still hold the channel and should
         * release it eventually. Also, logically, closing the channel is not dependent on the
         * TransactionStream.
         */
    resourcePoolReleaser = () -> {
        if (channelPool != null) {
            channelPool.release();
        }
    };
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) Channel(org.jboss.netty.channel.Channel) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap)

Aggregations

ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)24 InetSocketAddress (java.net.InetSocketAddress)10 ChannelFuture (org.jboss.netty.channel.ChannelFuture)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)10 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)7 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)7 Channel (org.jboss.netty.channel.Channel)4 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 NioClientBossPool (org.jboss.netty.channel.socket.nio.NioClientBossPool)2 NioWorkerPool (org.jboss.netty.channel.socket.nio.NioWorkerPool)2 HttpClientCodec (org.jboss.netty.handler.codec.http.HttpClientCodec)2 HttpRequestEncoder (org.jboss.netty.handler.codec.http.HttpRequestEncoder)2 HttpResponseDecoder (org.jboss.netty.handler.codec.http.HttpResponseDecoder)2 LoggingHandler (org.jboss.netty.handler.logging.LoggingHandler)2 HashedWheelTimer (org.jboss.netty.util.HashedWheelTimer)2 Checkpoint (com.linkedin.databus.core.Checkpoint)1 FooterAwareHttpChunkAggregator (com.linkedin.databus.core.test.netty.FooterAwareHttpChunkAggregator)1 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)1