$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 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  virtual ~TriggerUnknownAction() noexcept = default;
53 
57  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  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  sol::table register_lua_table(const std::string& field);
111 
112  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  cloe::Logger logger() const { return cloe::logger::get("cloe"); }
119 
125 
126  size_t process_pending_lua_triggers(const cloe::Sync& sync);
127  size_t process_pending_web_triggers(const cloe::Sync& sync);
128 
129  void insert_trigger_from_lua(const cloe::Sync& sync, const sol::object& obj);
130  void execute_action_from_lua(const cloe::Sync& sync, const sol::object& obj);
131 
132  protected:
133  cloe::ActionPtr make_action(const sol::object& lua) const;
134  cloe::ActionPtr make_action(const cloe::Conf& c) const;
135  cloe::EventPtr make_event(const cloe::Conf& c) const;
136  cloe::TriggerPtr make_trigger(cloe::Source s, const cloe::Conf& c) const;
137  cloe::TriggerPtr make_trigger(const sol::table& tbl) const;
138  void queue_trigger(cloe::Source s, const cloe::Conf& c) { queue_trigger(make_trigger(s, c)); }
139  void queue_trigger(cloe::TriggerPtr&& tp);
140  void store_trigger(cloe::TriggerPtr&& tp, const cloe::Sync& sync);
141  cloe::CallbackResult execute_trigger(cloe::TriggerPtr&& tp, const cloe::Sync& sync);
142 
143  // for access to protected methods
144  friend TriggerRegistrar;
145 
146  private:
147  // Options:
148  bool allow_errors_ = false;
149 
150  // Factories:
151  std::map<std::string, cloe::ActionFactoryPtr> actions_;
152  std::map<std::string, cloe::EventFactoryPtr> events_;
153  sol::state_view lua_;
154  cloe::DataBroker* db_; // non-owning
155 
156  // Execution:
157  std::shared_ptr<cloe::TriggerRegistrar> executer_registrar_;
158 
159  // Storage:
160  std::map<std::string, std::shared_ptr<cloe::Callback>> storage_;
161 
162  // Input:
163  std::list<cloe::TriggerPtr> input_queue_;
164  mutable std::mutex input_mutex_;
165 
166  // History:
167  std::vector<HistoryTrigger> history_;
168 };
169 
170 void register_usertype_coordinator(sol::table& lua, const cloe::Sync& sync);
171 
172 } // namespace engine
Definition: data_broker.hpp:1217
Definition: registrar.hpp:121
Definition: sync.hpp:34
Definition: trigger.hpp:73
Definition: coordinator.hpp:99
cloe::Duration process(const cloe::Sync &)
Definition: coordinator.cpp:252
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:76
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
Definition: coordinator.hpp:82
CallbackResult
Definition: trigger.hpp:514
Source
Definition: trigger.hpp:351