ReleaseFiles.cpp

См. документацию.
00001 /* 
00002  * File:   ReleaseFile.cpp
00003  * Author: nick
00004  * 
00005  * Created on 5 Сентябрь 2011 г., 18:01
00006  */
00007 
00008 #include "ReleaseFiles.h"
00009 //#include "main.h"
00010 
00011 /*ReleaseFile::ReleaseFile() {
00012 }
00013 
00014 ReleaseFile::ReleaseFile(const ReleaseFile& orig) {
00015 }
00016 
00017 ReleaseFile::~ReleaseFile() {
00018 }*/
00019 
00020 
00021 ReleaseFiles::ReleaseFiles(Server &protocol,ServerConfig* sc) {
00022         // TODO Auto-generated constructor stub
00023         this->sc=sc;
00024         this->protocol=&protocol;
00025         protocol.addIQHandler(this,ReleaseFile_xmlns);
00026 }
00027 
00028 void ReleaseFiles::handleTag(TagIQ &tag){
00029         if (tag.getType()==TagIQ::Get){
00030 
00031                 cout<<"Закрытие файла"<<endl;
00032                 ReleaseFile t(tag);
00033                 string dir;
00034                 fuse_file_info fi;
00035                 #ifdef debug_output
00036                 cout <<"GET FILE!!!"<<endl;
00037                 #endif
00038                 t.getFile(dir,fi);
00039 
00040                 #ifdef debug_output
00041                 cout <<"GET FILE OK!!!"<<endl;
00042                 #endif
00043                 ReleaseFile resrf(protocol->getCurNodeSend() ,"server",tag.getFrom(),TagIQ::Result,tag.getID());
00044                 //Если в списке файлов такой есть
00045                 int result=-1;
00046                 pthread_mutex_t* openedFiles_mutex=GlobalData::get_Instance()->get_openedFiles_mutex();
00047                 pthread_mutex_lock(openedFiles_mutex);
00048                 map<string,OpenedFilesInfo>* openedFiles=GlobalData::get_Instance()->get_OpenedFiles();
00049 
00050                 cout<<"Имя файла "<<dir<<endl;
00051                 cout<<"Все открытые файлы"<<endl;
00052                 map<string,OpenedFilesInfo>::iterator bi=openedFiles->begin();
00053                 while(bi!=openedFiles->end()){
00054                     cout<<"filename"<< bi->first<<endl;
00055                     ++bi;
00056                 }
00057                 cout<<"=================="<<endl;
00058                 map<string,OpenedFilesInfo>::iterator it= openedFiles->find(dir);
00059                 if(it!=openedFiles->end()){
00060                     cout<<"Файл найден в списке открытых файлов"<<endl;
00061                     //Если текущий клиент его открыл
00062                     map<unsigned int, unsigned int>::iterator ito=it->second.file_info.find(protocol->getClientID());
00063                     if(ito!=it->second.file_info.end()){
00064                         cout<<"Файл был открыт текущим клиентом, уменьшаем счетчик"<<endl;
00065                         result=0;                        
00066                         ito->second--;
00067                         cout <<"Файл открыт "<<ito->second<<" раз"<<endl;
00068                         //Если файл больше не открыт клиентом
00069                         if(ito->second==0){
00070                             cout <<"т.юк. Файл открыт 0 раз, этим клиентом, удаляем"<<endl;
00071                             it->second.file_info.erase(ito);
00072                             //Если файл не откыт ни одним клиентом
00073                             if(it->second.file_info.begin()==it->second.file_info.end()){
00074                                 cout <<"т.юк. Файл открыт 0 всеми клиентами удаляем его вообще"<<endl;
00075                                 openedFiles->erase(it);
00076                             }
00077                         }
00078                     }
00079                 }
00080                 pthread_mutex_unlock(openedFiles_mutex);
00081 /*                if (openedFiles.erase(dir)>0){
00082                     resrf.setResult(0);
00083                 }else{
00084                     resrf.setResult(-1);
00085                 }*/
00086                 resrf.setResult(result);
00087                 protocol->sendNode(resrf);
00088         }
00089 }
00090 
00091 ReleaseFiles::~ReleaseFiles() {
00092 // TODO Auto-generated destructor stub
00093     this->protocol->removeIQHandler(this);
00094 
00095     pthread_mutex_t* openedFiles_mutex=GlobalData::get_Instance()->get_openedFiles_mutex();
00096     pthread_mutex_lock(openedFiles_mutex);
00097     map<string,OpenedFilesInfo>* openedFiles=GlobalData::get_Instance()->get_OpenedFiles();
00098 
00099     map<string,OpenedFilesInfo>::iterator itb;//= openedFiles.begin();
00100     map<string,OpenedFilesInfo>::iterator itbb= openedFiles->begin();
00101     while(itbb!=openedFiles->end()){
00102         itb=itbb;
00103         ++itbb;
00104         map<unsigned int,unsigned int>::iterator itf=itb->second.file_info.find(protocol->getClientID());
00105         if(itf!=itb->second.file_info.end()){
00106             itb->second.file_info.erase(itf);
00107             if(itb->second.file_info.begin()==itb->second.file_info.end()){
00108                 openedFiles->erase(itb);
00109             }
00110         }
00111     }
00112     pthread_mutex_unlock(openedFiles_mutex);
00113 }