/** ****************************************************************************** @file /common/rm/rm.cpp @brief Spolecny predek ke vsem RM, definice cest ke zdrojum @author Vta @version 1.0 ******************************************************************************/ #include "rm.h" using namespace rm; int TRM::startNewGroup() { this->lock(); this->group++; this->unlock(); return this->group; } int TRM::getCurrentGroup() { this->lock(); int ret=this->group; this->unlock(); return ret; } int TRM::getGroupOfObject(int id) { this->lock(); int ret=(*this->groups)[id]; this->unlock(); return ret; } void TRM::writeGroup(int id) { this->lock(); (*this->groups)[id]=this->group; this->unlock(); } int TRM::freeLastGroup() { this->lock(); int j; for (j=this->groups->getNext(-1);j!=-1;j=this->groups->getNext(j)) // pres vsechny { if ((*this->groups)[j]==this->group) { this->freeData(j); } } this->group--; this->unlock(); return this->group; } void TRM::freeHavingGroup(int group) { this->lock(); int j; for (j=this->groups->getNext(-1);j!=-1;j=this->groups->getNext(j)) // pres vsechny { if ((*this->groups)[j]==group) // pokud je to ta spravana skupina { this->freeData(j); } } this->unlock(); } int TRM::readInfoFile(char *file) { this->lock(); int i=this->index->readFile(file); this->unlock(); return i; } int TRM::requestAll() { this->lock(); DA ids; this->index->getListOfIds(&ids,0,0,NULL); int count=0; int i; for (i=ids.getNext(-1);i!=-1;i=ids.getNext(i)) { count++; (*(this->buffer))[this->buffer->getMaxId()+1]=i; } this->unlock(); return count; } int TRM::request(int id) { this->lock(); (*(this->buffer))[this->buffer->getMaxId()+1]=id; int rq=(this->buffer->getMaxId()+1); this->unlock(); return rq; } void TRM::loadBuffer(void) { this->lock(); int max=this->buffer->getMaxId(); this->toload=max; int i; for (i=0;i<=max;i++) { try { this->loadData((*(this->buffer))[i]); GLOBALLOGID(PRIORITY_LOADING_QUEUE,"Loading object having id: %i",(*(this->buffer))[i]); this->status=i; } catch (E_8K_RM e) { this->status=-1; this->buffer->reset(); this->buffer->resize(10); THROW(E_8K_RM,e.getDescription()); } } this->buffer->reset(); this->buffer->resize(10); // zmensim velikost, buffer se nebude prilis casto this->unlock(); } TRM::TRM() { if ( (this->mutex=SDL_CreateMutex()) == NULL ) { GLOBALLOGID(PRIORITY_FATAL,"The mutex can not be created"); THROW(E_8K_RM,"The mutex can not be created"); } this->buffer=new DA; this->groups=new DA; this->index=new TXMLdata(1); this->status=-1; this->toload=-1; this->group=1; } int TRM::getStatus() { // toto neni v locku, musi se na to pristupovat z vlaken return this->status; } int TRM::getObjectsToLoad() { // toto neni v locku, musi se na to pristupovat z vlaken return this->toload; } TRM::~TRM() { delete (this->buffer); delete (this->index); delete (this->groups); SDL_DestroyMutex(mutex); } int TRM::saveToFile(char * pack,char * file) { char path[K8_MAX_PATH_LEN]; // toto nemuze byt globalni this->lock(); int retvalue; strpartpath(path,RESOURCES_DIR,pack,XML_DIR,file,K8_MAX_PATH_LEN,".xml"); retvalue=this->index->saveToFile(path); this->unlock(); return retvalue; } void TRM::lock(void) { if ( SDL_mutexP(this->mutex) < 0 ) { GLOBALLOGID(PRIORITY_FATAL, "The mutex can not be locked"); THROW(E_8K_RM,"The mutex can not be locked"); } GLOBALLOGID(PRIORITY_MUTEX,"Locked %i\n",SDL_ThreadID()); } void TRM::unlock(void) { if ( SDL_mutexV(this->mutex) < 0 ) { GLOBALLOGID(PRIORITY_FATAL, "The mutex can not be unlocked"); THROW(E_8K_RM,"The mutex can not be unlocked"); } GLOBALLOGID(PRIORITY_MUTEX,"Unlocked %i\n",SDL_ThreadID()); }