<commit_before>//******************************************************************
//
// Copyright 2014 Intel Corporation.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "WrapResource.h"
#include "RestInput.h"
#include "LineInput.h"
#include "OICMiddle.h"

using namespace std;

#define BUFLEN 10000
#define MAX_CONNS 5

static bool enableDebug = false; // set to true to print debug messages

void printDebugMessage(std::string message)
{
    if (enableDebug) {
        cout << "RestInput: " << message  << endl;
    }
}

RestInput::RestInput(LineInput *lineInput) : m_lineInput(lineInput)
{
    m_data = (char*)malloc(BUFLEN);
    m_thread = new std::thread[MAX_CONNS];
    m_threadCount = 0;
}

bool RestInput::init()
{
    m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (m_sockfd < 0) {
        cerr << "Failed to open socket. Exiting" << endl;
        return false;
    }
    m_port = 1441; //listening on port 1441

    m_serverAddr.sin_family = AF_INET;
    m_serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    m_serverAddr.sin_port = htons(m_port);

    if (::bind(m_sockfd, (struct sockaddr*)&m_serverAddr, sizeof m_serverAddr) < 0) {
        cerr << "Failed to bind. Exiting " << endl;
        return false;
    }

    listen(m_sockfd, MAX_CONNS);
    startAccept(m_sockfd);
    return true;
}

// accept incoming connection(s)
void RestInput::startAccept(int &sockfd)
{
    if (m_threadCount >= MAX_CONNS) {
        cerr << " Max # of connections reached. Skipping " << endl;
        return;
    } else {
        while (true) {
            int connfd = accept(sockfd, (struct sockaddr *)NULL, NULL);
            if (connfd < 0) {
                cerr << " Failed to accept incoming connection " << endl;
                return;
            }
            int n = read(connfd, m_data, BUFLEN);
            if (n < 0) {
                cerr << "Failed to read from socket" << endl;
                return;
            }
            startThread();
        }
    }
}

// start client thread
void RestInput::startThread()
{
    std::thread t(&RestInput::processClient, this);<commit_msg>Close fd after use in RestInput<commit_after>//******************************************************************
//
// Copyright 2014 Intel Corporation.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include "WrapResource.h"
#include "RestInput.h"
#include "LineInput.h"
#include "OICMiddle.h"

using namespace std;

#define BUFLEN 10000
#define MAX_CONNS 5

static bool enableDebug = false; // set to true to print debug messages

void printDebugMessage(std::string message)
{
    if (enableDebug) {
        cout << "RestInput: " << message  << endl;
    }
}

RestInput::RestInput(LineInput *lineInput) : m_lineInput(lineInput)
{
    m_data = (char*)malloc(BUFLEN);
    m_thread = new std::thread[MAX_CONNS];
    m_threadCount = 0;
}

bool RestInput::init()
{
    m_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (m_sockfd < 0) {
        cerr << "Failed to open socket. Exiting" << endl;
        return false;
    }
    m_port = 1441; //listening on port 1441

    m_serverAddr.sin_family = AF_INET;
    m_serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    m_serverAddr.sin_port = htons(m_port);

    if (::bind(m_sockfd, (struct sockaddr*)&m_serverAddr, sizeof m_serverAddr) < 0) {
        cerr << "Failed to bind. Exiting " << endl;
        return false;
    }

    listen(m_sockfd, MAX_CONNS);
    startAccept(m_sockfd);
    return true;
}

// accept incoming connection(s)
void RestInput::startAccept(int &sockfd)
{
    if (m_threadCount >= MAX_CONNS) {
        cerr << " Max # of connections reached. Skipping " << endl;
        return;
    } else {
        while (true) {
            int connfd = accept(sockfd, (struct sockaddr *)NULL, NULL);
            if (connfd < 0) {
                cerr << " Failed to accept incoming connection " << endl;
                return;
            }
            int n = read(connfd, m_data, BUFLEN);
            close(connfd);
            if (n < 0) {
                cerr << "Failed to read from socket" << endl;
                return;
            }
            startThread();
        }
    }
}

// start client thread
void RestInput::startThread()
{
    std::thread t(&RestInput::processClient, this);
{'fraction_non_alphanumeric': 0.17915543575920934, 'fraction_numerical': 0.010062893081761006, 'mean_word_length': 2.5205566097406704, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2114201', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1671, 'n_words': 491}