Mixe for Privacy and Anonymity in the Internet
CAAccountingDBInterface.hpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification,
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice,
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice,
00011     this list of conditions and the following disclaimer in the documentation and/or
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
00015     may be used to endorse or promote products derived from this software without specific
00016     prior written permission.
00017 
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #ifndef __CAACCOUNTINGDBINTERFACE__
00029 #define __CAACCOUNTINGDBINTERFACE__
00030 #include "doxygen.h"
00031 #ifdef PAYMENT
00032 #include "CAQueue.hpp"
00033 #include "CAXMLCostConfirmation.hpp"
00034 #include "CAThread.hpp"
00035 
00036 #define MAX_DB_CONNECTIONS 3
00037 #define PG_PROTOCOL_VERSION_3 3
00038 
00039 #define RESULT_FORMAT_TEXT 0
00040 
00041 #define PREPARED_STMT_NAME_STORE_CC "storeCCStatement"
00042 #define PREPARED_STMT_QUERY_STORE_CC \
00043   "UPDATE COSTCONFIRMATIONS SET BYTES = $1::bigint, XMLCC = $2::varchar(2000), SETTLED = $3::integer WHERE ACCOUNTNUMBER = $4::bigint AND CASCADE = $5::varchar(200)"
00044 #define PREPARED_STMT_PARAMS_STORE_CC 5
00045 
00046 #define STMT_CLEAR_ACCOUNT_STATUS "DELETE FROM ACCOUNTSTATUS WHERE ACCOUNTNUMBER = '%llu'"
00047 
00054 class CAAccountingDBInterface
00055   {
00056   public:
00057 
00065     //SINT32 createTables();
00066     //SINT32 dropTables();
00067 
00068     SINT32 storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
00069 
00070     SINT32 getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
00071 
00072 
00077     //SINT32 getUnsettledCostConfirmations(CAQueue &q, UINT8* cascadeId);
00078     SINT32 getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
00079 
00084     SINT32 markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
00085 
00090     SINT32 deleteCC(UINT64 accountNumber, UINT8* cascadeId);
00091 
00092     SINT32 storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
00093     SINT32 getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
00094 
00095     SINT32 storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
00096     SINT32 getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
00097     SINT32 clearAccountStatus(UINT64 a_accountNumber);
00098 
00105     SINT32 checkCountAllQuery(UINT8* a_query, UINT32& r_count);
00106 
00107     /* Requests a DB connection. Blocks until it is available and returns a
00108      * DB Interface which is then owned by the requesting thread. In general the returned
00109      * interface is already connected but it is also returned if a connection could not be
00110      * established
00111      */
00112     static CAAccountingDBInterface *getConnection();
00113 
00114     /* Requests a DB connection. Returns a connected DB Interface
00115      * if it is available, which is then owned by the requesting thread.
00116      * Returns NULL otherwise.
00117      */
00118     //static CAAccountingDBInterface *getConnectionNB();
00119 
00120     /* Release the DBConnection which must be owned by the calling thread
00121      * Connection will not be disconnected
00122      */
00123     static SINT32 releaseConnection(CAAccountingDBInterface *dbIf);
00124 
00125     /* static initialization of the DBConnections */
00126     static SINT32 init();
00127     /* removes all DBconnections */
00128     static SINT32 cleanup();
00129 
00130     private:
00131 
00132       CAAccountingDBInterface();
00133       ~CAAccountingDBInterface();
00134 
00135       /* thread unsafe DB query functions */
00136       SINT32 __storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
00137       SINT32 __getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
00138 
00139       //SINT32 __getUnsettledCostConfirmations(CAQueue &q, UINT8* cascadeId);
00140       SINT32 __getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
00141 
00142       SINT32 __markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
00143       SINT32 __deleteCC(UINT64 accountNumber, UINT8* cascadeId);
00144 
00145       SINT32 __storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
00146       SINT32 __getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
00147 
00148       SINT32 __storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
00149       SINT32 __getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
00150       SINT32 __clearAccountStatus(UINT64 a_accountNumber);
00151 
00152       SINT32 __checkCountAllQuery(UINT8* a_query, UINT32& r_count);
00153 
00162       SINT32 initDBConnection();
00163 
00168       SINT32 terminateDBConnection();
00169       friend class CAAccountingInstance;
00170 
00171       bool isDBConnected();
00172 
00177       bool checkConnectionStatus();
00178 
00179       bool checkOwner();
00180 
00181       bool testAndSetOwner();
00182       bool testAndResetOwner();
00183 
00184       PGresult *monitored_PQexec(PGconn *conn, const char *query);
00185 
00187       PGconn * m_dbConn;
00188       bool m_bConnected;
00189 
00190       /* The owner of the connection */
00191       volatile thread_id_t m_owner;
00192       /* indicates wether this connection is not owned by a thread.
00193        * (There is no reliable value of m_owner to indicate this).
00194        */
00195       volatile bool m_free;
00196       int m_protocolVersion;
00197 #ifdef PG_PROTO_VERSION_3
00198       char *m_pStoreCCStmt;
00199 #endif
00200       /* to ensure atomic access to m_owner and m_free */
00201       CAMutex *m_pConnectionMutex;
00202 
00203 
00204       static CAConditionVariable *ms_pConnectionAvailable;
00205       /* WaitNr for a thread requesting a connection: if the ms_nextThreadNr equals the thread's
00206        * waitNumber it's his turn to obtain the next free connection
00207        */
00208       static volatile UINT64 ms_threadWaitNr;
00209       /* Indicates which thread obtains the next available connection */
00210       static volatile UINT64 ms_nextThreadNr;
00211 
00212       static CAAccountingDBInterface *ms_pDBConnectionPool[];
00213   };
00214 #endif //PAYMENT
00215 #endif