00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef __CAACCOUNTINGINSTANCE__
00032 #define __CAACCOUNTINGINSTANCE__
00033 #include "doxygen.h"
00034 #ifdef PAYMENT
00035 #include "CAFirstMix.hpp"
00036 #include "CAFirstMixChannelList.hpp"
00037 #include "CASymCipher.hpp"
00038 #include "CAQueue.hpp"
00039 #include "CAThread.hpp"
00040 #include "CATempIPBlockList.hpp"
00041 #include "CAThreadPool.hpp"
00042 #include "CAAccountingDBInterface.hpp"
00043 #include "CAAccountingBIInterface.hpp"
00044 #include "CAAccountingControlChannel.hpp"
00045 #include "CAAccountingSettleThread.hpp"
00046 #include "CACmdLnOptions.hpp"
00047 #include "Hashtable.hpp"
00048 #include "CAMix.hpp"
00049 #include "xml/DOM_Output.hpp"
00050 #include "CAStatusManager.hpp"
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define CHALLENGE_TIMEOUT 15
00061 #define HARD_LIMIT_TIMEOUT 30
00062 #define AUTH_TIMEOUT 15
00063 #define CRITICAL_SUBSEQUENT_BI_CONN_ERRORS 5
00064
00065 #define CLIENT_VERSION_STR_LEN 20
00066 #define PREPAID_PROTO_CLIENT_VERSION "00.10.057"
00067
00068 #define CRITICAL_SETTLE_FLAGS \
00069 (AUTH_INVALID_ACCOUNT | AUTH_ACCOUNT_EMPTY | AUTH_BLOCKED | AUTH_UNKNOWN)
00070
00071 struct t_fmhashtableentry;
00072
00073 struct AccountLoginHashEntry
00074 {
00075 UINT64 accountNumber;
00076 UINT64 confirmedBytes;
00077 UINT64 userID;
00078 UINT32 authFlags;
00079 UINT32 authRemoveFlags;
00080 UINT32 count;
00081 CAMutex *ownerLock;
00082 struct t_fmhashtableentry *ownerRef;
00083 bool loginOngoing;
00084 };
00085
00086 struct SettleEntry
00087 {
00088 UINT64 accountNumber;
00089 UINT32 authFlags;
00090 UINT32 authRemoveFlags;
00091 UINT64 confirmedBytes;
00092 UINT64 diffBytes;
00093 SettleEntry* nextEntry;
00094 SINT32 storedStatus;
00095 };
00096
00097 bool testAndSetLoginOwner(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00098 bool resetLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00099 bool testLoginEntryOwner(struct AccountLoginHashEntry *loginEntry, fmHashTableEntry *ownerRef);
00100 bool isLoginOngoing(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00101 inline bool testLoginEntryOwner_internal(struct AccountLoginHashEntry *loginEntry, struct t_fmhashtableentry *ownerRef);
00102
00112 class CAAccountingInstance
00113 {
00114 friend class CAFirstMixA;
00115 friend class CAFirstMix;
00116 public:
00117
00121 static SINT32 init(CAFirstMix* callingMix)
00122 {
00123 ms_pInstance = new CAAccountingInstance(callingMix);
00124 MONITORING_FIRE_PAY_EVENT(ev_pay_aiInited);
00125 return E_SUCCESS;
00126 }
00127
00128 static SINT32 clean()
00129 {
00130 if(ms_pInstance != NULL)
00131 {
00132 delete ms_pInstance;
00133 ms_pInstance=NULL;
00134 }
00135 if(m_preparedCCRequest != NULL)
00136 {
00137 m_preparedCCRequest->release();
00138 m_preparedCCRequest = NULL;
00139 }
00140 MONITORING_FIRE_PAY_EVENT(ev_pay_aiShutdown);
00141 return E_SUCCESS;
00142 }
00143
00147 static UINT32 getAuthFlags(fmHashTableEntry * pHashEntry);
00148
00153 static SINT32 cleanupTableEntry(fmHashTableEntry * pHashEntry);
00154 static SINT32 initTableEntry(fmHashTableEntry * pHashEntry);
00155
00159 static SINT32 handleJapPacket(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
00160
00167 static SINT32 isIPAddressBlocked(const UINT8 ip[4])
00168 {
00169
00170 return 0;
00171 }
00172
00180 static SINT32 processJapMessage(fmHashTableEntry * pHashEntry,const XERCES_CPP_NAMESPACE::DOMDocument* a_DomDoc);
00181 static UINT32 getNrOfUsers();
00182
00183 static SINT32 loginProcessStatus(fmHashTableEntry *pHashEntry);
00184 static SINT32 finishLoginProcess(fmHashTableEntry *pHashEntry);
00185
00186
00187 static SINT32 settlementTransaction();
00188 static SettleEntry *__handleSettleResult(CAXMLCostConfirmation *pCC, CAXMLErrorMessage *pErrMsg, CAAccountingDBInterface *dbInterface,
00189 UINT64 a_iSettlementTransactionNr);
00190 static void __commitSettlementToDatabase(SettleEntry *entryList, CAAccountingDBInterface *dbInterface);
00191 static void __commitSettlementToLoginTable(SettleEntry *entryList);
00192 static SINT32 newSettlementTransaction();
00193 static SINT32 __newSettlementTransaction(UINT32 *nrOfSettledCCs);
00194
00195
00196 static const SINT32 HANDLE_PACKET_CONNECTION_OK;
00197 static const SINT32 HANDLE_PACKET_CONNECTION_UNCHECKED;
00198 static const SINT32 HANDLE_PACKET_HOLD_CONNECTION;
00199 static const SINT32 HANDLE_PACKET_PREPARE_FOR_CLOSING_CONNECTION;
00200 static const SINT32 HANDLE_PACKET_CLOSE_CONNECTION;
00201
00202
00203 static const UINT32 MAX_SETTLED_CCS;
00204 private:
00205
00206 CAAccountingInstance(CAFirstMix* callingMix);
00207 ~CAAccountingInstance();
00208
00209 struct t_aiqueueitem
00210 {
00211 XERCES_CPP_NAMESPACE::DOMDocument* pDomDoc;
00212 tAiAccountingInfo* pAccInfo;
00213 void (CAAccountingInstance::*handleFunc)(tAiAccountingInfo*,DOMElement*);
00214 };
00215 typedef struct t_aiqueueitem aiQueueItem;
00216
00217 static SINT32 handleJapPacket_internal(fmHashTableEntry *pHashEntry, bool a_bControlMessage, bool a_bMessageToJAP);
00218
00219 static UINT64 unlockLogin(fmHashTableEntry *ownerRef);
00220 static void processJapMessageLoginHelper(fmHashTableEntry *pHashEntry,
00221 UINT32 handlerReturnvalue,
00222 bool finishLogin);
00226 UINT32 handleCostConfirmation(tAiAccountingInfo* pAccInfo, DOMElement* root );
00227 UINT32 handleCostConfirmation_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
00228
00232 UINT32 handleAccountCertificate(tAiAccountingInfo* pAccInfo, DOMElement* root );
00233 UINT32 handleAccountCertificate_internal(tAiAccountingInfo* pAccInfo, DOMElement* root );
00234
00235
00239 UINT32 handleChallengeResponse(tAiAccountingInfo* pAccInfo, DOMElement* root);
00240 UINT32 handleChallengeResponse_internal(tAiAccountingInfo* pAccInfo, DOMElement* root);
00241
00242 bool cascadeMatchesCC(CAXMLCostConfirmation *pCC);
00243
00244 static SINT32 getPrepaidBytes(tAiAccountingInfo* pAccInfos);
00245 SINT32 prepareCCRequest(CAMix* callingMix, UINT8* a_AiName);
00246 static SINT32 makeInitialCCRequest(CAXMLCostConfirmation *pCC, XERCES_CPP_NAMESPACE::DOMDocument* & doc, SINT32 prepaidBytes);
00247 static SINT32 makeCCRequest( const UINT64 accountNumber, const UINT64 transferredBytes, XERCES_CPP_NAMESPACE::DOMDocument* & doc);
00248 static SINT32 sendInitialCCRequest(tAiAccountingInfo* pAccInfo, CAXMLCostConfirmation *pCC, SINT32 prepaidBytes);
00249 static SINT32 sendCCRequest(tAiAccountingInfo* pAccInfo);
00250 static SINT32 makeAccountRequest( XERCES_CPP_NAMESPACE::DOMDocument* & doc);
00251 static SINT32 sendAILoginConfirmation(tAiAccountingInfo* pAccInfo, const UINT32 code, UINT8 * message);
00252
00253
00254 static SINT32 returnKickout(tAiAccountingInfo* pAccInfo);
00255 static SINT32 returnPrepareKickout(tAiAccountingInfo* pAccInfo, CAXMLErrorMessage* a_error);
00256
00257 static void setPrepaidBytesToZero(tAiAccountingInfo* pAccInfo);
00258
00259 static inline void setPrepaidBytesToZero_internal(tAiAccountingInfo* pAccInfo);
00260
00265
00266
00268 static THREAD_RETURN processThread(void* a_param);
00269
00270 static const UINT64 PACKETS_BEFORE_NEXT_CHECK;
00271
00272 static const UINT32 MAX_TOLERATED_MULTIPLE_LOGINS;
00273
00274 static XERCES_CPP_NAMESPACE::DOMDocument* m_preparedCCRequest;
00275
00277 CAThreadPool* m_aiThreadPool;
00278
00280 CAMutex *m_pMutex;
00281
00282 Hashtable* m_certHashCC;
00283
00285 Hashtable* m_currentAccountsHashtable;
00286 CAFirstMix *m_mix;
00287
00289 UINT8* m_AiName;
00290
00292
00293
00294 UINT8* m_currentCascade;
00295
00297 UINT8** m_allHashes;
00298 UINT32 m_allHashesLen;
00299
00301
00302 CAAccountingBIInterface *m_pPiInterface;
00303
00304 UINT32 m_iSoftLimitBytes;
00305 UINT32 m_iHardLimitBytes;
00306
00313 CATempIPBlockList * m_pIPBlockList;
00314
00318 static CAAccountingInstance * ms_pInstance;
00319
00324
00325
00327 CAAccountingSettleThread * m_pSettleThread;
00328
00329 static SINT32 m_prepaidBytesMinimum;
00330
00331 bool m_bThreadRunning;
00332
00333
00334
00335
00336 volatile UINT64 m_nextSettleNr;
00337 volatile UINT64 m_settleWaitNr;
00338 CAConditionVariable *m_pSettlementMutex;
00339
00340 volatile UINT32 m_seqBIConnErrors;
00341
00342 static volatile UINT64 m_iCurrentSettleTransactionNr;
00343 };
00344
00345
00346 #endif
00347 #endif //Payment