4 * Copyright (c) 2014 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 <sys/epoll.h>
21 #include <sys/socket.h>
23 #include <systemd/sd-daemon.h>
25 #include <sensor_loader.h>
26 #include <command_common.h>
27 #include <command_worker.h>
28 #include <external_sensor_worker.h>
29 #include <external_sensor_service.h>
31 #include <sensor_event_poller.h>
32 #include <client_info_manager.h>
34 #define SYSTEMD_SOCKET_MAX 2
48 int server::get_systemd_socket(const char *name)
50 int type = SOCK_STREAM;
57 if (!strcmp(name, EVENT_CHANNEL_PATH)) {
58 type = SOCK_SEQPACKET;
63 if (sd_listen_fds(fd_env) < 0) {
64 _E("Failed to listen fds from systemd");
68 for (fd_index = 0; fd_index < SYSTEMD_SOCKET_MAX; ++fd_index) {
69 fd = SD_LISTEN_FDS_START + fd_index;
71 if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
78 void server::accept_command_channel(void)
80 _I("Command channel acceptor is started");
83 csocket client_command_socket;
85 if (!m_command_channel_accept_socket.is_valid()) {
86 _E("Failed to accept, event_channel_accept_socket is closed");
90 if (!m_command_channel_accept_socket.accept(client_command_socket)) {
91 _E("Failed to accept command channel from a client");
100 _D("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
102 /* TODO: if socket is closed, it should be erased */
103 client_command_sockets.push_back(client_command_socket);
105 thread worker_dispatcher(&server::dispatch_worker, this, client_command_socket);
106 worker_dispatcher.detach();
109 _I("Command channel acceptor is terminated");
112 void server::accept_event_channel(void)
114 _I("Event channel acceptor is started!");
117 csocket client_event_socket;
119 if (!m_event_channel_accept_socket.is_valid()) {
120 _E("Failed to accept, event_channel_accept_socket is closed");
124 if (!m_event_channel_accept_socket.accept(client_event_socket)) {
125 _E("Failed to accept event channel from a client");
134 /* TODO: if socket is closed, it should be erased */
135 client_event_sockets.push_back(client_event_socket);
137 _D("New client(socket_fd : %d) connected", client_event_socket.get_socket_fd());
139 thread event_channel_creator(&server::dispatch_event_channel_creator, this, client_event_socket);
140 event_channel_creator.detach();
143 _I("Event channel acceptor is terminated");
146 void server::dispatch_worker(csocket socket)
150 if (socket.recv(&worker_type, sizeof(worker_type)) <= 0) {
151 _E("Failed to get worker type");
156 if (worker_type == CLIENT_TYPE_SENSOR_CLIENT) {
157 command_worker *worker;
158 worker = new(std::nothrow) command_worker(socket);
161 _E("Failed to allocate memory");
166 if (!worker->start()) {
167 _E("Failed to start command worker");
170 } else if (worker_type == CLIENT_TYPE_EXTERNAL_SOURCE) {
171 external_sensor_worker *worker;
172 worker = new(std::nothrow) external_sensor_worker(socket);
175 _E("Failed to allocate memory");
180 if (!worker->start()) {
181 _E("Failed to start external worker");
185 _E("Not supported worker type: %d", worker_type);
190 void server::dispatch_event_channel_creator(csocket socket)
194 if (socket.recv(&client_type, sizeof(client_type)) <= 0) {
195 _E("Failed to get client type");
200 if (client_type == CLIENT_TYPE_SENSOR_CLIENT) {
201 sensor_event_dispatcher::get_instance().accept_event_connections(socket);
202 } else if (client_type == CLIENT_TYPE_EXTERNAL_SOURCE) {
203 external_sensor_service::get_instance().accept_command_channel(socket);
205 _E("Not supported client type: %d", client_type);
210 void server::poll_event(void)
212 _I("Event poller is started");
214 sensor_event_poller poller;
216 if (!poller.poll()) {
217 _E("Failed to poll event");
222 bool server::listen_command_channel(void)
225 const int MAX_PENDING_CONNECTION = 10;
227 sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
230 _I("Succeeded to get systemd socket(%d)", sock_fd);
231 m_command_channel_accept_socket = csocket(sock_fd);
235 INFO("Failed to get systemd socket, create it by myself!");
236 if (!m_command_channel_accept_socket.create(SOCK_STREAM)) {
237 _E("Failed to create command channel");
241 if (!m_command_channel_accept_socket.bind(COMMAND_CHANNEL_PATH)) {
242 _E("Failed to bind command channel");
243 m_command_channel_accept_socket.close();
247 if (!m_command_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
248 _E("Failed to listen command channel");
255 bool server::listen_event_channel(void)
258 const int MAX_PENDING_CONNECTION = 32;
260 sock_fd = get_systemd_socket(EVENT_CHANNEL_PATH);
263 _I("Succeeded to get systemd socket(%d)", sock_fd);
264 m_event_channel_accept_socket = csocket(sock_fd);
268 INFO("Failed to get systemd socket, create it by myself!");
270 if (!m_event_channel_accept_socket.create(SOCK_SEQPACKET)) {
271 _E("Failed to create event channel");
275 if (!m_event_channel_accept_socket.bind(EVENT_CHANNEL_PATH)) {
276 _E("Failed to bind event channel");
277 m_event_channel_accept_socket.close();
281 if (!m_event_channel_accept_socket.listen(MAX_PENDING_CONNECTION)) {
282 _E("Failed to listen event channel");
283 m_event_channel_accept_socket.close();
290 void server::close_socket(void)
292 m_command_channel_accept_socket.close();
293 m_event_channel_accept_socket.close();
295 for (unsigned int i = 0; i < client_command_sockets.size(); ++i)
296 client_command_sockets[i].close();
298 for (unsigned int i = 0; i < client_event_sockets.size(); ++i)
299 client_event_sockets[i].close();
301 client_command_sockets.clear();
302 client_event_sockets.clear();
305 void server::initialize(void)
308 m_mainloop = g_main_loop_new(NULL, false);
310 sensor_event_dispatcher::get_instance().run();
311 external_sensor_service::get_instance().run();
313 listen_command_channel();
314 listen_event_channel();
316 std::thread event_channel_accepter(&server::accept_event_channel, this);
317 event_channel_accepter.detach();
319 std::thread command_channel_accepter(&server::accept_command_channel, this);
320 command_channel_accepter.detach();
322 std::thread event_poller(&server::poll_event, this);
323 event_poller.detach();
325 sd_notify(0, "READY=1");
327 g_main_loop_run(m_mainloop);
330 void server::terminate(void)
332 sensor_event_dispatcher::get_instance().stop();
337 void server::run(void)
343 void server::stop(void)
345 _I("Sensord server stopped");
350 g_main_loop_quit(m_mainloop);
351 g_main_loop_unref(m_mainloop);
356 server& server::get_instance(void)