// snfGBUdbmgr.hpp
// Copyright (C) 2006 - 2009 ARM Research Labs, LLC.
// See www.armresearch.com for the copyright terms.
//
// This module manages the GBUdb(s) that are used in the SNF scanner engine.
// It is responsible for setting parameters, monitoring activity, and handling
// scheduled maintenance tasks.

#ifndef snfGBUdbmgr_included
#define snfGBUdbmgr_included

#include "threading.hpp"
#include "timing.hpp"
#include "snfCFGmgr.hpp"
#include "snfLOGmgr.hpp"
#include "GBUdb.hpp"

using namespace std;

class snfLOGmgr;

class snfGBUdbmgr : public Thread {
  private:
    Mutex MyMutex;
    GBUdb* MyGBUdb;
    snfLOGmgr* MyLOGmgr;
    bool Configured;
    volatile bool TimeToStop;

    // Condensation parts

    Timeout CondenseGuardTime;
    bool TimeTriggerOnOff;
    Timeout TimeTrigger;
    bool PostsTriggerOnOff;
    int PostsTriggerValue;
    bool RecordsTriggerOnOff;
    int RecordsTriggerValue;
    bool SizeTriggerOnOff;
    int SizeTriggerValue;

    // Checkpoint parts

    bool CheckpointOnOff;
    Timeout CheckpointTrigger;

    // Utility functions

    void DoMaintenanceWork();

  public:
    snfGBUdbmgr();                                                              // Clean init and start thread.
    ~snfGBUdbmgr();                                                             // Clean shutdown & stop thread.
    void linkGBUdb(GBUdb& G);                                                   // Connect to our GBUdb.
    void linkLOGmgr(snfLOGmgr& L);                                              // Connect to our LOGmgr.
    void configure(snfCFGData& CFGData);                                        // Establish or change our CFG.
    void load();                                                                // Load the GBUdb as configured.
    void stop();                                                                // Stop the thread.
    void myTask();                                                              // Establish our thread's task.

    void GetAlertsForSync(list<GBUdbAlert>& AlertList);                         // Fill AlertList w/ outgoing alerts.
    void ProcessReflections(list<GBUdbAlert>& Reflections);                     // Integrate returning reflections.

    const static ThreadType Type;                                               // The thread's type.

};

#endif
