Search in sources :

Example 1 with EzySimpleUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    header |= 1 << 5;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    EzySession session = spy(EzyAbstractSession.class);
    session.setToken(sessionToken);
    when(sessionManager.getSession(sessionId)).thenReturn(session);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzyDatagramChannelPool channelPool = new EzyDatagramChannelPool(1);
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.setSessionManager(sessionManager);
    handler.setResponseApi(responseApi);
    handler.setDatagramChannelPool(channelPool);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) Test(org.testng.annotations.Test)

Example 2 with EzySimpleUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method testExceptionCase.

@Test
public void testExceptionCase() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenThrow(new IllegalArgumentException("test"));
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) ByteBuffer(java.nio.ByteBuffer) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 3 with EzySimpleUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleUdpReceivedPacketTest method test.

@Test
public void test() {
    DatagramChannel channel = null;
    try {
        channel = DatagramChannel.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
    InetSocketAddress address = new InetSocketAddress(12345);
    byte[] bytes = new byte[] { 1, 2, 3 };
    EzySimpleUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(channel, address, bytes);
    assert packet.getAddress() != null;
    assert packet.getBytes() != null;
    System.out.println(packet.getChannel());
    packet.release();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) DatagramChannel(java.nio.channels.DatagramChannel) Test(org.testng.annotations.Test)

Example 4 with EzySimpleUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method testNotHandshakeCase.

@Test
public void testNotHandshakeCase() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenReturn(handlerGroup);
    EzyNioSession session = new EzySimpleSession();
    session.setToken(sessionToken);
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.getClientAddress()).thenReturn(new InetSocketAddress(12345));
    session.setChannel(channel);
    when(handlerGroup.getSession()).thenReturn(session);
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) InetSocketAddress(java.net.InetSocketAddress) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) ByteBuffer(java.nio.ByteBuffer) EzySimpleSession(com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) Test(org.testng.annotations.Test)

Example 5 with EzySimpleUdpReceivedPacket

use of com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method testInvalidMessageCase.

@Test
public void testInvalidMessageCase() throws Exception {
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    byte[] bytes = new byte[] { 1, 2, 3 };
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Aggregations

EzySimpleUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket)6 InetSocketAddress (java.net.InetSocketAddress)6 EzyUdpReceivedPacket (com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket)5 Test (org.testng.annotations.Test)5 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)4 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)3 ByteBuffer (java.nio.ByteBuffer)3 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)1 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)1 EzySimpleSession (com.tvd12.ezyfoxserver.nio.entity.EzySimpleSession)1 EzyNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)1 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)1 EzyDatagramChannelPool (com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool)1 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)1 DatagramChannel (java.nio.channels.DatagramChannel)1