ReadFiles.cpp

См. документацию.
00001 /*
00002  * ReadFiles.cpp
00003  *
00004  *  Created on: 14.06.2011
00005  *      Author: nick
00006  */
00007 
00008 #include <errno.h>
00009 
00010 #include "ReadFiles.h"
00011 
00012 ReadFiles::ReadFiles(Protocol &protocol, ServerConfig* sc, BinaryProtocol* bp) {
00013     this->sc = sc;
00014     this->bp = bp;
00015     this->protocol = &protocol;    
00016     this->max_readsize=sc->getModuleAttributeInt(L"lib_file_opertion",L"max_read_size",409600);
00017     cout<<"max_readsize"<<max_readsize<<endl;
00018     protocol.addIQHandler(this, ReadFile_xmlns);
00019 }
00020 
00021 void ReadFiles::handleTag(TagIQ &tag) {
00022     //Node* parent=NULL,string from="", string to="", IQType type=TagIQ::Get, string id=""
00023     TagIQ::IQType t = tag.getType();
00024     //
00025     #ifdef debug_output
00026     cout << "STEP1 read" << endl;
00027     #endif
00028     if (t == TagIQ::Set) {
00029         //==============
00030         string path;
00031         size_t size;
00032         size_t offset;
00033         fuse_file_info fi;
00034         //================
00035         int fDesc;
00036         struct stat fb;
00037 
00038         //Открыть файл перейти в опредиленную позицию,
00039         //посчитать сколько можно прочесть, вернуть ответ(Запрос на чтение)
00040         ReadFile rf(tag);
00041         #ifdef debug_output
00042         cout << "STEP2 read" << endl;
00043         #endif
00044         //ReadFile(Node* parent=NULL,string from="", string to="", IQType type=TagIQ::Get, string id="");
00045         ReadFile rfResp(NULL, "server", "client", TagIQ::Get, tag.getID());
00046         rf.getFileData(path, offset, size, fi);
00047         #ifdef debug_output
00048         cout << "path:" << path << " offset:" << offset << "size:" << size << endl;
00049         #endif
00050         string dpath = sc->getSharedDir() + path;
00051         //const char* dpath=(sc->getSharedDir()+path).c_str();
00052         fDesc = open(dpath.c_str(), O_RDONLY/*fi.flags*/);
00053         if (fDesc == -1) {
00054             cout << "erroropenfile!" << errno << endl;
00055             cout << "filename" << dpath << endl;
00056         }
00057 
00058         if (fstat(fDesc, &fb) == -1) {
00059             cout << "fstat error" << errno << endl;
00060         }
00061         size_t szforread = fb.st_size - offset;
00062         #ifdef debug_output
00063         cout << "szforread" << szforread << "file size" << fb.st_size << endl;
00064         #endif
00065         if (szforread < 0) {
00066             #ifdef debug_output
00067             cout << "ELSE1 READ" << endl;
00068             #endif
00069             szforread = 0;
00070         } else if (size < szforread) {
00071             #ifdef debug_output
00072             cout << "ELSE2 READ" << endl;
00073             #endif
00074             szforread = size;
00075         }
00076         #ifdef debug_output
00077         cout << "STEP3 read" << endl;
00078         #endif
00079         unsigned long int pktID = bp->getNextId();
00080         rfResp.setResult(pktID, szforread);
00081         if (szforread == 0) {
00082             protocol->sendNode(rfResp);
00083         } else {
00084             //protocol->sendNode(rfResp);
00085             #ifdef debug_output
00086             cout << "lalal1" << endl;
00087             #endif
00088             TagIQ rt = protocol->sendIQResp(rfResp, ReadFile_xmlns, QUERY_TAG);
00089             #ifdef debug_output
00090             cout << "!!!!lalalal!!! namespace:" << rt.getNamesapace() << "typeIQ:" << rt.getType() << endl;
00091             #endif
00092             if ((rt.getKind(ReadFile_xmlns) != NULL) && (rt.getType() == TagIQ::Result)) {
00093 
00094                 /*lseek(fDesc,offset,SEEK_SET);
00095                 ssize_t sr1=szforread;
00096                 ssize_t sr=0;//Результат чтения за под раз
00097                 void* buf=malloc(szforread);
00098                 char* buf1=(char*)buf;
00099                 do{
00100                         cout<<"цикл1"<<endl;
00101                         sr=read(fDesc,buf1,sr1);
00102                         if(sr!=-1){
00103                                 sr1-=sr;
00104                                 buf1+=sr;
00105                         }
00106                         if(sr==0){
00107                                 cout<<"конец файла"<<endl;
00108                         }
00109                 }while ((sr1>0)&&(sr>0));
00110                 bp->setSendBufferData(buf,szforread-sr1,pktID);
00111                 free(buf);*/
00112                 #ifdef debug_output
00113                 cout << "OK, sending file data" << endl;
00114                 #endif
00115                 //TODO нечать передачу по бинарному протоколу
00116                 //Максимальный размер который можно с диска прочитать за раз
00117                 //size_t max_readSize = 4096000;
00118                 //Размер яитаемый за раз, если он больше максимального, присвоить максимальный
00119                 size_t rzize = szforread;
00120                 if (rzize > max_readsize) {
00121                     rzize = max_readsize;
00122                 }
00123 
00124                 //Выделяем буфер для чтения за раз
00125                 void* buf = malloc(rzize);
00126                 //Перемещаем на нужное место указатель в файле
00127                 lseek(fDesc, offset, SEEK_SET);
00128 
00129                 //Сколько надо всего прочитать
00130                 size_t rzizeall = szforread;
00131                 while (rzizeall > 0) {
00132                     //Текущая позиция в буфере
00133                     char* buf1 = (char*) buf;
00134                     ssize_t sr = 0; //Результат чтения за под раз
00135                     ssize_t sr1 = rzize; //сколько нужно прочесть за раз
00136                     do {//Читаем по подразу пока не прочтем за раз
00137                         #ifdef debug_output
00138                         cout << "цикл1" << endl;
00139                         #endif
00140                         sr = read(fDesc, buf1, sr1);
00141                         if (sr != -1) {
00142                             sr1 -= sr;
00143                             buf1 += sr;
00144                         }
00145                     } while ((sr1 > 0) && (sr > 0));
00146                     //отправлеяем пакет который прочли за раз
00147                     //Сколько нужно прочесть - сколько прочесть не вышло
00148                     bp->setSendBufferData(buf, rzize - sr1, pktID);
00149                     rzizeall -= (rzize - sr1);
00150                     #ifdef debug_output
00151                     cout << "sr1" << sr1 << endl;
00152                     #endif
00153                     if (sr == -1) {
00154                         cout << "ОШИБКА ЧТЕНИЯ!" << errno << endl;
00155                         //Если была ошибка чтения то нужно прервать чтение
00156                         break;
00157                     }
00158                     if (sr == 0) {
00159                         cout << "Конец файла!" << endl;
00160                         break;
00161                     }
00162                     #ifdef debug_output
00163                     cout << "цикл2" << endl;
00164                     #endif
00165                 }
00166                 free(buf);
00167             } else {
00168                 cout << "!!Нет ответа от клиента о подетверждении передачи содержимого!!!!" << endl;
00169             }
00170         }
00171         close(fDesc);
00172         #ifdef debug_output
00173         cout << "STEPFINAL read" << endl;
00174         #endif
00175     }
00176     #ifdef debug_output
00177     cout << "STEP END read" << endl;
00178     #endif
00179 }
00180 
00181 ReadFiles::ReadFiles() {
00182     // TODO Auto-generated constructor stub
00183 
00184 }
00185 
00186 ReadFiles::~ReadFiles() {
00187     // TODO Auto-generated destructor stub
00188     this->protocol->removeIQHandler(this);
00189 }