common/wp-context.cpp
common/wp-result.cpp
common/kvp-container.cpp
- common/icontext.cpp
common/dispatcher.cpp
common/mainloop.cpp
common/service.cpp
namespace Csr {
namespace Client {
-AsyncLogic::AsyncLogic(ContextShPtr &context, const Callback &cb,
- void *userdata, const std::function<bool()> &isStopped) :
- m_origCtx(context),
+AsyncLogic::AsyncLogic(HandleExt *handle, void *userdata,
+ const std::function<bool()> &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"))
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<Callback::Id, Task> AsyncLogic::scanDirs(const std::shared_ptr<StrSet>
- &dirs)
+AsyncLogic::Ending AsyncLogic::scanDirs(const std::shared_ptr<StrSet> &dirs)
{
// TODO: canonicalize dirs. (e.g. Can omit subdirectory it there is
// parent directory in set)
- std::pair<Callback::Id, Task> 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<Callback::Id, Task> 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 =
// 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<csr_cs_detected_h>(r));
return task;
}
-std::pair<Callback::Id, Task> AsyncLogic::scanFiles(const
- std::shared_ptr<StrSet> &fileSet)
+AsyncLogic::Ending AsyncLogic::scanFiles(const std::shared_ptr<StrSet> &fileSet)
{
for (const auto &file : *fileSet) {
if (m_isStopped()) {
// 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<csr_cs_detected_h>(ret.second));
});
}
-void AsyncLogic::add(IResult *r)
-{
- m_ctx->add(r);
-}
-
-}
-}
+} // namespace Client
+} // namespace Csr
#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<Callback::Id, Task>;
+
+ AsyncLogic(HandleExt *handle, void *userdata,
const std::function<bool()> &isStopped);
virtual ~AsyncLogic();
- std::pair<Callback::Id, Task> scanFiles(const std::shared_ptr<StrSet> &files);
- std::pair<Callback::Id, Task> scanDir(const std::string &dir);
- std::pair<Callback::Id, Task> scanDirs(const std::shared_ptr<StrSet> &dirs);
+ Ending scanFiles(const std::shared_ptr<StrSet> &files);
+ Ending scanDir(const std::string &dir);
+ Ending scanDirs(const std::shared_ptr<StrSet> &dirs);
void stop(void);
template<typename T>
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<ResultPtr> m_results;
Callback m_cb;
void *m_userdata;
{
T value;
- m_origCtx->get(static_cast<int>(key), value);
+ m_handle->getContext()->get(static_cast<int>(key), value);
m_ctx->set(static_cast<int>(key), value);
}
return CSR_ERROR_UNKNOWN; // deserialization logic error
if (ret.second->hasValue()) {
- hExt->add(ret.second);
+ hExt->add(ResultPtr(ret.second));
*pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
} else {
*pdetected = nullptr;
return CSR_ERROR_UNKNOWN; // deserialization logic error
if (ret.second->hasValue()) {
- hExt->add(ret.second);
+ hExt->add(ResultPtr(ret.second));
*pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
} else {
*pdetected = nullptr;
}
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();
});
auto hExt = reinterpret_cast<Client::HandleExt *>(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();
});
}
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();
});
return CSR_ERROR_UNKNOWN; // deserialization logic error
if (ret.second->hasValue()) {
- hExt->add(ret.second);
+ hExt->add(ResultPtr(ret.second));
*pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
} else {
*pdetected = nullptr;
return CSR_ERROR_UNKNOWN; // deserialization logic error
if (ret.second->hasValue()) {
- hExt->add(ret.second);
+ hExt->add(ResultPtr(ret.second));
*pdetected = reinterpret_cast<csr_cs_detected_h>(ret.second);
} else {
*pdetected = nullptr;
DEBUG("client async thread done! tid: " << std::this_thread::get_id());
});
+
{
std::lock_guard<std::mutex> l(m_mutex);
m_workerMap.emplace(t.get_id(), std::move(t));
return *this;
}
+void HandleExt::add(ResultPtr &&ptr)
+{
+ std::lock_guard<std::mutex> l(m_resultsMutex);
+ m_results.emplace_back(std::forward<ResultPtr>(ptr));
+}
+
+void HandleExt::add(ResultListPtr &&ptr)
+{
+ std::lock_guard<std::mutex> l(m_resultsMutex);
+ m_resultLists.emplace_back(std::forward<ResultListPtr>(ptr));
+}
+
} // namespace Client
} // namespace Csr
Callback m_cb; // TODO: to refine..
+ virtual void add(ResultPtr &&) override;
+ virtual void add(ResultListPtr &&) override;
+
private:
struct Worker {
std::atomic<bool> isDone;
std::atomic<bool> m_stop;
std::mutex m_mutex;
+ std::mutex m_resultsMutex;
std::map<std::thread::id, Worker> m_workerMap;
};
namespace Client {
Handle::Handle(ContextShPtr &&context) :
- m_ctx(std::move(context))
+ m_ctx(std::forward<ContextShPtr>(context))
{
if (!m_ctx)
throw std::logic_error("context shouldn't be null");
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<ResultPtr>(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>(resultListPtr));
+ m_resultLists.emplace_back(std::forward<ResultListPtr>(ptr));
}
} // namespace Client
#include <utility>
#include <memory>
+#include <vector>
#include "common/icontext.h"
#include "common/dispatcher.h"
template<typename Type, typename ...Args>
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<ResultPtr> m_results;
+ std::vector<ResultListPtr> m_resultLists;
+
private:
std::unique_ptr<Dispatcher> m_dispatcher;
ContextShPtr m_ctx;
return ret.first;
}
- h->add(ret.second);
+ h->add(ResultPtr(ret.second));
*presult = reinterpret_cast<csr_wp_check_result_h>(ret.second);
return CSR_ERROR_NONE;
+++ /dev/null
-/*
- * 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 <stdexcept>
-#include <utility>
-
-#include "common/audit/logger.h"
-
-namespace Csr {
-
-IContext::IContext()
-{
-}
-
-IContext::~IContext()
-{
-}
-
-void IContext::add(ResultPtr &&item)
-{
- std::lock_guard<std::mutex> l(m_mutex);
- m_results.emplace_back(std::forward<ResultPtr>(item));
-}
-
-void IContext::add(IResult *item)
-{
- std::lock_guard<std::mutex> l(m_mutex);
- m_results.emplace_back(item);
-}
-
-void IContext::add(ResultListPtr &&item)
-{
- std::lock_guard<std::mutex> l(m_mutex);
- m_resultLists.emplace_back(std::forward<ResultListPtr>(item));
-}
-
-size_t IContext::size() const
-{
- std::lock_guard<std::mutex> l(m_mutex);
- return m_results.size();
-}
-
-} // namespace Csr
*/
#pragma once
-#include <vector>
#include <memory>
-#include <mutex>
#include "common/dispatcher.h"
#include "common/serialization.h"
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<ResultPtr> m_results;
- std::vector<ResultListPtr> m_resultLists;
-
-private:
- mutable std::mutex m_mutex;
};
} // namespace Csr