Search in sources :

Example 1 with EzyNioSocketAcceptor

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

the class EzySocketServerBootstrap method newSocketAcceptanceLoopHandler.

private EzySocketEventLoopHandler newSocketAcceptanceLoopHandler(EzyNioSocketAcceptor socketAcceptor) {
    EzySocketEventLoopOneHandler loopHandler = new EzyNioSocketAcceptanceLoopHandler();
    loopHandler.setThreadPoolSize(getSocketAcceptorPoolSize());
    socketAcceptor.setTcpNoDelay(getSocketTcpNoDelay());
    socketAcceptor.setReadSelector(readSelector);
    socketAcceptor.setOwnSelector(acceptSelector);
    socketAcceptor.setAcceptableConnections(new ArrayList<>());
    socketAcceptor.setHandlerGroupManager(handlerGroupManager);
    loopHandler.setEventHandler(socketAcceptor);
    return loopHandler;
}
Also used : EzySocketEventLoopOneHandler(com.tvd12.ezyfoxserver.socket.EzySocketEventLoopOneHandler)

Example 2 with EzyNioSocketAcceptor

use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor 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 3 with EzyNioSocketAcceptor

use of com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor 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 4 with EzyNioSocketAcceptor

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

the class EzyNioSocketAcceptorTest method test.

@Test
public void test() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    Selector ownSelector = spy(ExSelector.class);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_ACCEPT);
    ExServerSocketChannel serverChannel1 = spy(ExServerSocketChannel.class);
    when(selectionKey1.channel()).thenReturn(serverChannel1);
    SocketChannel channel1 = spy(ExSocketChannel.class);
    when(serverChannel1.accept()).thenReturn(channel1);
    Socket socket1 = new Socket();
    when(channel1.socket()).thenReturn(socket1);
    Selector readSelector = spy(ExSelector.class);
    channel1.configureBlocking(false);
    when(channel1.register(readSelector, SelectionKey.OP_READ)).thenReturn(selectionKey1);
    EzyNioSocketAcceptor acceptor = new EzyNioSocketAcceptor();
    acceptor.setAcceptableConnections(new ArrayList<>());
    acceptor.setHandlerGroupManager(handlerGroupManager);
    acceptor.setOwnSelector(ownSelector);
    acceptor.setReadSelector(readSelector);
    acceptor.handleEvent();
    List<SocketChannel> acceptableConnections = FieldUtil.getFieldValue(acceptor, "acceptableConnections");
    assert acceptableConnections.size() == 1;
    acceptor.handleAcceptableConnections();
    assert acceptableConnections.size() == 0;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioSocketAcceptor(com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor) Socket(java.net.Socket) Selector(java.nio.channels.Selector) AbstractSelector(java.nio.channels.spi.AbstractSelector) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with EzyNioSocketAcceptor

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

the class EzyNioSocketAcceptorTest method acceptConnectionExceptionCase.

@Test
public void acceptConnectionExceptionCase() throws Exception {
    EzyHandlerGroupManager handlerGroupManager = newHandlerGroupManager();
    Selector ownSelector = spy(ExSelector.class);
    SelectionKey selectionKey1 = spy(ExSelectionKey.class);
    SelectionKey selectionKey2 = spy(ExSelectionKey.class);
    when(ownSelector.selectedKeys()).thenReturn(Sets.newHashSet(selectionKey1, selectionKey2));
    when(selectionKey1.isValid()).thenReturn(true);
    when(selectionKey1.readyOps()).thenReturn(SelectionKey.OP_ACCEPT);
    ExServerSocketChannel serverChannel1 = spy(ExServerSocketChannel.class);
    when(selectionKey1.channel()).thenReturn(serverChannel1);
    SocketChannel channel1 = spy(ExSocketChannel.class);
    when(serverChannel1.accept()).thenReturn(channel1);
    when(channel1.socket()).thenThrow(new IllegalStateException("server maintain"));
    Selector readSelector = spy(ExSelector.class);
    EzyNioSocketAcceptor acceptor = new EzyNioSocketAcceptor();
    acceptor.setAcceptableConnections(new ArrayList<>());
    acceptor.setHandlerGroupManager(handlerGroupManager);
    acceptor.setOwnSelector(ownSelector);
    acceptor.setReadSelector(readSelector);
    acceptor.handleEvent();
    List<SocketChannel> acceptableConnections = FieldUtil.getFieldValue(acceptor, "acceptableConnections");
    assert acceptableConnections.size() == 1;
    acceptor.handleAcceptableConnections();
    assert acceptableConnections.size() == 0;
}
Also used : SelectionKey(java.nio.channels.SelectionKey) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) 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)

Aggregations

EzyNioSocketAcceptor (com.tvd12.ezyfoxserver.nio.socket.EzyNioSocketAcceptor)5 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)5 BaseTest (com.tvd12.test.base.BaseTest)5 Selector (java.nio.channels.Selector)5 SocketChannel (java.nio.channels.SocketChannel)5 Test (org.testng.annotations.Test)5 SelectionKey (java.nio.channels.SelectionKey)4 ServerSocketChannel (java.nio.channels.ServerSocketChannel)3 AbstractSelector (java.nio.channels.spi.AbstractSelector)3 com.tvd12.ezyfox.codec (com.tvd12.ezyfox.codec)2 Sets (com.tvd12.ezyfox.collect.Sets)2 EzyExecutors (com.tvd12.ezyfox.concurrent.EzyExecutors)2 EzyEntityFactory (com.tvd12.ezyfox.factory.EzyEntityFactory)2 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)2 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)2 EzyCommand (com.tvd12.ezyfoxserver.constant.EzyCommand)2 EzyConnectionType (com.tvd12.ezyfoxserver.constant.EzyConnectionType)2 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)2 EzyHandlerGroupBuilderFactoryImpl (com.tvd12.ezyfoxserver.nio.builder.impl.EzyHandlerGroupBuilderFactoryImpl)2 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)2