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 <systemd/sd-daemon.h>
22 #include <sensor_loader.h>
23 #include <command_common.h>
24 #include <command_worker.h>
26 #include <sys/epoll.h>
27 #include <sensor_event_poller.h>
42 int server::get_systemd_socket(const char *name)
44 int type = SOCK_STREAM;
45 const int listening = 1;
49 if (sd_listen_fds(1) != 1)
52 fd = SD_LISTEN_FDS_START + 0;
54 if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
60 void server::accept_client(void)
62 command_worker *cmd_worker;
64 INFO("Client acceptor is started");
67 csocket client_command_socket;
69 if (!m_client_accep_socket.accept(client_command_socket)) {
70 ERR("Failed to accept connection request from a client");
74 DBG("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
76 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
79 ERR("Failed to allocate memory");
83 if(!cmd_worker->start())
88 void server::poll_event(void)
90 INFO("Event poller is started");
92 sensor_event_poller poller;
95 ERR("Failed to poll event");
100 void server::run(void)
103 const int MAX_PENDING_CONNECTION = 5;
105 m_mainloop = g_main_loop_new(NULL, false);
107 sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
110 INFO("Succeeded to get systemd socket(%d)", sock_fd);
111 m_client_accep_socket = csocket(sock_fd);
113 ERR("Failed to get systemd socket, create it by myself!");
114 if (!m_client_accep_socket.create(SOCK_STREAM)) {
115 ERR("Failed to create command channel");
119 if(!m_client_accep_socket.bind(COMMAND_CHANNEL_PATH)) {
120 ERR("Failed to bind command channel");
121 m_client_accep_socket.close();
125 if(!m_client_accep_socket.listen(MAX_PENDING_CONNECTION)) {
126 ERR("Failed to listen command channel");
131 sensor_event_dispatcher::get_instance().run();
133 thread client_accepter(&server::accept_client, this);
134 client_accepter.detach();
136 thread event_poller(&server::poll_event, this);
137 event_poller.detach();
139 sd_notify(0, "READY=1");
141 g_main_loop_run(m_mainloop);
142 g_main_loop_unref(m_mainloop);
147 void server::stop(void)
150 g_main_loop_quit(m_mainloop);
152 m_client_accep_socket.close();
155 server& server::get_instance()