/* flow2query.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2020, 2021 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/>.
*/

#ifndef __FLOW2QUERY_H
#define __FLOW2QUERY_H 1

#include <map>
#include <set>

#include "checkcast.h"
#include "groupbytime.h"
#include "timequery.h"
#include "summaryio.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class Flow2Query : public TimeQuery
    {
    public:
        struct Key;

                                        Flow2Query();
                                        ~Flow2Query();

        bool                            timeByHour();
        bool                            timeByDay();
        bool                            timeByMonth();
        bool                            hideTime();
        void                            saveVlans();
        bool                            setUnicastFilter();
        bool                            setBroadcastFilter();
        void                            invert();
        void                            level3();
        void                            addMac( const MacAddress& );
        void                            fetch( std::map<Key,SummaryIo>& );
        time_t                          regroup( time_t ) const;
        bool                            shouldSaveVlans() const;
        bool                            checkCast( MacAddress ) const;
        bool                            isInverted() const;
        bool                            isLevel3() const;
        const std::set<MacAddress>&     macs() const;

    protected:
        struct Search_;

        GroupByTime*                    groupByTime_;
        bool                            shouldSaveVlans_;
        CheckCast*                      checkCast_;
        bool                            isInverted_;
        bool                            isLevel3_;
        std::set<MacAddress>            macs_;
    };

    /**********************************************************************************************************/
    struct Flow2Query::Key
    {
        time_t                          group;
        MacAddress                      host;
        MacAddress                      peer;
        int                             vlan;
        int                             etype;

        bool                            operator<( const Key& ) const;
    };
}

/*
****************************************************************************************************************
****************************************************************************************************************
*/

namespace trafficmeter
{
    //----------------------------------------------------------------------------------------------------------
    inline time_t
    Flow2Query::regroup( time_t group ) const
    {
        return groupByTime_? (*groupByTime_)( group ) : group;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Flow2Query::shouldSaveVlans() const
    {
        return shouldSaveVlans_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Flow2Query::checkCast( MacAddress mac ) const
    {
        return checkCast_? (*checkCast_)( mac ) : true;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Flow2Query::isInverted() const
    {
        return isInverted_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Flow2Query::isLevel3() const
    {
        return isLevel3_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline const std::set<MacAddress>&
    Flow2Query::macs() const
    {
        return macs_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Flow2Query::Key::operator<( const Key& rhs ) const
    {
        if ( group != rhs.group )
        {
            return group < rhs.group;
        }

        if ( vlan != rhs.vlan )
        {
            return vlan < rhs.vlan;
        }

        if ( host != rhs.host )
        {
            return host < rhs.host;
        }

        if ( peer != rhs.peer )
        {
            return peer < rhs.peer;
        }

        return etype < rhs.etype;
    }
}

#endif  // __FLOW2QUERY_H
