aboutsummaryrefslogtreecommitdiff
path: root/src/EventManager.hpp
blob: 2e4184e84aa035c69796b9ef48d69177de71641c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H 1

#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.h>

#include <functional>
#include <string>
#include <vector>

typedef void *EvUserData;
typedef void ev_c_callback(struct evhttp_request *, EvUserData);
typedef std::function<void(struct evhttp_request *, EvUserData)> EvFunction;

struct ev_callback {
  EvFunction cb;
  EvUserData ud;
};

typedef std::tuple<std::string, struct ev_callback> EvUrlCallback;

static inline void default_evhttp_callback(struct evhttp_request *const req,
                                           EvUserData ud) {
  (void)ud;

  evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type",
                    "text/html");

  struct evbuffer *const output = evbuffer_new();
  if (output != nullptr) {
    evbuffer_add_printf(output, "%s\n",
                        "<html><body><b>default page</b></body></html>");
    evhttp_send_reply(req, 200, "OK", output);
    evbuffer_free(output);
  }
}

class EventManager {
public:
  EventManager() : m_DefaultCallback({default_evhttp_callback, nullptr}) {}
  ~EventManager();

  bool Init(std::string = "127.0.0.1", uint16_t port = 9000);
  void setDefaultCallback(EvFunction fn, EvUserData dat);
  void addCallback(std::string url, EvFunction fn, EvUserData dat);

private:
  struct ev_callback m_DefaultCallback;
  std::vector<EvUrlCallback> m_UrlCallbacks;

  struct event_config *m_EvConfig = nullptr;
  struct event_base *m_EvBase = nullptr;
  struct evhttp *m_EvHttp = nullptr;
  struct evhttp_bound_socket *m_EvSocket = nullptr;
  struct evconnlistener *m_EvListener = nullptr;
  struct event *m_EvTermEvent = nullptr;
};

#endif