-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9744a50
commit b754d85
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef MANTID_LIVEDATA_FILEWATCHER_H_ | ||
#define MANTID_LIVEDATA_FILEWATCHER_H_ | ||
|
||
#include "Poco/DirectoryWatcher.h" | ||
#include "Poco/Delegate.h" | ||
#include "Poco/FileStream.h" | ||
#include "MantidKernel/System.h" | ||
|
||
namespace Mantid { | ||
namespace LiveData { | ||
|
||
/** FileWatcher : TODO: DESCRIPTION | ||
Copyright © 2017 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge | ||
National Laboratory & European Spallation Source | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class DLLExport FileWatcher { | ||
public: | ||
FileWatcher(std::string path); | ||
//~FileWatcher(); | ||
std::string get_path(); | ||
bool hasChanged(); | ||
std::vector<Poco::File> changedFiles; | ||
|
||
void start(); | ||
|
||
private: | ||
bool m_changed; | ||
std::string m_path; | ||
|
||
void onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev); | ||
void onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent & ev); | ||
void onItemModified(const Poco::DirectoryWatcher::DirectoryEvent & ev); | ||
void onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent & ev); | ||
void onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent & ev); | ||
}; | ||
|
||
} // namespace LiveData | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_LIVEDATA_FILEWATCHER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "MantidLiveData/FileWatcher.h" | ||
#include <iostream> | ||
|
||
namespace Mantid { | ||
namespace LiveData { | ||
/** | ||
* The constructor | ||
*/ | ||
FileWatcher::FileWatcher(std::string path) | ||
: m_path(path), m_changed(false) {} | ||
|
||
std::string FileWatcher::get_path() { | ||
return m_path; | ||
} | ||
|
||
bool FileWatcher::hasChanged() { | ||
return m_changed; | ||
} | ||
|
||
void FileWatcher::start() { | ||
Poco::DirectoryWatcher dw(m_path, Poco::DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2); | ||
dw.itemAdded += Poco::delegate(this, &FileWatcher::onItemAdded); | ||
dw.itemRemoved += Poco::delegate(this, &FileWatcher::onItemRemoved); | ||
dw.itemModified += Poco::delegate(this, &FileWatcher::onItemModified); | ||
dw.itemMovedFrom += Poco::delegate(this, &FileWatcher::onItemMovedFrom); | ||
dw.itemMovedTo += Poco::delegate(this, &FileWatcher::onItemMovedTo); | ||
while (true) {} | ||
} | ||
|
||
void FileWatcher::onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev) | ||
{ | ||
std::cout << "ADDED: item at " << ev.item.path() << std::endl; | ||
m_changed = true; | ||
} | ||
|
||
void FileWatcher::onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent& ev) | ||
{ | ||
std::cout << "REMOVED: item at " << ev.item.path() << std::endl; | ||
m_changed = true; | ||
} | ||
|
||
void FileWatcher::onItemModified(const Poco::DirectoryWatcher::DirectoryEvent& ev) | ||
{ | ||
std::cout << "MODIFIED: item at " << ev.item.path() << std::endl; | ||
m_changed = true; | ||
} | ||
|
||
// Never happens (on win)? | ||
void FileWatcher::onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent& ev) | ||
{ | ||
std::cout << "MOVED FROM: item at " << ev.item.path() << std::endl; | ||
m_changed = true; | ||
} | ||
|
||
// Never happens (on win)? | ||
void FileWatcher::onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent& ev) | ||
{ | ||
std::cout << "MOVED TO: item at " << ev.item.path() << std::endl; | ||
m_changed = true; | ||
} | ||
|
||
// TODO set changed method | ||
|
||
// TODO read | ||
|
||
// TODO white/blacklist files? (regex?) | ||
|
||
} // namespace LiveData | ||
} // namespace Mantid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef MANTID_LIVEDATA_FILEWATCHERTEST_H_ | ||
#define MANTID_LIVEDATA_FILEWATCHERTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
#include <fstream> | ||
#include <stdio.h> | ||
|
||
#include "MantidLiveData/FileWatcher.h" | ||
|
||
static const std::string folderPath = "C:\\Instrument\\FileWatcherTestBed\\"; | ||
static std::vector<std::string> testFiles; | ||
using Mantid::LiveData::FileWatcher; | ||
|
||
class FileWatcherTest : public CxxTest::TestSuite { | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static FileWatcherTest *createSuite() { return new FileWatcherTest(); } | ||
static void destroySuite( FileWatcherTest *suite ) { delete suite; } | ||
|
||
void test_pathIsSet() | ||
{ | ||
FileWatcher fw(folderPath); | ||
TS_ASSERT_EQUALS(fw.get_path(), folderPath); | ||
} | ||
|
||
void test_flagIsFalse(){ | ||
FileWatcher fw(folderPath); | ||
TS_ASSERT_EQUALS(fw.hasChanged(), false); | ||
} | ||
|
||
void test_flagSetToTrueIfFileAdded() { | ||
FileWatcher fw(folderPath); | ||
fw.start(); | ||
|
||
std::ofstream testFile = openFile("example.txt"); | ||
testFile.close(); | ||
|
||
Sleep(1000); | ||
TS_ASSERT_EQUALS(fw.hasChanged(), true); | ||
} | ||
std::ofstream openFile(std::string fileName) { | ||
std::ofstream file; | ||
|
||
// check doesnt exist | ||
std::string filePath = folderPath + fileName; | ||
testFiles.push_back(filePath); | ||
|
||
file.open(filePath); | ||
return file; | ||
} | ||
|
||
void tearDown() override { | ||
// Remove all files created | ||
for (auto const& filePath : testFiles) { | ||
remove(filePath.c_str()); | ||
} | ||
} | ||
|
||
}; | ||
|
||
#endif /* MANTID_LIVEDATA_FILEWATCHERTEST_H_ */ |