/*
** SellaNMS
** $Id: rate.h,v 1.6 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_COMMON_RATE_H
#define __DGS_COMMON_RATE_H

#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include "defaults.h"
#include <time.h>
#include <stdint.h>
#include <pthread.h>
#include <stl/string>
#include <stl/hash_map>

#define RATE_INTERVAL_DEF	60
#define RATE_CLEAN_DEF		2500

typedef struct rate_entry_t {
   time_t *last_update;
   unsigned int *samples;
} _rate_entry_t;

typedef hash_map<string, rate_entry_t*> rate_data_t;

class Rate {
	public:
		Rate();
		Rate(unsigned int interval);
		Rate(unsigned int interval, unsigned int decay);
		Rate(Rate const &);
		~Rate();
		Rate const& operator=(Rate const &src);

		void setDebug(bool flag = true);
		bool getDebug();

		void setInterval(unsigned int interval);
		unsigned int getInterval();

		void setClean(unsigned int clean);
		unsigned int getClean();

		unsigned int getRate(string &source, unsigned int sub_interval = 0);

		void increment(string &source);

		void clear(string &source);

		void clear();

		void clearStale();

	private:
		pthread_mutex_t hashMutex;

		rate_data_t data;

		bool debug;
		unsigned int clean;
		unsigned int increments;
		unsigned int interval;
		unsigned int samples; /* interval + 1, so we don't count the current second. */
};

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
