From 94b08af3a6ff275818e9e6104dc556d93d95e36a Mon Sep 17 00:00:00 2001 From: "kibak.yoon" Date: Wed, 5 Apr 2017 09:57:43 +0900 Subject: [PATCH] sensord: ipc: add ipc_server class - ipc_server class is a helper for server - ipc_server class that makes it easy to bind/listen/accept a channel to use for server. - it creates and bind/accepts socket/channel/channel_event_handler Change-Id: If79e27a6babcf73126b56f65e750176a9ddd5c78 Signed-off-by: kibak.yoon --- src/shared/accept_event_handler.cpp | 47 +++++++++++++++ src/shared/accept_event_handler.h | 42 ++++++++++++++ src/shared/ipc_server.cpp | 111 ++++++++++++++++++++++++++++++++++++ src/shared/ipc_server.h | 59 +++++++++++++++++++ 4 files changed, 259 insertions(+) create mode 100644 src/shared/accept_event_handler.cpp create mode 100644 src/shared/accept_event_handler.h create mode 100644 src/shared/ipc_server.cpp create mode 100644 src/shared/ipc_server.h diff --git a/src/shared/accept_event_handler.cpp b/src/shared/accept_event_handler.cpp new file mode 100644 index 0000000..527d967 --- /dev/null +++ b/src/shared/accept_event_handler.cpp @@ -0,0 +1,47 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * 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. + * + */ + +#include "accept_event_handler.h" + +#include "sensor_log.h" +#include "ipc_server.h" + +using namespace ipc; + +accept_event_handler::accept_event_handler(ipc_server *server) +: m_server(server) +{ +} + +bool accept_event_handler::handle(int fd, event_condition condition) +{ + retv_if((condition & (EVENT_HUP)), false); + + stream_socket *cli_sock = new(std::nothrow) stream_socket(); + retvm_if(!cli_sock, false, "Failed to allocate memory"); + + m_server->accept(*cli_sock); + + channel *_ch = new(std::nothrow) channel(cli_sock); + retvm_if(!_ch, false, "Failed to allocate memory"); + + m_server->register_channel(cli_sock->get_fd(), _ch); + + return true; +} diff --git a/src/shared/accept_event_handler.h b/src/shared/accept_event_handler.h new file mode 100644 index 0000000..814f560 --- /dev/null +++ b/src/shared/accept_event_handler.h @@ -0,0 +1,42 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * 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. + * + */ + +#ifndef __ACCEPT_EVENT_HANDLER__ +#define __ACCEPT_EVENT_HANDLER__ + +#include "event_handler.h" + +namespace ipc { + +class ipc_server; + +class accept_event_handler : public event_handler +{ +public: + accept_event_handler(ipc_server *server); + + bool handle(int fd, event_condition condition); + +private: + ipc_server *m_server; +}; + +} + +#endif /* __ACCEPT_EVENT_HANDLER__ */ diff --git a/src/shared/ipc_server.cpp b/src/shared/ipc_server.cpp new file mode 100644 index 0000000..9723a98 --- /dev/null +++ b/src/shared/ipc_server.cpp @@ -0,0 +1,111 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * 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. + * + */ + +#include "ipc_server.h" + +#include "channel.h" +#include "sensor_log.h" +#include "event_loop.h" +#include "channel_event_handler.h" +#include "accept_event_handler.h" + +using namespace ipc; + +#define MAX_CONNECTIONS 1000 + +ipc_server::ipc_server(const std::string &path) +{ + m_accept_sock.create(path); +} + +ipc_server::~ipc_server() +{ +} + +bool ipc_server::set_option(int option, int value) +{ + /* TODO */ + return true; +} + +bool ipc_server::set_option(const std::string &option, int value) +{ + /* TODO */ + return true; +} + +void ipc_server::accept(ipc::socket &cli_sock) +{ + m_accept_sock.accept(cli_sock); + + _I("Accepted[%d]", cli_sock.get_fd()); +} + +bool ipc_server::bind(channel_handler *handler, event_loop *loop) +{ + m_handler = handler; + m_event_loop = loop; + + m_accept_sock.bind(); + m_accept_sock.listen(MAX_CONNECTIONS); + + register_acceptor(); + + _I("Bound[%d]", m_accept_sock.get_fd()); + return true; +} + +void ipc_server::register_channel(int fd, channel *ch) +{ + channel_event_handler *ev_handler = new(std::nothrow) channel_event_handler(ch, m_handler); + retm_if(!ev_handler, "Failed to allocate memory"); + + ch->bind(ev_handler, m_event_loop); + uint64_t id = m_event_loop->add_event(fd, + (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), ev_handler); + + if (id == 0) + delete ev_handler; +} + +void ipc_server::register_acceptor(void) +{ + int fd = m_accept_sock.get_fd(); + + m_accept_handler = new(std::nothrow) accept_event_handler(this); + retm_if(!m_accept_handler, "Failed to allocate memory"); + + uint64_t id = m_event_loop->add_event(fd, + (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), m_accept_handler); + + if (id == 0) { + delete m_accept_handler; + m_accept_handler = NULL; + } +} + +bool ipc_server::close(void) +{ + m_accept_sock.close(); + + m_handler = NULL; + + _I("Closed"); + return true; +} diff --git a/src/shared/ipc_server.h b/src/shared/ipc_server.h new file mode 100644 index 0000000..7e033d0 --- /dev/null +++ b/src/shared/ipc_server.h @@ -0,0 +1,59 @@ +/* + * sensord + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * 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. + * + */ + +#ifndef __IPC_SERVER_H__ +#define __IPC_SERVER_H__ + +#include + +#include "stream_socket.h" +#include "channel.h" +#include "channel_handler.h" +#include "accept_event_handler.h" +#include "event_loop.h" + +namespace ipc { + +class ipc_server { +public: + ipc_server(const std::string &path); + ~ipc_server(); + + bool set_option(int option, int value); + bool set_option(const std::string &option, int value); + + bool bind(channel_handler *handler, event_loop *loop); + bool close(void); + + /* TODO: only accept_handler should use these functions */ + void accept(ipc::socket &cli_sock); + void register_channel(int fd, channel *ch); + void register_acceptor(void); + +private: + stream_socket m_accept_sock; + + event_loop *m_event_loop; + channel_handler *m_handler; + accept_event_handler *m_accept_handler; +}; + +} + +#endif /* __IPC_SERVER_H__ */ -- 2.7.4