2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #ifndef SHAREDDATALAYER_ENGINE_HPP_
23 #define SHAREDDATALAYER_ENGINE_HPP_
26 #include "private/timer.hpp"
28 namespace shareddatalayer
33 * Engine provides the basic services needed by asynchronous library:
34 * - File descriptor monitoring
45 virtual ~Engine() = default;
47 virtual int fd() const = 0;
49 virtual void handleEvents() = 0;
51 /** There is data to read. */
52 static const unsigned int EVENT_IN;
54 /** Writing is now possible. */
55 static const unsigned int EVENT_OUT;
57 /** Error condition. */
58 static const unsigned int EVENT_ERR;
61 static const unsigned int EVENT_HUP;
64 * Event handler function type.
66 * @param events Pending events.
68 using EventHandler = std::function<void(unsigned int events)>;
71 * Add a new file descriptor to be monitored. The added file descriptor
72 * must be deleted with deleteMonitoredFD() function when no longer
75 * @param fd The file descriptor to monitor.
76 * @param events The events to monitor as a bit mask of EVENT_IN and/or
77 * EVENT_OUT. There is no need to explicitly monitor
78 * EVENT_ERR or EVENT_HUP as they are automatically
80 * @param eh The event handler to call when any of the requested
83 * @see modifyMonitoredFD
84 * @see deleteMonitoredFD
86 virtual void addMonitoredFD(int fd, unsigned int events, const EventHandler& eh) = 0;
89 * Add a new file descriptor to be monitored. The added file descriptor
90 * is automatically deleted with deleteMonitorFD() upon its destructor.
91 * This is achieved by using the FileDescriptor::atClose() function.
93 * @param fd The file descriptor to monitor.
94 * @param events The events to monitor as a bit mask of EVENT_IN and/or
95 * EVENT_OUT. There is no need to explicitly monitor
96 * EVENT_ERR or EVENT_HUP as they are automatically
98 * @param eh The event handler to call when any of the requested
101 * @see modifyMonitoredFD
103 virtual void addMonitoredFD(FileDescriptor& fd, unsigned int events, const EventHandler& eh) = 0;
106 * Modify monitored events related to an earlier added file descriptor.
108 * @param fd The file descriptor whose monitored events to modify.
109 * @param events The new events to monitor, bitmask of EVENTS_IN and/or
110 * EVENT_OUT. There is no need to explicitly monitor
111 * EVENT_ERR or EVENT_HUP as they are automatically
114 * @see addMonitoredFD
115 * @see deleteMonitoredFD
117 virtual void modifyMonitoredFD(int fd, unsigned int events) = 0;
120 * Stop monitoring the given file descriptor.
122 * @param fd The file descriptor to stop monitoring.
124 * @see addMonitoredFD
125 * @see modifyMonitoredFD
127 virtual void deleteMonitoredFD(int fd) = 0;
129 using Callback = std::function<void()>;
132 * Post a callback function. The function will be called by the
133 * application's thread which is running the event loop and monitoring
134 * the library epoll file descriptor.
136 * The main use cases for this function are
137 * 1. To push work for the application's thread
138 * 2. Fake asynchronous API with synchronous implementation
140 * @note This function <b>is</b> thread-safe.
142 virtual void postCallback(const Callback& callback) = 0;
144 virtual void run() = 0;
146 virtual void stop() = 0;
148 Engine(const Engine&) = delete;
149 Engine(Engine&&) = delete;
150 Engine& operator = (const Engine&) = delete;
151 Engine& operator = (Engine&&) = delete;
154 virtual void armTimer(Timer& timer, const Timer::Duration& duration, const Timer::Callback& cb) = 0;
156 virtual void disarmTimer(const Timer& timer) = 0;