<commit_before>/**
 * A class which describes the abstract properties and actions of a
 * controller.
 *
 * @date                    Jun 2, 2014
 * @author                  <NAME>
 * @version                 0.1
 *
 * Copyright 2013 <NAME>
 *
 * 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.
 */

// BEGIN Includes. ///////////////////////////////////////////////////

// System dependencies.
#include <cassert>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

// Application dependencies.
#include <ias/application/controller_application.h>
#include <ias/application/constants.h>
#include <ias/server/device_server.h>
#include <ias/network/posix/posix_tcp_socket.h>
#include <ias/network/posix/posix_tcp_server_socket.h>
#include <ias/network/posix/ssl/posix_ssl_socket.h>
#include <ias/network/proxy/socks.h>
#include <ias/network/util.h>
#include <ias/logger/logger.h>
#include <ias/logger/file/file_logger.h>
#include <ias/logger/console/console_logger.h>

// END Includes. /////////////////////////////////////////////////////

void ControllerApplication::initialize( void ) {
    mSocket = nullptr;
    mDeviceServer = nullptr;
}

void ControllerApplication::setup( const int argc , const char ** argv ) {
    int index;

    // Retrieve the configuration file path.
    index = flagIndex(argc,argv,kFlagConfig);
    if( index >= 0 && (index + 1) <= argc && strlen(argv[index + 1]) > 0 ) {
        std::string configurationPath;

        configurationPath = argv[index + 1];
        readConfiguration(configurationPath);
        initializeLogger();
        connectToServer();
        if( mSocket != nullptr ) {
            authenticateWithServer();
            if( mSocket->isConnected() ) {
                readDevices();
                allocateDeviceServer();
                if( mDeviceServer != nullptr )
                    startDeviceProcesses();
            }
        }
    }
}

void ControllerApplication::readConfiguration( const std::string & filePath ) {
    std::ifstream file(filePath);
    std::string line;
    std::string key;
    std::string value;
    std::size_t i;

    // Checking the precondition.
    assert( !filePath.empty() );

    while( std::getline(file,line) ) {
        trim(line);
        if( line.empty() || line.at(0) == '#' )
            continue;
        i = line.find('=');
        key = line.substr(0,i);
        value = line.substr(i + 1,line.length());
        trim(key); trim(value);
        if( !key.empty() && !value.empty() )
            mProperties.add(key,value);
    }
    file.close();
}

void ControllerApplication::initializeSslContext( void ) {
    mSslContext = SSL_CTX_new(SSLv23_client_method());
}

bool ControllerApplication::sslRequested( void ) const {
    return ( !mProperties.get(kConfigHostSslEnabled).empty() );
}

int ControllerApplication::connectToProxy( const std::string & proxyAddress,
                                           const std::string & proxyPort,
                                           const std::string & serverAddress,
                                           const std::string & serverPort ) const {
    std::size_t pPort;
    std::size_t sPort;
    int fd;
<commit_msg>Fix issue, precondition was not met.<commit_after>/**
 * A class which describes the abstract properties and actions of a
 * controller.
 *
 * @date                    Jun 2, 2014
 * @author                  <NAME>
 * @version                 0.1
 *
 * Copyright 2013 <NAME>
 *
 * 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.
 */

// BEGIN Includes. ///////////////////////////////////////////////////

// System dependencies.
#include <cassert>
#include <cstring>
#include <fstream>
#include <sstream>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

// Application dependencies.
#include <ias/application/controller_application.h>
#include <ias/application/constants.h>
#include <ias/server/device_server.h>
#include <ias/network/posix/posix_tcp_socket.h>
#include <ias/network/posix/posix_tcp_server_socket.h>
#include <ias/network/posix/ssl/posix_ssl_socket.h>
#include <ias/network/proxy/socks.h>
#include <ias/network/util.h>
#include <ias/logger/logger.h>
#include <ias/logger/file/file_logger.h>
#include <ias/logger/console/console_logger.h>

// END Includes. /////////////////////////////////////////////////////

void ControllerApplication::initialize( void ) {
    mSocket = nullptr;
    mDeviceServer = nullptr;
}

void ControllerApplication::setup( const int argc , const char ** argv ) {
    int index;

    // Retrieve the configuration file path.
    index = flagIndex(argc,argv,kFlagConfig);
    if( index >= 0 && (index + 1) <= argc && strlen(argv[index + 1]) > 0 ) {
        std::string configurationPath;

        configurationPath = argv[index + 1];
        readConfiguration(configurationPath);
        initializeLogger();
        connectToServer();
        if( mSocket != nullptr ) {
            authenticateWithServer();
            if( mSocket->isConnected() ) {
                readDevices();
                allocateDeviceServer();
                if( mDeviceServer != nullptr )
                    startDeviceProcesses();
            }
        }
    }
}

void ControllerApplication::readConfiguration( const std::string & filePath ) {
    std::ifstream file(filePath);
    std::string line;
    std::string key;
    std::string value;
    std::size_t i;

    // Checking the precondition.
    assert( !filePath.empty() );

    while( std::getline(file,line) ) {
        trim(line);
        if( line.empty() || line.at(0) == '#' )
            continue;
        i = line.find('=');
        key = line.substr(0,i);
        value = line.substr(i + 1,line.length());
        trim(key); trim(value);
        if( !key.empty() && !value.empty() )
            mProperties.add(key,value);
    }
    file.close();
}

void ControllerApplication::initializeSslContext( void ) {
    mSslContext = SSL_CTX_new(SSLv23_client_method());
}

bool ControllerApplication::sslRequested( void ) const {
    bool requested;

    requested = false;
    if( mProperties.contains(kConfigHostSslEnabled) ) {
        const std::string & config = mProperties.get(kConfigHostSslEnabled);

        if( config == "1" )
            requested = true;
    }

    return ( requested );
}

int ControllerApplication::connectToProxy( const std::string & proxyAddress,
                                           const std::string & proxyPort,
                                           const std::string & serverAddress,
                                           const std::string & serverPort ) const {
    std::size_t pPort;
    std::size_t sPort;
    int fd;

{'fraction_non_alphanumeric': 0.1436115404430706, 'fraction_numerical': 0.006568778979907265, 'mean_word_length': 2.3702256944444446, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883972', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2130, 'n_words': 568}