sensord: clean up sf_common.h/sensor_common.h/sensor_logs.h
[platform/core/system/sensord.git] / src / server / server.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <systemd/sd-daemon.h>
21 #include <server.h>
22 #include <sensor_loader.h>
23 #include <command_common.h>
24 #include <command_worker.h>
25 #include <thread>
26 #include <sys/epoll.h>
27 #include <sensor_event_poller.h>
28
29 using std::thread;
30
31 server::server()
32 : m_mainloop(NULL)
33 {
34
35 }
36
37 server::~server()
38 {
39         stop();
40 }
41
42 int server::get_systemd_socket(const char *name)
43 {
44         int type = SOCK_STREAM;
45         const int listening = 1;
46         size_t length = 0;
47         int fd = -1;
48
49         if (sd_listen_fds(1) != 1)
50                 return -1;
51
52         fd = SD_LISTEN_FDS_START + 0;
53
54         if (sd_is_socket_unix(fd, type, listening, name, length) > 0)
55                 return fd;
56
57         return -1;
58 }
59
60 void server::accept_client(void)
61 {
62         command_worker *cmd_worker;
63
64         INFO("Client acceptor is started");
65
66         while (true) {
67                 csocket client_command_socket;
68
69                 if (!m_client_accep_socket.accept(client_command_socket)) {
70                         ERR("Failed to accept connection request from a client");
71                         continue;
72                 }
73
74                 DBG("New client (socket_fd : %d) connected", client_command_socket.get_socket_fd());
75
76                 cmd_worker = new(std::nothrow) command_worker(client_command_socket);
77
78                 if (!cmd_worker) {
79                         ERR("Failed to allocate memory");
80                         continue;
81                 }
82
83                 if(!cmd_worker->start())
84                         delete cmd_worker;
85         }
86 }
87
88 void server::poll_event(void)
89 {
90         INFO("Event poller is started");
91
92         sensor_event_poller poller;
93
94         if (!poller.poll()) {
95                 ERR("Failed to poll event");
96                 return;
97         }
98 }
99
100 void server::run(void)
101 {
102         int sock_fd = -1;
103         const int MAX_PENDING_CONNECTION = 5;
104
105         m_mainloop = g_main_loop_new(NULL, false);
106
107         sock_fd = get_systemd_socket(COMMAND_CHANNEL_PATH);
108
109         if (sock_fd >= 0) {
110                 INFO("Succeeded to get systemd socket(%d)", sock_fd);
111                 m_client_accep_socket = csocket(sock_fd);
112         } else {
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");
116                         return;
117                 }
118
119                 if(!m_client_accep_socket.bind(COMMAND_CHANNEL_PATH)) {
120                         ERR("Failed to bind command channel");
121                         m_client_accep_socket.close();
122                         return;
123                 }
124
125                 if(!m_client_accep_socket.listen(MAX_PENDING_CONNECTION)) {
126                         ERR("Failed to listen command channel");
127                         return;
128                 }
129         }
130
131         sensor_event_dispatcher::get_instance().run();
132
133         thread client_accepter(&server::accept_client, this);
134         client_accepter.detach();
135
136         thread event_poller(&server::poll_event, this);
137         event_poller.detach();
138
139         sd_notify(0, "READY=1");
140
141         g_main_loop_run(m_mainloop);
142         g_main_loop_unref(m_mainloop);
143
144         return;
145 }
146
147 void server::stop(void)
148 {
149         if(m_mainloop)
150                 g_main_loop_quit(m_mainloop);
151
152         m_client_accep_socket.close();
153 }
154
155 server& server::get_instance()
156 {
157         static server inst;
158         return inst;
159 }