use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler in project ezyfox-server by youngmonkeys.
the class EzyUdpServerBootstrap method newUdpDataHandler.
private EzyNioUdpDataHandler newUdpDataHandler() {
int handlerThreadPoolSize = getSocketHandlerPoolSize();
EzySimpleNioUdpDataHandler handler = new EzySimpleNioUdpDataHandler(handlerThreadPoolSize);
handler.setResponseApi(getResponseApi());
handler.setDatagramChannelPool(udpChannelPool);
handler.setSessionManager(getSessionManager());
handler.setSocketDataReceiver(socketDataReceiver);
handler.setHandlerGroupManager(handlerGroupManager);
return handler;
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler 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();
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method handleUdpHandshakeTokenSizeGreaterThan512.
@Test
public void handleUdpHandshakeTokenSizeGreaterThan512() {
// 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(514);
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);
// 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();
sut.destroy();
}
use of com.tvd12.ezyfoxserver.nio.handler.EzySimpleNioUdpDataHandler 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.nio.handler.EzySimpleNioUdpDataHandler in project ezyfox-server by youngmonkeys.
the class EzySimpleNioUdpDataHandlerTest method testExceptionCase.
@Test
public void testExceptionCase() 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;
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);
when(handlerGroupManager.getHandlerGroup(packet.getAddress())).thenThrow(new IllegalArgumentException("test"));
handler.setHandlerGroupManager(handlerGroupManager);
handler.fireUdpPacketReceived(packet);
Thread.sleep(200);
handler.destroy();
}
Aggregations