summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/CMakeLists.txt1
-rw-r--r--src/common/timer.cpp85
-rw-r--r--src/common/timer.h62
-rw-r--r--src/service/logic.cpp20
-rw-r--r--src/service/logic.h6
5 files changed, 172 insertions, 2 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 4cc3754..c5e965e 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -31,6 +31,7 @@ SET(${TARGET_CERT_CHECKER_COMMON}_SRCS
${COMMON_PATH}/mainloop.cpp
${COMMON_PATH}/service.cpp
${COMMON_PATH}/socket.cpp
+ ${COMMON_PATH}/timer.cpp
${COMMON_PATH}/log.cpp
)
diff --git a/src/common/timer.cpp b/src/common/timer.cpp
new file mode 100644
index 0000000..63267d5
--- /dev/null
+++ b/src/common/timer.cpp
@@ -0,0 +1,85 @@
+/*
+ * 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 timer.cpp
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @version 1.0
+ * @brief The implementation of timer
+ */
+#include "common/timer.h"
+
+namespace CCHECKER {
+
+Timer::Timer() :
+ m_isStop(false),
+ m_isRunning(false),
+ m_interval(10)
+{
+}
+
+Timer::~Timer()
+{
+}
+
+void Timer::timerStart(int interval)
+{
+ LogDebug("Timer start!!");
+
+ if(m_isRunning)
+ return;
+
+ m_isRunning = true;
+ m_interval = interval;
+ m_thread = std::thread(&Timer::run, this);
+}
+
+void Timer::timerStop()
+{
+ if (m_thread.joinable()) {
+ {
+ std::unique_lock<std::mutex> lock(m_mutex);
+ m_isStop = true;
+ m_isRunning = false;
+ m_cv.notify_one();
+ }
+ m_thread.join();
+ }
+
+ LogDebug("Timer stop!!");
+}
+
+void Timer::run()
+{
+ while(1) {
+ LogDebug("[timer] running interval : " << m_interval);
+
+ if (m_isStop)
+ break;
+
+ std::unique_lock<std::mutex> lock(m_mutex);
+ m_cv.wait_for(
+ lock,
+ std::chrono::seconds(m_interval),
+ [this] {
+ return m_isStop == true;
+ }
+ );
+
+ job();
+ }
+}
+
+} // namespace CCHECKER
diff --git a/src/common/timer.h b/src/common/timer.h
new file mode 100644
index 0000000..e24ac35
--- /dev/null
+++ b/src/common/timer.h
@@ -0,0 +1,62 @@
+/*
+ * 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 timer.h
+ * @author Sangwan Kwon (sangwan.kwon@samsung.com)
+ * @version 1.0
+ * @brief The implementation of timer
+ */
+#pragma once
+
+#include <condition_variable>
+#include <chrono>
+#include <future>
+
+#include "common/log.h"
+
+namespace CCHECKER {
+
+class Timer
+{
+public:
+ Timer(void);
+ virtual ~Timer(void);
+
+ Timer(const Timer &) = delete;
+ Timer &operator=(const Timer &) = delete;
+ Timer(Timer &&) = delete;
+ Timer &operator=(Timer &&) = delete;
+
+ void timerStart(int interval);
+ void timerStop(void);
+
+protected:
+ // This is for derived class member function.
+ virtual void job(void){}
+
+ void run();
+
+private:
+ std::mutex m_mutex;
+ std::condition_variable m_cv;
+ std::thread m_thread;
+
+ bool m_isStop;
+ bool m_isRunning;
+ int m_interval;
+};
+
+} // namespace CCHECKER
diff --git a/src/service/logic.cpp b/src/service/logic.cpp
index 84a7707..086dbf5 100644
--- a/src/service/logic.cpp
+++ b/src/service/logic.cpp
@@ -25,17 +25,18 @@
#include <stdexcept>
#include <set>
-#include "common/log.h"
#include <cchecker/sql_query.h>
#include <cchecker/UIBackend.h>
#include "common/binary-queue.h"
+#include "common/log.h"
using namespace std;
namespace CCHECKER {
namespace {
+
struct PkgmgrinfoEvent {
PkgmgrinfoEvent(uid_t _uid, const char *_pkgid)
: uid(_uid)
@@ -108,6 +109,8 @@ void Logic::clean(void)
delete m_sqlquery;
+ timerStop();
+
LogDebug("Cert-checker cleaning finish.");
}
@@ -575,6 +578,8 @@ void Logic::process_all()
break;
} else {
LogDebug("[thread] Check again : " << m_buffer.size());
+ // Timer running periodically
+ timerStart(3600);
}
} else if (!get_online()) {
LogDebug("[thread] No network. Buffer won't be processed.");
@@ -587,6 +592,19 @@ void Logic::process_all()
}
}
+void Logic::job(void)
+{
+ std::lock_guard<std::mutex> lock(m_mutex_cv);
+
+ if (m_buffer.empty()) {
+ LogDebug("[timer] Buffer is empty.");
+ timerStop();
+ } else {
+ LogDebug("[timer] Notify thread - periodic wakeup");
+ m_to_process.notify_one();
+ }
+}
+
void Logic::process_event(const event_t &event)
{
LogDebug("Move event from queue to (buffer and db).");
diff --git a/src/service/logic.h b/src/service/logic.h
index 834e172..659b776 100644
--- a/src/service/logic.h
+++ b/src/service/logic.h
@@ -33,6 +33,7 @@
#include <memory>
#include "common/types.h"
+#include "common/timer.h"
#include "common/binary-queue.h"
#include "service/app.h"
#include "service/certs.h"
@@ -61,7 +62,7 @@ enum pkgmgr_event_t {
EVENT_UNINSTALL
};
-class Logic {
+class Logic : public Timer {
public:
Logic(void);
virtual ~Logic(void);
@@ -134,6 +135,9 @@ class Logic {
bool call_ui(const app_t &app);
+ // Timer function
+ void job(void) override;
+
// main event loop data type
GMainLoop *m_loop;