8112a0bd74584172f4644b8a63d834747e6394f2
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / serviceprovider / SimulatorResourceServer.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * This file contains a class which has a set of native methods for manipulating
19  * the simulated resource by adding and removing attributes to its model,
20  * automating the attribute value updates, and changing the range of
21  * acceptable values of the attributes.
22  */
23 package org.oic.simulator.serviceprovider;
24
25 import java.util.Vector;
26
27 import org.oic.simulator.IAutomation;
28 import org.oic.simulator.SimulatorResourceModel;
29
30 /**
31  * This class represents a resource in the simulator. It provides a set of
32  * native methods for manipulating the attributes.
33  */
34 public class SimulatorResourceServer {
35
36     private String resourceName;
37     private String resourceURI;
38     private String resourceType;
39     private String interfaceType;
40
41     private long   nativeHandle;
42
43     private SimulatorResourceServer(long nativeHandle) {
44         this.nativeHandle = nativeHandle;
45     }
46
47     /**
48      * API to set the name of the resource. Example: Light, Fan, etc.
49      *
50      * @param resourceName
51      *            Name of the resource.
52      */
53     public void setName(String resourceName) {
54         this.resourceName = resourceName;
55     }
56
57     /**
58      * API to set the URI of the resource. Example: /oic/light, /oic/fan, etc.
59      *
60      * @param resourceURI
61      *            URI of the resource.
62      */
63     private void setURI(String resourceURI) {
64         this.resourceURI = resourceURI;
65     }
66
67     /**
68      * API to set the type of the resource. Example: oic.light, oic.fan, etc.
69      *
70      * @param resourceType
71      *            Type of the resource.
72      */
73     private void setResourceType(String resourceType) {
74         this.resourceType = resourceType;
75     }
76
77     /**
78      * API to set the interface type of the resource. Example: oic.if.baseline,
79      * oic.if.b, etc.
80      *
81      * @param interfaceType
82      *            Interface type of the resource.
83      */
84     private void setInterfaceType(String interfaceType) {
85         this.interfaceType = interfaceType;
86     }
87
88     /**
89      * API to get the resource name. Example: Light, Fan, etc.
90      *
91      * @return Resource name.
92      */
93     public String getName() {
94         return resourceName;
95     }
96
97     /**
98      * API to get the resource URI. Example: /oic/light, /oic/fan, etc.
99      *
100      * @return Resource URI.
101      */
102     public String getURI() {
103         return resourceURI;
104     }
105
106     /**
107      * API to get the resource Type. Example: oic.light, oic.fan, etc.
108      *
109      * @return Resource type.
110      */
111     public String getResourceType() {
112         return resourceType;
113     }
114
115     /**
116      * API to get the interface type of the resource. Example: oic.if.baseline,
117      * oic.if.b, etc.
118      *
119      * @return Interface type of the resource.
120      */
121     public String getInterfaceType() {
122         return interfaceType;
123     }
124
125     @Override
126     protected void finalize() throws Throwable {
127         dispose();
128     }
129
130     /**
131      * Native function to get the {@link SimulatorResourceModel} of the
132      * corresponding resource.
133      *
134      * @return {@link SimulatorResourceModel} object on success, otherwise null.
135      */
136     public native SimulatorResourceModel getModel();
137
138     /**
139      * Native function to automatically update the value of an attribute from
140      * its allowed values.
141      *
142      * @param attrName
143      *            Name of the attribute
144      *
145      * @param index
146      *            Index of the value in the allowed values.
147      */
148     public native void updateAttributeFromAllowedValues(String attrName,
149             int index);
150
151     /**
152      * Native function to set the range of allowed values. This function is
153      * intended to be used for integral type attributes.
154      *
155      * @param attrName
156      *            Name of the attribute
157      * @param min
158      *            Minimum value in the range.
159      * @param max
160      *            Maximum value in the range.
161      */
162     public native void setRange(String attrName, int min, int max);
163
164     /**
165      * Native function to add an attribute whose value is of type int.
166      *
167      * @param key
168      *            Name of the attribute
169      * @param value
170      *            Initial value of the attribute
171      */
172     public native void addAttributeInteger(String key, int value);
173
174     /**
175      * Native function to add an attribute whose value is of type double.
176      *
177      * @param key
178      *            Name of the attribute
179      * @param value
180      *            Initial value of the attribute
181      */
182     public native void addAttributeDouble(String key, double value);
183
184     /**
185      * Native function to add an attribute whose value is of type Boolean.
186      *
187      * @param key
188      *            Name of the attribute
189      * @param value
190      *            Initial value of the attribute
191      */
192     public native void addAttributeBoolean(String key, Boolean value);
193
194     /**
195      * Native function to add an attribute whose value is of type String.
196      *
197      * @param key
198      *            Name of the attribute
199      * @param value
200      *            Initial value of the attribute
201      */
202     public native void addAttributeStringN(String key, String value);
203
204     /**
205      * Native function to update the value of an attribute whose value is of
206      * type int.
207      *
208      * @param key
209      *            Name of the attribute
210      * @param value
211      *            New value of the attribute
212      */
213     public native void updateAttributeInteger(String key, int value);
214
215     /**
216      * Native function to update the value of an attribute whose value is of
217      * type double.
218      *
219      * @param key
220      *            Name of the attribute
221      * @param value
222      *            New value of the attribute
223      */
224     public native void updateAttributeDouble(String key, double value);
225
226     /**
227      * Native function to update the value of an attribute whose value is of
228      * type boolean.
229      *
230      * @param key
231      *            Name of the attribute
232      * @param value
233      *            New value of the attribute
234      */
235     public native void updateAttributeBoolean(String key, Boolean value);
236
237     /**
238      * Native function to update the value of an attribute whose value is of
239      * type String.
240      *
241      * @param key
242      *            Name of the attribute
243      * @param value
244      *            New value of the attribute
245      */
246     public native void updateAttributeStringN(String key, String value);
247
248     /**
249      * Native function to set the allowed values of attribute whose value is of
250      * type int.
251      *
252      * @param key
253      *            Name of the attribute
254      * @param allowedValues
255      *            Allowed values of the attribute
256      */
257     public native void setAllowedValuesInteger(String key,
258             Vector<Integer> allowedValues);
259
260     /**
261      * Native function to set the allowed values of attribute whose value is of
262      * type double.
263      *
264      * @param key
265      *            Name of the attribute
266      * @param allowedValues
267      *            Allowed values of the attribute
268      */
269     public native void setAllowedValuesDouble(String key,
270             Vector<Double> allowedValues);
271
272     /**
273      * Native function to set the allowed values of attribute whose value is of
274      * type String.
275      *
276      * @param key
277      *            Name of the attribute
278      * @param allowedValues
279      *            Allowed values of the attribute
280      */
281     public native void setAllowedValuesStringN(String key,
282             Vector<String> allowedValues);
283
284     /**
285      * Native function to start the resource level automation. This automation
286      * involves automatically updating all the possible values for all the
287      * attributes sequentially.
288      *
289      * @param typeOfAutomation
290      *            Indicates whether the automation is one-time or recursive.
291      * @param listener
292      *            Listener to be notified when automation ends.
293      *
294      * @return Automation ID using which the automation can be stopped.
295      */
296     public native int startResourceAutomation(int typeOfAutomation,
297             IAutomation listener);
298
299     /**
300      * Native function to start the attribute level automation. This automation
301      * involves automatically updating all the possible values for a given
302      * attribute sequentially.
303      *
304      * @param attrName
305      *            Name of the attribute to be automated.
306      * @param typeOfAutomation
307      *            Indicates whether the automation is one-time or recursive.
308      * @param listener
309      *            Listener to be notified when automation ends.
310      *
311      * @return Automation ID using which the automation can be stopped.
312      */
313     public native int startAttributeAutomation(String attrName,
314             int typeOfAutomation, IAutomation listener);
315
316     /**
317      * Native function to stop the automation.
318      *
319      * @param automationId
320      *            Using which a specific automation can be stopped.
321      */
322     public native void stopAutomation(int automationId);
323
324     /**
325      * Native function to remove an attribute from the resource model.
326      *
327      * @param key
328      *            Name of the attribute to be deleted
329      */
330     public native void removeAttribute(String key);
331
332     /**
333      * Native function to set the observation callback.
334      * 
335      * @param observer
336      *            Listener to be notified when clients start/stop observing.
337      */
338     public native void setObserverCallback(IObserver observer);
339
340     /**
341      * Native function to get the details of a list of observers.
342      * 
343      * @return An array of {@link ObserverInfo} objects.
344      */
345     public native ObserverInfo[] getObserversList();
346
347     /**
348      * Native function which sends notification to a specific observer.
349      * 
350      * @param id
351      *            Observer's Id.
352      */
353     public native void notifyObserver(int id);
354
355     /**
356      * Native function which sends notification to all observers.
357      */
358     public native void notifyAllObservers();
359
360     /**
361      * Native function to release the memory allocated to the native object for
362      * SimulatorResourceServer.
363      */
364     private native void dispose();
365 }