Search in sources :

Example 1 with EzyHandlerGroupManager

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

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

the class EzyNioSocketAcceptorTest method newHandlerGroupManager.

private 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).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 3 with EzyHandlerGroupManager

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

the class EzyNioSocketAcceptorTest method acceptConnectionTest.

@Test
public void acceptConnectionTest() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    Selector readSelector = Selector.open();
    EzyNioSocketAcceptor sut = new EzyNioSocketAcceptor();
    sut.setReadSelector(readSelector);
    SocketChannel clientChannel = SocketChannel.open();
    when(handlerGroupManager.newHandlerGroup(any(), any())).thenReturn(handlerGroup);
    sut.setHandlerGroupManager(handlerGroupManager);
    EzyNioSession session = mock(EzyNioSession.class);
    when(handlerGroup.getSession()).thenReturn(session);
    // when
    MethodInvoker.create().object(sut).method("acceptConnection").param(SocketChannel.class, clientChannel).call();
    // then
    verify(handlerGroupManager, times(1)).newHandlerGroup(any(), any());
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Selector(java.nio.channels.Selector) AbstractSelector(java.nio.channels.spi.AbstractSelector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 4 with EzyHandlerGroupManager

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

the class EzyNioSocketReaderTest method test.

@Test
public void test() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    EzySocketDataReceiver socketDataReceiver = EzySocketDataReceiver.builder().threadPoolSize(1).handlerGroupManager(handlerGroupManager).build();
    Selector ownSelector = spy(ExSelector.class);
    when(ownSelector.selectNow()).thenReturn(1);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    SelectionKey selectionKey3 = spy(ExSelectionKey.class);
    SelectionKey selectionKey4 = spy(ExSelectionKey.class);
    SelectionKey selectionKey5 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2, selectionKey3, selectionKey4, selectionKey5));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_READ);
    when(selectionKey2.isValid()).thenReturn(true);
    when(selectionKey2.readyOps()).thenReturn(SelectionKey.OP_WRITE);
    when(selectionKey3.isValid()).thenReturn(false);
    when(selectionKey4.isValid()).thenReturn(true);
    when(selectionKey4.readyOps()).thenReturn(SelectionKey.OP_READ);
    when(selectionKey5.isValid()).thenReturn(true);
    when(selectionKey5.readyOps()).thenReturn(SelectionKey.OP_READ);
    SocketChannel socketChannel1 = mock(SocketChannel.class);
    EzyChannel channel1 = new EzyNioSocketChannel(socketChannel1);
    handlerGroupManager.newHandlerGroup(channel1, EzyConnectionType.SOCKET);
    when(selectionKey1.channel()).thenReturn(socketChannel1);
    when(socketChannel1.isConnected()).thenReturn(true);
    when(socketChannel1.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> {
        ByteBuffer buffer = invocation.getArgumentAt(0, ByteBuffer.class);
        buffer.put("hello".getBytes());
        return "hello".length();
    });
    SocketChannel socketChannel4 = mock(SocketChannel.class);
    when(selectionKey4.channel()).thenReturn(socketChannel4);
    SocketChannel socketChannel5 = spy(ExSocketChannel.class);
    EzyChannel channel5 = new EzyNioSocketChannel(socketChannel5);
    when(selectionKey5.channel()).thenReturn(socketChannel5);
    doNothing().when(socketChannel5).close();
    handlerGroupManager.newHandlerGroup(channel5, EzyConnectionType.SOCKET);
    when(selectionKey5.channel()).thenReturn(socketChannel5);
    when(socketChannel5.isConnected()).thenReturn(true);
    when(socketChannel5.read(any(ByteBuffer.class))).then((Answer<Integer>) invocation -> -1);
    EzyNioSocketAcceptor socketAcceptor = new EzyNioSocketAcceptor();
    socketAcceptor.setReadSelector(ownSelector);
    socketAcceptor.setHandlerGroupManager(handlerGroupManager);
    socketAcceptor.setAcceptableConnections(new ArrayList<>());
    EzyNioSocketReader socketReader = new EzyNioSocketReader();
    socketReader.setOwnSelector(ownSelector);
    socketReader.setAcceptableConnectionsHandler(socketAcceptor);
    socketReader.setSocketDataReceiver(socketDataReceiver);
    socketReader.handleEvent();
}
Also used : EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyEntityFactory(com.tvd12.ezyfox.factory.EzyEntityFactory) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Selector(java.nio.channels.Selector) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) Test(org.testng.annotations.Test) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) EzyConnectionType(com.tvd12.ezyfoxserver.constant.EzyConnectionType) Answer(org.mockito.stubbing.Answer) SelectorProvider(java.nio.channels.spi.SelectorProvider) EzyExecutors(com.tvd12.ezyfox.concurrent.EzyExecutors) EzyNioSessionManagerImpl(com.tvd12.ezyfoxserver.nio.wrapper.impl.EzyNioSessionManagerImpl) SocketChannel(java.nio.channels.SocketChannel) EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzyHandlerGroupManagerImpl(com.tvd12.ezyfoxserver.nio.wrapper.impl.EzyHandlerGroupManagerImpl) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) ExecutorService(java.util.concurrent.ExecutorService) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyCommand(com.tvd12.ezyfoxserver.constant.EzyCommand) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) com.tvd12.ezyfox.codec(com.tvd12.ezyfox.codec) Sets(com.tvd12.ezyfox.collect.Sets) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) SelectionKey(java.nio.channels.SelectionKey) EzyHandlerGroupBuilderFactoryImpl(com.tvd12.ezyfoxserver.nio.builder.impl.EzyHandlerGroupBuilderFactoryImpl) com.tvd12.ezyfoxserver.socket(com.tvd12.ezyfoxserver.socket) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) BaseTest(com.tvd12.test.base.BaseTest) EzyNioSocketReader(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader) Queue(java.util.Queue) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) SelectionKey(java.nio.channels.SelectionKey) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) SocketChannel(java.nio.channels.SocketChannel) EzyNioSocketChannel(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketChannel) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) ByteBuffer(java.nio.ByteBuffer) EzyNioSocketReader(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketReader) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) Selector(java.nio.channels.Selector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with EzyHandlerGroupManager

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

the class EzySocketDataReceiverTest method udpReceiveButExceptionTest.

@Test
public void udpReceiveButExceptionTest() throws Exception {
    // given
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
    SocketChannel channel = mock(SocketChannel.class);
    EzyMessage message = mock(EzyMessage.class);
    EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
    doThrow(new RuntimeException()).when(handlerGroup).fireMessageReceived(message);
    when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
    // when
    sut.udpReceive(channel, message);
    Thread.sleep(120);
    // then
    verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
    verify(handlerGroup, times(1)).fireMessageReceived(message);
    sut.destroy();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) 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