]> git.sesse.net Git - casparcg/blob - common/filesystem_monitor.h
- Removed need of non-deterministic sleeps during server shutdown.
[casparcg] / common / filesystem_monitor.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #pragma once
23
24 #include <functional>
25 #include <set>
26
27 #include <boost/filesystem.hpp>
28 #include <boost/noncopyable.hpp>
29
30 #include "memory.h"
31 #include "future_fwd.h"
32 #include "enum_class.h"
33
34 namespace caspar {
35
36 /**
37  * A handle to an active filesystem monitor. Will stop scanning when destroyed.
38  */
39 class filesystem_monitor : boost::noncopyable
40 {
41 public:
42         typedef spl::shared_ptr<filesystem_monitor> ptr;
43
44         virtual ~filesystem_monitor() {}
45
46         /**
47          * @return a future made available when the initially available files have been
48          *         processed.
49          */
50         virtual std::future<void> initial_files_processed() = 0;
51
52         /**
53          * Reemmit the already known files as MODIFIED events.
54          */
55         virtual void reemmit_all() = 0;
56
57         /**
58          * Reemmit a specific file as a MODIFIED event.
59          *
60          * @param file The file to reemmit.
61          */
62         virtual void reemmit(const boost::filesystem::path& file) = 0;
63 };
64
65 /**
66  * The possible filesystem events.
67  */
68 enum class filesystem_event
69 {
70         CREATED = 1,
71         REMOVED = 2,
72         MODIFIED = 4,
73         // Only used for describing a bitmask where all events are wanted. Never used when calling a handler.
74         ALL = 7
75 };
76 ENUM_ENABLE_BITWISE(filesystem_event);
77
78 /**
79  * Handles filesystem events.
80  * <p>
81  * Will always be called from the same thread each time it is called.
82  *
83  * @param event Can be CREATED, REMOVED or MODIFIED.
84  * @param file  The file affected.
85  */
86 typedef std::function<void (filesystem_event event, const boost::filesystem::path& file)> filesystem_monitor_handler;
87
88 /**
89  * Called when the initially available files has been found.
90  * <p>
91  * Will be called from the same thread as filesystem_monitor_handler is called.
92  *
93  * @param initial_files The files that were initially available.
94  */
95 typedef std::function<void (const std::set<boost::filesystem::path>& initial_files)> initial_files_handler;
96
97 /**
98  * Factory for creating filesystem monitors.
99  */
100 class filesystem_monitor_factory : boost::noncopyable
101 {
102 public:
103         virtual ~filesystem_monitor_factory() {}
104
105         /**
106          * Create a new monitor.
107          *
108          * @param folder_to_watch         The folder to recursively watch.
109          * @param events_of_interest_mask A bitmask of the events to prenumerate on.
110          * @param report_already_existing Whether to initially report all files in
111          *                                the folder as CREATED or to only care
112          *                                about changes.
113          * @param handler                 The handler to call each time a change is
114          *                                detected (only events defined by
115          *                                events_of_interest_mask).
116          * @param initial_files_handler   The optional handler to call when the
117          *                                initially available files has been
118          *                                discovered.
119          *
120          * @return The filesystem monitor handle.
121          */
122         virtual filesystem_monitor::ptr create(
123                         const boost::filesystem::path& folder_to_watch,
124                         filesystem_event events_of_interest_mask,
125                         bool report_already_existing,
126                         const filesystem_monitor_handler& handler,
127                         const initial_files_handler& initial_files_handler =
128                                         [] (const std::set<boost::filesystem::path>&) { }) = 0;
129 };
130
131 }