<commit_before>/*************************************************************************
    > File Name: connection.cpp
    > Author: sudoku.huang
    > Mail: <EMAIL> 
    > Created Time: Monday, December 26, 2016 PM03:06:43 CST
 ************************************************************************/

#include "connection.h"
#include <ulog/ulog.h>
#include <utools/ustring.h>
#include <stdlib.h>
#include <string.h>
#include "lbsdb.h"

const CLbsConnection::handler_type CLbsConnection::handlers[] = {
    {"/favicon.ico",    &CLbsConnection::favicon},
    {"/tysetcpu",       &CLbsConnection::setcpu},
    {"/tyshowlive",     &CLbsConnection::dump},
    {NULL, NULL},
};

CLbsConnection::CLbsConnection(const LbsConf_t & config, CLbsMonitor & monitor, 
        uschedule & schudule, int fd, const char * ip)
    : config_(config)
    , monitor_(monitor)
    , schudule_(schudule)
    , socket_(1024, fd, &schudule_)
    , peerip_(ip)
    , http_(socket_, *this, false, false, true)
{
    socket_.set_timeout(30 * 1000); // 60s
}

CLbsConnection::~CLbsConnection()
{
}

void CLbsConnection::run()
{
    http_.run();
    monitor_.onDisconnect(socket_.socket());
    ulog(ulog_debug, "disconnect to fd:%d, (%s)!\n", 
        socket_.socket(), peerip_.c_str());
    ::close(socket_.socket());
    delete this;
}

int CLbsConnection::onhttp(const uhttprequest & request,
    uhttpresponse & response, int errcode)
{
    if (errcode == uhttp::en_socket_reset) {
        ulog(ulog_debug, "socket reset by peer(%s)!\n", peerip_.c_str());
        return -1;
    }

    if (request.method() == uhttp_method_unknown) {
        ulog(ulog_error, "unknown request from (%s)!\n", peerip_.c_str());
        return -1;
    }
    
    const std::string & path = request.uri().path();
    const CLbsConnection::handler_type * h;
    int ret = 0;
    
    for (h = handlers; h->cmd != NULL; h ++) {
        if (strcmp(h->cmd, path.c_str()) == 0) {
            ulog(ulog_debug, "find handler for path:%s\n", h->cmd);
            break;
        }
    }
    
    if (h->cmd == NULL) {
        if (get(request, response, errcode) != 0) {
            ulog(ulog_error, "unknown request from(%s):%s\n", 
                peerip_.c_str(), path.c_str());
            ret = notfind(request, response, errcode);
        } else {
            ret = 0;
        }
    } else {
        handler hf = h->func;
        ret = (this->*hf)(request, response, errcode);
    }<commit_msg>1. fix tcp timewait/fin_wait status.<commit_after>/*************************************************************************
    > File Name: connection.cpp
    > Author: sudoku.huang
    > Mail: <EMAIL> 
    > Created Time: Monday, December 26, 2016 PM03:06:43 CST
 ************************************************************************/

#include "connection.h"
#include <ulog/ulog.h>
#include <utools/ustring.h>
#include <stdlib.h>
#include <string.h>
#include "lbsdb.h"

const CLbsConnection::handler_type CLbsConnection::handlers[] = {
    {"/favicon.ico",    &CLbsConnection::favicon},
    {"/tysetcpu",       &CLbsConnection::setcpu},
    {"/tyshowlive",     &CLbsConnection::dump},
    {NULL, NULL},
};

CLbsConnection::CLbsConnection(const LbsConf_t & config, CLbsMonitor & monitor, 
        uschedule & schudule, int fd, const char * ip)
    : config_(config)
    , monitor_(monitor)
    , schudule_(schudule)
    , socket_(1024, fd, &schudule_)
    , peerip_(ip)
    , http_(socket_, *this, false, false, true)
{
    socket_.set_timeout(30 * 1000); // 60s
}

CLbsConnection::~CLbsConnection()
{
}

void CLbsConnection::run()
{
    http_.run();
    monitor_.onDisconnect(socket_.socket());
    ulog(ulog_debug, "disconnect to fd:%d, (%s)!\n", 
        socket_.socket(), peerip_.c_str());

    if (socket_.good()) {
        // if connection is ok, wait 20ms.
        // if client don't close, server close it active.
        char buf;
        socket_.set_timeout(20);
        socket_.read(&buf, 1);
    }
    
    ::close(socket_.socket());
    delete this;
}

int CLbsConnection::onhttp(const uhttprequest & request,
    uhttpresponse & response, int errcode)
{
    if (errcode == uhttp::en_socket_reset) {
        ulog(ulog_debug, "socket reset by peer(%s)!\n", peerip_.c_str());
        return -1;
    }

    if (request.method() == uhttp_method_unknown) {
        ulog(ulog_error, "unknown request from (%s)!\n", peerip_.c_str());
        return -1;
    }
    
    const std::string & path = request.uri().path();
    const CLbsConnection::handler_type * h;
    int ret = 0;
    
    for (h = handlers; h->cmd != NULL; h ++) {
        if (strcmp(h->cmd, path.c_str()) == 0) {
            ulog(ulog_debug, "find handler for path:%s\n", h->cmd);
            break;
        }
    }
    
    if (h->cmd == NULL) {
        if (get(request, response, errcode) != 0) {
            ulog(ulog_error, "unknown request from(%s):%s\n", 
                peerip_.c_str(), path.c_str());
            ret = notfind(request, response, errcode);
        } else {
            ret = 0;
        }
    } else {
        handler hf = h->func;
        ret = (this->*hf)(request, response, errcode);
    }
    
{'fraction_non_alphanumeric': 0.208195766168188, 'fraction_numerical': 0.012818022917071277, 'mean_word_length': 2.8119911176905994, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '7098627', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1682, 'n_words': 359}