Search in sources :

Example 6 with EzyResponseApi

use of com.tvd12.ezyfoxserver.api.EzyResponseApi 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 7 with EzyResponseApi

use of com.tvd12.ezyfoxserver.api.EzyResponseApi in project ezyfox-server by youngmonkeys.

the class EzySendResponseImpl method execute.

@Override
public void execute(EzyResponse response, EzySession recipient, boolean encrypted, boolean immediate, EzyTransportType transportType) {
    boolean success = false;
    EzyResponseApi responseApi = server.getResponseApi();
    EzyArray data = response.serialize();
    EzySimplePackage pack = newPackage(data, encrypted, transportType);
    pack.addRecipient(recipient);
    try {
        responseApi.response(pack, immediate);
        success = true;
    } catch (Exception e) {
        logger.error("send data: {}, to client: {} error", pack.getData(), recipient.getName(), e);
    } finally {
        pack.release();
    }
    boolean debug = server.getSettings().isDebug();
    if (debug && success && !ignoredLogCommands.contains(response.getCommand())) {
        logger.debug("send to: {} data: {}", recipient.getName(), data);
    }
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi)

Example 8 with EzyResponseApi

use of com.tvd12.ezyfoxserver.api.EzyResponseApi in project ezyfox-server by youngmonkeys.

the class EzySimpleNioUdpDataHandlerTest method handleUdpHandshakeSessionTokenIsNotMatch.

@SuppressWarnings("rawtypes")
@Test
public void handleUdpHandshakeSessionTokenIsNotMatch() throws Exception {
    // given
    EzySimpleNioUdpDataHandler sut = new EzySimpleNioUdpDataHandler(1);
    DatagramChannel channel = mock(DatagramChannel.class);
    InetSocketAddress address = new InetSocketAddress(3005);
    EzyMessage message = mock(EzyMessage.class);
    String sessionToken = RandomUtil.randomAlphabetString(8);
    long sessionId = 12345L;
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    when(message.getContent()).thenReturn(bytes);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    sut.setSessionManager(sessionManager);
    EzySession session = mock(EzySession.class);
    when(session.getToken()).thenReturn(RandomUtil.randomShortAlphabetString());
    when(sessionManager.getSession(sessionId)).thenReturn(session);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    sut.setResponseApi(responseApi);
    // when
    MethodInvoker.create().object(sut).method("handleUdpHandshake").param(DatagramChannel.class, channel).param(InetSocketAddress.class, address).param(EzyMessage.class, message).call();
    // then
    verify(message, times(1)).getContent();
    verify(sessionManager, times(1)).getSession(sessionId);
    verify(session, times(1)).getToken();
    verify(responseApi, times(1)).response(any());
    sut.destroy();
}
Also used : EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) EzyMessage(com.tvd12.ezyfox.codec.EzyMessage) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) Test(org.testng.annotations.Test)

Example 9 with EzyResponseApi

use of com.tvd12.ezyfoxserver.api.EzyResponseApi in project ezyfox-server by youngmonkeys.

the class EzySendResponseImplTest method responseMultiSuccessCase.

@Test
public void responseMultiSuccessCase() throws Exception {
    // when
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(true);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzySimpleServer server = new EzySimpleServer();
    server.setResponseApi(responseApi);
    server.setSettings(settings);
    EzySendResponseImpl cmd = new EzySendResponseImpl(server);
    EzyResponse response = new EzySimpleResponse(EzyCommand.APP_REQUEST);
    EzySession recipient = spy(EzyAbstractSession.class);
    List<EzySession> recipients = Collections.singletonList(recipient);
    // when
    cmd.execute(response, recipients, false, false, EzyTransportType.TCP);
    // then
    verify(responseApi, times(1)).response(any(EzyPackage.class), anyBoolean());
}
Also used : EzySimpleResponse(com.tvd12.ezyfoxserver.response.EzySimpleResponse) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyPackage(com.tvd12.ezyfoxserver.response.EzyPackage) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySendResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl) Test(org.testng.annotations.Test)

Example 10 with EzyResponseApi

use of com.tvd12.ezyfoxserver.api.EzyResponseApi in project ezyfox-server by youngmonkeys.

the class EzySendResponseImplTest method responseMultiSuccessCaseButIsPong.

@Test
public void responseMultiSuccessCaseButIsPong() throws Exception {
    // when
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(true);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzySimpleServer server = new EzySimpleServer();
    server.setResponseApi(responseApi);
    server.setSettings(settings);
    EzySendResponseImpl cmd = new EzySendResponseImpl(server);
    EzyResponse response = new EzySimpleResponse(EzyCommand.PONG);
    EzySession recipient = spy(EzyAbstractSession.class);
    List<EzySession> recipients = Collections.singletonList(recipient);
    // when
    cmd.execute(response, recipients, false, false, EzyTransportType.TCP);
    // then
    verify(responseApi, times(1)).response(any(EzyPackage.class), anyBoolean());
}
Also used : EzySimpleResponse(com.tvd12.ezyfoxserver.response.EzySimpleResponse) EzySimpleServer(com.tvd12.ezyfoxserver.EzySimpleServer) EzyPackage(com.tvd12.ezyfoxserver.response.EzyPackage) EzySimpleSettings(com.tvd12.ezyfoxserver.setting.EzySimpleSettings) EzyResponse(com.tvd12.ezyfoxserver.response.EzyResponse) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySendResponseImpl(com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl) Test(org.testng.annotations.Test)

Aggregations

EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)15 Test (org.testng.annotations.Test)11 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)10 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)10 EzySimpleSettings (com.tvd12.ezyfoxserver.setting.EzySimpleSettings)10 EzySendResponseImpl (com.tvd12.ezyfoxserver.command.impl.EzySendResponseImpl)8 EzyResponse (com.tvd12.ezyfoxserver.response.EzyResponse)8 EzySimpleResponse (com.tvd12.ezyfoxserver.response.EzySimpleResponse)8 EzyPackage (com.tvd12.ezyfoxserver.response.EzyPackage)7 EzyHandlerGroupManager (com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager)3 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)3 ByteBuffer (java.nio.ByteBuffer)3 EzyArray (com.tvd12.ezyfox.entity.EzyArray)2 EzyStreamingApi (com.tvd12.ezyfoxserver.api.EzyStreamingApi)2 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)2 EzyNioServerBootstrap (com.tvd12.ezyfoxserver.nio.EzyNioServerBootstrap)2 EzySimpleNioUdpDataHandler (com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler)2 EzySimpleStreamingSetting (com.tvd12.ezyfoxserver.setting.EzySimpleStreamingSetting)2 EzySimplePackage (com.tvd12.ezyfoxserver.socket.EzySimplePackage)2 InetSocketAddress (java.net.InetSocketAddress)2