From d62876ae37793742b3aee0f8b7937c7a58f89919 Mon Sep 17 00:00:00 2001 From: drbob Date: Mon, 25 Aug 2008 20:03:39 +0000 Subject: [PATCH] * fixes for windows * corrected allocation in ftfilecreator git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@707 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/ft/ftcontroller.cc | 4 ++ libretroshare/src/ft/ftextralist.cc | 8 +++ libretroshare/src/ft/ftfilecreator.cc | 99 ++++++++++++++++++++++----- libretroshare/src/ft/ftserver.cc | 4 ++ libretroshare/src/ft/pqitestor.cc | 4 ++ 5 files changed, 101 insertions(+), 18 deletions(-) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 762073625..a91399377 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -86,7 +86,11 @@ void ftController::run() while(1) { +#ifdef WIN32 + Sleep(1000); +#else sleep(1); +#endif std::cerr << "ftController::run()"; std::cerr << std::endl; diff --git a/libretroshare/src/ft/ftextralist.cc b/libretroshare/src/ft/ftextralist.cc index bda40f188..d57326708 100644 --- a/libretroshare/src/ft/ftextralist.cc +++ b/libretroshare/src/ft/ftextralist.cc @@ -61,8 +61,12 @@ void ftExtraList::run() /* Hash a file */ hashAFile(); +#ifdef WIN32 + Sleep(1); +#else /* microsleep */ usleep(10); +#endif } else { @@ -74,7 +78,11 @@ void ftExtraList::run() } /* sleep */ +#ifdef WIN32 + Sleep(1000); +#else sleep(1); +#endif } } } diff --git a/libretroshare/src/ft/ftfilecreator.cc b/libretroshare/src/ft/ftfilecreator.cc index 1f6a27ea4..4b17d099c 100644 --- a/libretroshare/src/ft/ftfilecreator.cc +++ b/libretroshare/src/ft/ftfilecreator.cc @@ -312,38 +312,62 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) i++; } + /* if we get here, there is no available chunk bigger + * than requested ... + * NB: Request size should be a larger than std_chunk_size. + * So Edge (sub chunk allocation) condition is handled here. + * + * Find largest remaining chunk. + */ + if (!found) { i=0; - uint64_t min = allocationTable.at(i)->max_chunk_size - chunk; - uint64_t diff = min; - int mini = -1; + uint64_t max = allocationTable.at(i)->max_chunk_size; + uint64_t size = max; + int maxi = -1; while(imax_chunk_size-chunk; - if(diff <= min && diff >0) + size = allocationTable.at(i)->max_chunk_size; + if(size > max) { - min = allocationTable.at(i)->max_chunk_size - chunk; - mini = i; + max = allocationTable.at(i)->max_chunk_size; + maxi = i; } i++; } - if (mini > -1) //mini or min + if (maxi > -1) //maxi or max { - offset = allocationTable.at(mini)->offset; - chunk = allocationTable.at(mini)->max_chunk_size; + offset = allocationTable.at(maxi)->offset; + chunk = allocationTable.at(maxi)->max_chunk_size; chunks_after = chunk/std_chunk_size; //10KB - chunks_rem = chunk % std_chunk_size; - chunk -= chunks_rem; - allocationTable.at(mini)->max_chunk_size=0; - allocationTable.at(mini)->timestamp = time(NULL); - allocationTable.at(mini)->chunk_status = ftChunk::ALLOCATED; + + /* Handle end condition ... + * max_chunk_size < std_chunk_size + * Trim if not end condition. + */ + if (chunks_after > 0) + { + chunks_rem = chunk % std_chunk_size; + chunk -= chunks_rem; + } + else + { + /* end condition */ + chunks_after = 1; + } + + allocationTable.at(maxi)->max_chunk_size=0; + allocationTable.at(maxi)->timestamp = time(NULL); + allocationTable.at(maxi)->chunk_status = ftChunk::ALLOCATED; found = true; } } //if not found - if (found) { + + if (found) + { std::cout << "Chunks remaining " << chunks_rem << std::endl; /* * update all previous chunks max available size @@ -386,11 +410,50 @@ bool ftFileChunker::getMissingChunk(uint64_t &offset, uint32_t &chunk) int ftFileChunker::monitor() { int reset = 0; + uint32_t prev_size = 0; + uint32_t size = 0; + std::cout<<"Running monitor.."<chunk_status == ftChunk::ALLOCATED && allocationTable.at(j)->timestamp - time(NULL) > 30){ + + RsStackMutex stack(chunkerMutex); /********** STACK LOCKED MTX ******/ + for(unsigned int j=allocationTable.size()-1 ;j>= 0;) + { + if((allocationTable.at(j)->chunk_status == ftChunk::ALLOCATED) && + (allocationTable.at(j)->timestamp - time(NULL) > 30)) + { allocationTable.at(j)->chunk_status = ftChunk::AVAIL; + if (j == allocationTable.size()-1) + { + /* at end */ + prev_size = 0; + size = file_size % std_chunk_size; + if (size == 0) + { + size = std_chunk_size; + } + } + else + { + prev_size = allocationTable.at(j+1)->max_chunk_size; + size = std_chunk_size; + } + + allocationTable.at(j)->max_chunk_size = size + prev_size; + prev_size = allocationTable.at(j)->max_chunk_size; + + for(j--; j >= 0; j--) + { + if (allocationTable.at(j)->chunk_status != ftChunk::AVAIL) + break; + + allocationTable.at(j)->max_chunk_size += prev_size; + prev_size = allocationTable.at(j)->max_chunk_size; reset++; + } + } + else + { + j--; } } return reset; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 933eb7336..5614fb268 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -168,7 +168,11 @@ void ftServer::run() { while(1) { +#ifdef WIN32 + Sleep(1000); +#else sleep(1); +#endif } } diff --git a/libretroshare/src/ft/pqitestor.cc b/libretroshare/src/ft/pqitestor.cc index ee3b8e563..bd88e64e1 100644 --- a/libretroshare/src/ft/pqitestor.cc +++ b/libretroshare/src/ft/pqitestor.cc @@ -115,7 +115,11 @@ void P3Hub::run() */ /* sleep a bit */ +#ifdef WIN32 + Sleep(1000); +#else sleep(1); +#endif } }