Search in sources :

Example 1 with EzyWsHandler

use of com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler in project ezyfox-server by youngmonkeys.

the class EzyWebSocketServerCreatorTest method createManagementTest.

@Test
public void createManagementTest() {
    // given
    EzyWebSocketServerCreator creator = new EzyWebSocketServerCreator();
    EzySimpleWebSocketSetting webSocketSetting = new EzySimpleWebSocketSetting();
    webSocketSetting.setManagementEnable(true);
    creator.setting(webSocketSetting);
    // when
    creator.create();
    // then
    EzyWsHandler wsHandler = MethodInvoker.create().object(creator).method("newWsHandler").invoke(EzyWsHandler.class);
    WebSocketCreator webSocketCreator = MethodInvoker.create().object(creator).method("newWebSocketCreator").param(EzyWsHandler.class, wsHandler).invoke(WebSocketCreator.class);
    webSocketCreator.createWebSocket(null, null);
}
Also used : EzySimpleWebSocketSetting(com.tvd12.ezyfoxserver.setting.EzySimpleWebSocketSetting) EzyWsHandler(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) EzyWebSocketServerCreator(com.tvd12.ezyfoxserver.nio.builder.impl.EzyWebSocketServerCreator) Test(org.testng.annotations.Test)

Example 2 with EzyWsHandler

use of com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler in project ezyfox-server by youngmonkeys.

the class EzyWebSocketServerCreator method create.

public Server create() {
    ContextHandler wsContextHandler = new ContextHandler("/ws");
    wsContextHandler.setAllowNullPathInfo(true);
    EzyWsHandler wsHandler = newWsHandler();
    wsContextHandler.setHandler(newWebSocketHandler(wsHandler));
    QueuedThreadPool threadPool = new QueuedThreadPool(16, 8);
    threadPool.setName("ezyfox-ws-handler");
    Server server = new Server(threadPool);
    ContextHandlerCollection contextHandlers = new ContextHandlerCollection();
    contextHandlers.addHandler(wsContextHandler);
    if (setting.isManagementEnable()) {
        contextHandlers.addHandler(managementHandler());
    }
    server.setHandler(contextHandlers);
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme("https");
    httpConfig.setSecurePort(setting.getSslPort());
    ServerConnector wsConnector = new ServerConnector(server);
    wsConnector.setPort(setting.getPort());
    wsConnector.setHost(setting.getAddress());
    server.addConnector(wsConnector);
    configServer(server, httpConfig, wsConnector);
    return server;
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) EzyWsHandler(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration)

Example 3 with EzyWsHandler

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

use of com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler in project ezyfox-server by youngmonkeys.

the class EzyWebSocketSecureServerCreatorTest method test.

@Test
public void test() throws Exception {
    SSLContext sslContext = SSLContext.getDefault();
    EzySimpleWebSocketSetting webSocketSetting = new EzySimpleWebSocketSetting();
    EzyWebSocketSecureServerCreator creator = new EzyWebSocketSecureServerCreator(sslContext);
    creator.setting(webSocketSetting);
    creator.create();
    EzyWsHandler wsHandler = MethodInvoker.create().object(creator).method("newWsHandler").invoke(EzyWsHandler.class);
    WebSocketCreator webSocketCreator = MethodInvoker.create().object(creator).method("newWebSocketCreator").param(EzyWsHandler.class, wsHandler).invoke(WebSocketCreator.class);
    webSocketCreator.createWebSocket(null, null);
}
Also used : EzySimpleWebSocketSetting(com.tvd12.ezyfoxserver.setting.EzySimpleWebSocketSetting) EzyWsHandler(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler) EzyWebSocketSecureServerCreator(com.tvd12.ezyfoxserver.nio.builder.impl.EzyWebSocketSecureServerCreator) SSLContext(javax.net.ssl.SSLContext) WebSocketCreator(org.eclipse.jetty.websocket.servlet.WebSocketCreator) BaseTest(com.tvd12.test.base.BaseTest) Test(org.testng.annotations.Test)

Example 5 with EzyWsHandler

use of com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler in project ezyfox-server by youngmonkeys.

