summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/filesystem/dirmon/DirMonInvoker.h
blob: 85f5174f2843ff9ef7fea4e5ff3cb6da78860ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program 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.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef DIRMONINVOKER_H_W09J90F0
#define DIRMONINVOKER_H_W09J90F0

#include <uscxml/config.h>
#include <uscxml/Interpreter.h>
#include <map>
#include <sys/stat.h>

#ifdef BUILD_AS_PLUGINS
#include "uscxml/plugins/Plugins.h"
#endif

namespace uscxml {

class DirectoryWatchMonitor;

class DirectoryWatch {
public:
	enum Action {
		ADDED = 1,
		MODIFIED = 2,
		DELETED = 4,
		EXISTING = 8
	};

	DirectoryWatch(const std::string& dir, bool recurse = false) : _dir(dir), _recurse(recurse), _lastChecked(0) {}
	~DirectoryWatch();

	void addMonitor(DirectoryWatchMonitor* monitor) {
		_monitors.insert(monitor);
	}
	void removeMonitor(DirectoryWatchMonitor* monitor) {
		_monitors.erase(monitor);
	}
	void updateEntries(bool reportAsExisting = false);
	void reportAsDeleted();

	std::map<std::string, struct stat> getAllEntries() {
		std::map<std::string, struct stat> entries;
		entries.insert(_knownEntries.begin(), _knownEntries.end());

		std::map<std::string, DirectoryWatch*>::iterator dirIter = _knownDirs.begin();
		while(dirIter != _knownDirs.end()) {
			std::map<std::string, struct stat> dirEntries = dirIter->second->getAllEntries();
			std::map<std::string, struct stat>::iterator dirEntryIter = dirEntries.begin();
			while(dirEntryIter != dirEntries.end()) {
				entries[dirIter->first + '/' + dirEntryIter->first] = dirEntryIter->second;
				dirEntryIter++;
			}
			dirIter++;
		}

		return entries;
	}

protected:
	DirectoryWatch(const std::string& dir, const std::string& relDir) : _dir(dir), _relDir(relDir), _recurse(true), _lastChecked(0) {}

	std::string _dir;
	std::string _relDir;

	bool _recurse;
	std::map<std::string, struct stat> _knownEntries;
	std::map<std::string, DirectoryWatch*> _knownDirs;
	std::set<DirectoryWatchMonitor*> _monitors;
	typedef std::set<DirectoryWatchMonitor*> _monitors_t;
	time_t _lastChecked;
};

class DirectoryWatchMonitor {
public:
	virtual void handleChanges(DirectoryWatch::Action action, const std::string dir, const std::string file, struct stat fileStat) = 0;
};

class DirMonInvoker : public InvokerImpl, public DirectoryWatchMonitor {
public:
	DirMonInvoker();
	virtual ~DirMonInvoker();
	virtual boost::shared_ptr<InvokerImpl> create(InterpreterImpl* interpreter);

	virtual std::list<std::string> getNames() {
		std::list<std::string> names;
		names.push_back("dirmon");
		names.push_back("DirectoryMonitor");
		names.push_back("http://uscxml.tk.informatik.tu-darmstadt.de/#dirmon");
		return names;
	}

	virtual Data getDataModelVariables();
	virtual void send(const SendRequest& req);
	virtual void cancel(const std::string sendId);
	virtual void invoke(const InvokeRequest& req);

	virtual void handleChanges(DirectoryWatch::Action action, const std::string dir, const std::string file, struct stat fileStat);

	static void run(void* instance);

protected:
	bool _reportExisting;
	bool _reportHidden;
	bool _recurse;

	std::string _dir;
	std::set<std::string> _suffixes;

	bool _isRunning;
	tthread::thread* _thread;
	tthread::recursive_mutex _mutex;

	DirectoryWatch* _watcher;
};

#ifdef BUILD_AS_PLUGINS
PLUMA_INHERIT_PROVIDER(DirMonInvoker, InvokerImpl);
#endif

}


#endif /* end of include guard: DIRMONINVOKER_H_W09J90F0 */