Search in sources :

Example 86 with SocketAddress

use of java.net.SocketAddress in project netty by netty.

the class OioDatagramChannel method doWrite.

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    for (; ; ) {
        final Object o = in.current();
        if (o == null) {
            break;
        }
        final ByteBuf data;
        final SocketAddress remoteAddress;
        if (o instanceof AddressedEnvelope) {
            @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) o;
            remoteAddress = envelope.recipient();
            data = envelope.content();
        } else {
            data = (ByteBuf) o;
            remoteAddress = null;
        }
        final int length = data.readableBytes();
        try {
            if (remoteAddress != null) {
                tmpPacket.setSocketAddress(remoteAddress);
            } else {
                if (!isConnected()) {
                    // NioDatagramChannel
                    throw new NotYetConnectedException();
                }
            }
            if (data.hasArray()) {
                tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
            } else {
                byte[] tmp = new byte[length];
                data.getBytes(data.readerIndex(), tmp);
                tmpPacket.setData(tmp);
            }
            socket.send(tmpPacket);
            in.remove();
        } catch (IOException e) {
            // Continue on write error as a DatagramChannel can write to multiple remote peers
            //
            // See https://github.com/netty/netty/issues/2665
            in.remove(e);
        }
    }
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) NotYetConnectedException(java.nio.channels.NotYetConnectedException) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 87 with SocketAddress

use of java.net.SocketAddress in project netty by netty.

the class BootstrapTest method testLateRegisterSuccessBindFailed.

@Test
public void testLateRegisterSuccessBindFailed() throws Exception {
    TestEventLoopGroup group = new TestEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group);
        bootstrap.channelFactory(new ChannelFactory<ServerChannel>() {

            @Override
            public ServerChannel newChannel() {
                return new LocalServerChannel() {

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return newFailedFuture(new SocketException());
                    }

                    @Override
                    public ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise) {
                        // Close the Channel to emulate what NIO and others impl do on bind failure
                        // See https://github.com/netty/netty/issues/2586
                        close();
                        return promise.setFailure(new SocketException());
                    }
                };
            }
        });
        bootstrap.childHandler(new DummyHandler());
        bootstrap.localAddress(new LocalAddress("1"));
        ChannelFuture future = bootstrap.bind();
        assertFalse(future.isDone());
        group.promise.setSuccess();
        final BlockingQueue<Boolean> queue = new LinkedBlockingQueue<Boolean>();
        future.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                queue.add(future.channel().eventLoop().inEventLoop(Thread.currentThread()));
                queue.add(future.isSuccess());
            }
        });
        assertTrue(queue.take());
        assertFalse(queue.take());
    } finally {
        group.shutdownGracefully();
        group.terminationFuture().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketException(java.net.SocketException) LocalAddress(io.netty.channel.local.LocalAddress) ChannelPromise(io.netty.channel.ChannelPromise) LocalServerChannel(io.netty.channel.local.LocalServerChannel) ServerChannel(io.netty.channel.ServerChannel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ChannelFutureListener(io.netty.channel.ChannelFutureListener) SocketException(java.net.SocketException) ConnectException(java.net.ConnectException) UnknownHostException(java.net.UnknownHostException) LocalServerChannel(io.netty.channel.local.LocalServerChannel) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 88 with SocketAddress

use of java.net.SocketAddress in project netty by netty.

the class BootstrapTest method testAsyncResolutionFailure.

@Test
public void testAsyncResolutionFailure() throws Exception {
    final Bootstrap bootstrapA = new Bootstrap();
    bootstrapA.group(groupA);
    bootstrapA.channel(LocalChannel.class);
    bootstrapA.resolver(new TestAddressResolverGroup(false));
    bootstrapA.handler(dummyHandler);
    final ServerBootstrap bootstrapB = new ServerBootstrap();
    bootstrapB.group(groupB);
    bootstrapB.channel(LocalServerChannel.class);
    bootstrapB.childHandler(dummyHandler);
    SocketAddress localAddress = bootstrapB.bind(LocalAddress.ANY).sync().channel().localAddress();
    // Connect to the server using the asynchronous resolver.
    ChannelFuture connectFuture = bootstrapA.connect(localAddress);
    // Should fail with the UnknownHostException.
    assertThat(connectFuture.await(10000), is(true));
    assertThat(connectFuture.cause(), is(instanceOf(UnknownHostException.class)));
    assertThat(connectFuture.channel().isOpen(), is(false));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 89 with SocketAddress

use of java.net.SocketAddress in project pinpoint by naver.

the class FutureGetInterceptor method getEndPoint.

private String getEndPoint(MemcachedNode handlingNode) {
    // TODO duplicated code : ApiInterceptor, FutureGetInterceptor
    final SocketAddress socketAddress = handlingNode.getSocketAddress();
    if (socketAddress instanceof InetSocketAddress) {
        final InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
        final String hostAddress = getHostAddress(inetSocketAddress);
        if (hostAddress == null) {
            // TODO return "Unknown Host";
            logger.debug("hostAddress is null");
            return null;
        }
        return hostAddress + ":" + inetSocketAddress.getPort();
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("invalid socketAddress:{}", socketAddress);
        }
        return null;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 90 with SocketAddress

use of java.net.SocketAddress in project netty by netty.

the class NioSocketChannelTest method testFlushAfterGatheredFlush.

/**
     * Reproduces the issue #1679
     */
@Test
public void testFlushAfterGatheredFlush() throws Exception {
    NioEventLoopGroup group = new NioEventLoopGroup(1);
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(group).channel(NioServerSocketChannel.class);
        sb.childHandler(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                // Trigger a gathering write by writing two buffers.
                ctx.write(Unpooled.wrappedBuffer(new byte[] { 'a' }));
                ChannelFuture f = ctx.write(Unpooled.wrappedBuffer(new byte[] { 'b' }));
                f.addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        // This message must be flushed
                        ctx.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 'c' }));
                    }
                });
                ctx.flush();
            }
        });
        SocketAddress address = sb.bind(0).sync().channel().localAddress();
        Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) address).getPort());
        DataInput in = new DataInputStream(s.getInputStream());
        byte[] buf = new byte[3];
        in.readFully(buf);
        assertThat(new String(buf, CharsetUtil.US_ASCII), is("abc"));
        s.close();
    } finally {
        group.shutdownGracefully().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DataInputStream(java.io.DataInputStream) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClosedChannelException(java.nio.channels.ClosedChannelException) DataInput(java.io.DataInput) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Aggregations

SocketAddress (java.net.SocketAddress)355 InetSocketAddress (java.net.InetSocketAddress)279 IOException (java.io.IOException)79 Test (org.junit.Test)73 Socket (java.net.Socket)48 InetAddress (java.net.InetAddress)35 Channel (org.jboss.netty.channel.Channel)33 SocketException (java.net.SocketException)27 UnknownHostException (java.net.UnknownHostException)25 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)23 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)23 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)20 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)19 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)19 Proxy (java.net.Proxy)18 HashMap (java.util.HashMap)17 DatagramPacket (java.net.DatagramPacket)16 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.apache.log4j.Logger)16 Test (org.testng.annotations.Test)16