Commit summary:
[platform/core/security/suspicious-activity-monitor.git] / communication / inc / connection.h
1 /**
2  * Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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  * @file   connection.h
20  * @brief  Connection class header
21  * @date   Created Feb 14, 2018
22  * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
23  */
24 #ifndef CONNECTION_H
25 #define CONNECTION_H
26
27 #include <string>
28 #include <mutex>
29 #include <list>
30 #include <map>
31 #include <condition_variable>
32 #include <chrono>
33 #include <functional>
34 #include "irestservice.h"
35 #include "reportcomposer.h"
36
37 namespace communication
38 {
39
40 class EventListener;
41
42 /**
43  * @brief The Connection class provides communication protocol between DSM server and device-agent
44  */
45 class Connection
46 {
47 public:
48     /**
49      * @typedef AllowedPredicate
50      * @brief AllowedPredicate
51      */
52     using AllowedPredicate = std::function<bool()>;
53
54     /**
55      * @brief Constructor
56      * @param duid device identifier
57      * @param timeout keepalive timeout duration
58      * @param restService pointer to the REST service
59      */
60     Connection(const std::string& duid, const std::chrono::milliseconds& timeout, IRestService* restService, AllowedPredicate&& allowed);
61
62     /**
63      * @brief addEventListener registers a listener for the events of particular type
64      * @param type event type that listener wants to receive
65      * @param listener pointer to a listener
66      * @return registered listener identifier
67      */
68     int addEventListener(const std::string& type, EventListener* listener);
69
70     /**
71      * @brief removeEventListener deregister event listener identified by particular id
72      * @param id event listener identifier
73      */
74     void removeEventListener(int id);
75
76     /**
77      * @brief addReportEvent add report event for transmission to the DSM server
78      * @param eventType type of the event
79      * @param event event content
80      */
81     void addReportEvent(const std::string& eventType, const Json::Value& event);
82
83     /**
84      * @brief loadResource load resource identified by URI
85      * @param uri resource uri
86      */
87     std::string loadResource(const std::string& uri) const;
88
89     /**
90      * @brief confirm sends confirmation request
91      * @param uri confirmation uri
92      * @param response confirmation request body
93      */
94     void confirm(const std::string& uri, const std::string& response) const;
95
96     /**
97      * @brief confirm sends confirmation request
98      * @param uri confirmation uri
99      * @param response confirmation request data
100      */
101     void confirm(const std::string& uri, const Json::Value& response) const;
102
103     /**
104      * @brief sendData sends data to the server
105      * @param data data to send
106      */
107     void sendData(const Json::Value& data) const;
108
109     /**
110      * @brief loop event processing loop
111      * @note the method blocks execution until Connection::stop() method is called
112      */
113     void loop();
114
115     /**
116      * @brief stop stops the event proceesing loop
117      */
118     void stop()
119     {
120         work = false;
121     }
122
123     /**
124      * @brief registerDevice registers the device
125      * @param data information about the device to register
126      *
127      */
128     const std::string& registerDevice(const Json::Value& data);
129
130     /**
131      * @brief setkeepAliveTimeout set KeepAlive timeout
132      * @param timeout new timeout duration
133      */
134     void setkeepAliveTimeout(const std::chrono::milliseconds& timeout)
135     {
136         keepAliveTimeout = timeout;
137     }
138
139     /**
140      * @brief Is connection allowed
141      */
142     bool isAllowed() const
143     {
144         return (work && allowedPredicate());
145     }
146
147 private:
148
149     typedef std::list<EventListener*> ListenerList;
150     typedef std::reference_wrapper<ListenerList> ListenersRef;
151     typedef std::pair<ListenersRef, EventListener*> ListenerInfo;
152
153     void checkUpdates();
154
155     std::map<int, std::pair<ListenersRef, EventListener*>> active;
156     std::map<std::string, ListenerList> listeners;
157     std::list<ReportEvent> reports;
158     std::chrono::milliseconds keepAliveTimeout;
159     SessionInfo sinfo;
160     IRestService* rest;
161     std::mutex locker;
162     std::condition_variable notice;
163     int lastId;
164     bool work;
165     AllowedPredicate allowedPredicate;
166 };
167
168 } // namespace communication
169
170 #endif // CONNECTION_H