DirMonInvoker.h
Go to the documentation of this file.
1 
20 #ifndef DIRMONINVOKER_H_W09J90F0
21 #define DIRMONINVOKER_H_W09J90F0
22 
23 #include "uscxml/config.h"
26 
27 #include <map>
28 #include <set>
29 #include <thread>
30 #include <mutex>
31 #include <sys/stat.h>
32 
33 #ifdef BUILD_AS_PLUGINS
34 #include "uscxml/plugins/Plugins.h"
35 #endif
36 
37 namespace uscxml {
38 
39 class DirectoryWatchMonitor;
40 
42 public:
43  enum Action {
44  ADDED = 1,
45  MODIFIED = 2,
46  DELETED = 4,
47  EXISTING = 8
48  };
49 
50  DirectoryWatch(const std::string& dir, bool recurse = false) : _dir(dir), _recurse(recurse), _lastChecked(0) {}
51  ~DirectoryWatch();
52 
53  void addMonitor(DirectoryWatchMonitor* monitor) {
54  _monitors.insert(monitor);
55  }
56  void removeMonitor(DirectoryWatchMonitor* monitor) {
57  _monitors.erase(monitor);
58  }
59  void updateEntries(bool reportAsExisting = false);
60  void reportAsDeleted();
61 
62  std::map<std::string, struct stat> getAllEntries() {
63  std::map<std::string, struct stat> entries;
64  entries.insert(_knownEntries.begin(), _knownEntries.end());
65 
66  std::map<std::string, DirectoryWatch*>::iterator dirIter = _knownDirs.begin();
67  while(dirIter != _knownDirs.end()) {
68  std::map<std::string, struct stat> dirEntries = dirIter->second->getAllEntries();
69  std::map<std::string, struct stat>::iterator dirEntryIter = dirEntries.begin();
70  while(dirEntryIter != dirEntries.end()) {
71  entries[dirIter->first + '/' + dirEntryIter->first] = dirEntryIter->second;
72  dirEntryIter++;
73  }
74  dirIter++;
75  }
76 
77  return entries;
78  }
79 
80  void setLogger(Logger logger) {
81  _logger = logger;
82  }
83 
84 protected:
85  DirectoryWatch(const std::string& dir, const std::string& relDir) : _dir(dir), _relDir(relDir), _recurse(true), _lastChecked(0) {}
86 
87  std::string _dir;
88  std::string _relDir;
89  Logger _logger;
90 
91  bool _recurse;
92  std::map<std::string, struct stat> _knownEntries;
93  std::map<std::string, DirectoryWatch*> _knownDirs;
94  std::set<DirectoryWatchMonitor*> _monitors;
95  typedef std::set<DirectoryWatchMonitor*> _monitors_t;
96  time_t _lastChecked;
97 };
98 
100 public:
101  virtual void handleChanges(DirectoryWatch::Action action, const std::string dir, const std::string file, struct stat fileStat) = 0;
102 };
103 
105 public:
106  DirMonInvoker();
107  virtual ~DirMonInvoker();
108  virtual std::shared_ptr<InvokerImpl> create(InvokerCallbacks* callbacks);
109 
110  virtual std::list<std::string> getNames() {
111  std::list<std::string> names;
112  names.push_back("dirmon");
113  names.push_back("DirectoryMonitor");
114  names.push_back("http://uscxml.tk.informatik.tu-darmstadt.de/#dirmon");
115  return names;
116  }
117 
118  virtual Data getDataModelVariables();
119  virtual void eventFromSCXML(const Event& event);
120  virtual void invoke(const std::string& source, const Event& invokeEvent);
121  virtual void uninvoke();
122 
123  virtual void handleChanges(DirectoryWatch::Action action, const std::string dir, const std::string file, struct stat fileStat);
124 
125  static void run(void* instance);
126 
127 protected:
128  bool _reportExisting;
129  bool _reportHidden;
130  bool _recurse;
131 
132  std::string _dir;
133  std::set<std::string> _suffixes;
134 
135  bool _isRunning;
136  std::thread* _thread;
137  std::recursive_mutex _mutex;
138 
139  DirectoryWatch* _watcher;
140 };
141 
142 #ifdef BUILD_AS_PLUGINS
143 PLUMA_INHERIT_PROVIDER(DirMonInvoker, InvokerImpl)
144 #endif
145 
146 }
147 
148 
149 #endif /* end of include guard: DIRMONINVOKER_H_W09J90F0 */
virtual void uninvoke()
The invokers's parent state was left at the end of a macro-step.
Definition: DirMonInvoker.cpp:155
Definition: Breakpoint.cpp:26
Definition: DirMonInvoker.h:99
virtual void invoke(const std::string &source, const Event &invokeEvent)
Invoker's parent state became active at the end of a macro-step.
Definition: DirMonInvoker.cpp:99
Definition: DirMonInvoker.h:104
Abstract base class for all invokers.
Definition: InvokerImpl.h:59
virtual std::list< std::string > getNames()
Return a list of names for types we implement.
Definition: DirMonInvoker.h:110
virtual Data getDataModelVariables()
Export a Data object for the `_x['name']` data-model namespace.
Definition: DirMonInvoker.cpp:71
virtual void handleChanges(DirectoryWatch::Action action, const std::string dir, const std::string file, struct stat fileStat)
Definition: DirMonInvoker.cpp:173
Definition: Event.h:94
Callbacks available for every invoker.
Definition: InvokerImpl.h:43
virtual std::shared_ptr< InvokerImpl > create(InvokerCallbacks *callbacks)
Factory demands a new instance.
Definition: DirMonInvoker.cpp:65
Definition: Logging.h:69
Definition: DirMonInvoker.h:41
virtual void eventFromSCXML(const Event &event)
Invoker received an event from the SCXML Interpreter.
Definition: DirMonInvoker.cpp:96
Definition: Data.h:44