Search in sources :

Example 81 with SocketAddress

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

the class DnsMessageUtil method appendAddresses.

private static StringBuilder appendAddresses(StringBuilder buf, DnsMessage msg) {
    if (!(msg instanceof AddressedEnvelope)) {
        return buf;
    }
    @SuppressWarnings("unchecked") AddressedEnvelope<?, SocketAddress> envelope = (AddressedEnvelope<?, SocketAddress>) msg;
    SocketAddress addr = envelope.sender();
    if (addr != null) {
        buf.append("from: ").append(addr).append(", ");
    }
    addr = envelope.recipient();
    if (addr != null) {
        buf.append("to: ").append(addr).append(", ");
    }
    return buf;
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) SocketAddress(java.net.SocketAddress)

Example 82 with SocketAddress

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

the class CombinedChannelDuplexHandlerTest method testOutboundEvents.

@Test
public void testOutboundEvents() {
    final Queue<Event> queue = new ArrayDeque<Event>();
    ChannelInboundHandler inboundHandler = new ChannelInboundHandlerAdapter();
    ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_ADDED);
        }

        @Override
        public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.HANDLER_REMOVED);
        }

        @Override
        public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.BIND);
        }

        @Override
        public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            queue.add(Event.CONNECT);
        }

        @Override
        public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DISCONNECT);
        }

        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.CLOSE);
        }

        @Override
        public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            queue.add(Event.DEREGISTER);
        }

        @Override
        public void read(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.READ);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            queue.add(Event.WRITE);
        }

        @Override
        public void flush(ChannelHandlerContext ctx) throws Exception {
            queue.add(Event.FLUSH);
        }
    };
    CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler> handler = new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(inboundHandler, outboundHandler);
    EmbeddedChannel channel = new EmbeddedChannel();
    channel.pipeline().addFirst(handler);
    doOutboundOperations(channel);
    assertEquals(Event.HANDLER_ADDED, queue.poll());
    assertEquals(Event.BIND, queue.poll());
    assertEquals(Event.CONNECT, queue.poll());
    assertEquals(Event.WRITE, queue.poll());
    assertEquals(Event.FLUSH, queue.poll());
    assertEquals(Event.READ, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.CLOSE, queue.poll());
    assertEquals(Event.DEREGISTER, queue.poll());
    handler.removeOutboundHandler();
    assertEquals(Event.HANDLER_REMOVED, queue.poll());
    // These should not be handled by the inboundHandler anymore as it was removed before
    doOutboundOperations(channel);
    // Should have not received any more events as it was removed before via removeInboundHandler()
    assertTrue(queue.isEmpty());
    assertTrue(channel.finish());
    assertTrue(queue.isEmpty());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ArrayDeque(java.util.ArrayDeque) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 83 with SocketAddress

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

the class CombinedChannelDuplexHandlerTest method testPromisesPassed.

@Test(timeout = 3000)
public void testPromisesPassed() {
    ChannelOutboundHandler outboundHandler = new ChannelOutboundHandlerAdapter() {

        @Override
        public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            promise.setSuccess();
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(outboundHandler, new CombinedChannelDuplexHandler<ChannelInboundHandler, ChannelOutboundHandler>(new ChannelInboundHandlerAdapter(), new ChannelOutboundHandlerAdapter()));
    ChannelPipeline pipeline = ch.pipeline();
    ChannelPromise promise = ch.newPromise();
    pipeline.connect(new InetSocketAddress(0), null, promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.bind(new InetSocketAddress(0), promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.close(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.disconnect(promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.write("test", promise);
    promise.syncUninterruptibly();
    promise = ch.newPromise();
    pipeline.deregister(promise);
    promise.syncUninterruptibly();
    ch.finish();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 84 with SocketAddress

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

the class NioDatagramChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    final SocketAddress remoteAddress;
    final ByteBuf data;
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg;
        remoteAddress = envelope.recipient();
        data = envelope.content();
    } else {
        data = (ByteBuf) msg;
        remoteAddress = null;
    }
    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    final ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), dataLen);
    final int writtenBytes;
    if (remoteAddress != null) {
        writtenBytes = javaChannel().send(nioData, remoteAddress);
    } else {
        writtenBytes = javaChannel().write(nioData);
    }
    return writtenBytes > 0;
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 85 with SocketAddress

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

the class NioDatagramChannel method filterOutboundMessage.

@Override
protected Object filterOutboundMessage(Object msg) {
    if (msg instanceof DatagramPacket) {
        DatagramPacket p = (DatagramPacket) msg;
        ByteBuf content = p.content();
        if (isSingleDirectBuffer(content)) {
            return p;
        }
        return new DatagramPacket(newDirectBuffer(p, content), p.recipient());
    }
    if (msg instanceof ByteBuf) {
        ByteBuf buf = (ByteBuf) msg;
        if (isSingleDirectBuffer(buf)) {
            return buf;
        }
        return newDirectBuffer(buf);
    }
    if (msg instanceof AddressedEnvelope) {
        @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg;
        if (e.content() instanceof ByteBuf) {
            ByteBuf content = (ByteBuf) e.content();
            if (isSingleDirectBuffer(content)) {
                return e;
            }
            return new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(newDirectBuffer(e, content), e.recipient());
        }
    }
    throw new UnsupportedOperationException("unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES);
}
Also used : DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope) AddressedEnvelope(io.netty.channel.AddressedEnvelope) DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

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