* 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()) {
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()
__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;
+}
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;
};
}