Elma
An event loop manager for embedded systems
process.cc
1 #include <stdexcept>
2 #include "elma.h"
3 
4 namespace elma {
5 
7 
11  Channel& Process::channel(string name) {
12  if ( _manager_ptr == NULL ) {
13  throw Exception("Cannot access channels in a process before the process is scheduled.");
14  } else {
15  return _manager_ptr->channel(name);
16  }
17  }
18 
19  void Process::watch(string event_name, std::function<void(Event&)> handler) {
20  if ( _manager_ptr == NULL ) {
21  throw Exception("Cannot access events in a process before the process is scheduled.");
22  } else {
23  _manager_ptr->watch(event_name, handler);
24  }
25  }
26 
27  void Process::emit(const Event& event) {
28  if ( _manager_ptr == NULL ) {
29  throw Exception("Cannot access events in a process before the process is scheduled.");
30  } else {
31  _manager_ptr->emit(event);
32  }
33  }
34 
35  void Process::http_get(std::string url, std::function<void(json&)> handler) {
36  _manager_ptr->client().get(url,handler);
37  }
38 
40 
44  duration<double, std::milli> time = last_update();
45  return time.count();
46  }
47 
49 
54  double Process::delta() {
55  duration<double, std::milli> diff = last_update() - previous_update();
56  return diff.count();
57  }
58 
59  // Manager interface for the _init method. Do not call directly.
60  void Process::_init() {
61  _status = STOPPED;
62  init();
63  }
64  // Manager interface for the _start method. Do not call directly.
65  void Process::_start(high_resolution_clock::duration elapsed) {
66  _status = RUNNING;
67  _start_time = high_resolution_clock::now();
68  _last_update = elapsed;
69  _num_updates = 0;
70  start();
71  }
72 
73  // Manager interface for the _update method. Do not call directly.
74  void Process::_update(high_resolution_clock::duration elapsed) {
75  _previous_update = _last_update;
76  _last_update = elapsed;
77  update();
78  _num_updates++;
79  }
80 
81  // Manager interface for the _stop method. Do not call directly.
82  void Process::_stop() {
83  _status = STOPPED;
84  stop();
85  }
86 
87  void Process::halt() { _manager_ptr->stop(); }
88 
89  void Process::set_manager(Manager * m_ptr) {
90  if ( _manager_ptr == NULL ) {
91  _manager_ptr = m_ptr;
92  } else {
93  throw Exception("A process cannot have two managers");
94  }
95  }
96 
97 }
98 
The Process Manager class.
Definition: manager.h:27
Channel & channel(string name)
Access a channel with the given name.
Definition: process.cc:11
virtual void init()=0
virtual void start()=0
high_resolution_clock::duration previous_update()
Definition: process.h:102
Channel & channel(string)
Definition: manager.cc:50
Manager & watch(string event_name, std::function< void(Event &)> handler)
Definition: manager.cc:68
Events that can be emitted, watched, and responded to with event handlers.
Definition: event.h:23
virtual void stop()=0
high_resolution_clock::duration last_update()
Definition: process.h:97
double milli_time()
The time since the last update in millisconds, as a double.
Definition: process.cc:43
virtual void update()=0
An exception class for Elma.
Definition: exceptions.h:13
Client & get(std::string url, std::function< void(json &)> handler)
Definition: client.cc:33
double delta()
The most recent amount of time between updates.
Definition: process.cc:54
A channel for sending double values to and from Process objects.
Definition: channel.h:21
Manager & emit(const Event &event)
Definition: manager.cc:85
Manager & stop()
Definition: manager.cc:123
Definition: channel.cc:5