Search in sources :

Example 61 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project camel by apache.

the class NettyUDPByteArrayProviderTest method createNettyUdpReceiver.

public void createNettyUdpReceiver() {
    group = new NioEventLoopGroup();
    bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline().addLast(new UdpHandler());
            channel.pipeline().addLast(new ByteArrayDecoder());
            channel.pipeline().addLast(new ContentHandler());
        }
    }).localAddress(new InetSocketAddress(getPort()));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ByteArrayDecoder(io.netty.handler.codec.bytes.ByteArrayDecoder)

Example 62 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project camel by apache.

the class NettyUdpConnectedSendTest method createNettyUdpReceiver.

public void createNettyUdpReceiver() {
    group = new NioEventLoopGroup();
    bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline().addLast(new UdpHandler());
            channel.pipeline().addLast(new ContentHandler());
        }
    }).localAddress(new InetSocketAddress(getPort()));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 63 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project flink by apache.

the class HttpTestClient method sendRequest.

/**
	 * Sends a request to to the server.
	 *
	 * <pre>
	 * HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/overview");
	 * request.headers().set(HttpHeaders.Names.HOST, host);
	 * request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
	 *
	 * sendRequest(request);
	 * </pre>
	 *
	 * @param request The {@link HttpRequest} to send to the server
	 */
public void sendRequest(HttpRequest request, FiniteDuration timeout) throws InterruptedException, TimeoutException {
    LOG.debug("Writing {}.", request);
    // Make the connection attempt.
    ChannelFuture connect = bootstrap.connect(host, port);
    Channel channel;
    if (connect.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
        channel = connect.channel();
    } else {
        throw new TimeoutException("Connection failed");
    }
    channel.writeAndFlush(request);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) TimeoutException(java.util.concurrent.TimeoutException)

Example 64 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project flink by apache.

the class NettyServerLowAndHighWatermarkTest method testLowAndHighWatermarks.

/**
	 * Verifies that the high and low watermark are set in relation to the page size.
	 *
	 * <p> The high and low water marks control the data flow to the wire. If the Netty write buffer
	 * has size greater or equal to the high water mark, the channel state becomes not-writable.
	 * Only when the size falls below the low water mark again, the state changes to writable again.
	 *
	 * <p> The Channel writability state needs to be checked by the handler when writing to the
	 * channel and is not enforced in the sense that you cannot write a channel, which is in
	 * not-writable state.
	 */
@Test
public void testLowAndHighWatermarks() throws Throwable {
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final NettyProtocol protocol = new NettyProtocol() {

        @Override
        public ChannelHandler[] getServerChannelHandlers() {
            // The channel handler implements the test
            return new ChannelHandler[] { new TestLowAndHighWatermarkHandler(error) };
        }

        @Override
        public ChannelHandler[] getClientChannelHandlers() {
            return new ChannelHandler[0];
        }
    };
    final NettyConfig conf = createConfig(PageSize);
    final NettyServerAndClient serverAndClient = initServerAndClient(protocol, conf);
    try {
        // We can't just check the config of this channel as it is the client's channel. We need
        // to check the server channel, because it is doing the data transfers.
        final Channel ch = connect(serverAndClient);
        // Wait for the channel to be closed
        awaitClose(ch);
        final Throwable t = error.get();
        if (t != null) {
            throw t;
        }
    } finally {
        shutdown(serverAndClient);
    }
}
Also used : Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandler(io.netty.channel.ChannelHandler) NettyServerAndClient(org.apache.flink.runtime.io.network.netty.NettyTestUtil.NettyServerAndClient) Test(org.junit.Test)

Example 65 with Channel

use of org.apache.flink.shaded.netty4.io.netty.channel.Channel in project ratpack by ratpack.

the class WebSocketEngine method connect.

@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
    PublicAddress publicAddress = context.get(PublicAddress.class);
    URI address = publicAddress.get(context);
    URI httpPath = address.resolve(path);
    URI wsPath;
    try {
        wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
    } catch (URISyntaxException e) {
        throw uncheck(e);
    }
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
    Request request = context.getRequest();
    HttpMethod method = valueOf(request.getMethod().getName());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
    nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
    final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
    final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
    final Channel channel = directChannelAccess.getChannel();
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
    }
    handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

Channel (io.netty.channel.Channel)889 Test (org.junit.Test)242 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)227 ChannelFuture (io.netty.channel.ChannelFuture)204 Bootstrap (io.netty.bootstrap.Bootstrap)201 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)193 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)176 InetSocketAddress (java.net.InetSocketAddress)169 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)152 IOException (java.io.IOException)143 EventLoopGroup (io.netty.channel.EventLoopGroup)142 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)138 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)131 ByteBuf (io.netty.buffer.ByteBuf)112 SocketChannel (io.netty.channel.socket.SocketChannel)105 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)99 ChannelPipeline (io.netty.channel.ChannelPipeline)98 CountDownLatch (java.util.concurrent.CountDownLatch)97 LocalChannel (io.netty.channel.local.LocalChannel)93 LocalServerChannel (io.netty.channel.local.LocalServerChannel)88