/* packetcollection.h
****************************************************************************************************************
****************************************************************************************************************

    Copyright (C) 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 __PACKETCOLLECTION_H
#define __PACKETCOLLECTION_H 1

#include <atomic>
#include <cstring>
#include <ctime>
#include <vector>
#include <libiclass/common>

#include "metadata.h"
#include "packet.h"
#include "ringallocator.h"
#include "tmdefs.h"

namespace trafficmeter
{
    /**********************************************************************************************************/
    class PacketCollection : public iclass::commont
    {
    public:
        struct Bucket;
        using Buckets                   = std::vector<Bucket*>;

                                        PacketCollection( int nChunks );
                                        ~PacketCollection();

        void                            push( const Metadata& meta, Packet packet );
        Buckets*                        pull();
        void                            flush();
        long                            queueSize() const;
        long                            nBuckets() const;

    protected:
        Bucket*                         getBucket_( int, time_t, int );

        const int                       nSlots_;
        RingAllocator*                  ring_;
        std::atomic<long>               queueSize_a_;
        std::atomic<long>               nBuckets_a_;
        Buckets                         slots_c_;
        Buckets*                        queue_c_;
        time_t                          oldestGroup_c_;
        time_t                          newestGroup_c_;
        volatile mutable bool           hasActivity_c_;
    };

    /**********************************************************************************************************/
    struct PacketCollection::Bucket
    {
        Packet*                         end;
        time_t                          oldest;
        const int                       slotNo;
        Packet                          packets[ BUCKET_PACKETS ];

                                        Bucket( int );
        void                            pushBack( const Packet& );
        void*                           operator new( size_t, RingAllocator* );
        void                            operator delete( void* );
    };
}

#endif  // __PACKETCOLLECTION_H
