use of io.netty.channel.socket.DatagramPacket in project ribbon by Netflix.
the class HelloUdpServerExternalResource method start.
public void start() {
int port;
try {
port = choosePort();
} catch (SocketException e) {
throw new RuntimeException("Error choosing point", e);
}
server = RxNetty.createUdpServer(port, new ConnectionHandler<DatagramPacket, DatagramPacket>() {
@Override
public Observable<Void> handle(final ObservableConnection<DatagramPacket, DatagramPacket> newConnection) {
return newConnection.getInput().flatMap(new Func1<DatagramPacket, Observable<Void>>() {
@Override
public Observable<Void> call(final DatagramPacket received) {
return Observable.interval(timeout, TimeUnit.MILLISECONDS).take(1).flatMap(new Func1<Long, Observable<Void>>() {
@Override
public Observable<Void> call(Long aLong) {
InetSocketAddress sender = received.sender();
LOG.info("Received datagram. Sender: " + sender);
ByteBuf data = newConnection.getChannel().alloc().buffer(WELCOME_MSG_BYTES.length);
data.writeBytes(WELCOME_MSG_BYTES);
return newConnection.writeAndFlush(new DatagramPacket(data, sender));
}
});
}
});
}
});
server.start();
LOG.info("UDP hello server started at port: " + port);
}
use of io.netty.channel.socket.DatagramPacket in project netty by netty.
the class DatagramPacketEncoderTest method testEncode.
@Test
public void testEncode() {
InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
InetSocketAddress sender = SocketUtils.socketAddress("127.0.0.1", 20000);
assertTrue(channel.writeOutbound(new DefaultAddressedEnvelope<String, InetSocketAddress>("netty", recipient, sender)));
DatagramPacket packet = channel.readOutbound();
try {
assertEquals("netty", packet.content().toString(CharsetUtil.UTF_8));
assertEquals(recipient, packet.recipient());
assertEquals(sender, packet.sender());
} finally {
packet.release();
}
}
use of io.netty.channel.socket.DatagramPacket in project netty by netty.
the class DnsQueryTest method writeQueryTest.
@Test
public void writeQueryTest() throws Exception {
InetSocketAddress addr = SocketUtils.socketAddress("8.8.8.8", 53);
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsQueryEncoder());
List<DnsQuery> queries = new ArrayList<DnsQuery>(5);
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("1.0.0.127.in-addr.arpa", DnsRecordType.PTR)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("www.example.com", DnsRecordType.A)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.AAAA)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.MX)));
queries.add(new DatagramDnsQuery(null, addr, 1).setRecord(DnsSection.QUESTION, new DefaultDnsQuestion("example.com", DnsRecordType.CNAME)));
for (DnsQuery query : queries) {
assertThat(query.count(DnsSection.QUESTION), is(1));
assertThat(query.count(DnsSection.ANSWER), is(0));
assertThat(query.count(DnsSection.AUTHORITY), is(0));
assertThat(query.count(DnsSection.ADDITIONAL), is(0));
embedder.writeOutbound(query);
DatagramPacket packet = embedder.readOutbound();
Assert.assertTrue(packet.content().isReadable());
packet.release();
Assert.assertNull(embedder.readOutbound());
}
}
use of io.netty.channel.socket.DatagramPacket in project netty by netty.
the class DnsResponseTest method readMalormedResponseTest.
@Test
public void readMalormedResponseTest() throws Exception {
EmbeddedChannel embedder = new EmbeddedChannel(new DatagramDnsResponseDecoder());
ByteBuf packet = embedder.alloc().buffer(512).writeBytes(malformedLoopPacket);
exception.expect(CorruptedFrameException.class);
embedder.writeInbound(new DatagramPacket(packet, null, new InetSocketAddress(0)));
}
use of io.netty.channel.socket.DatagramPacket in project camel by apache.
the class DatagramPacketEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, AddressedEnvelope<Object, InetSocketAddress> msg, List<Object> out) throws Exception {
if (msg.content() instanceof ByteBuf) {
ByteBuf payload = (ByteBuf) msg.content();
// Just wrap the message as DatagramPacket, need to make sure the message content is ByteBuf
DatagramPacket dp = new DatagramPacket(payload.retain(), msg.recipient());
out.add(dp);
}
}
Aggregations