DVector D_bjmak::getDataBufferFromReply(DVector& reply, DVector& hashBuffer, DVector& dataBuffer){ int dataPos = 0; int blockCount = reply.size(); char* hashBufferPtr = hashBuffer.data(); char* dataBufferPtr = dataBuffer.data(); DVector res; res.reserve(blockCount*bsize_); for(int i = 0; i < blockCount; ++i){ int dataBlockSize = byte_to_int64((hashBufferPtr+i*(D_LENGTH_OF_HASH+2)+D_LENGTH_OF_HASH), 2); if (reply[i] == 1){ //cout << " 1 " << dataBlockSize << endl; res.push_back(dataBufferPtr+dataPos, dataBlockSize); }else //cout << " - " << (int)reply[i] << " " << dataBuffer.size() << " " << hashBuffer.size()<< " " << dataBlockSize << endl; dataPos += dataBlockSize; } //cout << endl; return move(res); }
bool D_bjmak::sendBuffer(QVector<FileID>& ids, qint64 bufferSize){ //QString idsBuffer; //QString hashBuffer; //QString dataBuffer; std::cout << "sending buffer of " << ids.size() << std::endl; server->connectionPtr->sendMsg("NEXT"); const int hashBufferSize = bufferSize / bsize_; DVector hashBuffer; hashBuffer.reserve(hashBufferSize); DVector dataBuffer; hashBuffer.reserve(bufferSize); DVector idsBuffer; hashBuffer.reserve(3000000); int blockCount; int fd; bool isCleared = true; for(int i = 0; i < ids.size(); ++i){ if (!ids[i].isREG()){ idsBuffer.push_back(ids[i].toString()); //cout << "added not reg " << ids[i].getFullPath().toStdString() << endl; continue; } DVector tmpHashBuffer; DVector tmpDataBuffer; if ( (dataBuffer.size()+ids[i].getSize()) > bufferSize ){ blockCount = (bufferSize-dataBuffer.size()) / bsize_; isCleared = false; } else{ blockCount = ids[i].getSize() / bsize_; if ( (ids[i].getSize() % bsize_) != 0 ) ++blockCount; } fd = open(ids[i].getFullPath().toStdString().c_str(), O_RDONLY); if (fd == -1){ cout << "error " << strerror(errno) << endl; writeLog.warning() << "Error open file <" << ids[i].getFullPath() << "> " << strerror(errno) << DebusLogger::endl; continue; } if (!getHashesAndData(fd, tmpDataBuffer, tmpHashBuffer, blockCount)){ cout << "error " << strerror(errno) << endl; writeLog.warning() << "Error read file <" << ids[i].getFullPath() << "> " << strerror(errno) << DebusLogger::endl; continue; } idsBuffer.push_back(ids[i].toString()); //cout << "added reg " << ids[i].getFullPath().toStdString() << endl; //idsBuffer.push_back(ids[i].toQString()); hashBuffer.push_back(tmpHashBuffer); dataBuffer.push_back(tmpDataBuffer); if (isCleared) close(fd); } cout << "Sending idsbuffer " << idsBuffer.size() << endl; server->connectionPtr->sendMsg(idsBuffer.data(), idsBuffer.size()); cout << "Sending hashBuffer " << hashBuffer.size() << endl; server->connectionPtr->sendMsg(hashBuffer.data(), hashBuffer.size()); server->connectionPtr->recvMsg(); DVector reply; reply.push_back(server->connectionPtr->dataToVector()); cout << "Received reply size " << reply.size() << endl; DVector newDataBuffer; newDataBuffer = this->getDataBufferFromReply(reply, hashBuffer, dataBuffer); cout << "sending data size " << newDataBuffer.size() << endl; server->connectionPtr->sendMsg(newDataBuffer.data(), newDataBuffer.size()); if (!isCleared){ int clearedBlockCount = blockCount; blockCount = ids.last().getSize() / bsize_; if ( (ids.last().getSize() % bsize_) != 0 ) ++blockCount; blockCount -= clearedBlockCount; while(blockCount){ int i; if (blockCount*bsize_ > bufferSize) i = bufferSize/bsize_; else i = blockCount; cout << "bufferSize " << bufferSize << " " << " i " << i << endl; dataBuffer.clear(); hashBuffer.clear(); getHashesAndData(fd, dataBuffer, hashBuffer, i); cout << "dataBuffer" << dataBuffer.size() << " " << "hashBuffer" << hashBuffer.size() << endl; server->connectionPtr->sendMsg(hashBuffer.data(), hashBuffer.size()); cout << "clearedBlockCount" << clearedBlockCount << " " << "blockCount" << blockCount << endl; server->connectionPtr->recvMsg(); cout << "clearedBlockCount" << clearedBlockCount << " " << "blockCount" << blockCount << endl; DVector reply; reply.push_back(server->connectionPtr->dataToString()); DVector newDataBuffer; newDataBuffer = this->getDataBufferFromReply(reply, hashBuffer, dataBuffer); server->connectionPtr->sendMsg(newDataBuffer.data(), newDataBuffer.size()); blockCount -= i; } close(fd); } cout << "sended block" << endl; return true; }