agent daemon self-terminates when it is idle 75/141775/3
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 1 Aug 2017 10:34:52 +0000 (19:34 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 1 Aug 2017 10:41:22 +0000 (19:41 +0900)
Change-Id: I483ae9f0ce613694967552928919e90a83ea1bca
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/agent/PluginLoader.cpp
src/agent/PluginLoader.h

index c49b617..da09815 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <signal.h>
 #include <stdexcept>
 #include <sys/stat.h>
 #include <dirent.h>
 #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<PluginLoader*>(data);
+
+       if (loader->__isIdle()) {
+               _I("All plugins are idle.");
+               raise(SIGTERM);
+       }
+
+       loader->__exitSrcId = 0;
+
+       return G_SOURCE_REMOVE;
+}
index 557c22a..db06e36 100644 (file)
@@ -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<IAgentPlugin*> __plugins;
+               AgentUtil& __agentUtil;
        };
 
 }