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; }
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; }
@Override public boolean acceptOutboundMessage(Object msg) throws Exception { if (super.acceptOutboundMessage(msg)) { @SuppressWarnings("rawtypes") AddressedEnvelope envelope = (AddressedEnvelope) msg; return encoder.acceptOutboundMessage(envelope.content()) && envelope.sender() instanceof InetSocketAddress && envelope.recipient() instanceof InetSocketAddress; } return false; }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { // Replace the ByteBuf with a DatagramPacket. out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
@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.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen) : data.nioBuffer(data.readerIndex(), dataLen); final int writtenBytes; if (remoteAddress != null) { writtenBytes = javaChannel().send(nioData, remoteAddress); } else { writtenBytes = javaChannel().write(nioData); } return writtenBytes > 0; }
@Override public boolean acceptOutboundMessage(Object msg) throws Exception { if (super.acceptOutboundMessage(msg)) { @SuppressWarnings("rawtypes") AddressedEnvelope envelope = (AddressedEnvelope) msg; return encoder.acceptOutboundMessage(envelope.content()) && envelope.sender() instanceof InetSocketAddress && envelope.recipient() instanceof InetSocketAddress; } return false; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (!(obj instanceof AddressedEnvelope)) { return false; } @SuppressWarnings("unchecked") final AddressedEnvelope<?, SocketAddress> that = (AddressedEnvelope<?, SocketAddress>) obj; if (sender() == null) { if (that.sender() != null) { return false; } } else if (!sender().equals(that.sender())) { return false; } if (recipient() == null) { if (that.recipient() != null) { return false; } } else if (!recipient().equals(that.recipient())) { return false; } return true; }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { // Replace the ByteBuf with a DatagramPacket. out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!super.equals(obj)) { return false; } if (!(obj instanceof AddressedEnvelope)) { return false; } @SuppressWarnings("unchecked") final AddressedEnvelope<?, SocketAddress> that = (AddressedEnvelope<?, SocketAddress>) obj; if (sender() == null) { if (that.sender() != null) { return false; } } else if (!sender().equals(that.sender())) { return false; } if (recipient() == null) { if (that.recipient() != null) { return false; } } else if (!recipient().equals(that.recipient())) { return false; } return true; }
@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.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen) : data.nioBuffer(data.readerIndex(), dataLen); final int writtenBytes; if (remoteAddress != null) { writtenBytes = javaChannel().send(nioData, remoteAddress); } else { writtenBytes = javaChannel().write(nioData); } return writtenBytes > 0; }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<DnsQuery, InetSocketAddress> in, List<Object> out) throws Exception { final InetSocketAddress recipient = in.recipient(); final DnsQuery query = in.content(); final ByteBuf buf = allocateBuffer(ctx, in); boolean success = false; try { encodeHeader(query, buf); encodeQuestions(query, buf); encodeRecords(query, DnsSection.ADDITIONAL, buf); success = true; } finally { if (!success) { buf.release(); } } out.add(new DatagramPacket(buf, recipient, null)); }
@Override public boolean acceptOutboundMessage(Object msg) throws Exception { if (super.acceptOutboundMessage(msg)) { @SuppressWarnings("rawtypes") AddressedEnvelope envelope = (AddressedEnvelope) msg; return encoder.acceptOutboundMessage(envelope.content()) && envelope.sender() instanceof InetSocketAddress && envelope.recipient() instanceof InetSocketAddress; } return false; }
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content) ? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override protected Object filterOutboundMessage(Object msg) { if (msg instanceof DatagramPacket) { DatagramPacket packet = (DatagramPacket) msg; ByteBuf content = packet.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DatagramPacket(newDirectBuffer(packet, content), packet.recipient()) : msg; } if (msg instanceof ByteBuf) { ByteBuf buf = (ByteBuf) msg; return UnixChannelUtil.isBufferCopyNeededForWrite(buf)? newDirectBuffer(buf) : buf; } if (msg instanceof AddressedEnvelope) { @SuppressWarnings("unchecked") AddressedEnvelope<Object, SocketAddress> e = (AddressedEnvelope<Object, SocketAddress>) msg; if (e.content() instanceof ByteBuf && (e.recipient() == null || e.recipient() instanceof InetSocketAddress)) { ByteBuf content = (ByteBuf) e.content(); return UnixChannelUtil.isBufferCopyNeededForWrite(content)? new DefaultAddressedEnvelope<ByteBuf, InetSocketAddress>( newDirectBuffer(e, content), (InetSocketAddress) e.recipient()) : e; } } throw new UnsupportedOperationException( "unsupported message type: " + StringUtil.simpleClassName(msg) + EXPECTED_TYPES); }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<M, InetSocketAddress> msg, List<Object> out) throws Exception { assert out.isEmpty(); encoder.encode(ctx, msg.content(), out); if (out.size() != 1) { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only one message."); } Object content = out.get(0); if (content instanceof ByteBuf) { // Replace the ByteBuf with a DatagramPacket. out.set(0, new DatagramPacket((ByteBuf) content, msg.recipient(), msg.sender())); } else { throw new EncoderException( StringUtil.simpleClassName(encoder) + " must produce only ByteBuf."); } }
@Override protected void encode( ChannelHandlerContext ctx, AddressedEnvelope<DnsQuery, InetSocketAddress> in, List<Object> out) throws Exception { final InetSocketAddress recipient = in.recipient(); final DnsQuery query = in.content(); final ByteBuf buf = allocateBuffer(ctx, in); boolean success = false; try { encodeHeader(query, buf); encodeQuestions(query, buf); encodeRecords(query, DnsSection.ADDITIONAL, buf); success = true; } finally { if (!success) { buf.release(); } } out.add(new DatagramPacket(buf, recipient, null)); }
@Override protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<DnsResponse, InetSocketAddress> in, List<Object> out) throws Exception { final InetSocketAddress recipient = in.recipient(); final DnsResponse response = in.content(); final ByteBuf buf = allocateBuffer(ctx, in); boolean success = false; try { encodeHeader(response, buf); encodeQuestions(response, buf); encodeRecords(response, DnsSection.ANSWER, buf); encodeRecords(response, DnsSection.AUTHORITY, buf); encodeRecords(response, DnsSection.ADDITIONAL, buf); success = true; } finally { if (!success) { buf.release(); } } out.add(new DatagramPacket(buf, recipient, null)); }
@SuppressWarnings("unchecked") AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) o; remoteAddress = envelope.recipient(); data = envelope.content(); } else {
return e; return new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(newDirectBuffer(e, content), e.recipient());
return e; return new DefaultAddressedEnvelope<ByteBuf, SocketAddress>(newDirectBuffer(e, content), e.recipient());