4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "ipc_server.h"
23 #include "sensor_log.h"
24 #include "event_loop.h"
25 #include "channel_event_handler.h"
26 #include "accept_event_handler.h"
30 #define MAX_CONNECTIONS 1000
32 ipc_server::ipc_server(const std::string &path)
35 , m_accept_handler(NULL)
37 m_accept_sock.create(path);
40 ipc_server::~ipc_server()
44 bool ipc_server::set_option(int option, int value)
50 bool ipc_server::set_option(const std::string &option, int value)
56 void ipc_server::accept(ipc::socket &cli_sock)
58 m_accept_sock.accept(cli_sock);
60 _I("Accepted[%d]", cli_sock.get_fd());
63 bool ipc_server::bind(channel_handler *handler, event_loop *loop)
69 m_accept_sock.listen(MAX_CONNECTIONS);
73 _I("Bound[%d]", m_accept_sock.get_fd());
77 void ipc_server::register_channel(int fd, channel *ch)
79 channel_event_handler *ev_handler = new(std::nothrow) channel_event_handler(ch, m_handler);
80 retm_if(!ev_handler, "Failed to allocate memory");
82 ch->bind(ev_handler, m_event_loop);
83 uint64_t id = m_event_loop->add_event(fd,
84 (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), ev_handler);
90 void ipc_server::register_acceptor(void)
92 int fd = m_accept_sock.get_fd();
94 m_accept_handler = new(std::nothrow) accept_event_handler(this);
95 retm_if(!m_accept_handler, "Failed to allocate memory");
97 uint64_t id = m_event_loop->add_event(fd,
98 (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), m_accept_handler);
101 delete m_accept_handler;
102 m_accept_handler = NULL;
106 bool ipc_server::close(void)
108 m_accept_sock.close();