package pw.koj.jetstreem.core;

import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.nio.channels.ClosedChannelException;
import java.io.IOException;
import java.util.*;

public class StrmIOLoop extends Thread {
    private Selector selector;
    private StrmQueue taskQueue;

    private StrmIOLoop () {
        super();
        try {
            this.selector = Selector.open();
        } catch (IOException ex) {
            System.err.println("I/O error: " + ex.getMessage());
        }
        this.taskQueue = StrmQueue.getInstance();
    }

    public static StrmIOLoop initIOLoop() {
        StrmIOLoop loop = new StrmIOLoop();
        loop.start();
        return loop;
    }

    public void closeIOLoop() {
        this.interrupt();
    }

    public void run() {
        try {
            while (this.selector.select() > 0) {
                Set keys = this.selector.selectedKeys();
                for (Iterator<SelectionKey> it = keys.iterator(); it.hasNext(); ) {
                    SelectionKey key = it.next();
                    it.remove();
                    StrmIOTask task = (StrmIOTask)key.attachment();
                    taskQueue.push(task.strm(), task.func(), null);
                }
            }
        } catch (ClosedChannelException ex) {
            System.err.println("I/O channel is closed!");
        } catch (IOException ex) {
            System.err.println("I/O error: " + ex.getMessage());
        } finally {
            try {
                for (SelectionKey key: this.selector.keys()) {
                    key.channel().close();
                }
            } catch (IOException ex) {
                System.err.println("I/O error: " + ex.getMessage());
            }
        }
    }
}




@@ -301,16 +301,43 @@
 skQueue;
+%0A    private int ioWaitNum;
 %0A%0A    pr
@@ -590,24 +590,52 @@
 Instance();%0A
+        this.ioWaitNum = 0;%0A
     %7D%0A%0A    p
@@ -721,30 +721,8 @@
 ();%0A
-        loop.start();%0A
     
@@ -769,32 +769,32 @@
 closeIOLoop() %7B%0A
-
         this.int
@@ -802,32 +802,905 @@
 rrupt();%0A    %7D%0A%0A
+    public Selector selector() %7B%0A        return this.selector;%0A    %7D%0A%0A    public StrmQueue taskQueue() %7B%0A        return this.taskQueue;%0A    %7D%0A%0A    public int ioWaitNum() %7B%0A        return this.ioWaitNum;%0A    %7D%0A%0A    public void ioPush(SelectableChannel ch, Streem strm, StrmFunc cb, int key) %0A        throws ClosedChannelException %7B%0A        StrmIOTask task = new StrmIOTask(strm, cb);%0A        ch.register(this.selector, key, task);%0A    %7D%0A%0A    public void ioStart(Streem strm, SelectableChannel ch, StrmFunc cb, int key) %7B%0A        try %7B%0A            ioPush(ch, strm, cb, key);%0A            this.ioWaitNum++;%0A        %7D catch (ClosedChannelException ex) %7B%0A            System.err.println(%22Channel error: %22 + ex.getMessage());%0A        %7D%0A    %7D%0A%0A    public void ioStartRead(Streem strm, SelectableChannel ch, StrmFunc cb) %7B%0A        ioStart(strm, ch, cb, SelectionKey.IO_READ);%0A    %7D%0A%0A
     public void 

add misc methods in class StrmIOLoop