From 09397f8b1e1ab9945da4964d54916ef85d26c51b Mon Sep 17 00:00:00 2001 From: Kyungwook Tak Date: Tue, 26 Apr 2016 20:43:34 +0900 Subject: [PATCH] Move result list from context class to handle Change-Id: I47b3688b2d5e3170985d23613c6f18521c6d1ee3 Signed-off-by: Kyungwook Tak --- src/framework/CMakeLists.txt | 1 - src/framework/client/async-logic.cpp | 49 +++++++++-------------- src/framework/client/async-logic.h | 20 +++++----- src/framework/client/content-screening.cpp | 17 ++++---- src/framework/client/handle-ext.cpp | 13 ++++++ src/framework/client/handle-ext.h | 4 ++ src/framework/client/handle.cpp | 16 +++----- src/framework/client/handle.h | 10 ++++- src/framework/client/web-protection.cpp | 2 +- src/framework/common/icontext.cpp | 63 ------------------------------ src/framework/common/icontext.h | 17 +------- 11 files changed, 68 insertions(+), 144 deletions(-) delete mode 100644 src/framework/common/icontext.cpp diff --git a/src/framework/CMakeLists.txt b/src/framework/CMakeLists.txt index ef7eafc..e2c3766 100644 --- a/src/framework/CMakeLists.txt +++ b/src/framework/CMakeLists.txt @@ -33,7 +33,6 @@ SET(${TARGET_CSR_COMMON}_SRCS common/wp-context.cpp common/wp-result.cpp common/kvp-container.cpp - common/icontext.cpp common/dispatcher.cpp common/mainloop.cpp common/service.cpp diff --git a/src/framework/client/async-logic.cpp b/src/framework/client/async-logic.cpp index 20c6d40..1662e1e 100644 --- a/src/framework/client/async-logic.cpp +++ b/src/framework/client/async-logic.cpp @@ -29,11 +29,11 @@ namespace Csr { namespace Client { -AsyncLogic::AsyncLogic(ContextShPtr &context, const Callback &cb, - void *userdata, const std::function &isStopped) : - m_origCtx(context), +AsyncLogic::AsyncLogic(HandleExt *handle, void *userdata, + const std::function &isStopped) : + m_handle(handle), m_ctx(new CsContext), - m_cb(cb), + m_cb(handle->m_cb), m_userdata(userdata), m_isStopped(isStopped), m_dispatcher(new Dispatcher("/tmp/." SERVICE_NAME ".socket")) @@ -46,37 +46,30 @@ AsyncLogic::AsyncLogic(ContextShPtr &context, const Callback &cb, AsyncLogic::~AsyncLogic() { - DEBUG("AsyncLogic dtor. Results num in " - "mother ctx[" << m_origCtx->size() << "] " - "and here[" << m_ctx->size() << "]"); - - for (auto &resultPtr : m_ctx->m_results) - m_origCtx->add(std::move(resultPtr)); - - DEBUG("Integrated mother ctx results num: " << m_origCtx->size()); + for (auto &resultPtr : m_results) + m_handle->add(std::move(resultPtr)); } -std::pair AsyncLogic::scanDirs(const std::shared_ptr - &dirs) +AsyncLogic::Ending AsyncLogic::scanDirs(const std::shared_ptr &dirs) { // TODO: canonicalize dirs. (e.g. Can omit subdirectory it there is // parent directory in set) - std::pair t(Callback::Id::OnCompleted, [this] { + Ending e(Callback::Id::OnCompleted, [this] { if (m_cb.onCompleted) m_cb.onCompleted(this->m_userdata); }); for (const auto &dir : *dirs) { - t = scanDir(dir); + e = scanDir(dir); - if (t.first != Callback::Id::OnCompleted) - return t; + if (e.first != Callback::Id::OnCompleted) + return e; } - return t; + return e; } -std::pair AsyncLogic::scanDir(const std::string &dir) +AsyncLogic::Ending AsyncLogic::scanDir(const std::string &dir) { // For in case of there's already detected malware for dir auto retResults = @@ -98,7 +91,7 @@ std::pair AsyncLogic::scanDir(const std::string &dir) // Register already detected malwares to context to be freed with context. for (auto r : retResults.second) { - add(r); + m_results.emplace_back(r); if (m_cb.onDetected) m_cb.onDetected(m_userdata, reinterpret_cast(r)); @@ -125,8 +118,7 @@ std::pair AsyncLogic::scanDir(const std::string &dir) return task; } -std::pair AsyncLogic::scanFiles(const - std::shared_ptr &fileSet) +AsyncLogic::Ending AsyncLogic::scanFiles(const std::shared_ptr &fileSet) { for (const auto &file : *fileSet) { if (m_isStopped()) { @@ -164,7 +156,7 @@ std::pair AsyncLogic::scanFiles(const // malware detected! INFO("[Detected] file[" << file << "]"); - add(ret.second); + m_results.emplace_back(ret.second); if (m_cb.onDetected) m_cb.onDetected(m_userdata, reinterpret_cast(ret.second)); @@ -178,10 +170,5 @@ std::pair AsyncLogic::scanFiles(const }); } -void AsyncLogic::add(IResult *r) -{ - m_ctx->add(r); -} - -} -} +} // namespace Client +} // namespace Csr diff --git a/src/framework/client/async-logic.h b/src/framework/client/async-logic.h index 8d0a8d7..de49106 100644 --- a/src/framework/client/async-logic.h +++ b/src/framework/client/async-logic.h @@ -28,20 +28,22 @@ #include "common/cs-context.h" #include "common/dispatcher.h" #include "client/callback.h" +#include "client/handle-ext.h" namespace Csr { namespace Client { class AsyncLogic { public: - AsyncLogic(ContextShPtr &context, const Callback &cb, - void *userdata, + using Ending = std::pair; + + AsyncLogic(HandleExt *handle, void *userdata, const std::function &isStopped); virtual ~AsyncLogic(); - std::pair scanFiles(const std::shared_ptr &files); - std::pair scanDir(const std::string &dir); - std::pair scanDirs(const std::shared_ptr &dirs); + Ending scanFiles(const std::shared_ptr &files); + Ending scanDir(const std::string &dir); + Ending scanDirs(const std::shared_ptr &dirs); void stop(void); @@ -49,12 +51,10 @@ private: template void copyKvp(CsContext::Key); - void add(IResult *); - - ContextShPtr &m_origCtx; // for registering results for auto-release + Handle *m_handle; // for registering results for auto-release - // TODO: append it to handle context when destroyed ContextPtr m_ctx; + std::vector m_results; Callback m_cb; void *m_userdata; @@ -68,7 +68,7 @@ void AsyncLogic::copyKvp(CsContext::Key key) { T value; - m_origCtx->get(static_cast(key), value); + m_handle->getContext()->get(static_cast(key), value); m_ctx->set(static_cast(key), value); } diff --git a/src/framework/client/content-screening.cpp b/src/framework/client/content-screening.cpp index e0d2785..b60e662 100644 --- a/src/framework/client/content-screening.cpp +++ b/src/framework/client/content-screening.cpp @@ -224,7 +224,7 @@ int csr_cs_scan_data(csr_cs_context_h handle, const unsigned char *data, return CSR_ERROR_UNKNOWN; // deserialization logic error if (ret.second->hasValue()) { - hExt->add(ret.second); + hExt->add(ResultPtr(ret.second)); *pdetected = reinterpret_cast(ret.second); } else { *pdetected = nullptr; @@ -261,7 +261,7 @@ int csr_cs_scan_file(csr_cs_context_h handle, const char *file_path, return CSR_ERROR_UNKNOWN; // deserialization logic error if (ret.second->hasValue()) { - hExt->add(ret.second); + hExt->add(ResultPtr(ret.second)); *pdetected = reinterpret_cast(ret.second); } else { *pdetected = nullptr; @@ -384,8 +384,7 @@ int csr_cs_scan_files_async(csr_cs_context_h handle, const char *file_paths[], } hExt->dispatchAsync([hExt, user_data, fileSet] { - Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); }); l.scanFiles(fileSet).second(); }); @@ -407,8 +406,7 @@ int csr_cs_scan_dir_async(csr_cs_context_h handle, const char *dir_path, auto hExt = reinterpret_cast(handle); hExt->dispatchAsync([hExt, user_data, dir_path] { - Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); }); l.scanDir(dir_path).second(); }); @@ -439,8 +437,7 @@ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char *dir_paths[], } hExt->dispatchAsync([hExt, user_data, dirSet] { - Client::AsyncLogic l(hExt->getContext(), hExt->m_cb, user_data, - [&hExt] { return hExt->isStopped(); }); + Client::AsyncLogic l(hExt, user_data, [&hExt] { return hExt->isStopped(); }); l.scanDirs(dirSet).second(); }); @@ -662,7 +659,7 @@ int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path, return CSR_ERROR_UNKNOWN; // deserialization logic error if (ret.second->hasValue()) { - hExt->add(ret.second); + hExt->add(ResultPtr(ret.second)); *pdetected = reinterpret_cast(ret.second); } else { *pdetected = nullptr; @@ -754,7 +751,7 @@ int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path, return CSR_ERROR_UNKNOWN; // deserialization logic error if (ret.second->hasValue()) { - hExt->add(ret.second); + hExt->add(ResultPtr(ret.second)); *pdetected = reinterpret_cast(ret.second); } else { *pdetected = nullptr; diff --git a/src/framework/client/handle-ext.cpp b/src/framework/client/handle-ext.cpp index f06305f..7852520 100644 --- a/src/framework/client/handle-ext.cpp +++ b/src/framework/client/handle-ext.cpp @@ -108,6 +108,7 @@ void HandleExt::dispatchAsync(const Task &f) DEBUG("client async thread done! tid: " << std::this_thread::get_id()); }); + { std::lock_guard l(m_mutex); m_workerMap.emplace(t.get_id(), std::move(t)); @@ -138,5 +139,17 @@ HandleExt::Worker &HandleExt::Worker::operator=(HandleExt::Worker &&other) return *this; } +void HandleExt::add(ResultPtr &&ptr) +{ + std::lock_guard l(m_resultsMutex); + m_results.emplace_back(std::forward(ptr)); +} + +void HandleExt::add(ResultListPtr &&ptr) +{ + std::lock_guard l(m_resultsMutex); + m_resultLists.emplace_back(std::forward(ptr)); +} + } // namespace Client } // namespace Csr diff --git a/src/framework/client/handle-ext.h b/src/framework/client/handle-ext.h index 387e6fc..f4c1f24 100644 --- a/src/framework/client/handle-ext.h +++ b/src/framework/client/handle-ext.h @@ -48,6 +48,9 @@ public: Callback m_cb; // TODO: to refine.. + virtual void add(ResultPtr &&) override; + virtual void add(ResultListPtr &&) override; + private: struct Worker { std::atomic isDone; @@ -71,6 +74,7 @@ private: std::atomic m_stop; std::mutex m_mutex; + std::mutex m_resultsMutex; std::map m_workerMap; }; diff --git a/src/framework/client/handle.cpp b/src/framework/client/handle.cpp index ee4a5e1..52d11e6 100644 --- a/src/framework/client/handle.cpp +++ b/src/framework/client/handle.cpp @@ -28,7 +28,7 @@ namespace Csr { namespace Client { Handle::Handle(ContextShPtr &&context) : - m_ctx(std::move(context)) + m_ctx(std::forward(context)) { if (!m_ctx) throw std::logic_error("context shouldn't be null"); @@ -43,20 +43,14 @@ ContextShPtr &Handle::getContext() noexcept return m_ctx; } -void Handle::add(IResult *result) +void Handle::add(ResultPtr &&ptr) { - if (result == nullptr) - throw std::logic_error("result shouldn't be null"); - - m_ctx->add(result); + m_results.emplace_back(std::forward(ptr)); } -void Handle::add(ResultListPtr &&resultListPtr) +void Handle::add(ResultListPtr &&ptr) { - if (resultListPtr == nullptr) - throw std::logic_error("result list pointer shouldn't be null"); - - m_ctx->add(std::forward(resultListPtr)); + m_resultLists.emplace_back(std::forward(ptr)); } } // namespace Client diff --git a/src/framework/client/handle.h b/src/framework/client/handle.h index 88eecf8..e9e1614 100644 --- a/src/framework/client/handle.h +++ b/src/framework/client/handle.h @@ -23,6 +23,7 @@ #include #include +#include #include "common/icontext.h" #include "common/dispatcher.h" @@ -38,11 +39,16 @@ public: template Type dispatch(Args &&...); - void add(IResult *); - void add(ResultListPtr &&); + virtual void add(ResultPtr &&); + virtual void add(ResultListPtr &&); ContextShPtr &getContext(void) noexcept; +protected: + // for destroying with context + std::vector m_results; + std::vector m_resultLists; + private: std::unique_ptr m_dispatcher; ContextShPtr m_ctx; diff --git a/src/framework/client/web-protection.cpp b/src/framework/client/web-protection.cpp index 0dd8049..4f2de9d 100644 --- a/src/framework/client/web-protection.cpp +++ b/src/framework/client/web-protection.cpp @@ -118,7 +118,7 @@ int csr_wp_check_url(csr_wp_context_h handle, const char *url, return ret.first; } - h->add(ret.second); + h->add(ResultPtr(ret.second)); *presult = reinterpret_cast(ret.second); return CSR_ERROR_NONE; diff --git a/src/framework/common/icontext.cpp b/src/framework/common/icontext.cpp deleted file mode 100644 index ac31011..0000000 --- a/src/framework/common/icontext.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2016 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 icontext.cpp - * @author Kyungwook Tak (k.tak@samsung.com) - * @version 1.0 - * @brief Abstract class of context for both of cs / wp - */ -#include "common/icontext.h" - -#include -#include - -#include "common/audit/logger.h" - -namespace Csr { - -IContext::IContext() -{ -} - -IContext::~IContext() -{ -} - -void IContext::add(ResultPtr &&item) -{ - std::lock_guard l(m_mutex); - m_results.emplace_back(std::forward(item)); -} - -void IContext::add(IResult *item) -{ - std::lock_guard l(m_mutex); - m_results.emplace_back(item); -} - -void IContext::add(ResultListPtr &&item) -{ - std::lock_guard l(m_mutex); - m_resultLists.emplace_back(std::forward(item)); -} - -size_t IContext::size() const -{ - std::lock_guard l(m_mutex); - return m_results.size(); -} - -} // namespace Csr diff --git a/src/framework/common/icontext.h b/src/framework/common/icontext.h index a291860..8c58d31 100644 --- a/src/framework/common/icontext.h +++ b/src/framework/common/icontext.h @@ -21,9 +21,7 @@ */ #pragma once -#include #include -#include #include "common/dispatcher.h" #include "common/serialization.h" @@ -38,24 +36,13 @@ using ContextShPtr = std::shared_ptr; class IContext : public ISerializable, public KvpContainer { public: - IContext(); - virtual ~IContext(); + IContext() {} + virtual ~IContext() {} IContext(IContext &&) = delete; IContext &operator=(IContext &&) = delete; IContext(const IContext &) = delete; IContext &operator=(const IContext &) = delete; - - void add(ResultPtr &&); - void add(IResult *); - void add(ResultListPtr &&); - size_t size(void) const; - // for destroying with context - std::vector m_results; - std::vector m_resultLists; - -private: - mutable std::mutex m_mutex; }; } // namespace Csr -- 2.7.4