Mixe for Privacy and Anonymity in the Internet
CAAccountingDBInterface.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #ifndef __CAACCOUNTINGDBINTERFACE__
29 #define __CAACCOUNTINGDBINTERFACE__
30 #include "doxygen.h"
31 #ifdef PAYMENT
32 #include "CAQueue.hpp"
34 #include "CAThread.hpp"
35 
36 #define MAX_DB_CONNECTIONS 3
37 #define PG_PROTOCOL_VERSION_3 3
38 
39 #define RESULT_FORMAT_TEXT 0
40 
41 #define PREPARED_STMT_NAME_STORE_CC "storeCCStatement"
42 #define PREPARED_STMT_QUERY_STORE_CC \
43  "UPDATE COSTCONFIRMATIONS SET BYTES = $1::bigint, XMLCC = $2::varchar(2000), SETTLED = $3::integer WHERE ACCOUNTNUMBER = $4::bigint AND CASCADE = $5::varchar(200)"
44 #define PREPARED_STMT_PARAMS_STORE_CC 5
45 
46 #define STMT_CLEAR_ACCOUNT_STATUS "DELETE FROM ACCOUNTSTATUS WHERE ACCOUNTNUMBER = '%llu'"
47 
55  {
56  public:
57 
65  //SINT32 createTables();
66  //SINT32 dropTables();
67 
68  SINT32 storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
69 
70  SINT32 getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
71 
72 
77  //SINT32 getUnsettledCostConfirmations(CAQueue &q, UINT8* cascadeId);
78  SINT32 getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
79 
84  SINT32 markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
85 
90  SINT32 deleteCC(UINT64 accountNumber, UINT8* cascadeId);
91 
92  SINT32 storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
93  SINT32 getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
94 
95  SINT32 storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
96  SINT32 getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
97  SINT32 clearAccountStatus(UINT64 a_accountNumber);
98 
105  SINT32 checkCountAllQuery(UINT8* a_query, UINT32& r_count);
106 
107  /* Requests a DB connection. Blocks until it is available and returns a
108  * DB Interface which is then owned by the requesting thread. In general the returned
109  * interface is already connected but it is also returned if a connection could not be
110  * established
111  */
113 
114  /* Requests a DB connection. Returns a connected DB Interface
115  * if it is available, which is then owned by the requesting thread.
116  * Returns NULL otherwise.
117  */
118  //static CAAccountingDBInterface *getConnectionNB();
119 
120  /* Release the DBConnection which must be owned by the calling thread
121  * Connection will not be disconnected
122  */
124 
125  /* static initialization of the DBConnections */
126  static SINT32 init();
127  /* removes all DBconnections */
128  static SINT32 cleanup();
129 
130  private:
131 
134 
135  /* thread unsafe DB query functions */
136  SINT32 __storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8* ccCascade);
137  SINT32 __getCostConfirmation(UINT64 accountNumber, UINT8* cascadeId, CAXMLCostConfirmation **pCC, bool& a_bSettled);
138 
139  //SINT32 __getUnsettledCostConfirmations(CAQueue &q, UINT8* cascadeId);
140  SINT32 __getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8* cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs);
141 
142  SINT32 __markAsSettled(UINT64 accountNumber, UINT8* cascadeId, UINT64 a_transferredBytes);
143  SINT32 __deleteCC(UINT64 accountNumber, UINT8* cascadeId);
144 
145  SINT32 __storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8* cascadeId);
146  SINT32 __getPrepaidAmount(UINT64 accountNumber, UINT8* cascadeId, bool a_bDelete);
147 
148  SINT32 __storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode);
149  SINT32 __getAccountStatus(UINT64 a_accountNumber, UINT32& a_statusCode);
150  SINT32 __clearAccountStatus(UINT64 a_accountNumber);
151 
152  SINT32 __checkCountAllQuery(UINT8* a_query, UINT32& r_count);
153 
163 
169  friend class CAAccountingInstance;
170 
171  bool isDBConnected();
172 
177  bool checkConnectionStatus();
178 
179  bool checkOwner();
180 
181  bool testAndSetOwner();
182  bool testAndResetOwner();
183 
184  PGresult *monitored_PQexec(PGconn *conn, const char *query);
185 
187  PGconn * m_dbConn;
189 
190  /* The owner of the connection */
192  /* indicates wether this connection is not owned by a thread.
193  * (There is no reliable value of m_owner to indicate this).
194  */
195  volatile bool m_free;
197 #ifdef PG_PROTO_VERSION_3
198  char *m_pStoreCCStmt;
199 #endif
200  /* to ensure atomic access to m_owner and m_free */
202 
203 
205  /* WaitNr for a thread requesting a connection: if the ms_nextThreadNr equals the thread's
206  * waitNumber it's his turn to obtain the next free connection
207  */
208  static volatile UINT64 ms_threadWaitNr;
209  /* Indicates which thread obtains the next available connection */
210  static volatile UINT64 ms_nextThreadNr;
211 
213  };
214 #endif //PAYMENT
215 #endif
unsigned long long thread_id_t
Type of an ID for a thread which can be used to identify the current and other threads.
Definition: CAThread.hpp:70
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8 *cascadeId)
static CAAccountingDBInterface * getConnection()
static CAAccountingDBInterface * ms_pDBConnectionPool[]
SINT32 __clearAccountStatus(UINT64 a_accountNumber)
SINT32 __storePrepaidAmount(UINT64 accountNumber, SINT32 prepaidBytes, UINT8 *cascadeId)
SINT32 initDBConnection()
Initiates the database connection.
SINT32 checkCountAllQuery(UINT8 *a_query, UINT32 &r_count)
Takes and executes a query that counts databae records and tests if the result is valid.
SINT32 __markAsSettled(UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
Marks this account as settled.
SINT32 storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8 *ccCascade)
Creates the tables we need in the DB.
SINT32 clearAccountStatus(UINT64 a_accountNumber)
bool checkConnectionStatus()
Checks if the connection still exists and tries to reconnect if not.
SINT32 __getCostConfirmation(UINT64 accountNumber, UINT8 *cascadeId, CAXMLCostConfirmation **pCC, bool &a_bSettled)
Gets the latest cost confirmation stored for the given user account.
static volatile UINT64 ms_threadWaitNr
SINT32 __storeCostConfirmation(CAXMLCostConfirmation &cc, UINT8 *ccCascade)
stores a cost confirmation in the DB
SINT32 __getAccountStatus(UINT64 a_accountNumber, UINT32 &a_statusCode)
SINT32 __checkCountAllQuery(UINT8 *a_query, UINT32 &r_count)
SINT32 __getPrepaidAmount(UINT64 accountNumber, UINT8 *cascadeId, bool a_bDelete)
static SINT32 releaseConnection(CAAccountingDBInterface *dbIf)
static CAConditionVariable * ms_pConnectionAvailable
SINT32 getCostConfirmation(UINT64 accountNumber, UINT8 *cascadeId, CAXMLCostConfirmation **pCC, bool &a_bSettled)
SINT32 getAccountStatus(UINT64 a_accountNumber, UINT32 &a_statusCode)
SINT32 __deleteCC(UINT64 accountNumber, UINT8 *cascadeId)
PGconn * m_dbConn
connection to postgreSQL database
SINT32 __getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8 *cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs)
Fills the CAQueue with pointer to all non-settled cost confirmations.
SINT32 __storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode)
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 dat...
PGresult * monitored_PQexec(PGconn *conn, const char *query)
static volatile UINT64 ms_nextThreadNr
SINT32 getUnsettledCostConfirmations(CAXMLCostConfirmation ***resultCCs, UINT8 *cascadeId, UINT32 *nrOfCCs, UINT32 a_maxCCs)
Fills the CAQueue with all non-settled cost confirmations.
SINT32 storeAccountStatus(UINT64 a_accountNumber, UINT32 a_statusCode)
SINT32 markAsSettled(UINT64 accountNumber, UINT8 *cascadeId, UINT64 a_transferredBytes)
Marks this account as settled.
SINT32 getPrepaidAmount(UINT64 accountNumber, UINT8 *cascadeId, bool a_bDelete)
SINT32 terminateDBConnection()
Terminates the database connection.
This is the AI (accounting instance or abrechnungsinstanz in german) class.