Elma
An event loop manager for embedded systems
manager.h
1 #ifndef _ELMA_MANAGER_H
2 #define _ELMA_MANAGER_H
3 
4 #include <vector>
5 #include <map>
6 #include <chrono>
7 #include <functional>
8 #include <mutex>
9 
10 #include "elma.h"
11 
12 namespace elma {
13 
14  using std::string;
15  using std::vector;
16  using std::map;
17  using namespace std::chrono;
18 
19  class Channel;
20  class Process;
21 
23 
26 
27  class Manager {
28 
29  public:
30 
32  Manager() : _running(false), _simulated_time(false), _nice_sleep_amount(1_us) {}
33 
34  Manager& schedule(Process& process, high_resolution_clock::duration period);
35  Manager& remove(Process& process);
36  Manager& all(std::function<void(Process&)> f);
37 
38  Manager& set_priority(Process& process, int priority);
39  Manager& sort_processes();
40 
41  Manager& init();
42  Manager& start();
43  Manager& update();
44  Manager& stop();
45 
46  Manager& use_simulated_time();
47  Manager& use_real_time();
48 
49  Manager& run(high_resolution_clock::duration runtime);
50  Manager& run();
51  Manager& run(std::function<bool()> condition);
52 
55  inline high_resolution_clock::time_point start_time() { return _start_time; }
56 
59  inline high_resolution_clock::duration elapsed() { return _elapsed; }
60 
64  inline std::mutex& get_update_mutex() { return _update_mutex; }
65 
68  inline Manager& set_niceness(high_resolution_clock::duration n) {
69  _nice_sleep_amount = n;
70  return *this;
71  }
72 
73  // Channel Interface
74  Manager& add_channel(Channel&);
75  Channel& channel(string);
76 
77  // Event Interface
78  Manager& watch(string event_name, std::function<void(Event&)> handler);
79  Manager& emit(const Event& event);
80  Client& client() { return _client; }
81 
82  private:
83 
84  void update_elapsed_time();
85 
86  const int Priority_min = -5, Priority_max = 15;
87  vector<Process *> _processes;
88  map<string, Channel *> _channels;
89  map<string, vector<std::function<void(Event&)>>> event_handlers;
90  high_resolution_clock::time_point _start_time;
91  high_resolution_clock::duration _elapsed;
92  Client _client;
93  bool _running;
94  bool _simulated_time;
95 
96  std::mutex _update_mutex;
97  high_resolution_clock::duration _nice_sleep_amount;
98 
99  };
100 
101 }
102 
103 #endif
The Process Manager class.
Definition: manager.h:27
high_resolution_clock::duration elapsed()
Definition: manager.h:59
An HTTP client for connecting to json services.
Definition: client.h:34
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
std::mutex & get_update_mutex()
Definition: manager.h:64
Manager & set_niceness(high_resolution_clock::duration n)
Definition: manager.h:68
high_resolution_clock::time_point start_time()
Definition: manager.h:55
A channel for sending double values to and from Process objects.
Definition: channel.h:21
Manager()
Default constructor.
Definition: manager.h:32
Definition: channel.cc:5
An abstract base class for processes.
Definition: process.h:24