mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-03 14:45:12 -04:00
Addition of next stage of new FileTransfer Code:
* Completed rough ftExtraList class (with Test Case) * Added data flow interface (ftData.h) * Added ftDataMultiplex (server + client modules). * Finished parts of ftcontroller / ftserver. * Minor Tweaks to ftTransferModules interface for compilation. Related Changes in other parts of the code: * Added new Job/Queue Thread Class. * Added more user-friendly directory functions. * Added FileInfo print operator. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@650 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
a9bda83565
commit
79727897dd
19 changed files with 1245 additions and 106 deletions
|
@ -196,6 +196,31 @@ int RsDirUtil::breakupDirList(std::string path,
|
|||
|
||||
|
||||
|
||||
bool RsDirUtil::checkDirectory(std::string dir)
|
||||
{
|
||||
struct stat buf;
|
||||
int val = stat(dir.c_str(), &buf);
|
||||
if (val == -1)
|
||||
{
|
||||
#ifdef RSDIR_DEBUG
|
||||
std::cerr << "RsDirUtil::checkDirectory() ";
|
||||
std::cerr << dir << " doesn't exist" << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
else if (!S_ISDIR(buf.st_mode))
|
||||
{
|
||||
// Some other type - error.
|
||||
#ifdef RSDIR_DEBUG
|
||||
std::cerr << "RsDirUtil::checkDirectory() ";
|
||||
std::cerr << dir << " is not Directory" << std::endl;
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool RsDirUtil::checkCreateDirectory(std::string dir)
|
||||
{
|
||||
struct stat buf;
|
||||
|
@ -289,11 +314,24 @@ bool RsDirUtil::cleanupDirectory(std::string cleandir, std::list<std::string> k
|
|||
return true;
|
||||
}
|
||||
|
||||
/* slightly nicer helper function */
|
||||
bool RsDirUtil::hashFile(std::string filepath,
|
||||
std::string &name, std::string &hash, uint64_t &size)
|
||||
{
|
||||
if (getFileHash(filepath, hash, size))
|
||||
{
|
||||
/* extract file name */
|
||||
name = RsDirUtil::getTopDir(filepath);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#include <openssl/sha.h>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
/* Function to hash, and get details of a file */
|
||||
bool RsDirUtil::getFileHash(std::string filepath,
|
||||
std::string &hash, uint64_t &size)
|
||||
|
|
|
@ -43,9 +43,13 @@ std::string removeRootDirs(std::string path, std::string root);
|
|||
int breakupDirList(std::string path,
|
||||
std::list<std::string> &subdirs);
|
||||
|
||||
bool checkDirectory(std::string dir);
|
||||
bool checkCreateDirectory(std::string dir);
|
||||
bool cleanupDirectory(std::string dir, std::list<std::string> keepFiles);
|
||||
|
||||
bool hashFile(std::string filepath,
|
||||
std::string &name, std::string &hash, uint64_t &size);
|
||||
|
||||
bool getFileHash(std::string filepath,
|
||||
std::string &hash, uint64_t &size);
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
|
||||
#include "rsthreads.h"
|
||||
#include <unistd.h> /* for usleep() */
|
||||
|
||||
extern "C" void* rsthread_init(void* p)
|
||||
{
|
||||
|
@ -56,4 +57,43 @@ pthread_t createThread(RsThread &thread)
|
|||
}
|
||||
|
||||
|
||||
RsQueueThread::RsQueueThread(uint32_t min, uint32_t max, double relaxFactor )
|
||||
:mMinSleep(min), mMaxSleep(max), mRelaxFactor(relaxFactor)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
void RsQueueThread::run()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
bool doneWork = false;
|
||||
while(workQueued() && doWork())
|
||||
{
|
||||
doneWork = true;
|
||||
}
|
||||
time_t now = time(NULL);
|
||||
if (doneWork)
|
||||
{
|
||||
mLastWork = now;
|
||||
mLastSleep = (uint32_t)
|
||||
(mMinSleep + (mLastSleep - mMinSleep) / 2.0);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t deltaT = now - mLastWork;
|
||||
double frac = deltaT / mRelaxFactor;
|
||||
|
||||
mLastSleep += (uint32_t)
|
||||
((mMaxSleep-mMinSleep) * (frac + 0.05));
|
||||
if (mLastSleep > mMaxSleep)
|
||||
{
|
||||
mLastSleep = mMaxSleep;
|
||||
}
|
||||
}
|
||||
|
||||
usleep(1000 * mLastSleep);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
|
||||
#include <pthread.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
/* RsIface Thread Wrappers */
|
||||
|
||||
|
@ -75,4 +76,23 @@ virtual void run() = 0; /* called once the thread is started */
|
|||
};
|
||||
|
||||
|
||||
class RsQueueThread
|
||||
{
|
||||
RsQueueThread(uint32_t min, uint32_t max, double relaxFactor );
|
||||
virtual ~RsQueueThread() { return; }
|
||||
|
||||
virtual void run();
|
||||
|
||||
virtual bool workQueued() = 0;
|
||||
virtual bool doWork() = 0;
|
||||
|
||||
private:
|
||||
uint32_t mMinSleep; /* ms */
|
||||
uint32_t mMaxSleep; /* ms */
|
||||
uint32_t mLastSleep; /* ms */
|
||||
time_t mLastWork; /* secs */
|
||||
float mRelaxFactor;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue