Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_resource.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file simulator_resource.h
23  *
24  * @brief This file provides a abstract class SimulatorResource, for representing simulated resource.
25  */
26
27 #ifndef SIMULATOR_RESOURCE_H_
28 #define SIMULATOR_RESOURCE_H_
29
30 #include "simulator_uncopyable.h"
31 #include "simulator_resource_model.h"
32 #include "simulator_exceptions.h"
33
34 enum class ObservationStatus
35 {
36     REGISTER,
37     UNREGISTER
38 };
39
40 typedef struct
41 {
42     uint8_t id;
43     std::string address;
44     uint16_t port;
45 } ObserverInfo;
46
47 /**
48  * @class   SimulatorResource
49  * @brief   Abstract class represents simulated resource.
50  */
51 class SimulatorResource : private UnCopyable
52 {
53     public:
54         enum class Type
55         {
56             SINGLE_RESOURCE,
57             COLLECTION_RESOURCE
58         };
59
60         /**
61          * Callback method for receiving notifications when resource representation model changes.
62          *
63          * @param uri - URI of resource whose representation model got changed.
64          * @param resModel - Resource model.
65          */
66         typedef std::function<void (const std::string &uri, const SimulatorResourceModel &resModel)>
67         ResourceModelUpdateCallback;
68
69         /**
70          * Callback method for receiving notifications when observer is registered/unregistered
71          * with resource.
72          *
73          * @param uri - Resource URI
74          * @param state - OBSERVE_REGISTER if observer is registered, otherwise OBSERVE_UNREGISTER.
75          * @param observerInfo - Information about observer.
76          */
77         typedef std::function<void (const std::string &uri, ObservationStatus state,
78                                     const ObserverInfo &observerInfo)> ObserverCallback;
79
80         /**
81          * API to get the type which indicates whether resource is single or collection resource.
82          *
83          * @return Type of resource.
84          */
85         virtual bool isCollection() const;
86
87         /**
88          * API to get the type which indicates whether resource is single or collection resource.
89          *
90          * @return Type of resource.
91          */
92         virtual SimulatorResource::Type getType() const;
93
94         /**
95          * API to get the name of the resource.
96          *
97          * @return Resource name.
98          */
99         virtual std::string getName() const;
100
101         /**
102          * API to get the resource URI.
103          *
104          * @return Resource URI.
105          */
106         virtual std::string getURI() const;
107
108         /**
109          * API to get the resource type.
110          *
111          * @return Resource type.
112          */
113         virtual std::string getResourceType() const;
114
115         /**
116          * API to get the interfaces resource is bound with.
117          *
118          * @return Interface type.
119          */
120         virtual std::vector<std::string> getInterface() const;
121
122         /**
123          * API to get the observable state of resource.
124          *
125          * @return bool - true if resource is observable, otherwise false.
126          */
127         virtual bool isObservable() const = 0;
128
129         /**
130          * API to get the discoverable state of resource.
131          *
132          * @return bool - true if resource is discoverable, otherwise false.
133          */
134         virtual bool isDiscoverable() const = 0;
135
136         /**
137          * API to get the start state of resource.
138          *
139          * @return bool - true if resource is started, otherwise false.
140          */
141         virtual bool isStarted() const = 0;
142
143         /**
144          * API to set the name of the resource.
145          *
146          * @param name - Name to be set.
147          *
148          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
149          */
150         virtual void setName(const std::string &name) = 0;
151
152         /**
153          * API to set the resource URI.
154          *
155          * @param uri - URI to be set.
156          *
157          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
158          */
159         virtual void setURI(const std::string &uri) = 0;
160
161         /**
162          * API to set the resource type.
163          *
164          * @param resourceType - resource type string.
165          *
166          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
167          */
168         virtual void setResourceType(const std::string &resourceType) = 0;
169
170         /**
171          * API to set interface to resource.
172          *
173          * @param interfaceType - interface to be set.
174          *
175          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
176          */
177         virtual void setInterface(const std::string &interfaceType) = 0;
178
179         /**
180          * API to set list of interfaces to resource.
181          *
182          * @param interfaceTypes - interfaces to be set.
183          *
184          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
185          */
186         virtual void setInterface(const std::vector<std::string> &interfaceTypes) = 0;
187
188         /**
189          * API to add interface type for resource.
190          *
191          * @param interfaceType - interface to be added for resource.
192          *
193          * NOTE: API throws @InvalidArgsException, @SimulatorException exceptions.
194          */
195         virtual void addInterface(const std::string &interfaceType) = 0;
196
197         /**
198          * API to make the resource observable or non-observable.
199          *
200          * @param state - true for resource to be observable, otherwise false.
201          *
202          * NOTE: API throws @SimulatorException exceptions.
203          */
204         virtual void setObservable(bool state) = 0;
205
206         /**
207          * API to make the resource discoverable or non-discoverable.
208          *
209          * @param state - true for resource to be discoverable, otherwise false.
210          *
211          * NOTE: API throws @SimulatorException exceptions.
212          */
213         virtual void setDiscoverable(bool state) = 0;
214
215         /**
216          * API to set the callback for receiving the notifications when
217          * observer is registered or unregistered with resource.
218          *
219          * @param callback - Callback to be set for receiving the notifications.
220          */
221         virtual void setObserverCallback(ObserverCallback callback) = 0;
222
223         /**
224          * API to set the callback for receiving the notifications when the
225          * resource's representation model changes.
226          *
227          * @param callback - Callback to be set for receiving the notifications.
228          */
229         virtual void setModelChangeCallback(ResourceModelUpdateCallback callback) = 0;
230
231         /**
232          * API to start the resource.
233          *
234          * NOTE: API throws @SimulatorException exception.
235          */
236         virtual void start() = 0;
237
238         /**
239          * API to stop the resource.
240          *
241          * NOTE: API throws @SimulatorException exception.
242          */
243         virtual void stop() = 0;
244
245         /**
246          * API to get SimulatorResourceModel of resource.
247          *
248          * @return Resource model of the resource.
249          */
250         virtual SimulatorResourceModel getResourceModel() = 0;
251
252         /**
253          * API to get observers which are registered with resource.
254          *
255          * @return vector of ObserverInfo.
256          */
257         virtual std::vector<ObserverInfo> getObservers() const = 0;
258
259         /**
260          * API to notify resource's representation to specific observer.
261          *
262          * @param observerID - ID of observer.
263          *
264          * NOTE: API throws @SimulatorException exception.
265          */
266         virtual void notify(int observerID) = 0;
267
268         /**
269          * API to notify resource's representation to all registered observers.
270          *
271          * NOTE: API throws @SimulatorException exception.
272          */
273         virtual void notifyAll() = 0;
274
275     protected:
276         SimulatorResource::Type m_type;
277         std::string m_name;
278         std::string m_uri;
279         std::string m_resourceType;
280         std::vector<std::string> m_interfaces;
281 };
282
283 typedef std::shared_ptr<SimulatorResource> SimulatorResourceSP;
284
285 #endif