CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file_watcher.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_FILE_WATCHER_H_
6 #define CVMFS_FILE_WATCHER_H_
7 
8 #include <pthread.h>
9 
10 #include <map>
11 #include <string>
12 
13 namespace file_watcher {
14 
15 enum Event {
23 };
24 
25 class EventHandler {
26  public:
27  EventHandler();
28  virtual ~EventHandler();
29 
45  virtual bool Handle(const std::string &file_path,
46  Event event,
47  bool *clear_handler) = 0;
48 };
49 
50 struct WatchRecord {
51  WatchRecord() : file_path_(), handler_(NULL) { }
52 
53  WatchRecord(const std::string &path, file_watcher::EventHandler *h)
54  : file_path_(path), handler_(h) { }
55 
56  std::string file_path_;
58 };
59 
60 class FileWatcher {
61  public:
62  typedef std::map<std::string, EventHandler *> HandlerMap;
63 
64  FileWatcher();
65  virtual ~FileWatcher();
66  static FileWatcher *Create();
67 
68  void RegisterHandler(const std::string &file_path, EventHandler *handler);
69 
70  bool Spawn();
71 
72  void Stop();
73 
74  protected:
75  // Delays controlling the backoff throttle when registering new watches
76  static const unsigned kInitialDelay;
77  static const unsigned kMaxDelay;
78  static const unsigned kResetDelay;
79 
80  void RegisterFilter(const std::string &file_path, EventHandler *handler);
81 
82  virtual bool RunEventLoop(const HandlerMap &handler_map, int read_pipe,
83  int write_pipe) = 0;
84 
85  virtual int TryRegisterFilter(const std::string &file_path) = 0;
86 
87  std::map<int, WatchRecord> watch_records_;
88 
89  private:
90  static void *BackgroundThread(void *d);
91 
93 
96 
97  pthread_t thread_;
98 
99  bool started_;
100 };
101 
102 } // namespace file_watcher
103 
104 #endif // CVMFS_FILE_WATCHER_H_
void RegisterFilter(const std::string &file_path, EventHandler *handler)
static const unsigned kMaxDelay
Definition: file_watcher.h:77
static FileWatcher * Create()
Definition: file_watcher.cc:42
static void * BackgroundThread(void *d)
Definition: file_watcher.cc:98
static const unsigned kResetDelay
Definition: file_watcher.h:78
virtual bool RunEventLoop(const HandlerMap &handler_map, int read_pipe, int write_pipe)=0
std::map< std::string, EventHandler * > HandlerMap
Definition: file_watcher.h:62
WatchRecord(const std::string &path, file_watcher::EventHandler *h)
Definition: file_watcher.h:53
file_watcher::EventHandler * handler_
Definition: file_watcher.h:57
static const unsigned kInitialDelay
Definition: file_watcher.h:76
virtual bool Handle(const std::string &file_path, Event event, bool *clear_handler)=0
void RegisterHandler(const std::string &file_path, EventHandler *handler)
Definition: file_watcher.cc:54
std::map< int, WatchRecord > watch_records_
Definition: file_watcher.h:87
virtual int TryRegisterFilter(const std::string &file_path)=0