Search in sources :

Example 16 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory in project yyl_example by Relucent.

the class NettyClient method main.

public static void main(String[] args) {
    ClientBootstrap bootstrap = new ClientBootstrap(new //
    NioClientSocketChannelFactory(//
    Executors.newCachedThreadPool(), //
    Executors.newCachedThreadPool()));
    // Set up the default event pipeline.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(new StringDecoder(), new StringEncoder(), new ClientHandler());
        }
    });
    // Start the connection attempt.
    ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 8000));
    // Wait until the connection is closed or the connection attempt fails.
    future.getChannel().getCloseFuture().awaitUninterruptibly();
    // Shut down thread pools to exit.
    bootstrap.releaseExternalResources();
}
Also used : StringEncoder(org.jboss.netty.handler.codec.string.StringEncoder) ChannelFuture(org.jboss.netty.channel.ChannelFuture) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) StringDecoder(org.jboss.netty.handler.codec.string.StringDecoder) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 17 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory in project cdap by caskdata.

the class NettyRouter method bootstrapServer.

private void bootstrapServer(final ChannelUpstreamHandler connectionTracker) throws ServiceBindException {
    ExecutorService serverBossExecutor = createExecutorService(serverBossThreadPoolSize, "router-server-boss-thread-%d");
    ExecutorService serverWorkerExecutor = createExecutorService(serverWorkerThreadPoolSize, "router-server-worker-thread-%d");
    serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(serverBossExecutor, serverWorkerExecutor));
    serverBootstrap.setOption("backlog", serverConnectionBacklog);
    serverBootstrap.setOption("child.bufferFactory", new DirectChannelBufferFactory());
    // Setup the pipeline factory
    serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (isSSLEnabled()) {
                // Add SSLHandler is SSL is enabled
                pipeline.addLast("ssl", sslHandlerFactory.create());
            }
            pipeline.addLast("tracker", connectionTracker);
            pipeline.addLast("http-response-encoder", new HttpResponseEncoder());
            pipeline.addLast("http-decoder", new HttpRequestDecoder());
            pipeline.addLast("http-status-request-handler", new HttpStatusRequestHandler());
            if (securityEnabled) {
                pipeline.addLast("access-token-authenticator", new SecurityAuthenticationHttpHandler(realm, tokenValidator, configuration, accessTokenTransformer, discoveryServiceClient));
            }
            // for now there's only one hardcoded rule, but if there will be more, we may want it generic and configurable
            pipeline.addLast("http-request-handler", new HttpRequestHandler(clientBootstrap, serviceLookup, ImmutableList.<ProxyRule>of()));
            return pipeline;
        }
    });
    // Start listening on ports.
    ImmutableMap.Builder<Integer, String> serviceMapBuilder = ImmutableMap.builder();
    for (Map.Entry<String, Integer> forward : serviceToPortMap.entrySet()) {
        int port = forward.getValue();
        String service = forward.getKey();
        String boundService = serviceLookup.getService(port);
        if (boundService != null) {
            LOG.warn("Port {} is already configured to service {}, ignoring forward for service {}", port, boundService, service);
            continue;
        }
        InetSocketAddress bindAddress = new InetSocketAddress(hostname, port);
        LOG.info("Starting Netty Router for service {} on address {}...", service, bindAddress);
        try {
            Channel channel = serverBootstrap.bind(bindAddress);
            InetSocketAddress boundAddress = (InetSocketAddress) channel.getLocalAddress();
            serviceMapBuilder.put(boundAddress.getPort(), service);
            channelGroup.add(channel);
            // Update service map
            serviceLookup.updateServiceMap(serviceMapBuilder.build());
            LOG.info("Started Netty Router for service {} on address {}.", service, boundAddress);
        } catch (ChannelException e) {
            if ((Throwables.getRootCause(e) instanceof BindException)) {
                throw new ServiceBindException("Router", hostname.getCanonicalHostName(), port, e);
            }
            throw e;
        }
    }
}
Also used : ServiceBindException(co.cask.cdap.common.ServiceBindException) HttpRequestHandler(co.cask.cdap.gateway.router.handlers.HttpRequestHandler) InetSocketAddress(java.net.InetSocketAddress) SecurityAuthenticationHttpHandler(co.cask.cdap.gateway.router.handlers.SecurityAuthenticationHttpHandler) HttpStatusRequestHandler(co.cask.cdap.gateway.router.handlers.HttpStatusRequestHandler) HttpRequestDecoder(org.jboss.netty.handler.codec.http.HttpRequestDecoder) ChannelException(org.jboss.netty.channel.ChannelException) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) Channel(org.jboss.netty.channel.Channel) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) DirectChannelBufferFactory(org.jboss.netty.buffer.DirectChannelBufferFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ImmutableMap(com.google.common.collect.ImmutableMap) HttpResponseEncoder(org.jboss.netty.handler.codec.http.HttpResponseEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 18 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory in project cdap by caskdata.

the class NettyRouter method bootstrapClient.

private void bootstrapClient(final ChannelUpstreamHandler connectionTracker) {
    ExecutorService clientBossExecutor = createExecutorService(clientBossThreadPoolSize, "router-client-boss-thread-%d");
    ExecutorService clientWorkerExecutor = createExecutorService(clientWorkerThreadPoolSize, "router-client-worker-thread-%d");
    clientBootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(new NioClientBossPool(clientBossExecutor, clientBossThreadPoolSize), new NioWorkerPool(clientWorkerExecutor, clientWorkerThreadPoolSize)));
    ChannelPipelineFactory pipelineFactory = new ClientChannelPipelineFactory(connectionTracker, connectionTimeout, timer);
    clientBootstrap.setPipelineFactory(pipelineFactory);
    clientBootstrap.setOption("bufferFactory", new DirectChannelBufferFactory());
}
Also used : NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) NioClientBossPool(org.jboss.netty.channel.socket.nio.NioClientBossPool) NioWorkerPool(org.jboss.netty.channel.socket.nio.NioWorkerPool) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) ExecutorService(java.util.concurrent.ExecutorService) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) DirectChannelBufferFactory(org.jboss.netty.buffer.DirectChannelBufferFactory)

