From 1d2864befcfaf4ac5e760e6d2ac36a753b18c994 Mon Sep 17 00:00:00 2001 From: c-mendoza Date: Tue, 5 Dec 2017 19:14:22 -0500 Subject: [PATCH] DirectoryWatcher now notifies if file count has changed in a directory --- src/MediaServer/DirectoryWatcher.cpp | 45 +++++++++++++++++++++++----- src/MediaServer/DirectoryWatcher.h | 18 ++++++++++- src/MediaServer/MediaServer.cpp | 19 +++++++----- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/MediaServer/DirectoryWatcher.cpp b/src/MediaServer/DirectoryWatcher.cpp index 3c86543..14f697d 100644 --- a/src/MediaServer/DirectoryWatcher.cpp +++ b/src/MediaServer/DirectoryWatcher.cpp @@ -4,8 +4,9 @@ namespace ofx { namespace piMapper { DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ + directoryPath = path; _mediaType = watcherMediaType; - + if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ _directory.allowExt("mp4"); _directory.allowExt("h264"); @@ -21,20 +22,27 @@ DirectoryWatcher::DirectoryWatcher(string path, int watcherMediaType){ ofLogFatalError("DirectoryWatcher::DirectoryWatcher", "Unkonwn media type"); exit(EXIT_FAILURE); } - - _directory.listDir(path); + + _directory.listDir(directoryPath); _directory.sort(); - + for(int i = 0; i < _directory.size(); ++i){ - _filePaths.push_back(path + _directory.getName(i)); - - ofFile file(path + _directory.getName(i)); + _filePaths.push_back(directoryPath + _directory.getName(i)); + + ofFile file(directoryPath + _directory.getName(i)); if(_mediaType == SourceType::SOURCE_TYPE_VIDEO){ file.copyTo("sources/videos/" + _directory.getName(i)); }else if(_mediaType == SourceType::SOURCE_TYPE_IMAGE){ file.copyTo("sources/images/" + _directory.getName(i)); } } + + dirSize = _directory.size(); +} + +DirectoryWatcher::~DirectoryWatcher() { + endWatch(); +// waitForThread(false); } vector & DirectoryWatcher::getFilePaths(){ @@ -45,5 +53,28 @@ int DirectoryWatcher::getMediaType(){ return _mediaType; } +void DirectoryWatcher::beginWatch(int intervalInMillis) { + watchInterval = intervalInMillis; + startThread(); +} + +void DirectoryWatcher::endWatch() { + stopThread(); +} + +void DirectoryWatcher::threadedFunction() { + + while (isThreadRunning()) { + + int newSize = _directory.listDir(); + if (newSize != dirSize) { + ofLogVerbose("DirectoryWatcher") << "Directory changed"; + ofNotifyEvent(directoryFileCountChangedEvent, this); + } + + sleep(watchInterval); + } +} + } // namespace piMapper } // namespace ofx diff --git a/src/MediaServer/DirectoryWatcher.h b/src/MediaServer/DirectoryWatcher.h index 717bbde..bcd3879 100644 --- a/src/MediaServer/DirectoryWatcher.h +++ b/src/MediaServer/DirectoryWatcher.h @@ -6,16 +6,32 @@ namespace ofx { namespace piMapper { -class DirectoryWatcher { +class DirectoryWatcher : public ofThread { public: DirectoryWatcher(string path, int watcherMediaType); + virtual ~DirectoryWatcher(); vector & getFilePaths(); int getMediaType(); + void beginWatch(int intervalInMillis = 5000); + void endWatch(); + void threadedFunction(); + + /** + * Triggered when the file count of a directory increases + * or decreases. + * + * Sender is a pointer to this DirectoryWatcher + */ + ofEvent directoryFileCountChangedEvent; private: ofDirectory _directory; vector _filePaths; + std::string directoryPath; int _mediaType; + + int dirSize; + long watchInterval; // in millis. }; } // namespace piMapper diff --git a/src/MediaServer/MediaServer.cpp b/src/MediaServer/MediaServer.cpp index 78d1af8..41b1747 100644 --- a/src/MediaServer/MediaServer.cpp +++ b/src/MediaServer/MediaServer.cpp @@ -19,14 +19,17 @@ MediaServer::MediaServer(): { // By initialising all above we also copy files from external media // to the ofxPiMapper storage. - - imageWatcher = DirectoryWatcher( - ofToDataPath(DEFAULT_IMAGES_DIR, true), - SourceType::SOURCE_TYPE_IMAGE); - - videoWatcher = DirectoryWatcher( - ofToDataPath(DEFAULT_VIDEOS_DIR, true), - SourceType::SOURCE_TYPE_VIDEO); + imageWatcher.beginWatch(); + + +// imageWatcher = DirectoryWatcher( +// ofToDataPath(DEFAULT_IMAGES_DIR, true), +// SourceType::SOURCE_TYPE_IMAGE); +// imageWatcher.beginWatch(); +// +// videoWatcher = DirectoryWatcher( +// ofToDataPath(DEFAULT_VIDEOS_DIR, true), +// SourceType::SOURCE_TYPE_VIDEO); } void MediaServer::setup(){