the class EzyWsHandlerTest method test.

@Test
public void test() throws Exception {
    EzySimpleSessionManagementSetting sessionManagementSetting = new EzySimpleSessionManagementSetting();
    EzyNioSessionManager sessionManager = (EzyNioSessionManager) EzyNioSessionManagerImpl.builder().maxRequestPerSecond(new EzySimpleSessionManagementSetting.EzySimpleMaxRequestPerSecond()).tokenGenerator(new EzySimpleSessionTokenGenerator()).build();
    EzyCodecFactory codecFactory = mock(EzyCodecFactory.class);
    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();
    EzyHandlerGroupManager handlerGroupManager = EzyHandlerGroupManagerImpl.builder().handlerGroupBuilderFactory(handlerGroupBuilderFactory).build();
    EzySocketDataReceiver socketDataReceiver = EzySocketDataReceiver.builder().threadPoolSize(1).handlerGroupManager(handlerGroupManager).build();
    EzyWsHandler handler = EzyWsHandler.builder().sessionManagementSetting(sessionManagementSetting).sessionManager(sessionManager).handlerGroupManager(handlerGroupManager).socketDataReceiver(socketDataReceiver).build();
    Session session = mock(Session.class);
    handler.onConnect(session);
    handler.onMessage(session, "hello");
    handler.onMessage(session, "hello".getBytes(), 0, 5);
    handler.onError(session, new TimeoutException("timeout"));
    handler.onError(session, new IllegalStateException("maintain"));
    handler.onClose(session, 39999, "test");
    handler.onClose(session, 3000, "test");
    Session session2 = mock(Session.class);
    handler.onMessage(session2, "hello");
    handler.onMessage(session2, "hello".getBytes(), 0, 5);
    handler.onError(session2, new TimeoutException("timeout"));
    handler.onError(session2, new IllegalStateException("maintain"));
    handler.onClose(session2, 39999, "test");
    handler.onClose(session2, 3000, "test");
}
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) TimeoutException(java.util.concurrent.TimeoutException) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzySimpleStreamingSetting(com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting) EzyNioSessionManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager) EzyWsHandler(com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler) EzyCodecFactory(com.tvd12.ezyfoxserver.codec.EzyCodecFactory) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) EzySocketDataReceiver(com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver) ExecutorService(java.util.concurrent.ExecutorService) EzySimpleStatistics(com.tvd12.ezyfoxserver.statistics.EzySimpleStatistics) EzySimpleSessionTokenGenerator(com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator) 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)

Aggregations

EzyWsHandler (com.tvd12.ezyfoxserver.nio.websocket.EzyWsHandler)5 Test (org.testng.annotations.Test)4 BaseTest (com.tvd12.test.base.BaseTest)3 EzyNioSession (com.tvd12.ezyfoxserver.nio.entity.EzyNioSession)2 EzyNioSessionManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyNioSessionManager)2 EzySimpleWebSocketSetting (com.tvd12.ezyfoxserver.setting.EzySimpleWebSocketSetting)2 Session (org.eclipse.jetty.websocket.api.Session)2 WebSocketCreator (org.eclipse.jetty.websocket.servlet.WebSocketCreator)2 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)1 EzyCodecFactory (com.tvd12.ezyfoxserver.codec.EzyCodecFactory)1 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)1 EzyWebSocketSecureServerCreator (com.tvd12.ezyfoxserver.nio.builder.impl.EzyWebSocketSecureServerCreator)1 EzyWebSocketServerCreator (com.tvd12.ezyfoxserver.nio.builder.impl.EzyWebSocketServerCreator)1 EzyHandlerGroupBuilderFactory (com.tvd12.ezyfoxserver.nio.factory.EzyHandlerGroupBuilderFactory)1 EzySocketDataReceiver (com.tvd12.ezyfoxserver.nio.socket.EzySocketDataReceiver)1 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)1 EzySimpleSessionTokenGenerator (com.tvd12.ezyfoxserver.service.impl.EzySimpleSessionTokenGenerator)1 EzySimpleSessionManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleSessionManagementSetting)1 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)1 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)1