Search in sources :

Example 1 with EzySimplePackage

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

the class EzyWsResponseApiTest method responseTest.

@Test
public void responseTest() throws Exception {
    // given
    EzyObjectToStringEncoder encoder = mock(EzyObjectToStringEncoder.class);
    EzyWsResponseApi sut = new EzyWsResponseApi(encoder);
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(true);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.WEBSOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack);
    // then
    verify(encoder, times(1)).encode(data, String.class);
    verify(encoder, times(0)).toMessageContent(data);
    verify(encoder, times(0)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToStringEncoder(com.tvd12.ezyfox.codec.EzyObjectToStringEncoder) EzyWsResponseApi(com.tvd12.ezyfoxserver.api.EzyWsResponseApi) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) Test(org.testng.annotations.Test)

Example 2 with EzySimplePackage

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

the class EzySocketResponseApiTest method secureResponseImmediateTest.

@Test
public void secureResponseImmediateTest() throws Exception {
    // given
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    byte[] bytes = RandomUtil.randomShortByteArray();
    EzyObjectToByteEncoder encoder = mock(EzyObjectToByteEncoder.class);
    when(encoder.toMessageContent(data)).thenReturn(bytes);
    EzySocketResponseApi sut = new EzySocketResponseApi(encoder);
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(true);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack, true);
    // then
    verify(encoder, times(1)).toMessageContent(data);
    verify(encoder, times(sessionCount)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToByteEncoder(com.tvd12.ezyfox.codec.EzyObjectToByteEncoder) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySocketResponseApi(com.tvd12.ezyfoxserver.api.EzySocketResponseApi) Test(org.testng.annotations.Test)

Example 3 with EzySimplePackage

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

the class EzySocketResponseApiTest method normalResponseTest.

@Test
public void normalResponseTest() throws Exception {
    // given
    EzyArray data = EzyEntityFactory.EMPTY_ARRAY;
    EzyObjectToByteEncoder encoder = mock(EzyObjectToByteEncoder.class);
    byte[] bytes = RandomUtil.randomShortByteArray();
    when(encoder.encode(data)).thenReturn(bytes);
    EzySocketResponseApi sut = new EzySocketResponseApi(encoder);
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(false);
    pack.setTransportType(EzyTransportType.TCP);
    int sessionCount = RandomUtil.randomSmallInt() + 1;
    List<EzySession> sessions = RandomUtil.randomList(sessionCount, () -> {
        EzySession session = mock(EzySession.class);
        when(session.getConnectionType()).thenReturn(EzyConnectionType.SOCKET);
        return session;
    });
    pack.addRecipients(sessions);
    // when
    sut.response(pack);
    // then
    verify(encoder, times(1)).encode(data);
    verify(encoder, times(0)).toMessageContent(data);
    verify(encoder, times(0)).encryptMessageContent(any(byte[].class), any(byte[].class));
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObjectToByteEncoder(com.tvd12.ezyfox.codec.EzyObjectToByteEncoder) EzySession(com.tvd12.ezyfoxserver.entity.EzySession) EzySocketResponseApi(com.tvd12.ezyfoxserver.api.EzySocketResponseApi) Test(org.testng.annotations.Test)

Example 4 with EzySimplePackage

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

the class EzySendResponseImpl method newPackage.

protected EzySimplePackage newPackage(EzyArray data, boolean encrypted, EzyTransportType transportType) {
    EzySimplePackage pack = new EzySimplePackage();
    pack.setData(data);
    pack.setEncrypted(encrypted);
    pack.setTransportType(transportType);
    return pack;
}
Also used : EzySimplePackage(com.tvd12.ezyfoxserver.socket.EzySimplePackage)

Example 5 with EzySimplePackage

use of com.tvd12.ezyfoxserver.socket.EzySimplePackage 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)

Aggregations

EzySimplePackage (com.tvd12.ezyfoxserver.socket.EzySimplePackage)8 EzyArray (com.tvd12.ezyfox.entity.EzyArray)7 Test (org.testng.annotations.Test)5 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 EzyObjectToByteEncoder (com.tvd12.ezyfox.codec.EzyObjectToByteEncoder)3 EzySocketResponseApi (com.tvd12.ezyfoxserver.api.EzySocketResponseApi)3 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)2 EzyObjectToStringEncoder (com.tvd12.ezyfox.codec.EzyObjectToStringEncoder)1 EzyWsResponseApi (com.tvd12.ezyfoxserver.api.EzyWsResponseApi)1 BaseTest (com.tvd12.test.base.BaseTest)1