/* param.cc
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2020, 2021, 2026 Askar Almatov

    This file is part of Trafficmeter.

    Trafficmeter is free software: you can redistribute it and/or modify it under the terms of the GNU General
    Public License as published by the Free Software Foundation, either version 3 of the License, or (at your
    option) any later version.

    Trafficmeter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
    implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
    License for more details.

    You should have received a copy of the GNU General Public License along with Trafficmeter. If not, see
    <http://www.gnu.org/licenses/>.
*/

#include "param.h"
#include "tmdefs.h"

using namespace trafficmeter;

using std::string;

/**************************************************************************************************************/
static void
validateInteger_( int* value, const string& str, int min, int max )
{
    try
    {
        int     newValue = stoi( str );

        if ( newValue >= min && newValue <= max )
        {
            *value = newValue;
        }
    }
    catch ( ... )
    {
    }
}

/**************************************************************************************************************/
Param::Param( const string& section ) :
    IniConfig( section ),
    shouldSavePackets( false ),
    shouldCheckErspan( false ),
    verbosity( DEFAULT_VERBOSITY ),
    nExtractors( DEFAULT_EXTRACTORS ),
    nFlowWriters( DEFAULT_FLOW_WRITERS ),
    nStatWriters( DEFAULT_STAT_WRITERS ),
    nExtractorChunks( DEFAULT_EXTRACTOR_CHUNKS ),
    nCollectionChunks( DEFAULT_COLLECTION_CHUNKS ),
    flowQuota( DEFAULT_FLOW_QUOTA ),
    statQuota( DEFAULT_STAT_QUOTA ),
    flow2Quota( DEFAULT_FLOW2_QUOTA ),
    packetQuota( DEFAULT_PACKET_QUOTA )
{
}

/**************************************************************************************************************/
bool
Param::readFile( const Path& configFile )
{
    if ( !IniConfig::readFile(configFile) )
    {
        return false;
    }

    sourceNames = std::move( getList("Source") );
    shouldSavePackets = getValue( "SavePackets" ) != "false" && hasKey( "SavePackets" );
    shouldCheckErspan = getValue( "Erspan" ) != "false" && hasKey( "Erspan" );
    validateInteger_( &verbosity, getValue("Verbosity"), 0, 100 );
    validateInteger_( &nExtractors, getValue("Extractors"), 1, 1 << EXTENSION_LENGTH );
    validateInteger_( &nFlowWriters, getValue("FlowWriters"), 1, 256 );
    validateInteger_( &nStatWriters, getValue("StatWriters"), 1, 256 );
    validateInteger_( &nExtractorChunks, getValue("ExtractorChunks"), 1, 800 );
    validateInteger_( &nCollectionChunks, getValue("CollectionChunks"), 1, 300 );
    validateInteger_( &flowQuota, getValue("FlowQuota"), 1, 100000000 );
    validateInteger_( &statQuota, getValue("StatQuota"), 1, 10000000 );
    validateInteger_( &flow2Quota, getValue("Flow2Quota"), 1, 1000000 );
    validateInteger_( &packetQuota, getValue("PacketQuota"), 1, 1000000000 );
    return true;
}
