$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 <sol/state_view.hpp> // for state_view
34 #include <sol/table.hpp> // for table
35 
36 #include <cloe/cloe_fwd.hpp> // for Registrar, DataBroker
37 #include <cloe/trigger.hpp> // for Trigger, Action, Event, ...
38 
39 namespace engine {
40 
41 // Forward declarations:
42 class TriggerRegistrar; // from coordinator.cpp
43 
49  public:
50  TriggerUnknownAction(const std::string& key, const cloe::Conf& c)
51  : TriggerInvalid(c, "unknown action: " + key), key_(key) {}
52  ~TriggerUnknownAction() noexcept override = default;
53 
57  [[nodiscard]] const char* key() const { return key_.c_str(); }
58 
59  private:
60  std::string key_;
61 };
62 
68  public:
69  TriggerUnknownEvent(const std::string& key, const cloe::Conf& c)
70  : TriggerInvalid(c, "unknown event: " + key), key_(key) {}
71  virtual ~TriggerUnknownEvent() noexcept = default;
72 
76  [[nodiscard]] const char* key() const { return key_.c_str(); }
77 
78  private:
79  std::string key_;
80 };
81 
83  HistoryTrigger(cloe::Duration d, cloe::TriggerPtr&& t) : when(d), what(std::move(t)) {}
84 
85  friend void to_json(cloe::Json& j, const HistoryTrigger& t);
86 
87  public:
88  cloe::Duration when;
89  cloe::TriggerPtr what;
90 };
91 
99 class Coordinator {
100  public:
101  Coordinator(sol::state_view lua, cloe::DataBroker* db);
102 
103  const std::vector<HistoryTrigger>& history() const { return history_; }
104 
105  void register_action(const std::string& key, cloe::ActionFactoryPtr&& af);
106 
107  void register_event(const std::string& key, cloe::EventFactoryPtr&& ef,
108  std::shared_ptr<cloe::Callback> storage);
109 
110  [[nodiscard]] sol::table register_lua_table(const std::string& field);
111 
112  [[nodiscard]] cloe::DataBroker* data_broker() const { return db_; }
113 
114  std::shared_ptr<cloe::TriggerRegistrar> trigger_registrar(cloe::Source s);
115 
116  void enroll(cloe::Registrar& r);
117 
118  [[nodiscard]] cloe::Logger logger() const { return cloe::logger::get("cloe"); }
119 
123  [[nodiscard]] std::map<std::string, fable::Json> trigger_action_schemas() const;
124 
128  [[nodiscard]] std::map<std::string, fable::Json> trigger_event_schemas() const;
129 
135 
136  size_t process_pending_lua_triggers(const cloe::Sync& sync);
137  size_t process_pending_web_triggers(const cloe::Sync& sync);
138 
139  void insert_trigger_from_lua(const cloe::Sync& sync, const sol::object& obj);
140  void execute_action_from_lua(const cloe::Sync& sync, const sol::object& obj);
141 
142  protected:
143  [[nodiscard]] cloe::ActionPtr make_action(const sol::object& lua) const;
144  [[nodiscard]] cloe::ActionPtr make_action(const cloe::Conf& c) const;
145  [[nodiscard]] cloe::EventPtr make_event(const cloe::Conf& c) const;
146  [[nodiscard]] cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
147  [[nodiscard]] cloe::TriggerPtr make_trigger(const sol::table& tbl) const;
148  void queue_trigger(cloe::Source s, const cloe::Conf& c) { queue_trigger(make_trigger(s, c)); }
149  void queue_trigger(cloe::TriggerPtr&& tp);
150  void store_trigger(cloe::TriggerPtr&& tp, const cloe::Sync& sync);
151  cloe::CallbackResult execute_trigger(cloe::TriggerPtr&& tp, const cloe::Sync& sync);
152 
153  // for access to protected methods
154  friend TriggerRegistrar;
155 
156  private:
157  // Options:
158  bool allow_errors_ = false;
159 
160  // Factories:
161  std::map<std::string, cloe::ActionFactoryPtr> actions_;
162  std::map<std::string, cloe::EventFactoryPtr> events_;
163  sol::state_view lua_;
164  cloe::DataBroker* db_; // non-owning
165 
166  // Execution:
167  std::shared_ptr<cloe::TriggerRegistrar> executer_registrar_;
168 
169  // Storage:
170  std::map<std::string, std::shared_ptr<cloe::Callback>> storage_;
171 
172  // Input:
173  std::list<cloe::TriggerPtr> input_queue_;
174  mutable std::mutex input_mutex_;
175 
176  // History:
177  std::vector<HistoryTrigger> history_;
178 };
179 
180 void register_usertype_coordinator(sol::table& lua, const cloe::Sync& sync);
181 
182 } // namespace engine
Definition: data_broker.hpp:1217
Definition: registrar.hpp:121
Definition: sync.hpp:34
Definition: trigger.hpp:73
Definition: coordinator.hpp:99
std::map< std::string, fable::Json > trigger_action_schemas() const
Definition: coordinator.cpp:85
cloe::Duration process(const cloe::Sync &)
Definition: coordinator.cpp:268
std::map< std::string, fable::Json > trigger_event_schemas() const
Definition: coordinator.cpp:93
Definition: coordinator.cpp:62
Definition: coordinator.hpp:48
const char * key() const
Definition: coordinator.hpp:57
Definition: coordinator.hpp:67
const char * key() const
Definition: coordinator.hpp:76
Definition: conf.hpp:81
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: coordinator.hpp:82
CallbackResult
Definition: trigger.hpp:514
Source
Definition: trigger.hpp:351