184b2179eSNicolas Bonnefon #ifndef WATCHTOWER_H 284b2179eSNicolas Bonnefon #define WATCHTOWER_H 384b2179eSNicolas Bonnefon 487e05652SNicolas Bonnefon #include <memory> 5c540156cSNicolas Bonnefon #include <atomic> 6c540156cSNicolas Bonnefon #include <thread> 7c540156cSNicolas Bonnefon #include <mutex> 887e05652SNicolas Bonnefon 9c540156cSNicolas Bonnefon #include "watchtowerlist.h" 10*b278d183SNicolas Bonnefon 11*b278d183SNicolas Bonnefon // FIXME 12*b278d183SNicolas Bonnefon #include "inotifywatchtowerdriver.h" 13c540156cSNicolas Bonnefon 14c540156cSNicolas Bonnefon // Allow the client to register for notification on an arbitrary number 15c540156cSNicolas Bonnefon // of files. It will be notified to any change (creation/deletion/modification) 16c540156cSNicolas Bonnefon // on those files. 17c540156cSNicolas Bonnefon // It is passed a platform specific driver. 18*b278d183SNicolas Bonnefon // template<typename Driver> 1987e05652SNicolas Bonnefon class WatchTower { 2087e05652SNicolas Bonnefon public: 2187e05652SNicolas Bonnefon // Registration object to implement RAII 2287e05652SNicolas Bonnefon using Registration = std::shared_ptr<void>; 2387e05652SNicolas Bonnefon 2487e05652SNicolas Bonnefon // Create an empty watchtower 25*b278d183SNicolas Bonnefon WatchTower(); 2687e05652SNicolas Bonnefon // Destroy the object 27c540156cSNicolas Bonnefon ~WatchTower(); 2887e05652SNicolas Bonnefon 2987e05652SNicolas Bonnefon // Add a file to the notification list. notification will be called when 3087e05652SNicolas Bonnefon // the file is modified, moved or deleted. 3187e05652SNicolas Bonnefon // Lifetime of the notification is tied to the Registration object returned. 3287e05652SNicolas Bonnefon // Note the notification function is called with a mutex held and in a 3387e05652SNicolas Bonnefon // third party thread, beware of races! 34c540156cSNicolas Bonnefon Registration addFile( const std::string& file_name, 35c540156cSNicolas Bonnefon std::function<void()> notification ); 36a0936e1eSNicolas Bonnefon 37a0936e1eSNicolas Bonnefon private: 38*b278d183SNicolas Bonnefon // The driver (parametrised) 39*b278d183SNicolas Bonnefon INotifyWatchTowerDriver driver_; 40a0936e1eSNicolas Bonnefon 41c540156cSNicolas Bonnefon // List of files/dirs observed 42c540156cSNicolas Bonnefon ObservedFileList observed_file_list_; 43a0936e1eSNicolas Bonnefon 44c540156cSNicolas Bonnefon // Protects the observed_file_list_ 45c540156cSNicolas Bonnefon std::mutex observers_mutex_; 46c540156cSNicolas Bonnefon 47c540156cSNicolas Bonnefon // Thread 48c540156cSNicolas Bonnefon std::atomic_bool running_; 49c540156cSNicolas Bonnefon std::thread thread_; 50c540156cSNicolas Bonnefon 51c540156cSNicolas Bonnefon // Exist as long as the onject exists, to ensure observers won't try to 52c540156cSNicolas Bonnefon // call us if we are dead. 53c540156cSNicolas Bonnefon std::shared_ptr<void> heartBeat_; 54c540156cSNicolas Bonnefon 55c540156cSNicolas Bonnefon // Private member functions 56c540156cSNicolas Bonnefon static void removeNotification( WatchTower* watch_tower, 57c540156cSNicolas Bonnefon std::shared_ptr<void> notification ); 58c540156cSNicolas Bonnefon void run(); 5987e05652SNicolas Bonnefon }; 6084b2179eSNicolas Bonnefon 6184b2179eSNicolas Bonnefon #endif 62