public class NioEventLoop_ServerTest {
private Selector selector(NioEventLoop eventLoop) throws NoSuchFieldException {
Class<?> clazz = eventLoop.getClass();
Field field = null;
try {
field = clazz.getDeclaredField("selector");
} catch (NoSuchFieldException e) {
field = clazz.getDeclaredField("unwrappedSelector");
}
field.setAccessible(true);
try {
return (Selector) field.get(eventLoop);
} catch (IllegalAccessException e) {
throw new NoSuchFieldException(e.getMessage());
}
}
private class SocketChannelHandler implements NioTask<SelectableChannel> {
private NioEventLoop eventLoop;
public SocketChannelHandler(NioEventLoop eventLoop) {
this.eventLoop = eventLoop;
}
@Override
public void channelReady(SelectableChannel ch, SelectionKey key) throws Exception {
System.out.println("channelReady...");
if (key.isAcceptable()) {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
try {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
socketChannel.configureBlocking(false);
eventLoop.register(socketChannel, SelectionKey.OP_READ, this);
}
} catch (ClosedChannelException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (key.isReadable()) {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(256);
try {
int nbytes = socketChannel.read(buffer);
System.out.println(nbytes + ", " + buffer.toString());
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void channelUnregistered(SelectableChannel ch, Throwable cause) throws Exception {
System.out.println("channelUnregistered...");
}
}
@Test
public void test() throws Exception {
System.setProperty("io.netty.noKeySetOptimization", "true");
SelectorProvider selectorProvider = SelectorProvider.provider();
SelectStrategyFactory selectStrategyFactory = DefaultSelectStrategyFactory.INSTANCE;
RejectedExecutionHandler rejectedExecutionHandler = RejectedExecutionHandlers.reject();
Executor executor = new ThreadPerTaskExecutor(new DefaultThreadFactory(getClass()));
final NioEventLoop eventLoop = new NioEventLoop(null, executor, selectorProvider,
selectStrategyFactory.newSelectStrategy(), rejectedExecutionHandler);
Selector selector = selector(eventLoop);
System.out.println(selector.getClass());
ServerSocketChannel channel0 = null;
try {
channel0 = ServerSocketChannel.open();
System.out.println(channel0.getClass().getName());
} catch (IOException e) {
e.printStackTrace();
}
try {
channel0.configureBlocking(false);
} catch (IOException e) {
e.printStackTrace();
}
try {
channel0.socket().bind(new InetSocketAddress(1239));
} catch (IOException e) {
e.printStackTrace();
}
// try {
// SelectionKey key = channel0.register(selector, SelectionKey.OP_ACCEPT);
// System.out.println(key.interestOps());
// } catch (ClosedChannelException e) {
// e.printStackTrace();
// }
eventLoop.register(channel0, SelectionKey.OP_ACCEPT, new SocketChannelHandler(eventLoop));
Runnable worker = new Runnable() {
@Override
public void run() {
System.out.println("running...");
}
};
eventLoop.execute(worker);
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}