${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
--- /dev/null
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */