Search in sources :

Example 1 with EzyNioSession

use of com.tvd12.ezyfoxserver.nio.entity.EzyNioSession in project ezyfox-server by youngmonkeys.

the class EzyNioSocketAcceptor method doAcceptConnection.

private void doAcceptConnection(SocketChannel clientChannel) throws Exception {
    clientChannel.configureBlocking(false);
    clientChannel.socket().setTcpNoDelay(tcpNoDelay);
    EzyChannel channel = new EzyNioSocketChannel(clientChannel);
    EzyNioHandlerGroup handlerGroup = handlerGroupManager.newHandlerGroup(channel, EzyConnectionType.SOCKET);
    EzyNioSession session = handlerGroup.getSession();
    SelectionKey selectionKey = clientChannel.register(readSelector, SelectionKey.OP_READ);
    session.setProperty(EzyNioSession.SELECTION_KEY, selectionKey);
}
Also used : SelectionKey(java.nio.channels.SelectionKey) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzyNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)

Example 2 with EzyNioSession

use of com.tvd12.ezyfoxserver.nio.entity.EzyNioSession in project ezyfox-server by youngmonkeys.

the class EzySimpleNioHandlerGroupTest method writePacketToSocketSessionKeyIsInvalid.

@Test
public void writePacketToSocketSessionKeyIsInvalid() throws Exception {
    // given
    EzySimpleNioHandlerGroup sut = newHandlerGroup();
    EzyNioSession session = FieldUtil.getFieldValue(sut, "session");
    SelectionKey selectionKey = mock(SelectionKey.class);
    when(session.getSelectionKey()).thenReturn(selectionKey);
    when(selectionKey.isValid()).thenReturn(false);
    EzyPacket packet = mock(EzyPacket.class);
    when(packet.getData()).thenReturn(new byte[] { 1, 2, 3, 5 });
    when(packet.isBinary()).thenReturn(true);
    ByteBuffer writeBuffer = ByteBuffer.allocate(5);
    // when
    MethodInvoker.create().object(sut).method("writePacketToSocket").param(EzyPacket.class, packet).param(Object.class, writeBuffer).call();
    // then
    verify(session, times(1)).getSelectionKey();
    verify(selectionKey, times(1)).isValid();
}
Also used : SelectionKey(java.nio.channels.SelectionKey) EzySimpleNioHandlerGroup(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 3 with EzyNioSession

use of com.tvd12.ezyfoxserver.nio.entity.EzyNioSession 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 EzyNioSession

use of com.tvd12.ezyfoxserver.nio.entity.EzyNioSession in project ezyfox-server by youngmonkeys.

the class EzyWsHandlerTest method setChannelClosedWithChannelNull.

@Test
public void setChannelClosedWithChannelNull() {
    // given
    EzyNioSessionManager sessionManager = mock(EzyNioSessionManager.class);
    EzyWsHandler sut = EzyWsHandler.builder().sessionManager(sessionManager).build();
    Session connection = mock(Session.class);
    EzyNioSession session = mock(EzyNioSession.class);
    when(sessionManager.getSession(connection)).thenReturn(session);
    // when
    MethodInvoker.create().object(sut).method("setChannelClosed").param(Session.class, connection).call();
    // then
    verify(sessionManager, times(1)).getSession(connection);
    verify(session, times(1)).getChannel();
}
Also used : EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzyWsHandler(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with EzyNioSession

use of com.tvd12.ezyfoxserver.nio.entity.EzyNioSession in project ezyfox-server by youngmonkeys.

the class EzySimpleWsHandlerGroupTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue webSocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionManager sessionManager = EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    EzyStatistics statistics = new EzySimpleStatistics();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    EzySimpleServer server = new EzySimpleServer();
    server.setSettings(settings);
    server.setSessionManager(sessionManager);
    EzyServerControllers controllers = mock(EzyServerControllers.class);
    server.setControllers(controllers);
    EzyInterceptor interceptor = mock(EzyInterceptor.class);
    when(controllers.getInterceptor(EzyCommand.PING)).thenReturn(interceptor);
    EzyController controller = mock(EzyController.class);
    when(controllers.getController(EzyCommand.PING)).thenReturn(controller);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setServer(server);
    serverContext.init();
    EzyChannel channel = mock(EzyChannel.class);
    when(channel.isConnected()).thenReturn(true);
    when(channel.getConnection()).thenReturn(SocketChannel.open());
    when(channel.getConnectionType()).thenReturn(EzyConnectionType.WEBSOCKET);
    EzyNioSession session = (EzyNioSession) sessionManager.provideSession(channel);
    ExEzyByteToObjectDecoder decoder = new ExEzyByteToObjectDecoder();
    ExecutorService statsThreadPool = EzyExecutors.newFixedThreadPool(1, "stats");
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzyHandlerGroupBuilderFactory handlerGroupBuilderFactory = EzyHandlerGroupBuilderFactoryImpl.builder().statistics(statistics).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue).codecFactory(new ExCodecFactory()).sessionTicketsRequestQueues(sessionTicketsRequestQueues).socketSessionTicketsQueue(socketSessionTicketsQueue).webSocketSessionTicketsQueue(webSocketSessionTicketsQueue).build();
    EzySimpleWsHandlerGroup group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).build();
    group.fireBytesReceived("hello");
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
    ((EzyAbstractSession) session).setStreamingEnable(true);
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
    EzySimplePacket packet = new EzySimplePacket();
    packet.setBinary(false);
    packet.setData("world".getBytes());
    packet.setTransportType(EzyTransportType.TCP);
    ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
    group.firePacketSend(packet, writeBuffer);
    group.sendPacketNow(packet);
    group.fireChannelRead(EzyCommand.PING, EzyEntityArrays.newArray(EzyCommand.PING.getId(), EzyEntityFactory.EMPTY_ARRAY));
    EzyInterceptor streamInterceptor = mock(EzyInterceptor.class);
    when(controllers.getStreamingInterceptor()).thenReturn(streamInterceptor);
    EzyStreamingController streamController = mock(EzyStreamingController.class);
    when(controllers.getStreamingController()).thenReturn(streamController);
    group.fireStreamBytesReceived(new byte[] { 0, 1, 2 });
    EzyPacket droppedPacket = mock(EzyPacket.class);
    when(droppedPacket.getSize()).thenReturn(12);
    group.addDroppedPacket(droppedPacket);
    EzyPacket failedPacket = mock(EzyPacket.class);
    when(failedPacket.getData()).thenReturn(new byte[] { 1, 2, 3 });
    when(failedPacket.isBinary()).thenReturn(false);
    when(channel.write(any(ByteBuffer.class), anyBoolean())).thenThrow(new IllegalStateException("maintain"));
    group.firePacketSend(failedPacket, writeBuffer);
    MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello").invoke();
    MethodInvoker.create().object(group).method("executeHandleReceivedBytes").param("hello".getBytes()).invoke();
    group.fireChannelInactive();
    Thread.sleep(2000);
    group.destroy();
    group.destroy();
    EzySocketStreamQueue streamQueue1 = mock(EzySocketStreamQueue.class);
    when(streamQueue1.add(any())).thenThrow(new IllegalStateException("queue full"));
    group = (EzySimpleWsHandlerGroup) handlerGroupBuilderFactory.newBuilder(channel, EzyConnectionType.WEBSOCKET).session(session).decoder(decoder).serverContext(serverContext).statsThreadPool(statsThreadPool).streamQueue(streamQueue1).sessionTicketsRequestQueues(sessionTicketsRequestQueues).build();
    group.fireBytesReceived(new byte[] { 127, 2, 3, 4, 5, 6 }, 0, 5);
}
Also used : EzySimpleSessionManagementSetting(com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting) EzyHandlerGroupBuilderFactory(com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzyStatistics(com.tvd12.ezyfoxserver.statistics.EzyStatistics) EzyInterceptor(com.tvd12.ezyfoxserver.interceptor.EzyInterceptor) EzyStreamingController(com.tvd12.ezyfoxserver.controller.EzyStreamingController) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) EzySimpleWsHandlerGroup(com.tvd12.ezyfoxserver.nio.websocket.EzySimpleWsHandlerGroup) EzyController(com.tvd12.ezyfoxserver.controller.EzyController) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) EzyNioSession(com.tvd12.ezyfoxserver.nio.entity.EzyNioSession) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) ByteBuffer(java.nio.ByteBuffer) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)11 Test (org.testng.annotations.Test)10 BaseTest (com.tvd12.test.base.BaseTest)9 ByteBuffer (java.nio.ByteBuffer)7 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)6 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)6 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)6 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)6 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)6 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)6 EzySimpleNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioHandlerGroup)5 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)5 EzySimpleStatistics (com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics)5 EzyStatistics (com.tvd12.ezyfoxserver.statistics.EzyStatistics)5 ExecutorService (java.util.concurrent.ExecutorService)5 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)4 EzyNioHandlerGroup (com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup)3 SelectionKey (java.nio.channels.SelectionKey)3 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)2 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)2