// Path: src/main/java/com/wizzardo/epoll/Utils.java
//     public static int readInt(byte[] b, int offset) {
// //        if (b.length - offset < 4 || offset < 0)
// //            throw new IndexOutOfBoundsException("byte[].length = " + b.length + ", but offset = " + offset + " and we need 4 bytes");
// 
//         return ((b[offset] & 0xff) << 24) + ((b[offset + 1] & 0xff) << 16) + ((b[offset + 2] & 0xff) << 8) + ((b[offset + 3] & 0xff));
//     }


import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static com.wizzardo.epoll.Utils.readInt;
package com.wizzardo.epoll;



/**
 * @author: wizzardo
 * Date: 6/25/14
 */
public class IOThread<T extends Connection> extends EpollCore<T> {
    protected final int number;
    protected final int divider;
    protected T[] connections = (T[]) new Connection[1000];
    protected Map<Integer, T> newConnections = new ConcurrentHashMap<Integer, T>();
    protected LinkedHashMap<Long, T> timeouts = new LinkedHashMap<Long, T>();

    public IOThread(int number, int divider) {
        this.number = number;
        this.divider = divider;
        setName("IOThread-" + number);
    }

    @Override
    public void run() {
        byte[] events = new byte[this.events.capacity()];
//        System.out.println("start new ioThread");

        long prev = System.nanoTime();
        while (running) {
            this.events.position(0);
            long now = System.nanoTime() * 1000;
            long nowMinusSecond = now - 1_000_000_000_000L;// 1 sec
            int r = waitForEvents(500);
//                System.out.println("events length: "+r);
            this.events.limit(r);
            this.events.get(events, 0, r);
            int i = 0;
            while (i < r) {
                int event = events[i];
// Path: src/main/java/com/wizzardo/epoll/Utils.java
//     public static int readInt(byte[] b, int offset) {
// //        if (b.length - offset < 4 || offset < 0)
// //            throw new IndexOutOfBoundsException("byte[].length = " + b.length + ", but offset = " + offset + " and we need 4 bytes");
// 
//         return ((b[offset] & 0xff) << 24) + ((b[offset + 1] & 0xff) << 16) + ((b[offset + 2] & 0xff) << 8) + ((b[offset + 3] & 0xff));
//     }


// Path: src/main/java/com/wizzardo/epoll/IOThread.java
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static com.wizzardo.epoll.Utils.readInt;

package com.wizzardo.epoll;



/**
 * @author: wizzardo
 * Date: 6/25/14
 */
public class IOThread<T extends Connection> extends EpollCore<T> {
    protected final int number;
    protected final int divider;
    protected T[] connections = (T[]) new Connection[1000];
    protected Map<Integer, T> newConnections = new ConcurrentHashMap<Integer, T>();
    protected LinkedHashMap<Long, T> timeouts = new LinkedHashMap<Long, T>();

    public IOThread(int number, int divider) {
        this.number = number;
        this.divider = divider;
        setName("IOThread-" + number);
    }

    @Override
    public void run() {
        byte[] events = new byte[this.events.capacity()];
//        System.out.println("start new ioThread");

        long prev = System.nanoTime();
        while (running) {
            this.events.position(0);
            long now = System.nanoTime() * 1000;
            long nowMinusSecond = now - 1_000_000_000_000L;// 1 sec
            int r = waitForEvents(500);
//                System.out.println("events length: "+r);
            this.events.limit(r);
            this.events.get(events, 0, r);
            int i = 0;
            while (i < r) {
                int event = events[i];
                int fd = readInt(events, i + 1);