From: Mu-Woong Lee Date: Tue, 1 Aug 2017 10:34:52 +0000 (+0900) Subject: agent daemon self-terminates when it is idle X-Git-Tag: submit/tizen/20170904.032954^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F75%2F141775%2F3;p=platform%2Fcore%2Fcontext%2Fcontext-service.git agent daemon self-terminates when it is idle Change-Id: I483ae9f0ce613694967552928919e90a83ea1bca Signed-off-by: Mu-Woong Lee --- diff --git a/src/agent/PluginLoader.cpp b/src/agent/PluginLoader.cpp index c49b617..da09815 100644 --- a/src/agent/PluginLoader.cpp +++ b/src/agent/PluginLoader.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -21,11 +22,13 @@ #include "AgentUtil.h" #include "PluginLoader.h" -#define PLUGIN_DIR _LIBDIR_ "/" CTX_AGENT_PLUGIN_DIR +#define PLUGIN_DIR _LIBDIR_ "/" CTX_AGENT_PLUGIN_DIR +#define IDLE_EXIT_DELAY 5 using namespace ctx; PluginLoader::PluginLoader(AgentUtil& agentUtil) : + __exitSrcId(0), __agentUtil(agentUtil) { if (!__load()) { @@ -43,14 +46,27 @@ PluginLoader::~PluginLoader() void PluginLoader::send(uint16_t id, uint8_t length, const void* command) { - for (auto& agent : __plugins) { - if (agent->getId() == id) { - agent->doAction(length, command); - return; + auto exitOnIdle = [this]() { + if (__exitSrcId > 0) { + g_source_remove(__exitSrcId); + __exitSrcId = 0; } + + if (__isIdle()) + __exitSrcId = g_timeout_add_seconds(IDLE_EXIT_DELAY, __exit, this); + }; + + for (auto& agent : __plugins) { + if (agent->getId() != id) + continue; + + agent->doAction(length, command); + exitOnIdle(); + return; } _W("Agent not found"); + exitOnIdle(); } bool PluginLoader::__load() @@ -115,3 +131,27 @@ void PluginLoader::__unload() __plugins.clear(); } + +bool PluginLoader::__isIdle() +{ + for (auto* plugin : __plugins) { + if (!plugin->isIdle()) + return false; + } + + return true; +} + +gboolean PluginLoader::__exit(gpointer data) +{ + PluginLoader* loader = static_cast(data); + + if (loader->__isIdle()) { + _I("All plugins are idle."); + raise(SIGTERM); + } + + loader->__exitSrcId = 0; + + return G_SOURCE_REMOVE; +} diff --git a/src/agent/PluginLoader.h b/src/agent/PluginLoader.h index 557c22a..db06e36 100644 --- a/src/agent/PluginLoader.h +++ b/src/agent/PluginLoader.h @@ -38,8 +38,14 @@ namespace ctx { bool __load(const std::string& filePath); void __unload(); - AgentUtil& __agentUtil; + // true, if all plugins are idle. + bool __isIdle(); + + static gboolean __exit(gpointer data); + + guint __exitSrcId; std::vector __plugins; + AgentUtil& __agentUtil; }; }