7e166bab15476794b8e6109d13541a045230f146
[platform/core/security/key-manager.git] / src / manager / client-async / descriptor-set.h
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file       descriptor-set.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <map>
25 #include <functional>
26 #include <dpl/exception.h>
27 #include <poll.h>
28 #include <noncopyable.h>
29
30 namespace CKM {
31
32 class IDescriptorSet
33 {
34 public:
35     // int is for descriptor, short is for revents,
36     typedef std::function<void(int, short)> Callback;
37
38     virtual void add(int fd, short events, Callback&& callback) = 0;
39     virtual void remove(int fd, bool close_fd = true) = 0;
40 protected:
41     // I don't want anyone to manage object lifetime via interface.
42     IDescriptorSet() {}
43     ~IDescriptorSet() {}
44 };
45
46 /**
47  * @brief Wrapper for poll()
48  */
49 class DescriptorSet : public IDescriptorSet
50 {
51 public:
52     DescriptorSet();
53     virtual ~DescriptorSet();
54
55     NONCOPYABLE(DescriptorSet);
56
57     /*
58      * Add descriptor fd to watched set. Watches for events. Takes ownership of fd (closes it). Will
59      * synchronously call supported callback when an event occurs on descriptor. If descriptor
60      * already exists in the set events and callback will be overwritten.
61      *
62      * @param fd       descriptor to be watched
63      * @param events   events to watch for
64      * @param callback callback to be called when an event on descriptor occurs
65      */
66     virtual void add(int fd, short events, Callback&& callback);
67     /*
68      * Removes give descriptor from watched set and closes it.
69      *
70      * @param fd       descriptor to be removed and closed
71      */
72     virtual void remove(int fd, bool close_fd = true);
73
74     /*
75      * Wait for descriptor events using poll().
76      * Synchronously calls provided descriptor callbacks.
77      *
78      * @param timeout_ms  timeout in ms. egative value means no timeout.
79      *
80      * @throws Timeout exception in case of timeout
81      * @throws InternalError in case of other error
82      */
83     void wait(int timeout_ms = 10000);
84     /*
85      * Removes and closes all descriptors
86      */
87     void purge();
88
89     DECLARE_EXCEPTION_TYPE(CKM::Exception, InternalError);
90     DECLARE_EXCEPTION_TYPE(CKM::Exception, Timeout);
91
92 protected:
93     // returns false if there are no descriptors to wait for
94     bool rebuildPollfd();
95     void notify(int descCount);
96
97     struct DescriptorData
98     {
99         DescriptorData(short e, Callback&& c) : events(e), callback(std::move(c)) {}
100
101         short events;
102         Callback callback;
103     };
104
105     std::map<int, DescriptorData> m_descriptors;
106
107     // true if pollfd needs update
108     bool m_dirty;
109     pollfd* m_fds;
110 };
111
112 } /* namespace CKM */