Example 19 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory in project storm by apache.

the class ThriftNettyClientCodec method pipelineFactory.

public ChannelPipelineFactory pipelineFactory() {
    return new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("encoder", new ThriftEncoder());
            pipeline.addLast("decoder", new ThriftDecoder());
            if (authMethod == AuthMethod.KERBEROS) {
                try {
                    LOG.debug("Adding KerberosSaslClientHandler to pacemaker client pipeline.");
                    pipeline.addLast(KERBEROS_HANDLER, new KerberosSaslClientHandler(client, storm_conf, AuthUtils.LOGIN_CONTEXT_PACEMAKER_CLIENT, host));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (authMethod == AuthMethod.DIGEST) {
                try {
                    LOG.debug("Adding SaslStormClientHandler to pacemaker client pipeline.");
                    pipeline.addLast(SASL_HANDLER, new SaslStormClientHandler(client));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else {
                client.channelReady();
            }
            pipeline.addLast("PacemakerClientHandler", new PacemakerClientHandler(client));
            return pipeline;
        }
    };
}
Also used : PacemakerClientHandler(org.apache.storm.pacemaker.PacemakerClientHandler) SaslStormClientHandler(org.apache.storm.messaging.netty.SaslStormClientHandler) IOException(java.io.IOException) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) KerberosSaslClientHandler(org.apache.storm.messaging.netty.KerberosSaslClientHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 20 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory in project storm by apache.

the class ThriftNettyServerCodec method pipelineFactory.

public ChannelPipelineFactory pipelineFactory() {
    return new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("encoder", new ThriftEncoder());
            pipeline.addLast("decoder", new ThriftDecoder());
            if (authMethod == AuthMethod.DIGEST) {
                try {
                    LOG.debug("Adding SaslStormServerHandler to pacemaker server pipeline.");
                    pipeline.addLast(SASL_HANDLER, new SaslStormServerHandler((ISaslServer) server));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (authMethod == AuthMethod.KERBEROS) {
                try {
                    LOG.debug("Adding KerberosSaslServerHandler to pacemaker server pipeline.");
                    ArrayList<String> authorizedUsers = new ArrayList(1);
                    authorizedUsers.add((String) storm_conf.get(Config.NIMBUS_DAEMON_USER));
                    pipeline.addLast(KERBEROS_HANDLER, new KerberosSaslServerHandler((ISaslServer) server, storm_conf, AuthUtils.LOGIN_CONTEXT_PACEMAKER_SERVER, authorizedUsers));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            } else if (authMethod == AuthMethod.NONE) {
                LOG.debug("Not authenticating any clients. AuthMethod is NONE");
            }
            pipeline.addLast("handler", new StormServerHandler(server));
            return pipeline;
        }
    };
}
Also used : ISaslServer(org.apache.storm.messaging.netty.ISaslServer) KerberosSaslServerHandler(org.apache.storm.messaging.netty.KerberosSaslServerHandler) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SaslStormServerHandler(org.apache.storm.messaging.netty.SaslStormServerHandler) StormServerHandler(org.apache.storm.messaging.netty.StormServerHandler) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SaslStormServerHandler(org.apache.storm.messaging.netty.SaslStormServerHandler)

Aggregations

ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)31 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)29 InetSocketAddress (java.net.InetSocketAddress)12 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)11 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)10 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)7 LoggingHandler (org.jboss.netty.handler.logging.LoggingHandler)7 SimpleTestServerConnection (com.linkedin.databus2.test.container.SimpleTestServerConnection)6 HttpServerCodec (org.jboss.netty.handler.codec.http.HttpServerCodec)6 Log4JLoggerFactory (org.jboss.netty.logging.Log4JLoggerFactory)6 BeforeClass (org.testng.annotations.BeforeClass)6 InvalidConfigException (com.linkedin.databus.core.util.InvalidConfigException)5 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)5 IOException (java.io.IOException)5 ChannelFactory (org.jboss.netty.channel.ChannelFactory)5 DatabusHttpClientImpl (com.linkedin.databus.client.DatabusHttpClientImpl)4 DbusEventV2Factory (com.linkedin.databus.core.DbusEventV2Factory)4 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)4 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)4 DbusEventBuffer (com.linkedin.databus.core.DbusEventBuffer)3