Add first version
[ric-plt/sdl.git] / include / private / engineimpl.hpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
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
17 #ifndef SHAREDDATALAYER_ENGINEIMPL_HPP_
18 #define SHAREDDATALAYER_ENGINEIMPL_HPP_
19
20 #include <map>
21 #include <memory>
22 #include <vector>
23 #include <sys/epoll.h>
24 #include "private/engine.hpp"
25 #include "private/filedescriptor.hpp"
26
27 namespace shareddatalayer
28 {
29     class TimerFD;
30     class EventFD;
31     class System;
32
33     class EngineImpl: public Engine
34     {
35     public:
36         EngineImpl();
37
38         explicit EngineImpl(System& system);
39
40         ~EngineImpl();
41
42         int fd() const override { return epollFD; }
43
44         void handleEvents() override;
45
46         void addMonitoredFD(int fd, unsigned int events, const EventHandler& eh) override;
47
48         void addMonitoredFD(FileDescriptor& fd, unsigned int events, const EventHandler& eh) override;
49
50         void modifyMonitoredFD(int fd, unsigned int events) override;
51
52         void deleteMonitoredFD(int fd) override;
53
54         void postCallback(const Callback& callback) override;
55
56         void run() override;
57
58         void stop() override;
59
60     protected:
61         void armTimer(Timer& timer, const Timer::Duration& duration, const Timer::Callback& cb) override;
62
63         void disarmTimer(const Timer& timer) override;
64
65     private:
66         TimerFD& getTimerFD();
67
68         EventFD& getEventFD();
69
70         void epollWait(int timeout);
71
72         void callHandler(const epoll_event& e);
73
74         System& system;
75         bool stopped;
76         FileDescriptor epollFD;
77         std::map<int, EventHandler> handlers;
78         std::vector<epoll_event> ebuffer;
79         std::unique_ptr<TimerFD> timerFD;
80         std::unique_ptr<EventFD> eventFD;
81     };
82 }
83
84 #endif