<commit_before>package com.wizzardo.httpserver;

import com.wizzardo.epoll.EpollServer;
import com.wizzardo.httpserver.request.Header;
import com.wizzardo.httpserver.response.Response;
import simplehttpserver.concurrent.NonBlockingQueue;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author: moxa
 * Date: 11/5/13
 */
public class HttpServer extends EpollServer<HttpConnection> {

    private NonBlockingQueue<Runnable> queue = new NonBlockingQueue<Runnable>();

    public HttpServer(int port) {
        this(null, port);
    }

    public HttpServer(String host, int port) {
        super(host, port);
        for (int i = 0; i < 16; i++) {
            new Worker(queue, "worker_" + i);
        }
    }

    @Override
    protected HttpConnection createConnection(int fd, int ip, int port) {
        return new HttpConnection(this, fd, ip, port);
    }

    @Override
    public void onRead(final HttpConnection connection) {

        queue.add(new Runnable() {
            @Override
            public void run() {
                ByteBuffer b;
                try {

                    while ((b = read(connection, connection.getBufferSize())).limit() > 0) {
                        if (connection.check(b))
                            break;
                    }
                    if (!connection.isHttpReady())
                        return;

                } catch (IOException e) {
                    e.printStackTrace();
                    connection.reset("exception while reading");
                    close(connection);
                    return;
                }

                connection.reset("read headers");

                try {
                    Response response = handleRequest(connection);

                    connection.write(response.toReadableBytes());
                } catch (Throwable t) {
                    t.printStackTrace();
                    //TODO render error page
                }

                onWrite(connection);
            }
        });

    }
//    final byte[] bytes = FileTools.bytes(new File("/usr/share/nginx/html/resources/data/img.jpg"));

    public Response handleRequest(HttpConnection connection) {
        return new Response()
                .appendHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_KEEP_ALIVE)
                .appendHeader(Header.KEY_CONTENT_TYPE, Header.VALUE_CONTENT_TYPE_HTML_UTF8)
                .setBody("It's alive!".getBytes());
//        return new Response()
//                .appendHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_KEEP_ALIVE)
//                .appendHeader(Header.KEY_CONTENT_TYPE, "image/jpg")
//                .setBody(bytes);
    }


    @Override
    public void onConnect(HttpConnection httpConnection) {
//        System.out.println("new connection " + httpConnection);
    }

    @Override
    public void onDisconnect(HttpConnection httpConnection) {
//        System.out.println("close " + httpConnection);
    }

    public static void main(String[] args) {
        HttpServer server = new HttpServer(8084);
        server.start();
    }
}<commit_msg>new epoll-lib implementation<commit_after>package com.wizzardo.httpserver;

import com.wizzardo.epoll.EpollServer;
import com.wizzardo.epoll.IOThread;
import com.wizzardo.httpserver.request.Header;
import com.wizzardo.httpserver.response.Response;
import simplehttpserver.concurrent.NonBlockingQueue;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author: moxa
 * Date: 11/5/13
 */
public class HttpServer extends EpollServer<HttpConnection> {

    private NonBlockingQueue<Runnable> queue = new NonBlockingQueue<Runnable>();
    private int workersCount;

    public HttpServer(int port) {
        this(null, port);
    }

    public HttpServer(String host, int port) {
        this(host, port, 16);
    }

    public HttpServer(String host, int port, int workersCount) {
        super(host, port);
        this.workersCount = workersCount;
        System.out.println("worker count: " + workersCount);
        for (int i = 0; i < workersCount; i++) {
            new Worker(queue, "worker_" + i);
        }
    }

    @Override
    protected HttpConnection createConnection(int fd, int ip, int port) {
        return new HttpConnection(fd, ip, port);
    }

    @Override
    protected IOThread<HttpConnection> createIOThread() {
        return new HttpIOThread();
    }

    private class HttpIOThread extends IOThread<HttpConnection> {

        @Override
        public void onRead(final HttpConnection connection) {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    ByteBuffer b;
                    try {

                        while ((b = read(connection, connection.getBufferSize())).limit() > 0) {
                            if (connection.check(b))
                                break;
                        }
                        if (!connection.isHttpReady())
                            return;

                    } catch (IOException e) {
                        e.printStackTrace();
                        connection.reset("exception while reading");
                        connection.close();
                        return;
                    }

                    connection.reset("read headers");

                    try {
                        Response response = handleRequest(connection);

                        connection.write(response.toReadableBytes());
                    } catch (Throwable t) {
                        t.printStackTrace();
                        //TODO render error page
                    }

                    onWrite(connection);
                }
            };
            if (workersCount > 0)
                queue.add(runnable);
            else
                runnable.run();
        }

        @Override
        public void onConnect(HttpConnection httpConnection) {
//        System.out.println("new connection " + httpConnection);
        }

        @Override
        public void onDisconnect(HttpConnection httpConnection) {
//        System.out.println("close " + httpConnection);
        }

//    final byte[] bytes = FileTools.bytes(new File("/usr/share/nginx/html/resources/data/img.jpg"));

        public Response handleRequest(HttpConnection connection) {
            return new Response()
                    .appendHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_KEEP_ALIVE)
                    .appendHeader(Header.KEY_CONTENT_TYPE, Header.VALUE_CONTENT_TYPE_HTML_UTF8)
                    .setBody("It's alive!".getBytes());
//        return new Response()
//                .appendHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_KEEP_ALIVE)
//                .appendHeader(Header.KEY_CONTENT_TYPE, "image/jpg")
//                .setBody(bytes);
        }
    }

    public static void main(String[] args) {
        HttpServer server = new HttpServer(null, 8084, args.length > 0 ? Integer.parseInt(args[0]) : 16);
        server.start();
    }
}
{'fraction_non_alphanumeric': 0.10026870315372649, 'fraction_numerical': 0.004666949512091642, 'mean_word_length': 1.76357952325127, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6334914', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1754, 'n_words': 382}