InterfaceInstance *GetInterface(const char *pzModelType) { if (m_nThreadMode == 1 || m_nThreadMode == 2) { if(m_nThreadMode == 1) { // wait for the one and only gthread_cond_wait(&m_cond, &m_condLock); } InterfaceInstance *pII = (InterfaceInstance *)m_lstInterfaces.First(); pII->InUse(1); return pII; } if (m_nThreadMode == 3) { // find one that's not in use gthread_mutex_lock(&m_lock); InterfaceInstance *pII = 0; GListIterator it(&m_lstInterfaces); while (it()) { pII = (InterfaceInstance *)it++; if ( !pII->InUse() ) { pII->InUse(1); gthread_mutex_unlock(&m_lock); return pII; // and return it } } // unlock the mutex while we create a new instance, this will take a while gthread_mutex_unlock(&m_lock); pII = new InterfaceInstance(pzModelType); pII->InUse(1); gthread_mutex_lock(&m_lock); // store the instance then return it m_lstInterfaces.AddLast(pII); gthread_mutex_unlock(&m_lock); return pII; } else if (m_nThreadMode == 4) { return new InterfaceInstance(pzModelType); } return 0; }
bool XMLObjectCache::tempToAlternate( __int64 nKey ) { GString strKey; strKey << nKey; bool bSuccess = false; gthread_mutex_lock(&m_csForeign); DynamicXMLObject *pDXO = (DynamicXMLObject *)m_cacheForeign->Lookup((const char *)strKey); if (pDXO->getUserLanguageObject()) { GString strAltKey; strAltKey << (__int64)(void *)pDXO->getUserLanguageObject(); m_cacheForeignAlternate->Insert((const char *)strAltKey,pDXO); } bSuccess = ( m_cacheForeign->RemoveKey((const char *)strKey) != 0 ); gthread_mutex_unlock(&m_csForeign); return bSuccess; }
void XMLObjectCache::dumpForeignCache() { TRACE_MESSAGE("****** Begin Foreign Object Instance Cache Dump ******"); gthread_mutex_lock(&m_csForeign); GHashIterator iter2(m_cacheForeignAlternate); while (iter2()) { DynamicXMLObject *pObj = (DynamicXMLObject *)iter2++; GString str; str.Format("Object In Alt-Index Cache - %s[%s]\n", pObj->GetObjectType(), pObj->GetObjectTag() ); TRACE_MESSAGE((const char *)str); } TRACE_MESSAGE("-----------------------------------------------\n"); GHashIterator iter(m_cacheForeign); while (iter()) { DynamicXMLObject *pObj = (DynamicXMLObject *)iter++; GString str; str << "Object In Cache - " << (__int64)(void *)pObj; TRACE_MESSAGE((const char *)str); } gthread_mutex_unlock(&m_csForeign); TRACE_MESSAGE("****** End Foreign Object Instance Cache Dump ******"); }
GString *XMLObjectCache::getState(const char *pzXMLTag, const char* oid) { // Synchronized with releaseState gthread_mutex_lock(&m_csState); GString* pObject = 0; if (oid && oid[0]) { XMLObjectFactoryManager &rFM = XMLObjectFactoryManager::getFactoryManager(); const char *pzClassName = rFM.GetSDKClassName(pzXMLTag); if (pzClassName) { if ( oid && oid[0] ) { GString strKey; strKey << oid << pzClassName; pObject = (GString*)m_cacheState->Lookup((const char *)strKey); } } } gthread_mutex_unlock(&m_csState); return pObject; }
bool XMLObjectCache::releaseForeign( DynamicXMLObject *pO ) { GString strKey; strKey << (__int64)(void *)pO; bool bSuccess = false; gthread_mutex_lock(&m_csForeign); bSuccess = ( m_cacheForeign->RemoveKey((const char *)strKey) != 0 ); void *pLangObject = pO->getUserLanguageObject(); if (pLangObject) { if (pLangObject != (void *)-1) { GString strAltKey; strAltKey << (__int64)pLangObject; m_cacheForeignAlternate->RemoveKey((const char *)strAltKey); } } gthread_mutex_unlock(&m_csForeign); return bSuccess; }
GString *XMLObjectCache::findState( const char* hashKey ) { gthread_mutex_lock(&m_csState); GString* pObject = 0; pObject = (GString *)m_cacheState->Lookup(hashKey); gthread_mutex_unlock(&m_csState); return pObject; }
// **************************************************************** // Applies to the Data cache (web pages, images, or other data) // **************************************************************** void XMLObjectCache::EnterCacheData(long length, void *pData, const char *pzKey) { gthread_mutex_lock(&m_csData); m_cacheData->Insert(pzKey, pData); GString strKey; strKey << (__int64)pData; m_cacheData->Insert((const char *)strKey, (void *)length); gthread_mutex_unlock(&m_csData); }
void XMLObjectCache::enterState(const char *oid, const char *pzClassName, const char *pState ) { releaseState(pzClassName,oid); GString strKey; strKey << oid << pzClassName; gthread_mutex_lock(&m_csState); m_cacheState->Insert((const char *)strKey, (void *)new GString(pState)); gthread_mutex_unlock(&m_csState); }
bool XMLObjectCache::releaseForeign( __int64 nUserObj ) { GString strAltKey; strAltKey << nUserObj; gthread_mutex_lock(&m_csForeign); m_cacheForeignAlternate->RemoveKey((const char *)strAltKey); gthread_mutex_unlock(&m_csForeign); return true; }
InterfaceInstance *GetInterfaceInstance(const char *pzModelType) { gthread_mutex_lock(&m_lock); InterfaceController *pIC = (InterfaceController *)m_hashCache.Lookup(pzModelType); gthread_mutex_unlock(&m_lock); if (!pIC) { // add a new one pIC = new InterfaceController(); pIC->AddInterface( new InterfaceInstance(pzModelType) ); gthread_mutex_lock(&m_lock); m_hashCache.Insert(pzModelType, (void *)pIC); gthread_mutex_unlock(&m_lock); } return pIC->GetInterface(pzModelType); }
void XMLObjectCache::enterForeign( DynamicXMLObject *pO ) { GString strKey; strKey << (__int64)(void *)pO; gthread_mutex_lock(&m_csForeign); m_cacheForeign->Insert((const char *)strKey, pO); gthread_mutex_unlock(&m_csForeign); }
void XMLObjectCache::emptyStateCache() { gthread_mutex_lock(&m_csState); GHashIterator iter(m_cacheState); while (iter()) { delete (GString *)iter++; } gthread_mutex_unlock(&m_csState); }
void AddInterface(InterfaceInstance *pI) { // m_lstInterfaces should be empty m_nThreadMode = pI->GetThreadMode(); pI->InUse(0); gthread_mutex_lock(&m_lock); m_lstInterfaces.AddLast(pI); gthread_mutex_unlock(&m_lock); }
DynamicXMLObject *XMLObjectCache::isForeign( DynamicXMLObject *pO ) { DynamicXMLObject *pReturn = 0; GString strKey; strKey << (__int64)(void *)pO; gthread_mutex_lock(&m_csForeign); pReturn = (DynamicXMLObject *)m_cacheForeign->Lookup((const char *)strKey); gthread_mutex_unlock(&m_csForeign); return pReturn; }
void *XMLObjectCache::UncacheData(const char* pzKey) { gthread_mutex_lock(&m_csData); bool bSuccess = 0; void *pData = m_cacheData->Lookup(pzKey); if (pData) { bSuccess = ( m_cacheData->RemoveKey(pzKey) != 0 ); } gthread_mutex_unlock(&m_csData); return pData; }
DynamicXMLObject *XMLObjectCache::DXOFromNative( __int64 ptr ) { DynamicXMLObject *pReturn; GString strKey; strKey << ptr; gthread_mutex_lock(&m_csForeign); pReturn = (DynamicXMLObject *)m_cacheForeignAlternate->Lookup( (const char *)strKey._str ); gthread_mutex_unlock(&m_csForeign); return pReturn; }
void XMLObjectCache::addAlternate( DynamicXMLObject *pDXO ) { if (pDXO->getUserLanguageObject()) { GString strAltKey; strAltKey << (__int64)(void *)pDXO->getUserLanguageObject(); gthread_mutex_lock(&m_csForeign); m_cacheForeignAlternate->Insert((const char *)strAltKey,pDXO); gthread_mutex_unlock(&m_csForeign); enterForeign( pDXO ); } }
void XMLObjectCache::dumpStateCache() { TRACE_MESSAGE("****** Begin Object State Cache Dump ******"); gthread_mutex_lock(&m_csState); GHashIterator iter(m_cacheState); while (iter()) { GString *pObj = (GString *)iter++; GString str; str.Format("State In Cache - %s", pObj->StrVal()); TRACE_MESSAGE((const char *)str); } gthread_mutex_unlock(&m_csState); TRACE_MESSAGE("****** End Object State Cache Dump ******"); }
void ReturnInterfaceInstance(InterfaceInstance *pII) { gthread_mutex_lock(&m_lock); InterfaceController *pIC = (InterfaceController *)m_hashCache.Lookup(pII->GetDriverName()); gthread_mutex_unlock(&m_lock); if (pIC) { pIC->ReturnInterface(pII); } else { // throw } }
void *XMLObjectCache::GetCacheData(const char *pzKey, long *length) { void *pRet; gthread_mutex_lock(&m_csData); pRet = m_cacheData->Lookup(pzKey); if (pRet && length) { GString strKey; strKey << (__int64)pRet; *length = (long)m_cacheData->Lookup((const char *)strKey); } gthread_mutex_unlock(&m_csData); return pRet; }
void XMLObjectCache::stateCacheStatus(GStringList *pDest) { gthread_mutex_lock(&m_csState); GHashIterator iter(m_cacheState); GString strCacheEntry; while (iter()) { GString* pObject = (GString*)iter++; if (pObject) { strCacheEntry += pObject->StrVal(); strCacheEntry += "\n"; } pDest->AddLast((const char *)strCacheEntry); } gthread_mutex_unlock(&m_csState); }
// remove a state entry from the cache bool XMLObjectCache::releaseState(const char* className,const char* oid) { // Synchronized with getState gthread_mutex_lock(&m_csState); bool bSuccess = false; if (className && oid) { GString strKey; strKey << oid << className; // take the object out of the cache, but don't delete it yet. bSuccess = ( m_cacheState->RemoveKey((const char *)strKey) != 0 ); } gthread_mutex_unlock(&m_csState); return bSuccess; }
void XMLObjectCache::foreignCacheStatus(GStringList *pDest) { gthread_mutex_lock(&m_csForeign); GHashIterator iter(m_cacheForeign); GString strCacheEntry; while (iter()) { DynamicXMLObject* pObject = (DynamicXMLObject*)iter++; if (pObject) { strCacheEntry += pObject->GetObjectType(); strCacheEntry += " "; strCacheEntry += pObject->GetObjectTag(); strCacheEntry += "\n"; } pDest->AddLast((const char *)strCacheEntry); } gthread_mutex_unlock(&m_csForeign); }
gerror_t database_load(database_t*& to, const std::string& dbname, const std::string& dbpass, bool autocreateIfInexistante) { // Read the whole file into a string FILE* dbfile = fopen(dbname.c_str(), "rb"); if(!dbfile) { if(autocreateIfInexistante) return database_create(to, dbname, dbpass); exit(GERROR_CANTOPENFILE); } // Initialize the database //to = (database_t*) malloc(sizeof(database_t)); fseek(dbfile, 0, SEEK_END); size_t sz = (size_t) ftell(dbfile); rewind(dbfile); #ifdef GULTRA_DEBUG cout << "File size = " << sz << endl; #endif char* buffer = (char*) malloc(sz); fread(buffer, 1, sz, dbfile); #ifdef GULTRA_DEBUG cout << "Buffer = " << buffer << endl; #endif // Close the file and process the buffer fclose(dbfile); std::string in(buffer, sz); free(buffer); std::string out; #ifdef GULTRA_DEBUG cout << "[database_load] in = " << (uint32_t) in.c_str()[3] << endl; #endif // Uncrypt buffer, creating the key std::string ikey; std::string iiv; Encryption::user_create_keypass(ikey, iiv, dbpass.c_str(), dbpass.length()); guncrypt(in, out, ikey); const char* cursor = out.c_str(); const char* begin = cursor; #ifdef GULTRA_DEBUG cout << "[database_load] decrypted = { "; gthread_mutex_unlock(&__console_mutex); for(unsigned int i = 0; i < out.length(); ++i) { cout << (uint32_t) cursor[i] << ":"; gthread_mutex_unlock(&__console_mutex); } cout << " }" << endl; #endif // Now, read everything. while (cursor - begin < out.length()) { // Process every block database_process_block(cursor, to, dbname, ikey, iiv); } /* else if(block.info.type == BT_ACCEPTED) { if(curuser) { // Normally, this block tell that a given user is already accepted. // We stores its name, its key/iv pair. database_accepted_user_t auser; database_readformattedstring(&auser.name, cursor); database_readformattedstring(&auser.keys.key, cursor); database_readformattedstring(&auser.keys.iv, cursor); curuser->acceptedusers.push_back(auser); } } } */ return GERROR_NONE; }
void XMLObjectCache::EmptyDataCache() { gthread_mutex_lock(&m_csData); m_cacheData->RemoveAll(); gthread_mutex_unlock(&m_csData); }