Config.ih
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief Config internal header */
16 
17 #ifndef IH_SENF_Scheduler_Console_Config_
18 #define IH_SENF_Scheduler_Console_Config_ 1
19 
20 // Custom includes
21 #include <boost/noncopyable.hpp>
22 #include <boost/intrusive_ptr.hpp>
23 #include "Executor.hh"
24 #include <senf/Utils/intrusive_refcount.hh>
25 #include <senf/Utils/DiscardStream.hh>
26 
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 namespace senf {
30 namespace console {
31 namespace detail {
32 
33  /** \brief Internal: Executor wrapper implementing restricted execution
34 
35  A RestrictedExecutor will only process commands which a re children of a given node. It does
36  \e not follow any links.
37  */
38  class RestrictedExecutor
39  : boost::noncopyable
40  {
41  public:
42  typedef void result_type;
43 
44  //-/////////////////////////////////////////////////////////////////////////////////////////
45  //\/name Structors and default members
46  //\{
47 
48  RestrictedExecutor(DirectoryNode & root = senf::console::root());
49 
50  //\}
51  //-/////////////////////////////////////////////////////////////////////////////////////////
52 
53  void execute(std::ostream & output, ParseCommandInfo const & command);
54  ///< Execute command
55  /**< Output will be written to \a output.
56  Same as operator()(). */
57 
58  void operator()(std::ostream & output, ParseCommandInfo const & command);
59  ///< Execute command
60  /**< Output will be written to \a output.
61  Same as execute(). */
62 
63  GenericNode & getNode(ParseCommandInfo const & command);
64 
65  bool complete() const; ///< \c true, if all nodes have been parsed
66  bool parsed(GenericNode & node) const; ///< \c true. if \a node has been parsed
67  void reset(); ///< Reset node parse info state
68  /**< After a call to reset(), all information about already
69  parsed nodes is cleared. Calling parse() will parse the
70  complete config file again. */
71 
72  DirectoryNode & root() const;
73  void chroot(DirectoryNode & node);
74 
75  std::ostream & stream();
76 
77  class RestrictGuard;
78 
79  protected:
80 
81  private:
82  void policyCallback(DirectoryNode & dir, std::string const & item);
83  void insertParsedNode(DirectoryNode & node);
84 
85  typedef std::vector<DirectoryNode::weak_ptr> ParsedNodes;
86 
87  Executor executor_;
88  ParsedNodes parsedNodes_;
89  DirectoryNode::ptr restrict_;
90  DiscardStream stream_;
91 
92  friend class RestrictGuard;
93  };
94 
95  /** \brief Internal: Set restricted node of a RestrictedExecutor
96 
97  A RestrictGuard will set the node to which to restrict. It will automatically reset the node
98  in it's destructor.
99  */
100  class RestrictedExecutor::RestrictGuard
101  {
102  public:
103  //-/////////////////////////////////////////////////////////////////////////////////////////
104  //\/name Structors and default members
105  //\{
106 
107  explicit RestrictGuard(RestrictedExecutor & executor);
108  RestrictGuard(RestrictedExecutor & executor, DirectoryNode & restrict);
109  ~RestrictGuard();
110 
111  //\}
112  //-/////////////////////////////////////////////////////////////////////////////////////////
113 
114  protected:
115 
116  private:
117  RestrictedExecutor & executor_;
118 
119  };
120 
121  /** \brief Internal: ConfigSource base class
122 
123  All configuration sources derive from ConfigSource. A ConigSource somehow reads
124  configuration commands and passes them to a RestrictedExecutor.
125  */
126  class ConfigSource
127  : public senf::intrusive_refcount
128  {
129  public:
130  typedef boost::intrusive_ptr<ConfigSource> ptr;
131  virtual ~ConfigSource();
132 
133  void parse(RestrictedExecutor & executor);
134 
135  protected:
136 
137  private:
138  virtual void v_parse(RestrictedExecutor & executor) = 0;
139  };
140 
141 }}}
142 
143 //-/////////////////////////////////////////////////////////////////////////////////////////////////
144 #endif
145 
146 
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // comment-column: 40
151 // c-file-style: "senf"
152 // indent-tabs-mode: nil
153 // ispell-local-dictionary: "american"
154 // compile-command: "scons -u test"
155 // End: