Search in sources :

Example 1 with EzyResponseApi

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

the class EzySendResponseImplTest method responseOneSuccessButIsPong.

@Test
public void responseOneSuccessButIsPong() throws Exception {
    // given
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(true);
    EzyResponseApi responseApi = spy(EzyAbstractResponseApi.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);
    // when
    cmd.execute(response, recipient, 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 2 with EzyResponseApi

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

the class EzySendResponseImplTest method responseOneSuccessButNotDebug.

@Test
public void responseOneSuccessButNotDebug() throws Exception {
    // given
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(false);
    EzyResponseApi responseApi = spy(EzyAbstractResponseApi.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);
    // when
    cmd.execute(response, recipient, 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 3 with EzyResponseApi

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

the class EzySendResponseImplTest method responseOneExceptionCase.

@Test
public void responseOneExceptionCase() throws Exception {
    EzySimpleSettings settings = new EzySimpleSettings();
    settings.setDebug(true);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    doThrow(new IllegalArgumentException()).when(responseApi).response(any(EzyPackage.class), anyBoolean());
    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);
    cmd.execute(response, recipient, false, false, EzyTransportType.TCP);
}
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 4 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, Collection<EzySession> recipients, boolean encrypted, boolean immediate, EzyTransportType transportType) {
    boolean success = false;
    EzyResponseApi responseApi = server.getResponseApi();
    EzyArray data = response.serialize();
    EzySimplePackage pack = newPackage(data, encrypted, transportType);
    pack.addRecipients(recipients);
    try {
        responseApi.response(pack, immediate);
        success = true;
    } catch (Exception e) {
        logger.error("send data: {}, to client: {} error", pack.getData(), getRecipientsNames(recipients), e);
    } finally {
        pack.release();
    }
    boolean debug = server.getSettings().isDebug();
    if (debug && success && !ignoredLogCommands.contains(response.getCommand())) {
        logger.debug("send to: {} data: {}", getRecipientsNames(recipients), data);
    }
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi)

Example 5 with EzyResponseApi

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

the class EzySimpleNioUdpDataHandlerTest method test.

@SuppressWarnings("rawtypes")
@Test
public void test() throws Exception {
    String sessionToken = "12345678";
    long sessionId = 12345L;
    EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(1);
    int tokenSize = sessionToken.length();
    int messageSize = 0;
    // sessionIdSize
    messageSize += 8;
    // tokenLengthSize
    messageSize += 2;
    // messageSize
    messageSize += tokenSize;
    ByteBuffer buffer = ByteBuffer.allocate(1 + 2 + messageSize);
    byte header = 0;
    header |= 1 << 5;
    buffer.put(header);
    buffer.putShort((short) messageSize);
    buffer.putLong(sessionId);
    buffer.putShort((short) tokenSize);
    buffer.put(sessionToken.getBytes());
    buffer.flip();
    byte[] bytes = EzyByteBuffers.getBytes(buffer);
    EzyUdpReceivedPacket packet = new EzySimpleUdpReceivedPacket(DatagramChannel.open(), new InetSocketAddress(12345), bytes);
    EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
    EzySessionManager sessionManager = mock(EzySessionManager.class);
    EzySession session = spy(EzyAbstractSession.class);
    session.setToken(sessionToken);
    when(sessionManager.getSession(sessionId)).thenReturn(session);
    EzyResponseApi responseApi = mock(EzyResponseApi.class);
    EzyDatagramChannelPool channelPool = new EzyDatagramChannelPool(1);
    handler.setHandlerGroupManager(handlerGroupManager);
    handler.setSessionManager(sessionManager);
    handler.setResponseApi(responseApi);
    handler.setDatagramChannelPool(channelPool);
    handler.fireUdpPacketReceived(packet);
    Thread.sleep(200);
    handler.destroy();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EzySessionManager(com.tvd12.ezyfoxserver.wrapper.EzySessionManager) ByteBuffer(java.nio.ByteBuffer) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzyResponseApi(com.tvd12.ezyfoxserver.api.EzyResponseApi) EzyUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzyUdpReceivedPacket) EzySimpleNioUdpDataHandler(com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler) EzyDatagramChannelPool(com.tvd12.ezyfoxserver.socket.EzyDatagramChannelPool) EzySimpleUdpReceivedPacket(com.tvd12.ezyfoxserver.socket.EzySimpleUdpReceivedPacket) EzyHandlerGroupManager(com.tvd12.ezyfoxserver.nio.wrapper.EzyHandlerGroupManager) 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