Mixes for Privacy and Anonymity in the Internet
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
CAAccountingDBInterface Class Reference

Public Member Functions

SINT32 storeCostConfirmation (CAXMLCostConfirmation &cc, UINT8 *ccCascade)
 Creates the tables we need in the DB. More...
 
SINT32 getCostConfirmation (UINT64 accountNumber, UINT8 *cascadeId, CAXMLCostConfirmation **pCC, bool &a_bSettled)
 
SINT32 getUnsettledCostConfirmations (CAXMLCostConfirmation ***resultCCs, UINT8 *cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs)
 Fills the CAQueue with all non-settled cost confirmations. More...
 
SINT32 markAsSettled (UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
 Marks this account as settled. More...
 
SINT32 deleteCC (UINT64 accountNumber, UINT8 *cascadeId)
 if the BI reports an error while trying to settle a CC, this will be called to delete it from the database (otherwise the AI would try forever in vain to settle the unusable CC) More...
 
SINT32 storePrepaidAmount (UINT64 accountNumber, SINT32 prepaidBytes, UINT8 *cascadeId)
 
SINT32 getPrepaidAmount (UINT64 accountNumber, UINT8 *cascadeId, bool a_bDelete)
 
SINT32 storeAccountStatus (UINT64 a_accountNumber, UINT32 a_statusCode)
 
SINT32 getAccountStatus (UINT64 a_accountNumber, UINT32 &a_statusCode)
 
SINT32 clearAccountStatus (UINT64 a_accountNumber)
 
SINT32 checkCountAllQuery (UINT8 *a_query, UINT32 &r_count)
 Takes and executes a query that counts databae records and tests if the result is valid. More...
 

Static Public Member Functions

static CAAccountingDBInterfacegetConnection ()
 
static SINT32 releaseConnection (CAAccountingDBInterface *dbIf)
 
static SINT32 init ()
 
static SINT32 cleanup ()
 

Private Member Functions

 CAAccountingDBInterface ()
 Constructor. More...
 
 ~CAAccountingDBInterface ()
 Destructor. More...
 
SINT32 __storeCostConfirmation (CAXMLCostConfirmation &cc, UINT8 *ccCascade)
 stores a cost confirmation in the DB More...
 
SINT32 __getCostConfirmation (UINT64 accountNumber, UINT8 *cascadeId, CAXMLCostConfirmation **pCC, bool &a_bSettled)
 Gets the latest cost confirmation stored for the given user account. More...
 
SINT32 __getUnsettledCostConfirmations (CAXMLCostConfirmation ***resultCCs, UINT8 *cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs)
 Fills the CAQueue with pointer to all non-settled cost confirmations. More...
 
SINT32 __markAsSettled (UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
 Marks this account as settled. More...
 
SINT32 __deleteCC (UINT64 accountNumber, UINT8 *cascadeId)
 
SINT32 __storePrepaidAmount (UINT64 accountNumber, SINT32 prepaidBytes, UINT8 *cascadeId)
 
SINT32 __getPrepaidAmount (UINT64 accountNumber, UINT8 *cascadeId, bool a_bDelete)
 
SINT32 __storeAccountStatus (UINT64 a_accountNumber, UINT32 a_statusCode)
 
SINT32 __getAccountStatus (UINT64 a_accountNumber, UINT32 &a_statusCode)
 
SINT32 __clearAccountStatus (UINT64 a_accountNumber)
 
SINT32 __checkCountAllQuery (UINT8 *a_query, UINT32 &r_count)
 
SINT32 initDBConnection ()
 Initiates the database connection. More...
 
SINT32 terminateDBConnection ()
 Terminates the database connection. More...
 
bool isDBConnected ()
 
bool checkConnectionStatus ()
 Checks if the connection still exists and tries to reconnect if not. More...
 
bool checkOwner ()
 
bool testAndSetOwner ()
 
bool testAndResetOwner ()
 
PGresult * monitored_PQexec (PGconn *conn, const char *query)
 

Private Attributes

PGconn * m_dbConn
 connection to postgreSQL database More...
 
bool m_bConnected
 
volatile thread_id_t m_owner
 
volatile bool m_free
 
int m_protocolVersion
 
CAMutexm_pConnectionMutex
 

Static Private Attributes

static CAConditionVariablems_pConnectionAvailable = NULL
 
static volatile UINT64 ms_threadWaitNr = 0
 
static volatile UINT64 ms_nextThreadNr = 0
 
static CAAccountingDBInterfacems_pDBConnectionPool []
 

Friends

class CAAccountingInstance
 

Detailed Description

Author
Bastian Voigt

This class is used to store cost confirmations in a postgresql database

Constructor & Destructor Documentation

◆ CAAccountingDBInterface()

CAAccountingDBInterface::CAAccountingDBInterface ( )
private

Constructor.

References m_bConnected, m_dbConn, m_free, m_owner, and m_pConnectionMutex.

Referenced by init().

◆ ~CAAccountingDBInterface()

CAAccountingDBInterface::~CAAccountingDBInterface ( )
private

Destructor.

References m_pConnectionMutex, and terminateDBConnection().

Member Function Documentation

◆ __checkCountAllQuery()

SINT32 CAAccountingDBInterface::__checkCountAllQuery ( UINT8 a_query,
UINT32 r_count 
)
private

◆ __clearAccountStatus()

SINT32 CAAccountingDBInterface::__clearAccountStatus ( UINT64  a_accountNumber)
private

◆ __deleteCC()

SINT32 CAAccountingDBInterface::__deleteCC ( UINT64  accountNumber,
UINT8 cascadeId 
)
private

◆ __getAccountStatus()

SINT32 CAAccountingDBInterface::__getAccountStatus ( UINT64  a_accountNumber,
UINT32 a_statusCode 
)
private

◆ __getCostConfirmation()

SINT32 CAAccountingDBInterface::__getCostConfirmation ( UINT64  accountNumber,
UINT8 cascadeId,
CAXMLCostConfirmation **  pCC,
bool &  a_bSettled 
)
private

Gets the latest cost confirmation stored for the given user account.

Parameters
accountNumberthe account for which the cost confirmation is requested
pCCon return contains a pointer to the Cost confirmation (the caller is responsible for deleting this object), NULL in case of an error
Return values
E_SUCCESS,ifeverything is OK
E_NOT_CONNECTED,ifthe DB query could not be executed
E_NOT_FOUND,ifthere was no XMLCC found
E_UNKOWNin case of a general error

References checkConnectionStatus(), E_NOT_CONNECTED, E_NOT_FOUND, E_SUCCESS, E_UNKNOWN, m_dbConn, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, print64(), and CAMsg::printMsg().

Referenced by getCostConfirmation().

◆ __getPrepaidAmount()

SINT32 CAAccountingDBInterface::__getPrepaidAmount ( UINT64  accountNumber,
UINT8 cascadeId,
bool  a_bDelete 
)
private

◆ __getUnsettledCostConfirmations()

SINT32 CAAccountingDBInterface::__getUnsettledCostConfirmations ( CAXMLCostConfirmation ***  resultCCs,
UINT8 cascadeId,
UINT32 nrOfCCs,
UINT32  a_maxCCs 
)
private

Fills the CAQueue with pointer to all non-settled cost confirmations.

The caller is responsible for deleating this cost confirmations.

Return values
E_NOT_CONNECTEDif a connection to the DB could not be established
E_UNKNOWNin case of a general error
E_SUCCESS: database operation completed successfully (but queue might still be empty if there were no unsettled cost confirmation to find). Param: cascadeId : String identifier of a cascade for which to return the cost confirmations (concatenated hashes of all the cascade's mixes' price certs)

References checkConnectionStatus(), E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, m_dbConn, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, and CAMsg::printMsg().

Referenced by getUnsettledCostConfirmations().

◆ __markAsSettled()

SINT32 CAAccountingDBInterface::__markAsSettled ( UINT64  accountNumber,
UINT8 cascadeId,
UINT64  a_transferredBytes 
)
private

Marks this account as settled.

Todo:
what to do if there was a new CC stored while we were busy settling the old one?
Return values
E_NOT_CONNECTEDif a connection to the DB could not be established
E_UNKNOWNin case of a general error

References checkConnectionStatus(), E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, m_dbConn, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, and print64().

Referenced by markAsSettled().

◆ __storeAccountStatus()

SINT32 CAAccountingDBInterface::__storeAccountStatus ( UINT64  a_accountNumber,
UINT32  a_statusCode 
)
private

◆ __storeCostConfirmation()

SINT32 CAAccountingDBInterface::__storeCostConfirmation ( CAXMLCostConfirmation &  cc,
UINT8 ccCascade 
)
private

stores a cost confirmation in the DB

Todo:
optimize - maybe do check and insert/update in one step??

References __checkCountAllQuery(), checkConnectionStatus(), E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, len, m_dbConn, max, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, print64(), and CAMsg::printMsg().

Referenced by storeCostConfirmation().

◆ __storePrepaidAmount()

SINT32 CAAccountingDBInterface::__storePrepaidAmount ( UINT64  accountNumber,
SINT32  prepaidBytes,
UINT8 cascadeId 
)
private

◆ checkConnectionStatus()

bool CAAccountingDBInterface::checkConnectionStatus ( )
private

Checks if the connection still exists and tries to reconnect if not.

Returns
if the database connection is active after the call or not

References m_bConnected, m_dbConn, CAMsg::printMsg(), and terminateDBConnection().

Referenced by __deleteCC(), __getAccountStatus(), __getCostConfirmation(), __getPrepaidAmount(), __getUnsettledCostConfirmations(), __markAsSettled(), __storeAccountStatus(), __storeCostConfirmation(), __storePrepaidAmount(), getConnection(), and monitored_PQexec().

◆ checkCountAllQuery()

SINT32 CAAccountingDBInterface::checkCountAllQuery ( UINT8 a_query,
UINT32 r_count 
)

Takes and executes a query that counts databae records and tests if the result is valid.

Parameters
a_querya query that should return the count of database rows
r_countnumber of database rows; only valid if E_SUCCESS is returned

References __checkCountAllQuery(), checkOwner(), and E_UNKNOWN.

◆ checkOwner()

bool CAAccountingDBInterface::checkOwner ( )
private

◆ cleanup()

SINT32 CAAccountingDBInterface::cleanup ( )
static

◆ clearAccountStatus()

SINT32 CAAccountingDBInterface::clearAccountStatus ( UINT64  a_accountNumber)

◆ deleteCC()

SINT32 CAAccountingDBInterface::deleteCC ( UINT64  accountNumber,
UINT8 cascadeId 
)

if the BI reports an error while trying to settle a CC, this will be called to delete it from the database (otherwise the AI would try forever in vain to settle the unusable CC)

References __deleteCC(), checkOwner(), and E_UNKNOWN.

Referenced by CAAccountingInstance::__handleSettleResult(), and CAAccountingInstance::settlementTransaction().

◆ getAccountStatus()

SINT32 CAAccountingDBInterface::getAccountStatus ( UINT64  a_accountNumber,
UINT32 a_statusCode 
)

◆ getConnection()

CAAccountingDBInterface * CAAccountingDBInterface::getConnection ( )
static

◆ getCostConfirmation()

SINT32 CAAccountingDBInterface::getCostConfirmation ( UINT64  accountNumber,
UINT8 cascadeId,
CAXMLCostConfirmation **  pCC,
bool &  a_bSettled 
)

◆ getPrepaidAmount()

SINT32 CAAccountingDBInterface::getPrepaidAmount ( UINT64  accountNumber,
UINT8 cascadeId,
bool  a_bDelete 
)

◆ getUnsettledCostConfirmations()

SINT32 CAAccountingDBInterface::getUnsettledCostConfirmations ( CAXMLCostConfirmation ***  resultCCs,
UINT8 cascadeId,
UINT32 nrOfCCs,
UINT32  a_maxCCs 
)

◆ init()

SINT32 CAAccountingDBInterface::init ( )
static

◆ initDBConnection()

SINT32 CAAccountingDBInterface::initDBConnection ( )
private

Initiates the database connection.

This function is called inside the aiThread

Returns
E_NOT_CONNECTED if the connection could not be established
E_UNKNOWN if we are already connected
E_SUCCESS if all is OK

References E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, CACmdLnOptions::getDatabasePort(), CALibProxytest::getOptions(), m_bConnected, m_dbConn, and CAMsg::printMsg().

Referenced by getConnection(), and init().

◆ isDBConnected()

bool CAAccountingDBInterface::isDBConnected ( )
private

References m_bConnected.

◆ markAsSettled()

SINT32 CAAccountingDBInterface::markAsSettled ( UINT64  accountNumber,
UINT8 cascadeId,
UINT64  a_transferredBytes 
)

Marks this account as settled.

Todo:
what to do if there was a new CC stored while we were busy settling the old one?

References __markAsSettled(), checkOwner(), and E_UNKNOWN.

Referenced by CAAccountingInstance::__handleSettleResult(), and CAAccountingInstance::settlementTransaction().

◆ monitored_PQexec()

PGresult * CAAccountingDBInterface::monitored_PQexec ( PGconn *  conn,
const char *  query 
)
private

◆ releaseConnection()

SINT32 CAAccountingDBInterface::releaseConnection ( CAAccountingDBInterface dbIf)
static

◆ storeAccountStatus()

SINT32 CAAccountingDBInterface::storeAccountStatus ( UINT64  a_accountNumber,
UINT32  a_statusCode 
)

◆ storeCostConfirmation()

SINT32 CAAccountingDBInterface::storeCostConfirmation ( CAXMLCostConfirmation &  cc,
UINT8 ccCascade 
)

Creates the tables we need in the DB.

Returns
E_SUCCESS if all is OK
E_UNKNOWN if the query could not be executed
E_NOT_CONNECTED if we are not connected to the DB

References __storeCostConfirmation(), checkOwner(), and E_UNKNOWN.

Referenced by CAAccountingInstance::__handleSettleResult(), CAAccountingInstance::handleCostConfirmation_internal(), and CAAccountingInstance::settlementTransaction().

◆ storePrepaidAmount()

SINT32 CAAccountingDBInterface::storePrepaidAmount ( UINT64  accountNumber,
SINT32  prepaidBytes,
UINT8 cascadeId 
)

◆ terminateDBConnection()

SINT32 CAAccountingDBInterface::terminateDBConnection ( )
private

Terminates the database connection.

Returns
E_SUCCESS

References E_SUCCESS, m_bConnected, and m_dbConn.

Referenced by checkConnectionStatus(), cleanup(), and ~CAAccountingDBInterface().

◆ testAndResetOwner()

bool CAAccountingDBInterface::testAndResetOwner ( )
private

◆ testAndSetOwner()

bool CAAccountingDBInterface::testAndSetOwner ( )
private

Friends And Related Function Documentation

◆ CAAccountingInstance

friend class CAAccountingInstance
friend

Member Data Documentation

◆ m_bConnected

bool CAAccountingDBInterface::m_bConnected
private

◆ m_dbConn

PGconn* CAAccountingDBInterface::m_dbConn
private

◆ m_free

volatile bool CAAccountingDBInterface::m_free
private

◆ m_owner

volatile thread_id_t CAAccountingDBInterface::m_owner
private

◆ m_pConnectionMutex

CAMutex* CAAccountingDBInterface::m_pConnectionMutex
private

◆ m_protocolVersion

int CAAccountingDBInterface::m_protocolVersion
private

◆ ms_nextThreadNr

volatile UINT64 CAAccountingDBInterface::ms_nextThreadNr = 0
staticprivate

◆ ms_pConnectionAvailable

CAConditionVariable * CAAccountingDBInterface::ms_pConnectionAvailable = NULL
staticprivate

◆ ms_pDBConnectionPool

CAAccountingDBInterface * CAAccountingDBInterface::ms_pDBConnectionPool
staticprivate

Referenced by cleanup(), getConnection(), and init().

◆ ms_threadWaitNr

volatile UINT64 CAAccountingDBInterface::ms_threadWaitNr = 0
staticprivate