aboutsummaryrefslogtreecommitdiff
path: root/src/EventManager.hpp
blob: f9945104bce80ef5311eeb780f8878c6f1eff574 (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
#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H 1

#include "ContentManager.hpp"
#include "Filesystem.hpp"

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

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

using EvUserData = void *;
using EvFunction = std::function<void(struct evhttp_request *, EvUserData)>;

struct ev_callback
{
    EvFunction cb;
    EvUserData ud;
};

using EvUrlCallback = std::tuple<std::string, struct ev_callback>;

class EventManager
{
public:
    EventManager(std::shared_ptr<ContentManager> & cmgr);
    ~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);
    void AddChunkedCallback(std::string url, EvFunction fn, EvUserData dat, struct timeval chunk_timer);

private:
    std::shared_ptr<ContentManager> m_ContentManager;
    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 evconnlistener * m_EvListener = nullptr;
    struct event * m_EvTermEvent = nullptr;
};

#endif