From a4e40f847dc0bbc9e5569a23b1acd58c3d9f6f99 Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Tue, 2 May 2017 20:34:05 +0900 Subject: [PATCH] sensord: add lazy binding event handler to event loop - TBD: 1. thread-safe issue 2. sensor event: global main context or thread-default context Change-Id: I1d0f4464e14dc0e6bd0bb529ffe14cf682ef69af Signed-off-by: kibak.yoon --- src/client/sensor_listener.cpp | 4 +++- src/shared/channel.cpp | 9 +++++++++ src/shared/channel.h | 2 ++ src/shared/ipc_client.cpp | 4 ++-- src/shared/ipc_client.h | 3 ++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/client/sensor_listener.cpp b/src/client/sensor_listener.cpp index 95c411c..0e66ef7 100644 --- a/src/client/sensor_listener.cpp +++ b/src/client/sensor_listener.cpp @@ -152,7 +152,7 @@ bool sensor_listener::connect(void) m_cmd_channel = m_client->connect(NULL); retvm_if(!m_cmd_channel, false, "Failed to connect to server"); - m_evt_channel = m_client->connect(m_handler, &m_loop); + m_evt_channel = m_client->connect(m_handler, &m_loop, false); retvm_if(!m_evt_channel, false, "Failed to connect to server"); ipc::message msg; @@ -170,6 +170,8 @@ bool sensor_listener::connect(void) m_id = buf.listener_id; m_connected.store(true); + m_evt_channel->bind(); + _D("Listener ID[%d]", get_id()); return true; diff --git a/src/shared/channel.cpp b/src/shared/channel.cpp index 8dc25ac..8549f1e 100644 --- a/src/shared/channel.cpp +++ b/src/shared/channel.cpp @@ -24,6 +24,7 @@ #include #include "sensor_log.h" +#include "channel_event_handler.h" #define SYSTEMD_SOCK_BUF_SIZE 40000 @@ -111,6 +112,14 @@ void channel::bind(channel_handler *handler, event_loop *loop) m_handler->connected(this); } +void channel::bind(void) +{ + ret_if(!m_loop); + m_event_id = m_loop->add_event(m_socket->get_fd(), + (EVENT_IN | EVENT_HUP | EVENT_NVAL), + dynamic_cast(m_handler)); +} + bool channel::connect(channel_handler *handler, event_loop *loop) { if (!m_socket->connect()) diff --git a/src/shared/channel.h b/src/shared/channel.h index e37a7a9..db709b6 100644 --- a/src/shared/channel.h +++ b/src/shared/channel.h @@ -38,6 +38,8 @@ public: channel(socket *sock); ~channel(); + /* TODO */ + void bind(void); void bind(channel_handler *handler, event_loop *loop); bool connect(channel_handler *handler, event_loop *loop); diff --git a/src/shared/ipc_client.cpp b/src/shared/ipc_client.cpp index 71fdde4..1376ac7 100644 --- a/src/shared/ipc_client.cpp +++ b/src/shared/ipc_client.cpp @@ -50,7 +50,7 @@ channel *ipc_client::connect(channel_handler *handler) return connect(handler, NULL); } -channel *ipc_client::connect(channel_handler *handler, event_loop *loop) +channel *ipc_client::connect(channel_handler *handler, event_loop *loop, bool bind) { socket *sock; channel *ch; @@ -81,7 +81,7 @@ channel *ipc_client::connect(channel_handler *handler, event_loop *loop) ch->connect(ev_handler, loop); - if (loop) { + if (loop && bind) { uint64_t id = loop->add_event(sock->get_fd(), (EVENT_IN | EVENT_HUP | EVENT_NVAL), ev_handler); ch->set_event_id(id); diff --git a/src/shared/ipc_client.h b/src/shared/ipc_client.h index 8f52678..40d9182 100644 --- a/src/shared/ipc_client.h +++ b/src/shared/ipc_client.h @@ -38,7 +38,8 @@ public: /* call channel->disconnect() after you have used it */ channel *connect(channel_handler *handler); - channel *connect(channel_handler *handler, event_loop *loop); + /* TODO: remove bind parameter */ + channel *connect(channel_handler *handler, event_loop *loop, bool bind = true); private: std::string m_path; -- 2.7.4