use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzyNioSocketAcceptor method doAcceptConnection.
private void doAcceptConnection(SocketChannel clientChannel) throws Exception {
clientChannel.configureBlocking(false);
clientChannel.socket().setTcpNoDelay(tcpNoDelay);
EzyChannel channel = new EzyNioSocketChannel(clientChannel);
EzyNioHandlerGroup handlerGroup = handlerGroupManager.newHandlerGroup(channel, EzyConnectionType.SOCKET);
EzyNioSession session = handlerGroup.getSession();
SelectionKey selectionKey = clientChannel.register(readSelector, SelectionKey.OP_READ);
session.setProperty(EzyNioSession.SELECTION_KEY, selectionKey);
}
use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiver method processReadBytes.
private void processReadBytes(SocketChannel channel, ByteBuffer buffer) throws Exception {
buffer.flip();
byte[] binary = new byte[buffer.limit()];
buffer.get(binary);
EzyNioHandlerGroup handlerGroup = handlerGroupManager.getHandlerGroup(channel);
if (handlerGroup != null) {
handlerGroup.fireBytesReceived(binary);
}
}
use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzyNioSocketAcceptorTest method acceptConnectionTest.
@Test
public void acceptConnectionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
Selector readSelector = Selector.open();
EzyNioSocketAcceptor sut = new EzyNioSocketAcceptor();
sut.setReadSelector(readSelector);
SocketChannel clientChannel = SocketChannel.open();
when(handlerGroupManager.newHandlerGroup(any(), any())).thenReturn(handlerGroup);
sut.setHandlerGroupManager(handlerGroupManager);
EzyNioSession session = mock(EzyNioSession.class);
when(handlerGroup.getSession()).thenReturn(session);
// when
MethodInvoker.create().object(sut).method("acceptConnection").param(SocketChannel.class, clientChannel).call();
// then
verify(handlerGroupManager, times(1)).newHandlerGroup(any(), any());
}
use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiverTest method udpReceiveButExceptionTest.
@Test
public void udpReceiveButExceptionTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
SocketChannel channel = mock(SocketChannel.class);
EzyMessage message = mock(EzyMessage.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
doThrow(new RuntimeException()).when(handlerGroup).fireMessageReceived(message);
when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
// when
sut.udpReceive(channel, message);
Thread.sleep(120);
// then
verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
verify(handlerGroup, times(1)).fireMessageReceived(message);
sut.destroy();
}
use of com.tvd12.ezyfoxserver.nio.handler.EzyNioHandlerGroup in project ezyfox-server by youngmonkeys.
the class EzySocketDataReceiverTest method udpReceiveTest.
@Test
public void udpReceiveTest() throws Exception {
// given
EzyHandlerGroupManager handlerGroupManager = mock(EzyHandlerGroupManager.class);
EzySocketDataReceiver sut = EzySocketDataReceiver.builder().handlerGroupManager(handlerGroupManager).threadPoolSize(1).build();
SocketChannel channel = mock(SocketChannel.class);
EzyMessage message = mock(EzyMessage.class);
EzyNioHandlerGroup handlerGroup = mock(EzyNioHandlerGroup.class);
when(handlerGroupManager.getHandlerGroup(channel)).thenReturn(handlerGroup);
// when
sut.udpReceive(channel, message);
Thread.sleep(120);
// then
verify(handlerGroupManager, times(1)).getHandlerGroup(channel);
verify(handlerGroup, times(1)).fireMessageReceived(message);
sut.destroy();
}
Aggregations