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

List of all members.

Public Member Functions

SINT32 storeCostConfirmation (CAXMLCostConfirmation &cc, UINT8 *ccCascade)
 Creates the tables we need in the DB.
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.
SINT32 markAsSettled (UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
 Marks this account as settled.
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)
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.

Static Public Member Functions

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

Private Member Functions

 CAAccountingDBInterface ()
 Constructor.
 ~CAAccountingDBInterface ()
 Destructor.
SINT32 __storeCostConfirmation (CAXMLCostConfirmation &cc, UINT8 *ccCascade)
 stores a cost confirmation in the DB
SINT32 __getCostConfirmation (UINT64 accountNumber, UINT8 *cascadeId, CAXMLCostConfirmation **pCC, bool &a_bSettled)
 Gets the latest cost confirmation stored for the given user account.
SINT32 __getUnsettledCostConfirmations (CAXMLCostConfirmation ***resultCCs, UINT8 *cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs)
 Fills the CAQueue with pointer to all non-settled cost confirmations.
SINT32 __markAsSettled (UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
 Marks this account as settled.
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.
SINT32 terminateDBConnection ()
 Terminates the database connection.
bool isDBConnected ()
bool checkConnectionStatus ()
 Checks if the connection still exists and tries to reconnect if not.
bool checkOwner ()
bool testAndSetOwner ()
bool testAndResetOwner ()
PGresult * monitored_PQexec (PGconn *conn, const char *query)

Private Attributes

PGconn * m_dbConn
 connection to postgreSQL database
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

Constructor.

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

Referenced by init().

Destructor.

References m_pConnectionMutex, and terminateDBConnection().


Member Function Documentation

SINT32 CAAccountingDBInterface::__checkCountAllQuery ( UINT8 a_query,
UINT32 r_count 
) [private]
SINT32 CAAccountingDBInterface::__deleteCC ( UINT64  accountNumber,
UINT8 cascadeId 
) [private]
SINT32 CAAccountingDBInterface::__getAccountStatus ( UINT64  a_accountNumber,
UINT32 a_statusCode 
) [private]
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, CAXMLCostConfirmation::getInstance(), m_dbConn, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, print64(), and CAMsg::printMsg().

Referenced by getCostConfirmation().

SINT32 CAAccountingDBInterface::__getPrepaidAmount ( UINT64  accountNumber,
UINT8 cascadeId,
bool  a_bDelete 
) [private]
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, CAXMLCostConfirmation::getInstance(), m_dbConn, monitored_PQexec(), MONITORING_FIRE_PAY_EVENT, and CAMsg::printMsg().

Referenced by getUnsettledCostConfirmations().

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().

SINT32 CAAccountingDBInterface::__storeAccountStatus ( UINT64  a_accountNumber,
UINT32  a_statusCode 
) [private]
SINT32 CAAccountingDBInterface::__storePrepaidAmount ( UINT64  accountNumber,
SINT32  prepaidBytes,
UINT8 cascadeId 
) [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().

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.

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().

SINT32 CAAccountingDBInterface::getAccountStatus ( UINT64  a_accountNumber,
UINT32 a_statusCode 
)
SINT32 CAAccountingDBInterface::getCostConfirmation ( UINT64  accountNumber,
UINT8 cascadeId,
CAXMLCostConfirmation **  pCC,
bool &  a_bSettled 
)
SINT32 CAAccountingDBInterface::getPrepaidAmount ( UINT64  accountNumber,
UINT8 cascadeId,
bool  a_bDelete 
)
SINT32 CAAccountingDBInterface::getUnsettledCostConfirmations ( CAXMLCostConfirmation ***  resultCCs,
UINT8 cascadeId,
UINT32 nrOfCCs,
UINT32  a_maxCCs 
)

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().

References m_bConnected.

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().

PGresult * CAAccountingDBInterface::monitored_PQexec ( PGconn *  conn,
const char *  query 
) [private]
SINT32 CAAccountingDBInterface::storeAccountStatus ( UINT64  a_accountNumber,
UINT32  a_statusCode 
)

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().

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

Terminates the database connection.

Returns:
E_SUCCESS

References E_SUCCESS, m_bConnected, and m_dbConn.

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


Friends And Related Function Documentation

friend class CAAccountingInstance [friend]

Member Data Documentation

volatile bool CAAccountingDBInterface::m_free [private]
volatile UINT64 CAAccountingDBInterface::ms_nextThreadNr = 0 [static, private]

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

volatile UINT64 CAAccountingDBInterface::ms_threadWaitNr = 0 [static, private]