diff --git a/src/org/redkale/net/nio/AbstractLoop.java b/src/org/redkale/net/nio/AbstractLoop.java new file mode 100644 index 000000000..4c8b7c08f --- /dev/null +++ b/src/org/redkale/net/nio/AbstractLoop.java @@ -0,0 +1,54 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.net.nio; + +/** + * + * @author zhangjx + */ +public abstract class AbstractLoop extends Thread { + + private volatile Thread localThread; + + protected volatile boolean closed; + + protected String name; + + protected AbstractLoop(String name) { + this.name = name; + } + + @Override + public final void run() { + this.localThread = Thread.currentThread(); + beforeLoop(); + while (!closed) { + if (Thread.currentThread().isInterrupted()) break; + try { + doLoop(); + } catch (Throwable e) { + e.printStackTrace(); + } + } + afterLoop(); + } + + protected void beforeLoop() { + } + + protected abstract void doLoop(); + + protected void afterLoop() { + } + + public boolean isSameThread() { + return localThread == Thread.currentThread(); + } + + public void shutdown() { + this.closed = true; + } +} diff --git a/src/org/redkale/net/nio/NioEventLoop.java b/src/org/redkale/net/nio/NioEventLoop.java new file mode 100644 index 000000000..f63227761 --- /dev/null +++ b/src/org/redkale/net/nio/NioEventLoop.java @@ -0,0 +1,114 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.net.nio; + +import java.io.IOException; +import java.nio.channels.*; +import java.util.*; + +/** + * + * @author zhangjx + */ +public abstract class NioEventLoop extends AbstractLoop { + + protected final Selector selector; + + public NioEventLoop(String name) { + super(name); + try { + this.selector = Selector.open(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void processKey(SelectionKey key) { + if (key == null || !key.isValid()) return; + if (key.isAcceptable()) { + try { + acceptOP(key); + } catch (Throwable e) { + failedOP(key, e); + } + } else if (key.isConnectable()) { + try { + connectOP(key); + } catch (Throwable e) { + failedOP(key, e); + } + } else if (key.isReadable()) { + try { + readOP(key); + } catch (Throwable e) { + failedOP(key, e); + } + + } else if (key.isWritable()) { + try { + writeOP(key); + } catch (Throwable e) { + failedOP(key, e); + } + } + } + + @Override + protected final void doLoop() { + try { + doLoopProcessing(); + } catch (Throwable e) { + e.printStackTrace(); + } + + try { + selector.select(getSelectorTimeout()); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + Set selectedKeys = selector.selectedKeys(); + synchronized (selectedKeys) { + Iterator iter = selectedKeys.iterator(); + while (iter.hasNext()) { + SelectionKey key = (SelectionKey) iter.next(); + iter.remove(); + processKey(key); + } + } + } catch (ClosedSelectorException e) { + // do nothing + } + } + + protected long getSelectorTimeout() { + return 10; + } + + protected abstract void doLoopProcessing(); + + protected void acceptOP(SelectionKey key) { + throw new RuntimeException("Accept operation is not implemented!"); + } + + protected void connectOP(SelectionKey key) throws IOException { + throw new RuntimeException("Connect operation is not implemented!"); + } + + protected void readOP(SelectionKey key) throws IOException { + throw new RuntimeException("Accept operation is not implemented!"); + } + + protected void writeOP(SelectionKey key) throws IOException { + throw new RuntimeException("Accept operation is not implemented!"); + } + + protected void failedOP(SelectionKey key, Throwable e) { + // ignore the errors by default + } + +} diff --git a/src/org/redkale/net/nio/NioWorkerThread.java b/src/org/redkale/net/nio/NioWorkerThread.java new file mode 100644 index 000000000..311f337a1 --- /dev/null +++ b/src/org/redkale/net/nio/NioWorkerThread.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.redkale.net.nio; + +/** + * + * @author zhangjx + */ +public class NioWorkerThread extends NioEventLoop { + + public NioWorkerThread(String name) { + super(name); + } + + @Override + protected void doLoopProcessing() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +}