$darkmode
coordinator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 
25 #include <list> // for list<>
26 #include <map> // for map<>
27 #include <memory> // for unique_ptr<>, shared_ptr<>
28 #include <mutex> // for mutex
29 #include <queue> // for queue<>
30 #include <string> // for string
31 #include <vector> // for vector<>
32 
33 #include <cloe/trigger.hpp> // for Trigger, Action, Event, ...
34 
35 // Forward declaration:
36 namespace cloe {
37 class Registrar;
38 }
39 
40 namespace engine {
41 
42 // Forward declarations:
43 class TriggerRegistrar; // from trigger_manager.cpp
44 
50  public:
51  TriggerUnknownAction(const std::string& key, const cloe::Conf& c)
52  : TriggerInvalid(c, "unknown action: " + key), key_(key) {}
53  virtual ~TriggerUnknownAction() noexcept = default;
54 
58  const char* key() const { return key_.c_str(); }
59 
60  private:
61  std::string key_;
62 };
63 
69  public:
70  TriggerUnknownEvent(const std::string& key, const cloe::Conf& c)
71  : TriggerInvalid(c, "unknown event: " + key), key_(key) {}
72  virtual ~TriggerUnknownEvent() noexcept = default;
73 
77  const char* key() const { return key_.c_str(); }
78 
79  private:
80  std::string key_;
81 };
82 
84  HistoryTrigger(cloe::Duration d, cloe::TriggerPtr&& t) : when(d), what(std::move(t)) {}
85 
86  friend void to_json(cloe::Json& j, const HistoryTrigger& t);
87 
88  public:
89  cloe::Duration when;
90  cloe::TriggerPtr what;
91 };
92 
100 class Coordinator {
101  public:
102  Coordinator();
103 
104  const std::vector<HistoryTrigger>& history() const { return history_; }
105 
106  void register_action(const std::string& key, cloe::ActionFactoryPtr&& af);
107 
108  void register_event(const std::string& key, cloe::EventFactoryPtr&& ef,
109  std::shared_ptr<cloe::Callback> storage);
110 
111  std::shared_ptr<cloe::TriggerRegistrar> trigger_registrar(cloe::Source s);
112 
113  void enroll(cloe::Registrar& r);
114 
115  cloe::Logger logger() const { return cloe::logger::get("cloe"); }
116 
122 
123  protected:
124  cloe::ActionPtr make_action(const cloe::Conf& c) const;
125  cloe::EventPtr make_event(const cloe::Conf& c) const;
126  cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
127  void queue_trigger(cloe::Source s, const cloe::Conf& c) { queue_trigger(make_trigger(s, c)); }
128  void queue_trigger(cloe::TriggerPtr&& t);
129  void execute_trigger(cloe::TriggerPtr&& t, const cloe::Sync& s);
130 
131  // for access to protected methods
132  friend TriggerRegistrar;
133 
134  private:
135  // Options:
136  bool allow_errors_ = false;
137 
138  // Factories:
139  std::map<std::string, cloe::ActionFactoryPtr> actions_;
140  std::map<std::string, cloe::EventFactoryPtr> events_;
141 
142  // Execution:
143  std::shared_ptr<cloe::TriggerRegistrar> executer_registrar_;
144 
145  // Storage:
146  std::map<std::string, std::shared_ptr<cloe::Callback>> storage_;
147 
148  // Input:
149  std::list<cloe::TriggerPtr> input_queue_;
150  mutable std::mutex input_mutex_;
151 
152  // History:
153  std::vector<HistoryTrigger> history_;
154 };
155 
156 } // namespace engine
Definition: registrar.hpp:116
Definition: sync.hpp:34
Definition: trigger.hpp:73
Definition: coordinator.hpp:100
cloe::Duration process(const cloe::Sync &)
Definition: coordinator.cpp:179
Definition: coordinator.cpp:55
Definition: coordinator.hpp:49
const char * key() const
Definition: coordinator.hpp:58
Definition: coordinator.hpp:68
const char * key() const
Definition: coordinator.hpp:77
Definition: conf.hpp:82
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: coordinator.hpp:83
Source
Definition: trigger.hpp:351