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.
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 * @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>
31 #include <condition_variable>
34 #include "irestservice.h"
35 #include "reportcomposer.h"
37 namespace communication
43 * @brief The Connection class provides communication protocol between DSM server and device-agent
49 * @typedef AllowedPredicate
50 * @brief AllowedPredicate
52 using AllowedPredicate = std::function<bool()>;
56 * @param duid device identifier
57 * @param timeout keepalive timeout duration
58 * @param restService pointer to the REST service
60 Connection(const std::string& duid, const std::chrono::milliseconds& timeout, IRestService* restService, AllowedPredicate&& allowed);
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
68 int addEventListener(const std::string& type, EventListener* listener);
71 * @brief removeEventListener deregister event listener identified by particular id
72 * @param id event listener identifier
74 void removeEventListener(int id);
77 * @brief addReportEvent add report event for transmission to the DSM server
78 * @param eventType type of the event
79 * @param event event content
81 void addReportEvent(const std::string& eventType, const Json::Value& event);
84 * @brief loadResource load resource identified by URI
85 * @param uri resource uri
87 std::string loadResource(const std::string& uri) const;
90 * @brief confirm sends confirmation request
91 * @param uri confirmation uri
92 * @param response confirmation request body
94 void confirm(const std::string& uri, const std::string& response) const;
97 * @brief confirm sends confirmation request
98 * @param uri confirmation uri
99 * @param response confirmation request data
101 void confirm(const std::string& uri, const Json::Value& response) const;
104 * @brief sendData sends data to the server
105 * @param data data to send
107 void sendData(const Json::Value& data) const;
110 * @brief loop event processing loop
111 * @note the method blocks execution until Connection::stop() method is called
116 * @brief stop stops the event proceesing loop
124 * @brief registerDevice registers the device
125 * @param data information about the device to register
128 const std::string& registerDevice(const Json::Value& data);
131 * @brief setkeepAliveTimeout set KeepAlive timeout
132 * @param timeout new timeout duration
134 void setkeepAliveTimeout(const std::chrono::milliseconds& timeout)
136 keepAliveTimeout = timeout;
140 * @brief Is connection allowed
142 bool isAllowed() const
144 return (work && allowedPredicate());
149 typedef std::list<EventListener*> ListenerList;
150 typedef std::reference_wrapper<ListenerList> ListenersRef;
151 typedef std::pair<ListenersRef, EventListener*> ListenerInfo;
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;
162 std::condition_variable notice;
165 AllowedPredicate allowedPredicate;
168 } // namespace communication
170 #endif // CONNECTION_H