Apache Mesos
whitelist_watcher.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __WATCHER_WHITELIST_WATCHER_HPP__
18 #define __WATCHER_WHITELIST_WATCHER_HPP__
19 
20 #include <string>
21 
22 #include <process/process.hpp>
23 
24 #include <stout/duration.hpp>
25 #include <stout/hashset.hpp>
26 #include <stout/lambda.hpp>
27 #include <stout/option.hpp>
28 #include <stout/path.hpp>
29 
30 namespace mesos {
31 namespace internal {
32 
33 // A whitelist may be (1) absent, (2) empty, (3) non-empty. The
34 // watcher notifies the subscriber if the state of the whitelist
35 // changes or if the contents changes in case the whitelist is in
36 // state (3) non-empty.
37 class WhitelistWatcher : public process::Process<WhitelistWatcher>
38 {
39 public:
40  // By default the initial policy is assumed to be permissive, i.e.
41  // initial whitelist is in state (1), meaning that peers are
42  // accepted before a whitelist is first loaded. In this case a
43  // subscriber is notified only if a set of peers (possibly empty)
44  // is loaded from the whitelist file.
45  // If a subscriber initially uses a nonpermissive policy, i.e.
46  // initial whitelist is in state (2) or (3), peers before first
47  // loaded whitelist are rejected. In this case a subscriber must
48  // provide the initial whitelist and will be notified if the policy
49  // becomes permissive (no whitelist file) or if a set of peers
50  // loaded from the whitelist file changes.
51  // NOTE: The caller should ensure a callback exists throughout
52  // WhitelistWatcher's lifetime.
54  const Option<Path>& path,
55  const Duration& watchInterval,
56  const lambda::function<
57  void(const Option<hashset<std::string>>& whitelist)>& subscriber,
58  const Option<hashset<std::string>>& initialWhitelist = None());
59 
60 protected:
61  virtual void initialize();
62  void watch();
63 
64 private:
65  const Option<Path> path;
66  const Duration watchInterval;
67  lambda::function<void(const Option<hashset<std::string>>& whitelist)>
68  subscriber;
69  Option<hashset<std::string>> lastWhitelist;
70 };
71 
72 } // namespace internal {
73 } // namespace mesos {
74 
75 #endif // __WATCHER_WHITELIST_WATCHER_HPP__
Definition: path.hpp:26
WhitelistWatcher(const Option< Path > &path, const Duration &watchInterval, const lambda::function< void(const Option< hashset< std::string >> &whitelist)> &subscriber, const Option< hashset< std::string >> &initialWhitelist=None())
virtual void initialize()
Invoked when a process gets spawned.
Definition: duration.hpp:32
Definition: spec.hpp:30
Definition: whitelist_watcher.hpp:37
Definition: none.hpp:27
Definition: attributes.hpp:24
Definition: process.hpp:501