// Use sample

#include <windows.h>
#include "Connection.h"

DWORD g_num_connections = 50; // I set this from the registry

BOOL WINAPI GetFilterVersion (PHTTP_FILTER_VERSION pFilterVersion)
{
	// create a vector of sessions
	g_connections = new CConnection*[g_num_connections];
	g_inuse = new unsigned int[ (g_num_connections+31)/32 ]; // assumes 32 bits in an int
	memset(g_inuse,0,((g_num_connections+31)/32)*4);

	// try to connect to the DB; this might fail
	// if so, we will always try later on to restablish the connection
	// this prevents problems if the DB goes away and then comes back again;
	// or the connection is drop due to network conditions
	bool docreate=true;
	for (DWORD i=0;i<g_num_connections;++i) {
		if (_tcsicmp(db_type,_T("oledb"))==0)
			g_connections[i] = new COLEDBConnection;
		else
			g_connections[i] = new COCIConnection;
		g_connections[i]->setIndex(i);

		// Assume the DB is dead for now and give up creating connections.
		// We do this to avoid not being able to start due multiple timeout.
		if (i < g_startup_connections && docreate && 
			!g_connections[i]->createSession())
			docreate=false;
	}
	return TRUE;
}

// The main authentication code
DWORD WINAPI HttpFilterProc (PHTTP_FILTER_CONTEXT pFC,
                             DWORD dwNotificationType,
                             LPVOID pvNotification)
{
	// We are (want to) process an AUTH_COMPLETE notification.
	HTTP_FILTER_AUTH_COMPLETE_INFO* pAuthComplete = 
		(HTTP_FILTER_AUTH_COMPLETE_INFO*)pvNotification;

	// Select a database connection, ensuring that it is connected
	auto_connection connection;
	int ret = connection->isAccessAllowed(...);
}

// Filter cleanup
BOOL WINAPI TerminateFilter(DWORD dwFlags)
{
	// Delete the database connections
	delete [] g_inuse;
	if (g_connections) {
		for (DWORD i=0;i<g_num_connections;++i) {
			delete g_connections[i];
		}
		delete [] g_connections;
	}

	return TRUE;
}

