//=-- AFLimitations.h - Access Filter Plugin for supporting limitations ------=
//
//  AFLimitations Class:
//
//    This class imposes limitations on the number of hits that can be 
//  processed by MagicStats in one day.
//
//=---------------------------------------------------------------------------=
//  This file is copyright (c) 1997-1998 Chris Lattner
//=---------------------------------------------------------------------------=

#include "Serialize.h"
#include "AccessFormatPlugin.h"

// NOTE - Rename this class so that people can't figure out an easy way to hack
//   it by doing strings MagicStats | grep Limitation or something...
#define AFLimitations DynaLoadPluginExtensionMechanism

//  AFLimitations Class:
//
//    This class imposes limitations on the number of hits that can be 
//  processed by MagicStats in one day.
//
class AFLimitations : public PluginTemplate<AccessFilterPlugin,AFLimitations> {
public:
  inline static unsigned int GetCurrentVersion() {
    return 100;                       // Version 1.00
  }

  inline static const char *GetPluginName() {
    return "DynaLoadPluginExtensionMechanism";
  }

  inline AFLimitations() { 
    UpdateFreq = Plugin::UpdateDaily;
  }

  virtual int Initialize(const VTListExp *Params) {
    if (Params->GetNumElements() > 1) 
      return 1;    // Silently ignore plugin, too many params...

    const VTExp *MH = Params->GetListElement(1);  // Param 1 (0 = PluginName)

    if (!MH || MH->GetExpType() != VTExp::StringTy)
      return 1;

    MaxHits = MH->GetStringValue().atoi();
    CurHits = 0;
    return 0;
  }

  virtual int FilterAccess(AccessFormatPlugin &A) {
    if (LastAccessDate != A.GetDate()) {
      LastAccessDate = A.GetDate();
      CurHits = 0;
    }
    if (CurHits > MaxHits) 
      return 1;
    CurHits++;
    return 0;
  }

  virtual void LoadState(Serialize &S)  {
    S >> CurHits >> LastAccessDate;
  }

  virtual void SaveState(Serialize &S)  {
    S << CurHits << LastAccessDate;
  }

private:
  Date LastAccessDate;
  int CurHits;
  int MaxHits;
};
