/*
** SellaNMS
** $Id: connection_pool.h,v 1.7 2005/05/15 05:21:17 sella Exp $
** Copyright (c) 2001-2004 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

#ifndef __DGS_LIBS_STORAGE_CONNECTION_POOL_H
#define __DGS_LIBS_STORAGE_CONNECTION_POOL_H

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

#include "defaults.h"
#include "common/confignode.h"
#include <dbi/dbi.h>
#include <pthread.h>
#include <stl/slist>

#define DEFAULT_LIBS_STORAGE_CONNECTION_POOL_TIMEOUT 300
#define DEFAULT_LIBS_STORAGE_CONNECTION_POOL_TRIMDELAY 30
#define DEFAULT_LIBS_STORAGE_CONNECTION_POOL_MAX_SIZE 32
#define DEFAULT_LIBS_STORAGE_CONNECTION_POOL_MIN_SIZE 2

typedef struct conn_pool_t {
	slist<dbi_conn> list;
	pthread_mutex_t* mutex;
	time_t last; // when the connection was last used.
	bool exclusive; // true = owner requested exclusive lock.
	unsigned int users; //Count of current users.
	bool failed; // conneciton believed failed.
};

class ConnectionPool {
	public:
		ConnectionPool();
		ConnectionPool(ConnectionPool const &);
		~ConnectionPool();

		RESULT setConfig(ConfigNode &config);
		bool isConfigured();
		RESULT requestConnection(conn_pool_t *& connection, bool exclusive);
		RESULT releaseConnection(conn_pool_t *& connection);

	protected:
		RESULT initialize();
		void setDebug(bool debug = true);
		void closeAllConnections();
		RESULT connect(conn_pool_t *connections);
		void disconnect(conn_pool_t *connections);
		void printOptions(dbi_conn conn);

	private:
		ConfigNode config;
		bool debug;
		unsigned long timeout;
		unsigned long trimDelay;
		unsigned long maxSize;
		unsigned long minSize;

		slist<conn_pool_t> pool;
};

#endif

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