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