/* source.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 __SOURCE_H
#define __SOURCE_H 1

#include <atomic>
#include <string>

#include "timestamp.h"
#include "tmdefs.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class Source // abstract
    {
    public:
        struct Packet;

                                        Source( const std::string& name );
        virtual                         ~Source();

        void                            shouldCheckErspan();
        const std::string&              name() const;
        const std::string&              info() const;
        uintmax_t                       nReceived() const;
        uintmax_t                       nDropped() const;

        virtual void                    open() = 0;
        virtual void                    close() = 0;
        virtual bool                    checkForRecords() = 0;
        virtual bool                    copyRecords( uint8_t** destination, const uint8_t* destinationEnd ) = 0;
        virtual Packet                  getPacket() = 0;
        virtual Packet                  getPacketFromCopy( uint8_t** record ) const = 0;
        virtual bool                    isEthernet() const = 0;

    protected:
        const std::string               name_;
        std::string                     info_;
        bool                            shouldCheckErspan_;
        std::atomic<uintmax_t>          nReceived_a_;
        std::atomic<uintmax_t>          nDropped_a_;
    };

    /**********************************************************************************************************/
    struct Source::Packet
    {
        Timestamp                       timestamp;
        const uint8_t*                  data2;
        unsigned                        nBytes2;

        bool                            isValid() const;
    };
}

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

namespace trafficmeter
{
    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    Source::nReceived() const
    {
        return nReceived_a_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline uintmax_t
    Source::nDropped() const
    {
        return nDropped_a_;
    }

    //----------------------------------------------------------------------------------------------------------
    inline bool
    Source::Packet::isValid() const
    {
        return nBytes2 > 0;
    }
}

#endif  // __SOURCE_H
