Merge "sensord: delete batch latency/attribute when client is terminated unexpectedly...
[platform/core/system/sensord.git] / src / server / sensor_event_dispatcher.h
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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 #ifndef _SENSOR_EVENT_DISPATCHER_H_
21 #define _SENSOR_EVENT_DISPATCHER_H_
22
23 #include <sensor_common.h>
24 #include <sensor_event_queue.h>
25 #include <client_info_manager.h>
26 #include <csocket.h>
27 #include <virtual_sensor.h>
28 #include <unordered_map>
29 #include <list>
30 #include <vector>
31
32 typedef std::unordered_map<unsigned int, sensor_event_t> event_type_last_event_map;
33 typedef std::list<virtual_sensor *> virtual_sensors;
34
35 class sensor_event_dispatcher {
36 public:
37         static sensor_event_dispatcher& get_instance(void);
38
39         bool run(void);
40         bool stop(void);
41         void accept_event_connections(csocket client_socket);
42
43         void request_last_event(int client_id, sensor_id_t sensor_id);
44
45         bool add_active_virtual_sensor(virtual_sensor *sensor);
46         bool delete_active_virtual_sensor(virtual_sensor *sensor);
47
48 private:
49         csocket m_accept_socket;
50         cmutex m_mutex;
51         cmutex m_last_events_mutex;
52         event_type_last_event_map m_last_events;
53         virtual_sensors m_active_virtual_sensors;
54         cmutex m_active_virtual_sensors_mutex;
55         bool m_running;
56
57         sensor_event_dispatcher();
58         ~sensor_event_dispatcher();
59         sensor_event_dispatcher(sensor_event_dispatcher const&) {};
60         sensor_event_dispatcher& operator=(sensor_event_dispatcher const&);
61
62         void accept_event_channel(csocket client_socket);
63
64         void dispatch_event(void);
65         void send_sensor_events(std::vector<void *> &events);
66         static client_info_manager& get_client_info_manager(void);
67         static sensor_event_queue& get_event_queue(void);
68
69         bool is_record_event(unsigned int event_type);
70         void put_last_event(unsigned int event_type, const sensor_event_t &event);
71         bool get_last_event(unsigned int event_type, sensor_event_t &event);
72
73         bool has_active_virtual_sensor(virtual_sensor *sensor);
74         virtual_sensors get_active_virtual_sensors(void);
75
76         void sort_sensor_events(std::vector<void *> &events);
77 };
78
79 #endif /* _SENSOR_EVENT_DISPATCHER_H_ */