Merge branch 'devel/tizen' into tizen
[platform/core/system/sensord.git] / src / shared / ipc_server.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "ipc_server.h"
21
22 #include "channel.h"
23 #include "sensor_log.h"
24 #include "event_loop.h"
25 #include "channel_event_handler.h"
26 #include "accept_event_handler.h"
27
28 using namespace ipc;
29
30 #define MAX_CONNECTIONS 1000
31
32 ipc_server::ipc_server(const std::string &path)
33 : m_event_loop(NULL)
34 , m_handler(NULL)
35 , m_accept_handler(NULL)
36 {
37         m_accept_sock.create(path);
38 }
39
40 ipc_server::~ipc_server()
41 {
42 }
43
44 bool ipc_server::set_option(int option, int value)
45 {
46         /* TODO */
47         return true;
48 }
49
50 bool ipc_server::set_option(const std::string &option, int value)
51 {
52         /* TODO */
53         return true;
54 }
55
56 void ipc_server::accept(ipc::socket &cli_sock)
57 {
58         m_accept_sock.accept(cli_sock);
59
60         _I("Accepted[%d]", cli_sock.get_fd());
61 }
62
63 bool ipc_server::bind(channel_handler *handler, event_loop *loop)
64 {
65         m_handler = handler;
66         m_event_loop = loop;
67
68         m_accept_sock.bind();
69         m_accept_sock.listen(MAX_CONNECTIONS);
70
71         register_acceptor();
72
73         _I("Bound[%d]", m_accept_sock.get_fd());
74         return true;
75 }
76
77 void ipc_server::register_channel(int fd, channel *ch)
78 {
79         channel_event_handler *ev_handler = new(std::nothrow) channel_event_handler(ch, m_handler);
80         retm_if(!ev_handler, "Failed to allocate memory");
81
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);
85
86         if (id == 0)
87                 delete ev_handler;
88 }
89
90 void ipc_server::register_acceptor(void)
91 {
92         int fd = m_accept_sock.get_fd();
93
94         m_accept_handler = new(std::nothrow) accept_event_handler(this);
95         retm_if(!m_accept_handler, "Failed to allocate memory");
96
97         uint64_t id = m_event_loop->add_event(fd,
98                         (event_condition)(EVENT_IN | EVENT_HUP | EVENT_NVAL), m_accept_handler);
99
100         if (id == 0) {
101                 delete m_accept_handler;
102                 m_accept_handler = NULL;
103         }
104 }
105
106 bool ipc_server::close(void)
107 {
108         m_accept_sock.close();
109
110         m_handler = NULL;
111
112         _I("Closed");
113         return true;
114 }