Search in sources :

Example 1 with EzySocketDisconnection

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

the class EzySocketDisconnectionHandler method processDisconnection.

private void processDisconnection(EzySocketDisconnection disconnection) {
    try {
        EzySession session = disconnection.getSession();
        EzyConstant disconnectReason = disconnection.getDisconnectReason();
        EzySocketDataHandlerGroup handlerGroup = removeDataHandlerGroup(session);
        if (handlerGroup != null) {
            handlerGroup.fireChannelInactive(disconnectReason);
        } else {
            logger.warn("has no handler group with session: {}, ignore disconnection: {}", session, disconnection);
        }
    } finally {
        disconnection.release();
    }
}
Also used : EzyConstant(com.tvd12.ezyfox.constant.EzyConstant) EzySession(com.tvd12.ezyfoxserver.entity.EzySession)

Example 2 with EzySocketDisconnection

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

the class EzyNioServerBootstrapTest method test.

@Test
public void test() throws Exception {
    SSLContext sslContext = SSLContext.getDefault();
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzyStreamingApi streamingApi = mock(EzyStreamingApi.class);
    EzySocketStreamQueue streamQueue = new EzyBlockingSocketStreamQueue();
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySessionTicketsQueue socketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsQueue websocketSessionTicketsQueue = new EzyBlockingSessionTicketsQueue();
    EzySessionTicketsRequestQueues sessionTicketsRequestQueues = new EzySessionTicketsRequestQueues();
    EzySocketDisconnectionQueue socketDisconnectionQueue = new EzySocketDisconnectionQueue() {

        final BlockingQueue<EzySocketDisconnection> queue = new LinkedBlockingQueue<>();

        @Override
        public EzySocketDisconnection take() throws InterruptedException {
            return queue.take();
        }

        @Override
        public int size() {
            return 0;
        }

        @Override
        public void remove(EzySocketDisconnection disconnection) {
        }

        @Override
        public boolean isEmpty() {
            return false;
        }

        @Override
        public void clear() {
        }

        @Override
        public boolean add(EzySocketDisconnection disconnection) {
            return false;
        }
    };
    EzySimpleConfig config = new EzySimpleConfig();
    EzySimpleSettings settings = new EzySimpleSettings();
    EzySimpleStreamingSetting streaming = settings.getStreaming();
    streaming.setEnable(true);
    settings.getUdp().setActive(true);
    EzySimpleServer server = new EzySimpleServer();
    EzyServerControllers serverControllers = EzyServerControllersImpl.builder().build();
    server.setControllers(serverControllers);
    EzyEventControllersSetting eventControllersSetting = new EzySimpleEventControllersSetting();
    EzyEventControllers eventControllers = EzyEventControllersImpl.create(eventControllersSetting);
    server.setEventControllers(eventControllers);
    server.setConfig(config);
    server.setSettings(settings);
    EzySimpleServerContext serverContext = new EzySimpleServerContext();
    serverContext.setProperty(EzySocketUserRemovalQueue.class, new EzyBlockingSocketUserRemovalQueue());
    serverContext.setServer(server);
    serverContext.init();
    ExBootstrap localBootstrap = new ExBootstrap(new EzyBootstrap.Builder().context(serverContext));
    EzyNioServerBootstrap bootstrap = new EzyNioServerBootstrap();
    bootstrap.setContext(serverContext);
    bootstrap.setLocalBootstrap(localBootstrap);
    bootstrap.setSslContext(sslContext);
    bootstrap.setResponseApi(responseApi);
    bootstrap.setStreamingApi(streamingApi);
    bootstrap.setStreamQueue(streamQueue);
    bootstrap.setHandlerGroupManager(handlerGroupManager);
    bootstrap.setSocketSessionTicketsQueue(socketSessionTicketsQueue);
    bootstrap.setWebsocketSessionTicketsQueue(websocketSessionTicketsQueue);
    bootstrap.setSocketDisconnectionQueue(socketDisconnectionQueue);
    bootstrap.setSocketSessionTicketsRequestQueues(sessionTicketsRequestQueues);
    bootstrap.start();
    bootstrap.destroy();
    bootstrap.destroy();
}
Also used : EzyEventControllersSetting(com.tvd12.ezyfoxserver.setting.EzyEventControllersSetting) EzySimpleServerContext(com.tvd12.ezyfoxserver.context.EzySimpleServerContext) EzyStreamingApi(com.tvd12.ezyfoxserver.api.EzyStreamingApi) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) EzySimpleConfig(com.tvd12.ezyfoxserver.config.EzySimpleConfig) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyServerControllers(com.tvd12.ezyfoxserver.wrapper.EzyServerControllers) SSLContext(javax.net.ssl.SSLContext) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyEventControllers(com.tvd12.ezyfoxserver.wrapper.EzyEventControllers) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzyNioServerBootstrap(com.tvd12.ezyfoxserver.nio.EzyNioServerBootstrap) EzySimpleEventControllersSetting(com.tvd12.ezyfoxserver.setting.EzySimpleEventControllersSetting) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 3 with EzySocketDisconnection

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

the class EzySocketDisconnectionHandlerTest method processDisconnectionQueueExceptionCaseTest.

