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();
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations