Search in sources :

Example 11 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.

the class EzyHandlerGroupManagerImplTest method newHandlerGroupManager.

public EzyHandlerGroupManager newHandlerGroupManager() {
    EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
    when(codecFactory.newDecoder(any())).thenReturn(decoder);
    ExecutorService statsThreadPool = EzyExecutors.newSingleThreadExecutor("stats");
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    server.setSessionManager(sessionManager);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setServer(server);
    serverContext.init();
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzyStatistics statistics = new EzySimpleStatistics();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).statsThreadPool(statsThreadPool).streamQueue(streamQueue).disconnectionQueue(disconnectionQueue).codecFactory(codecFactory).serverContext(serverContext).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).socketSessionTicketsQueue(webSocketSessionTicketsQueue).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    return EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
}
Also used : EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)

Example 12 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.

the class EzyHandlerGroupManagerImplTest method mapSocketChannelNonContainsConnection.

@Test
public void mapSocketChannelNonContainsConnection() {
    // given
    EzyHandlerGroupManager sut = newHandlerGroupManager();
    EzySession session = mock(EzySession.class);
    EzyChannel channel = mock(EzyChannel.class);
    when(session.getChannel()).thenReturn(channel);
    Object connection = new Object();
    when(channel.getConnection()).thenReturn(connection);
    // when
    sut.mapSocketChannel(null, session);
    // then
    verify(session, times(1)).getChannel();
    verify(channel, times(1)).getConnection();
}
Also used : EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 13 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager 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 14 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketTest.

@Test
public void handleReceivedUdpPacketTest() throws Exception {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = messageBytes();
    InetSocketAddress udpAddress = new InetSocketAddress(3005);
    InetSocketAddress tcpAddress = new InetSocketAddress(123);
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    when(packet.getAddress()).thenReturn(udpAddress);
    SocketChannel socketChannel = mock(SocketChannel.class);
    when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
    sut.setHandlerGroupManager(handlerGroupManager);
    EzySocketDataReceiver socketDataReceiver = mock(EzySocketDataReceiver.class);
    sut.setSocketDataReceiver(socketDataReceiver);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
    verify(socketChannel, times(1)).getRemoteAddress();
    verify(socketDataReceiver, times(1)).udpReceive(any(SocketChannel.class), any(EzyMessage.class));
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Example 15 with EzyHandlerGroupManager

use of com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleReceivedUdpPacketIsNotOneClientTest.

@Test
public void handleReceivedUdpPacketIsNotOneClientTest() throws Exception {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    byte[] messageBytes = messageBytes();
    InetSocketAddress udpAddress = new InetSocketAddress("client1", 3005);
    InetSocketAddress tcpAddress = new InetSocketAddress("client2", 123);
    EzyUdpReceivedPacket packet = mock(EzyUdpReceivedPacket.class);
    when(packet.getBytes()).thenReturn(messageBytes);
    when(packet.getAddress()).thenReturn(udpAddress);
    SocketChannel socketChannel = mock(SocketChannel.class);
    when(socketChannel.getRemoteAddress()).thenReturn(tcpAddress);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    when(handlerGroupManager.getSocketChannel(udpAddress)).thenReturn(socketChannel);
    sut.setHandlerGroupManager(handlerGroupManager);
    // when
    MethodInvoker.create().object(sut).method("handleReceivedUdpPacket").param(EzyUdpReceivedPacket.class, packet).call();
    // then
    verify(handlerGroupManager, times(1)).getSocketChannel(udpAddress);
    verify(socketChannel, times(1)).getRemoteAddress();
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) Test(org.testng.annotations.Test)

Aggregations

EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)29 Test (org.testng.annotations.Test)28 BaseTest (com.tvd12.test.base.BaseTest)14 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)13 SocketChannel (java.nio.channels.SocketChannel)13 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)9 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)9 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)9 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)8 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)8 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)8 ExecutorService (java.util.concurrent.ExecutorService)8 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)7 EzyNioSessionManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager)7 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)7 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)7 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)7 InetSocketAddress (java.net.InetSocketAddress)7 ByteBuffer (java.nio.ByteBuffer)7 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)6