@Test
public void processDisconnectionQueueExceptionCaseTest() {
    EzySocketDisconnectionHandler handler = new EzySocketDisconnectionHandler();
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    EzySocketDataHandlerGroupRemover dataHandlerGroupRemover = mock(EzySocketDataHandlerGroupRemover.class);
    when(dataHandlerGroupRemover.removeHandlerGroup(any(EzySession.class))).thenThrow(new IllegalArgumentException());
    EzySession session = spy(EzyAbstractSession.class);
    EzySocketDisconnection disconnection = new EzySimpleSocketDisconnection(session);
    disconnectionQueue.add(disconnection);
    handler.setDisconnectionQueue(disconnectionQueue);
    handler.setDataHandlerGroupRemover(dataHandlerGroupRemover);
    handler.handleEvent();
}
Also used : EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 4 with EzySocketDisconnection

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

the class EzySocketDisconnectionHandlerTest method test.

@Test
public void test() {
    EzySocketDisconnectionHandler handler = new EzySocketDisconnectionHandler();
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    EzySocketDataHandlerGroupRemover dataHandlerGroupRemover = mock(EzySocketDataHandlerGroupRemover.class);
    EzySocketDataHandlerGroup handlerGroup = mock(EzySocketDataHandlerGroup.class);
    when(dataHandlerGroupRemover.removeHandlerGroup(any(EzySession.class))).thenReturn(handlerGroup);
    EzySession session = spy(EzyAbstractSession.class);
    EzySocketDisconnection disconnection = new EzySimpleSocketDisconnection(session);
    disconnectionQueue.add(disconnection);
    handler.setDisconnectionQueue(disconnectionQueue);
    handler.setDataHandlerGroupRemover(dataHandlerGroupRemover);
    handler.handleEvent();
    handler.destroy();
}
Also used : EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 5 with EzySocketDisconnection

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

the class V121SessionManagerTest method inspectTest.

@Test
public void inspectTest() throws Exception {
    // given
    EzySocketDisconnectionQueue disconnectionQueue = new EzyBlockingSocketDisconnectionQueue();
    SessionManager sut = (SessionManager) new SessionManager.Builder().validationInterval(100).validationDelay(100).objectFactory(() -> new Session(disconnectionQueue)).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    EzyChannel channel1 = mock(EzyChannel.class);
    when(channel1.getConnection()).thenReturn(new Object());
    EzyChannel channel2 = mock(EzyChannel.class);
    when(channel2.getConnection()).thenReturn(new Object());
    EzyChannel channel3 = mock(EzyChannel.class);
    when(channel3.getConnection()).thenReturn(new Object());
    Session session1 = sut.provideSession(channel1);
    session1.setLoggedIn(true);
    session1.setLastReadTime(System.currentTimeMillis());
    Session session2 = sut.provideSession(channel2);
    session2.setLoggedIn(true);
    session2.setLastReadTime(System.currentTimeMillis());
    Session session3 = sut.provideSession(channel3);
    session3.setLoggedIn(true);
    session3.setLastReadTime(System.currentTimeMillis());
    Thread[] threads = new Thread[10];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(() -> {
            long start = System.currentTimeMillis();
            long elapsedTime = 0;
            while (elapsedTime < 100) {
                EzyChannel channel = mock(EzyChannel.class);
                when(channel.getConnection()).thenReturn(new Object());
                sut.provideSession(channel);
                elapsedTime = System.currentTimeMillis() - start;
            }
        });
    }
    Thread disconnectionThread = new Thread(() -> {
        while (true) {
            try {
                EzySocketDisconnection item = disconnectionQueue.take();
                sut.clearSession((Session) item.getSession());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    disconnectionThread.start();
    // when
    sut.start();
    for (Thread thread : threads) {
        thread.start();
    }
    Thread.sleep(1000);
    disconnectionThread.interrupt();
    // then
    Asserts.assertEquals(3, sut.getAllSessionCount());
    sut.destroy();
}
Also used : EzySocketDisconnection(com.tvd12.ezyfoxserver.socket.EzySocketDisconnection) EzyChannel(com.tvd12.ezyfoxserver.socket.EzyChannel) EzySimpleSessionManager(com.tvd12.ezyfoxserver.wrapper.EzySimpleSessionManager) EzyBlockingSocketDisconnectionQueue(com.tvd12.ezyfoxserver.socket.EzyBlockingSocketDisconnectionQueue) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) EzySocketDisconnectionQueue(com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue) EzyAbstractSession(com.tvd12.ezyfoxserver.entity.EzyAbstractSession) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)6 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzyBlockingSocketDisconnectionQueue (com.tvd12.ezyfoxserver.socket.EzyBlockingSocketDisconnectionQueue)2 EzySocketDisconnection (com.tvd12.ezyfoxserver.socket.EzySocketDisconnection)2 EzyConstant (com.tvd12.ezyfox.constant.EzyConstant)1 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)1 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)1 EzyStreamingApi (com.tvd12.ezyfoxserver.api.EzyStreamingApi)1 EzySimpleConfig (com.tvd12.ezyfoxserver.config.EzySimpleConfig)1 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)1 EzyAbstractSession (com.tvd12.ezyfoxserver.entity.EzyAbstractSession)1 EzyNioServerBootstrap (com.tvd12.ezyfoxserver.nio.EzyNioServerBootstrap)1 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)1 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)1 EzyEventControllersSetting (com.tvd12.ezyfoxserver.setting.EzyEventControllersSetting)1 EzySimpleEventControllersSetting (com.tvd12.ezyfoxserver.setting.EzySimpleEventControllersSetting)1 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)1 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)1 EzyChannel (com.tvd12.ezyfoxserver.socket.EzyChannel)1 EzySocketDisconnectionQueue (com.tvd12.ezyfoxserver.socket.EzySocketDisconnectionQueue)1