<commit_before>//: ----------------------------------------------------------------------------
//: Copyright (C) 2014 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: \file:    nconn.cc
//: \details: TODO
//: \author:  <NAME>
//: \date:    02/07/2014
//:
//:   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.
//:
//: ----------------------------------------------------------------------------

//: ----------------------------------------------------------------------------
//: Includes
//: ----------------------------------------------------------------------------
#include "hlx/time_util.h"
#include "hlx/trace.h"
#include "hlx/status.h"
#include "nconn.h"
#include "nbq.h"
#include "evr.h"
#include "ndebug.h"
#include <errno.h>
#include <string.h>
#include <strings.h>

namespace ns_hlx {

//: ----------------------------------------------------------------------------
//: \details: TODO
//: \return:  TODO
//: \param:   TODO
//: ----------------------------------------------------------------------------
int32_t nconn::nc_run_state_machine(mode_t a_mode, nbq *a_in_q, nbq *a_out_q)
{
        //NDBG_PRINT("%sRUN_STATE_MACHINE%s: CONN[%p] STATE[%d] MODE: %d --START\n",
        //                ANSI_COLOR_BG_YELLOW, ANSI_COLOR_OFF, this, m_nc_state, a_mode);
state_top:
        //NDBG_PRINT("%sRUN_STATE_MACHINE%s: CONN[%p] STATE[%d] MODE: %d\n",
        //                ANSI_COLOR_FG_YELLOW, ANSI_COLOR_OFF, this, m_nc_state, a_mode);
        switch (m_nc_state)
        {

        // -------------------------------------------------
        // STATE: FREE
        // -------------------------------------------------
        case NC_STATE_FREE:
        {
                int32_t l_status;
                l_status = ncsetup();
                if(l_status != NC_STATUS_OK)
                {
                        //NDBG_PRINT("Error performing ncsetup\n");
                        return NC_STATUS_ERROR;
                }

                // TODO -check for errors
                m_nc_state = NC_STATE_CONNECTING;

                // Stats
                if(m_collect_stats_flag)
                {
                        m_connect_start_time_us = get_time_us();
                }

                goto state_top;
        }

        // -------------------------------------------------
        // STATE: LISTENING
        // -------------------------------------------------
        case NC_STATE_LISTENING:
        {
                int32_t l_status;
                l_status = ncaccept();
                if(l_status < 0)
                {
                        //NDBG_PRINT("Error performing ncaccept\n");
                        return NC_STATUS_ERROR;
                }
                //NDBG_PRINT("%sRUN_STATE_MACHINE%s: ACCEPT[%d]\n", ANSI_COLOR_BG_RED, ANSI_COLOR_OFF, l_status);
                // Returning client fd
                return l_status;
        }

        // -------------------------------------------------
        // STATE: CONNECTING
        // -------------------------------------------------
        case NC_STATE_CONNECTING:
        {
                int32_t l_status;
                //NDBG_PRINT("%sConnecting%s: host: %s\n", ANSI_COLOR_FG_RED, ANSI_COLOR_OFF, m_label.c_str());
                l_status = ncconnect();
                if(l_status == NC_STATUS_ERROR)
                {
                        //NDBG_PRINT("Error performing ncconnect for host: %s.\n", m_label.c_str());
                        return NC_STATUS_ERROR;
                }
                if(is_connecting())
                {
                        //NDBG_PRINT("Still connecting...\n");
                        return NC_STATUS_AGAIN;
                }
                //NDBG_PRINT("%sConnected%s: label: %s\n", ANSI_COLOR_FG_RED, ANSI_COLOR_OFF, m_label.c_str());
                // Returning client fd
                // If OK -change state to connected???
                m_nc_state = NC_STATE_CONNECTED;
                if(m_collect_stats_flag)
                {
                        m_stat.m_tt_connect_us = get_delta_time_us(m_connect_start_time_us);
                }
                if(m_connect_only)
                {
                        m_nc_state = NC_STATE_DONE;
                        return NC_STATUS_EOF;
                }
                if(a_out_q->read_avail())
                {
                        a_mode = NC_MODE_WRITE;
                }
                goto state_top;
        }

        // -------------------------------------------------
        // STATE: ACCEPTING
        // -------------------------------------------------
        case NC_STATE_ACCEPTING:
        {
                int32_t l_status;
                l_status = ncaccept();
                if(l_status == NC_STATUS_ERROR)<commit_msg>Adding debugging trace for connections.<commit_after>//: ----------------------------------------------------------------------------
//: Copyright (C) 2014 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: \file:    nconn.cc
//: \details: TODO
//: \author:  <NAME>
//: \date:    02/07/2014
//:
//:   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.
//:
//: ----------------------------------------------------------------------------

//: ----------------------------------------------------------------------------
//: Includes
//: ----------------------------------------------------------------------------
#include "hlx/time_util.h"
#include "hlx/trace.h"
#include "hlx/status.h"
#include "nconn.h"
#include "nbq.h"
#include "evr.h"
#include "ndebug.h"
#include <errno.h>
#include <string.h>
#include <strings.h>

namespace ns_hlx {

//: ----------------------------------------------------------------------------
//: \details: TODO
//: \return:  TODO
//: \param:   TODO
//: ----------------------------------------------------------------------------
int32_t nconn::nc_run_state_machine(mode_t a_mode, nbq *a_in_q, nbq *a_out_q)
{
        //NDBG_PRINT("%sRUN_STATE_MACHINE%s: CONN[%p] STATE[%d] MODE: %d --START\n",
        //                ANSI_COLOR_BG_YELLOW, ANSI_COLOR_OFF, this, m_nc_state, a_mode);
state_top:
        //NDBG_PRINT("%sRUN_STATE_MACHINE%s: CONN[%p] STATE[%d] MODE: %d\n",
        //                ANSI_COLOR_FG_YELLOW, ANSI_COLOR_OFF, this, m_nc_state, a_mode);
        switch (m_nc_state)
        {

        // -------------------------------------------------
        // STATE: FREE
        // -------------------------------------------------
        case NC_STATE_FREE:
        {
                int32_t l_status;
                l_status = ncsetup();
                if(l_status != NC_STATUS_OK)
                {
                        //NDBG_PRINT("Error performing ncsetup\n");
                        return NC_STATUS_ERROR;
                }

                // TODO -check for errors
                m_nc_state = NC_STATE_CONNECTING;

                // Stats
                if(m_collect_stats_flag)
                {
                        m_connect_start_time_us = get_time_us();
                }

                goto state_top;
        }

        // -------------------------------------------------
        // STATE: LISTENING
        // -------------------------------------------------
        case NC_STATE_LISTENING:
        {
                int32_t l_status;
                l_status = ncaccept();
                if(l_status < 0)
                {
                        //NDBG_PRINT("Error performing ncaccept\n");
                        return NC_STATUS_ERROR;
                }
                //NDBG_PRINT("%sRUN_STATE_MACHINE%s: ACCEPT[%d]\n", ANSI_COLOR_BG_RED, ANSI_COLOR_OFF, l_status);
                // Returning client fd
                return l_status;
        }

        // -------------------------------------------------
        // STATE: CONNECTING
        // -------------------------------------------------
        case NC_STATE_CONNECTING:
        {
                int32_t l_status;
                //NDBG_PRINT("%sConnecting%s: host: %s\n", ANSI_COLOR_FG_RED, ANSI_COLOR_OFF, m_label.c_str());
                l_status = ncconnect();
                if(l_status == NC_STATUS_ERROR)
                {
                        //NDBG_PRINT("Error performing ncconnect for host: %s.\n", m_label.c_str());
                        return NC_STATUS_ERROR;
                }
                if(is_connecting())
                {
                        //NDBG_PRINT("Still connecting...\n");
                        return NC_STATUS_AGAIN;
                }
                //NDBG_PRINT("%sConnected%s: label: %s\n", ANSI_COLOR_FG_RED, ANSI_COLOR_OFF, m_label.c_str());
                TRC_DEBUG("Connected: label: %s\n", m_label.c_str());
                // Returning client fd
                // If OK -change state to connected???
                m_nc_state = NC_STATE_CONNECTED;
                if(m_collect_stats_flag)
                {
                        m_stat.m_tt_connect_us = get_delta_time_us(m_connect_start_time_us);
                }
                if(m_connect_only)
                {
                        m_nc_state = NC_STATE_DONE;
                        return NC_STATUS_EOF;
                }
                if(a_out_q->read_avail())
                {
                        a_mode = NC_MODE_WRITE;
                }
                goto state_top;
        }

        // -------------------------------------------------
        // STATE: ACCEPTING
        // -------------------------------------------------
        case NC_STATE_ACCEPTING:
        {
                int32_t l_status;
                l_status = ncaccept();
                if(l_status == NC_STATUS_ERROR)
{'fraction_non_alphanumeric': 0.2509767441860465, 'fraction_numerical': 0.005023255813953489, 'mean_word_length': 0.9406137184115524, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '6504277', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2800, 'n_words': 565}