WriteFiles.cpp

См. документацию.
00001 /* 
00002  * File:   WriteFiles.cpp
00003  * Author: nick
00004  * 
00005  * Created on 9 Сентябрь 2011 г., 16:29
00006  */
00007 
00008 #include "WriteFiles.h"
00009 #include <errno.h>
00010 
00011 WriteFiles::WriteFiles(Protocol &protocol, ServerConfig* sc, BinaryProtocol* bp) {
00012     this->sc = sc;
00013     this->bp = bp;
00014     this->protocol = &protocol;
00015     this->max_readsize=sc->getModuleAttributeInt(L"lib_file_opertion",L"max_read_size",409600);
00016     cout<<"max_readsize"<<max_readsize<<endl;
00017     protocol.addIQHandler(this, WriteFile_xmlns);
00018 }
00019 
00020 void WriteFiles::handleTag(TagIQ &tag){
00021     //Node* parent=NULL,string from="", string to="", IQType type=TagIQ::Get, string id=""
00022     TagIQ::IQType t = tag.getType();
00023     //
00024     #ifdef debug_output
00025     cout << "STEP1 read" << endl;
00026     #endif
00027     if (t == TagIQ::Set) {
00028         //==============
00029         string path;
00030         size_t size;
00031         size_t offset;
00032         fuse_file_info fi;
00033         //================
00034         int fDesc;
00035         struct stat fb;
00036 
00037         //Открыть файл перейти в опредиленную позицию,
00038         //посчитать сколько можно прочесть, вернуть ответ(Запрос на чтение)
00039         WriteFile wf(tag);
00040         #ifdef debug_output
00041         cout << "STEP2 read" << endl;
00042         #endif
00043         //ReadFile(Node* parent=NULL,string from="", string to="", IQType type=TagIQ::Get, string id="");
00044         WriteFile wfResp(NULL, "server", "client", TagIQ::Set, tag.getID());
00045         wf.getFileData(path, offset, size, fi);
00046         #ifdef debug_output
00047         cout << "path:" << path << " offset:" << offset << "size:" << size << endl;
00048         #endif
00049         string dpath = sc->getSharedDir() + path;
00050         //const char* dpath=(sc->getSharedDir()+path).c_str();
00051         fDesc = open(dpath.c_str(), O_RDWR/*fi.flags*/ );
00052        if (fDesc == -1) {
00053             cout << "erroropenfile!" << errno << endl;
00054             cout << "filename" << dpath << endl;
00055         }
00056         
00057         lseek(fDesc,offset,SEEK_SET);
00058         
00059         unsigned int pktID=bp->getNextId();
00060 //        size_t szforread=size;
00061 //        while(szforread>0){
00062 //        size_t szoneread=szforread;
00063 //            if (szoneread>max_readsize){
00064 //                szoneread=max_readsize;
00065 //            }
00066             void* buf1= malloc(size);
00067             if (buf1==NULL){
00068                 cout<<"Не могу выделить памяти!!!"<<errno<<endl;
00069             }
00070             bp->setFillBuffer(buf1,size,pktID);
00071 
00072             wfResp.setResult(pktID,size);
00073             protocol->sendNode(wfResp);
00074             bp->waiteFillBuffer(pktID);
00075             size_t wres=0;
00076             size_t wbts=0;
00077             char* buf=(char*)buf1;
00078             while (wbts<size){
00079                wres=write(fDesc,buf,size);
00080                if (wres>0){
00081                    buf+=wres;
00082                    wbts+=wres;
00083                }else{
00084                    break;
00085                }
00086             }
00087             close(fDesc);
00088             free(buf1);
00089 
00090 
00091             //szoneread-=szoneread;
00092         //}
00093         //write()
00094 
00095 /*        if (fstat(fDesc, &fb) == -1) {
00096             cout << "fstat error" << errno << endl;
00097         }*/
00098 //        size_t szforrwrite = max_readsize;
00099 /*        #ifdef debug_output
00100         cout << "szforread" << szforread << "file size" << fb.st_size << endl;
00101         #endif
00102         if (szforread < 0) {
00103             #ifdef debug_output
00104             cout << "ELSE1 READ" << endl;
00105             #endif
00106             szforread = 0;
00107         } else if (size < szforread) {
00108             #ifdef debug_output
00109             cout << "ELSE2 READ" << endl;
00110             #endif
00111             szforread = size;
00112         }
00113         #ifdef debug_output
00114         cout << "STEP3 read" << endl;
00115         #endif
00116         unsigned long int pktID = bp->getNextId();
00117         rfResp.setResult(pktID, szforread);
00118         if (szforread == 0) {
00119             protocol->sendNode(rfResp);
00120         } else {
00121             //protocol->sendNode(rfResp);
00122             #ifdef debug_output
00123             cout << "lalal1" << endl;
00124             #endif
00125             TagIQ rt = protocol->sendIQResp(rfResp, ReadFile_xmlns, QUERY_TAG);
00126             #ifdef debug_output
00127             cout << "!!!!lalalal!!! namespace:" << rt.getNamesapace() << "typeIQ:" << rt.getType() << endl;
00128             #endif
00129             if ((rt.getKind(ReadFile_xmlns) != NULL) && (rt.getType() == TagIQ::Result)) {
00130 
00131                 /*lseek(fDesc,offset,SEEK_SET);
00132                 ssize_t sr1=szforread;
00133                 ssize_t sr=0;//Результат чтения за под раз
00134                 void* buf=malloc(szforread);
00135                 char* buf1=(char*)buf;
00136                 do{
00137                         cout<<"цикл1"<<endl;
00138                         sr=read(fDesc,buf1,sr1);
00139                         if(sr!=-1){
00140                                 sr1-=sr;
00141                                 buf1+=sr;
00142                         }
00143                         if(sr==0){
00144                                 cout<<"конец файла"<<endl;
00145                         }
00146                 }while ((sr1>0)&&(sr>0));
00147                 bp->setSendBufferData(buf,szforread-sr1,pktID);
00148                 free(buf);*/
00213     }
00214     #ifdef debug_output
00215     cout << "STEP END read" << endl;
00216     #endif
00217 
00218 }
00219 
00220 WriteFiles::WriteFiles() {
00221     // TODO Auto-generated constructor stub
00222 
00223 }
00224 
00225 WriteFiles::~WriteFiles() {
00226     // TODO Auto-generated destructor stub
00227     this->protocol->removeIQHandler(this);
00228 }