|
/****************************************************
* *
* Written by Stanic Igor 2012/2013 *
* FileSystemWatcher basic implementation *
* *
***************************************************/
#include "FileSystemWatcher.h"
using namespace std;
using namespace boost;
using namespace boost::filesystem;
FileSystemWatcher::FileSystemWatcher(const std::wstring &path, OpResults& errors, ChangeKind ch_kind)
: m_locationPath(path),
m_changesToWatch(ch_kind),
m_changed(),
m_stopThread(false),
m_wathcerThreadActive(false),
m_pause(false),
m_pause_lock(),
m_results(errors),
m_ResultsLock(),
m_lock(),
m_finalizer()
{
m_alertableThreadState = true;
//Handle for current location to be watched
m_Directory = INVALID_HANDLE_VALUE;
//for results of ReadDirectoryChangesW()
m_buffer = std::array<BYTE, 4096>();
ZeroMemory(&m_overlapped, sizeof(OVERLAPPED));
//hEvent shouldn't be used by system when completion func. is defined
//so it should be safe to use it.
m_overlapped.hEvent = this;
if(getHandle(path))
m_listenerThread = boost::thread(&FileSystemWatcher::process_events, this);
else
m_results.AddResult(OperationResult(false, TO_STRING_TYPE("FileSystemWatcher"), TO_STRING_TYPE("Unsuccessful attempt to obtain handle to file system location")));
}
FileSystemWatcher::~FileSystemWatcher()
{
if(m_wathcerThreadActive)
{
stopProcessing();
}
}
void FileSystemWatcher::setPaused(bool val)
{
boost::unique_lock<boost::mutex> lock(m_pause_lock);
m_pause = val;
}
bool FileSystemWatcher::getPaused()
{
return m_pause;
}
//Blocking func for terminating watcher thread
bool FileSystemWatcher::stopProcessing()
{
//if thread is alive
if(m_wathcerThreadActive)
{
m_stopThread = true;
boost::unique_lock<boost::mutex> lock(m_lock);
m_finalizer.wait(lock);
}
m_stopThread = false;
m_changed.disconnect_all_slots();
return true;
}
/* FileSystemWatcher implementation END */
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Software developer with couple of years of experience mostly with .NET programming and MS SQL databases currently interested in expanding knowledge to C++ and other operating systems.