Add poll() wrapper 46/28846/1
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 22 Aug 2014 09:58:38 +0000 (11:58 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Thu, 16 Oct 2014 13:44:30 +0000 (15:44 +0200)
Introduces Descriptor set class that is a wrapper for:
-waiting for descirptors using poll()
-adding/removing descriptors to/from pollfd
-callback invocation

Use tests from next commit for verification:
ckm-tests-internal -t DESCRIPTOR_SET_TEST
All should pass

Change-Id: I4b86e4407d899ace57ff872b0db37d045e3bb9af

src/manager/CMakeLists.txt
src/manager/common/descriptor-set.cpp [new file with mode: 0644]
src/manager/common/descriptor-set.h [new file with mode: 0644]

index c2ed42d..83daa5e 100644 (file)
@@ -22,6 +22,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/common/certificate-store.cpp
     ${COMMON_PATH}/common/key-impl.cpp
     ${COMMON_PATH}/common/pkcs12-impl.cpp
+    ${COMMON_PATH}/common/descriptor-set.cpp
     ${COMMON_PATH}/dpl/log/src/abstract_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/dlog_log_provider.cpp
     ${COMMON_PATH}/dpl/log/src/log.cpp
diff --git a/src/manager/common/descriptor-set.cpp b/src/manager/common/descriptor-set.cpp
new file mode 100644 (file)
index 0000000..181fae8
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file       descriptor-set.cpp
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#include "descriptor-set.h"
+#include <dpl/log/log.h>
+#include <string.h>
+
+namespace CKM {
+
+DescriptorSet::DescriptorSet() : m_dirty(true), m_fds(NULL) {
+}
+
+DescriptorSet::~DescriptorSet() {
+    purge();
+}
+
+void DescriptorSet::purge() {
+    for(auto it:m_descriptors)
+        close(it.first);
+    m_descriptors.clear();
+}
+
+void DescriptorSet::add(int fd, short events, Callback&& callback) {
+    // map operator[] requires empty DescriptorData constructor
+    auto it = m_descriptors.find(fd);
+    if (it == m_descriptors.end()) {
+        m_descriptors.insert(std::make_pair(fd,DescriptorData(events, std::move(callback))));
+    } else {
+        it->second.events = events;
+        it->second.callback = std::move(callback);
+    }
+    m_dirty = true;
+}
+
+void DescriptorSet::remove(int fd) {
+    if (0 != m_descriptors.erase(fd)) {
+        close(fd);
+        m_dirty = true;
+    }
+}
+
+void DescriptorSet::wait(int timeout_ms) {
+    if(!rebuildPollfd())
+        return;
+
+    // wait
+    int ret = TEMP_FAILURE_RETRY(poll(m_fds, m_descriptors.size(), timeout_ms));
+    if (ret == 0) {
+        ThrowMsg(Timeout, "Poll timeout");
+    } else if (ret < 0) {
+        int err = errno;
+        ThrowMsg(InternalError, "Poll failed " << strerror(err));
+    }
+
+    notify(ret);
+}
+
+bool DescriptorSet::rebuildPollfd() {
+    if (m_dirty) {
+       delete[] m_fds;
+       m_fds = NULL;
+       if (m_descriptors.empty()) {
+           LogWarning("Nothing to wait for");
+           return false;
+       }
+
+       m_fds = new pollfd[m_descriptors.size()];
+       size_t idx = 0;
+       for(const auto& it : m_descriptors) {
+           m_fds[idx].fd = it.first;
+           m_fds[idx].events = it.second.events;
+           idx++;
+       }
+       m_dirty = false;
+    }
+    return true;
+}
+
+void DescriptorSet::notify(int descCount) {
+    size_t size = m_descriptors.size();
+    for(size_t idx = 0;idx < size;++idx) {
+        const pollfd& pfd = m_fds[idx];
+        if (pfd.revents == 0)
+            continue;
+
+        /*
+         * Descriptors can be added/removed inside observer callback but:
+         * 1. m_fds is not affected. It will be regenerated in next wait()
+         * 2. No m_descriptors iterator will be invalidated
+         * 3. m_descriptors size is stored in local variable
+         */
+        m_descriptors.at(pfd.fd).callback(pfd.fd, pfd.revents);
+        descCount--;
+
+        // no more descriptors to check
+        if (descCount == 0)
+            break;
+    }
+    if (descCount != 0)
+        ThrowMsg(InternalError, "Number of notified descriptors do not match");
+}
+
+} /* namespace CKM */
diff --git a/src/manager/common/descriptor-set.h b/src/manager/common/descriptor-set.h
new file mode 100644 (file)
index 0000000..c6a0a4e
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file       descriptor-set.h
+ * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
+ * @version    1.0
+ */
+
+#pragma once
+
+#include <map>
+#include <functional>
+#include <dpl/exception.h>
+#include <poll.h>
+#include <noncopyable.h>
+
+namespace CKM {
+
+class IDescriptorSet
+{
+public:
+    // int is for descriptor, short is for revents,
+    typedef std::function<void(int, short)> Callback;
+
+    virtual void add(int fd, short events, Callback&& callback) = 0;
+    virtual void remove(int fd) = 0;
+protected:
+    // I don't want anyone to manage object lifetime via interface.
+    IDescriptorSet() {}
+    ~IDescriptorSet() {}
+};
+
+/**
+ * @brief Wrapper for poll()
+ */
+class DescriptorSet : public IDescriptorSet
+{
+public:
+    DescriptorSet();
+    virtual ~DescriptorSet();
+
+    NONCOPYABLE(DescriptorSet);
+
+    /*
+     * Add descriptor fd to watched set. Watches for events. Takes ownership of fd (closes it). Will
+     * synchronously call supported callback when an event occurs on descriptor. If descriptor
+     * already exists in the set events and callback will be overwritten.
+     *
+     * @param fd       descriptor to be watched
+     * @param events   events to watch for
+     * @param callback callback to be called when an event on descriptor occurs
+     */
+    virtual void add(int fd, short events, Callback&& callback);
+    /*
+     * Removes give descriptor from watched set and closes it.
+     *
+     * @param fd       descriptor to be removed and closed
+     */
+    virtual void remove(int fd);
+
+    /*
+     * Wait for descriptor events using poll().
+     * Synchronously calls provided descriptor callbacks.
+     *
+     * @param timeout_ms  timeout in ms. egative value means no timeout.
+     *
+     * @throws Timeout exception in case of timeout
+     * @throws InternalError in case of other error
+     */
+    void wait(int timeout_ms = 10000);
+    /*
+     * Removes and closes all descriptors
+     */
+    void purge();
+
+    DECLARE_EXCEPTION_TYPE(CKM::Exception, InternalError);
+    DECLARE_EXCEPTION_TYPE(CKM::Exception, Timeout);
+
+protected:
+    // returns false if there are no descriptors to wait for
+    bool rebuildPollfd();
+    void notify(int descCount);
+
+    struct DescriptorData
+    {
+        DescriptorData(short e, Callback&& c) : events(e), callback(std::move(c)) {}
+
+        short events;
+        Callback callback;
+    };
+
+    std::map<int, DescriptorData> m_descriptors;
+
+    // true if pollfd needs update
+    bool m_dirty;
+    pollfd* m_fds;
+};
+
+} /* namespace CKM */