Search in sources :

Example 56 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project lispflowmapping by opendaylight.

the class LispSouthboundHandlerTest method mapRegisterAndNotify__ValidExtraDataParsedSuccessfully.

@Test
@Ignore
public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
    byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
    extraDataPacket[mapRegisterPacket.length] = 0x9;
    System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
    DatagramPacket dp = new DatagramPacket(wrappedBuffer(extraDataPacket), new InetSocketAddress(0), new InetSocketAddress(0));
    testedLispService.handlePacket(dp);
    // Check map register fields.
    // XXX: test
    // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
    byte[] notifyResult = lastMapNotifyPacket().content().array();
    assertEquals(mapRegisterPacket.length, notifyResult.length);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 57 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project lispflowmapping by opendaylight.

the class LispSouthboundPluginTest method handleSerializedLispBufferTest_withIpv6.

/**
 * Tests {@link LispSouthboundPlugin#handleSerializedLispBuffer} method with ipv6.
 */
@Test
public void handleSerializedLispBufferTest_withIpv6() throws NoSuchFieldException, IllegalAccessException, UnknownHostException {
    final ArgumentCaptor<DatagramPacket> captor = ArgumentCaptor.forClass(DatagramPacket.class);
    final InetAddress address = InetAddress.getByAddress(IPV6_BINARY.getIpv6AddressBinary().getValue());
    final InetSocketAddress inetSocketAddress = new InetSocketAddress(address, PORT);
    // Ensures that NPE is not thrown.
    Mockito.when(channel.write(Mockito.any())).thenReturn(Mockito.mock(ChannelFuture.class));
    lispSouthboundPlugin.handleSerializedLispBuffer(TRANSPORT_ADDRESS_IPV6, PACKET, MessageType.MapRequest);
    Mockito.verify(channel).write(captor.capture());
    Mockito.verify(channel).flush();
    final DatagramPacket result = captor.getValue();
    assertArrayEquals(PACKET.array(), result.content().array());
    assertEquals(inetSocketAddress, result.recipient());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) InetAddress(java.net.InetAddress) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 58 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project LanternServer by LanternPowered.

the class QueryHandler method handleBasicStats.

private void handleBasicStats(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    LanternServer server = this.queryServer.getGame().getServer();
    // TODO: Find out how to support the size and max size properties
    final Cause cause = Cause.of(EventContext.empty(), new SimpleRemoteConnection((InetSocketAddress) ctx.channel().remoteAddress(), null));
    final QueryServerEvent.Basic event = SpongeEventFactory.createQueryServerEventBasic(cause, (InetSocketAddress) ctx.channel().localAddress(), "SMP", this.getWorldName(), server.getMotd().toPlain(), server.getMaxPlayers(), Integer.MAX_VALUE, server.getOnlinePlayers().size(), 0);
    Sponge.getEventManager().post(event);
    final InetSocketAddress address = event.getAddress();
    final ByteBuf buf = ctx.alloc().buffer();
    buf.writeByte(ACTION_STATS);
    buf.writeInt(sessionId);
    writeString(buf, event.getMotd());
    writeString(buf, event.getGameType());
    writeString(buf, event.getMap());
    writeString(buf, String.valueOf(event.getPlayerCount()));
    writeString(buf, String.valueOf(event.getMaxPlayerCount()));
    buf.writeShortLE(address.getPort());
    writeString(buf, address.getHostString());
    ctx.write(new DatagramPacket(buf, packet.sender()));
}
Also used : LanternServer(org.lanternpowered.server.LanternServer) SimpleRemoteConnection(org.lanternpowered.server.network.SimpleRemoteConnection) InetSocketAddress(java.net.InetSocketAddress) Cause(org.spongepowered.api.event.cause.Cause) DatagramPacket(io.netty.channel.socket.DatagramPacket) QueryServerEvent(org.spongepowered.api.event.server.query.QueryServerEvent) ByteBuf(io.netty.buffer.ByteBuf)

Example 59 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project LanternServer by LanternPowered.

the class QueryHandler method handleHandshake.

private void handleHandshake(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) {
    int challengeToken = queryServer.generateChallengeToken(packet.sender());
    ByteBuf out = ctx.alloc().buffer();
    out.writeByte(ACTION_HANDSHAKE);
    out.writeInt(sessionId);
    writeString(out, String.valueOf(challengeToken));
    ctx.write(new DatagramPacket(out, packet.sender()));
}
Also used : DatagramPacket(io.netty.channel.socket.DatagramPacket) ByteBuf(io.netty.buffer.ByteBuf)

Example 60 with DatagramPacket

use of io.netty.channel.socket.DatagramPacket in project netty by netty.

the class DatagramPacketEncoderTest method testEncode.

private void testEncode(boolean senderIsNull) {
    InetSocketAddress recipient = SocketUtils.socketAddress("127.0.0.1", 10000);
    InetSocketAddress sender = senderIsNull ? null : 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();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(io.netty.channel.socket.DatagramPacket) DefaultAddressedEnvelope(io.netty.channel.DefaultAddressedEnvelope)

Aggregations

DatagramPacket (io.netty.channel.socket.DatagramPacket)75 InetSocketAddress (java.net.InetSocketAddress)45 ByteBuf (io.netty.buffer.ByteBuf)38 Test (org.junit.Test)15 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)14 Channel (io.netty.channel.Channel)8 ChannelFuture (io.netty.channel.ChannelFuture)8 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 DatagramChannel (io.netty.channel.socket.DatagramChannel)8 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.jupiter.api.Test)6 Bootstrap (io.netty.bootstrap.Bootstrap)5 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)5 NioDatagramChannel (io.netty.channel.socket.nio.NioDatagramChannel)5 InetAddress (java.net.InetAddress)5 SocketAddress (java.net.SocketAddress)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AddressedEnvelope (io.netty.channel.AddressedEnvelope)4