Java SDK and Eclipse plugin for simulator.
[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 package org.oic.simulator.serviceprovider;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.IAutomation;
22 import org.oic.simulator.InvalidArgsException;
23 import org.oic.simulator.SimulatorException;
24 import org.oic.simulator.SimulatorResourceModel;
25
26 /**
27  * This class represents a resource in the simulator. It provides a set of
28  * native methods for manipulating the simulated resource by adding and removing
29  * attributes to its model, automating the attribute value updates, and changing
30  * the range of acceptable values of the attributes.
31  */
32 public class SimulatorResourceServer {
33
34     private String resourceName;
35     private String resourceURI;
36     private String resourceType;
37     private String interfaceType;
38
39     private long   nativeHandle;
40
41     private SimulatorResourceServer(long nativeHandle) {
42         this.nativeHandle = nativeHandle;
43     }
44
45     /**
46      * API to get the resource name. Example: Light, Fan, etc.
47      *
48      * @return Resource name.
49      */
50     public String getName() {
51         return resourceName;
52     }
53
54     /**
55      * API to get the resource URI. Example: /oic/light, /oic/fan, etc.
56      *
57      * @return Resource URI.
58      */
59     public String getURI() {
60         return resourceURI;
61     }
62
63     /**
64      * API to get the resource Type. Example: oic.light, oic.fan, etc.
65      *
66      * @return Resource type.
67      */
68     public String getResourceType() {
69         return resourceType;
70     }
71
72     /**
73      * API to get the interface type of the resource. Example: oic.if.baseline,
74      * oic.if.b, etc.
75      *
76      * @return Interface type of the resource.
77      */
78     public String getInterfaceType() {
79         return interfaceType;
80     }
81
82     @Override
83     protected void finalize() throws Throwable {
84         dispose();
85     }
86
87     /**
88      * Native function to get the {@link SimulatorResourceModel} of the
89      * corresponding resource.
90      *
91      * @return {@link SimulatorResourceModel} object on success, otherwise null.
92      * 
93      * @throws InvalidArgsException
94      *             This exception will be thrown if the native resource object
95      *             is invalid. values.
96      * @throws SimulatorException
97      *             This exception will be thrown for other errors.
98      */
99     public native SimulatorResourceModel getModel()
100             throws InvalidArgsException, SimulatorException;
101
102     /**
103      * Native function to add an attribute whose value is of type int.
104      *
105      * @param key
106      *            Name of the attribute.
107      * @param value
108      *            Initial value of the attribute.
109      * 
110      * @throws InvalidArgsException
111      *             This exception will be thrown if any parameter has invalid
112      *             values.
113      * @throws SimulatorException
114      *             This exception will be thrown for other errors.
115      */
116     public native void addAttributeInteger(String key, int value)
117             throws InvalidArgsException, SimulatorException;
118
119     /**
120      * Native function to add an attribute whose value is of type double.
121      *
122      * @param key
123      *            Name of the attribute.
124      * @param value
125      *            Initial value of the attribute.
126      * 
127      * @throws InvalidArgsException
128      *             This exception will be thrown if any parameter has invalid
129      *             values.
130      * @throws SimulatorException
131      *             This exception will be thrown for other errors.
132      */
133     public native void addAttributeDouble(String key, double value)
134             throws InvalidArgsException, SimulatorException;
135
136     /**
137      * Native function to add an attribute whose value is of type boolean.
138      *
139      * @param key
140      *            Name of the attribute.
141      * @param value
142      *            Initial value of the attribute.
143      * 
144      * @throws InvalidArgsException
145      *             This exception will be thrown if any parameter has invalid
146      *             values.
147      * @throws SimulatorException
148      *             This exception will be thrown for other errors.
149      */
150     public native void addAttributeBoolean(String key, boolean value)
151             throws InvalidArgsException, SimulatorException;
152
153     /**
154      * Native function to add an attribute whose value is of type String.
155      *
156      * @param key
157      *            Name of the attribute.
158      * @param value
159      *            Initial value of the attribute.
160      * 
161      * @throws InvalidArgsException
162      *             This exception will be thrown if any parameter has invalid
163      *             values.
164      * @throws SimulatorException
165      *             This exception will be thrown for other errors.
166      */
167     public native void addAttributeString(String key, String value)
168             throws InvalidArgsException, SimulatorException;
169
170     /**
171      * Native function to update the value of an attribute whose value is of
172      * type int.
173      *
174      * @param key
175      *            Name of the attribute.
176      * @param value
177      *            New value of the attribute.
178      * 
179      * @throws InvalidArgsException
180      *             This exception will be thrown if any parameter has invalid
181      *             values.
182      * @throws SimulatorException
183      *             This exception will be thrown for other errors.
184      */
185     public native void updateAttributeInteger(String key, int value)
186             throws InvalidArgsException, SimulatorException;
187
188     /**
189      * Native function to update the value of an attribute whose value is of
190      * type double.
191      *
192      * @param key
193      *            Name of the attribute.
194      * @param value
195      *            New value of the attribute.
196      * 
197      * @throws InvalidArgsException
198      *             This exception will be thrown if any parameter has invalid
199      *             values.
200      * @throws SimulatorException
201      *             This exception will be thrown for other errors.
202      */
203     public native void updateAttributeDouble(String key, double value)
204             throws InvalidArgsException, SimulatorException;
205
206     /**
207      * Native function to update the value of an attribute whose value is of
208      * type boolean.
209      *
210      * @param key
211      *            Name of the attribute.
212      * @param value
213      *            New value of the attribute.
214      * 
215      * @throws InvalidArgsException
216      *             This exception will be thrown if any parameter has invalid
217      *             values.
218      * @throws SimulatorException
219      *             This exception will be thrown for other errors.
220      */
221     public native void updateAttributeBoolean(String key, boolean value)
222             throws InvalidArgsException, SimulatorException;
223
224     /**
225      * Native function to update the value of an attribute whose value is of
226      * type String.
227      *
228      * @param key
229      *            Name of the attribute.
230      * @param value
231      *            New value of the attribute.
232      * 
233      * @throws InvalidArgsException
234      *             This exception will be thrown if any parameter has invalid
235      *             values.
236      * @throws SimulatorException
237      *             This exception will be thrown for other errors.
238      */
239     public native void updateAttributeString(String key, String value)
240             throws InvalidArgsException, SimulatorException;
241
242     /**
243      * Native function to automatically update the value of an attribute from
244      * its allowed values.
245      *
246      * @param attrName
247      *            Name of the attribute.
248      * @param index
249      *            Index of the value in the allowed values.
250      * 
251      * @throws InvalidArgsException
252      *             This exception will be thrown if any parameter has invalid
253      *             values.
254      * @throws SimulatorException
255      *             This exception will be thrown for other errors.
256      */
257     public native void updateAttributeFromAllowedValues(String attrName,
258             int index) throws InvalidArgsException, SimulatorException;
259
260     /**
261      * Native function to set the range of allowed values. This function is
262      * intended to be used for integral type attributes.
263      *
264      * @param attrName
265      *            Name of the attribute.
266      * @param min
267      *            Minimum value in the range.
268      * @param max
269      *            Maximum value in the range.
270      * 
271      * @throws InvalidArgsException
272      *             This exception will be thrown if any parameter has invalid
273      *             values.
274      * @throws SimulatorException
275      *             This exception will be thrown for other errors.
276      */
277     public native void setRange(String attrName, int min, int max)
278             throws InvalidArgsException, SimulatorException;
279
280     /**
281      * Native function to set the allowed values of attribute whose value is of
282      * type int.
283      *
284      * @param key
285      *            Name of the attribute.
286      * @param allowedValues
287      *            Allowed values of the attribute.
288      * 
289      * @throws InvalidArgsException
290      *             This exception will be thrown if any parameter has invalid
291      *             values.
292      * @throws SimulatorException
293      *             This exception will be thrown for other errors.
294      */
295     public native void setAllowedValuesInteger(String key,
296             Vector<Integer> allowedValues) throws InvalidArgsException,
297             SimulatorException;
298
299     /**
300      * Native function to set the allowed values of attribute whose value is of
301      * type double.
302      *
303      * @param key
304      *            Name of the attribute.
305      * @param allowedValues
306      *            Allowed values of the attribute.
307      * 
308      * @throws InvalidArgsException
309      *             This exception will be thrown if any parameter has invalid
310      *             values.
311      * @throws SimulatorException
312      *             This exception will be thrown for other errors.
313      */
314     public native void setAllowedValuesDouble(String key,
315             Vector<Double> allowedValues) throws InvalidArgsException,
316             SimulatorException;
317
318     /**
319      * Native function to set the allowed values of attribute whose value is of
320      * type String.
321      *
322      * @param key
323      *            Name of the attribute.
324      * @param allowedValues
325      *            Allowed values of the attribute.
326      * 
327      * @throws InvalidArgsException
328      *             This exception will be thrown if any parameter has invalid
329      *             values.
330      * @throws SimulatorException
331      *             This exception will be thrown for other errors.
332      */
333     public native void setAllowedValuesString(String key,
334             Vector<String> allowedValues) throws InvalidArgsException,
335             SimulatorException;
336
337     /**
338      * Native function to start the resource level automation. This automation
339      * involves automatically updating all the possible values for all the
340      * attributes sequentially.
341      *
342      * @param typeOfAutomation
343      *            {@link AutomationType} indicating whether the automation is
344      *            one-time or recursive.
345      * @param listener
346      *            Listener to be notified when automation ends.
347      *
348      * @return Automation ID using which the automation can be stopped.
349      * 
350      * @throws InvalidArgsException
351      *             This exception will be thrown if any parameter has invalid
352      *             values.
353      * @throws SimulatorException
354      *             This exception will be thrown for other errors.
355      */
356     public int startResourceAutomation(AutomationType typeOfAutomation,
357             IAutomation listener) throws InvalidArgsException,
358             SimulatorException {
359         return startResourceAutomation(typeOfAutomation.getValue(), listener);
360     }
361
362     private native int startResourceAutomation(int typeOfAutomation,
363             IAutomation listener) throws InvalidArgsException,
364             SimulatorException;
365
366     /**
367      * Native function to start the attribute level automation. This automation
368      * involves automatically updating all the possible values for a given
369      * attribute sequentially.
370      *
371      * @param attrName
372      *            Name of the attribute to be automated.
373      * @param typeOfAutomation
374      *            {@link AutomationType} indicating whether the automation is
375      *            one-time or recursive.
376      * @param listener
377      *            Listener to be notified when automation ends.
378      *
379      * @return Automation ID using which the automation can be stopped.
380      * 
381      * @throws InvalidArgsException
382      *             This exception will be thrown if any parameter has invalid
383      *             values.
384      * @throws SimulatorException
385      *             This exception will be thrown for other errors.
386      */
387     public int startAttributeAutomation(String attrName,
388             AutomationType typeOfAutomation, IAutomation listener)
389             throws InvalidArgsException, SimulatorException {
390         return startAttributeAutomation(attrName, typeOfAutomation.getValue(),
391                 listener);
392     }
393
394     private native int startAttributeAutomation(String attrName,
395             int typeOfAutomation, IAutomation listener)
396             throws InvalidArgsException, SimulatorException;
397
398     /**
399      * Native function to stop the automation.
400      *
401      * @param automationId
402      *            Using which a specific automation can be stopped.
403      * 
404      * @throws InvalidArgsException
405      *             This exception will be thrown if the native resource object
406      *             does not exist.
407      * @throws SimulatorException
408      *             This exception will be thrown for other errors.
409      */
410     public native void stopAutomation(int automationId)
411             throws InvalidArgsException, SimulatorException;
412
413     /**
414      * Native function to remove an attribute from the resource model.
415      *
416      * @param key
417      *            Name of the attribute to be deleted.
418      * 
419      * @throws InvalidArgsException
420      *             This exception will be thrown either if the parameter has
421      *             invalid value or the native resource object does not exist.
422      * @throws SimulatorException
423      *             This exception will be thrown for other errors.
424      */
425     public native void removeAttribute(String key) throws InvalidArgsException,
426             SimulatorException;
427
428     /**
429      * Native function to get the details of a list of observers.
430      * 
431      * @return An array of {@link ObserverInfo} objects.
432      * 
433      * @throws InvalidArgsException
434      *             This exception will be thrown if the native resource object
435      *             does not exist.
436      * @throws SimulatorException
437      *             This exception will be thrown for other errors.
438      */
439     public native ObserverInfo[] getObserversList()
440             throws InvalidArgsException, SimulatorException;
441
442     /**
443      * Native function to set the observation callback.
444      * 
445      * @param observer
446      *            Listener to be notified when clients start/stop observing.
447      * 
448      * @throws InvalidArgsException
449      *             This exception will be thrown either if the parameter has
450      *             invalid value or the native resource object does not exist.
451      * @throws SimulatorException
452      *             This exception will be thrown for other errors.
453      */
454     public native void setObserverCallback(IObserver observer)
455             throws InvalidArgsException, SimulatorException;
456
457     /**
458      * Native function which sends notification to a specific observer.
459      * 
460      * @param id
461      *            Observer's Id.
462      * 
463      * @throws InvalidArgsException
464      *             This exception will be thrown if the native resource object
465      *             does not exist.
466      * @throws SimulatorException
467      *             This exception will be thrown for other errors.
468      */
469     public native void notifyObserver(int id) throws InvalidArgsException,
470             SimulatorException;
471
472     /**
473      * Native function which sends notification to all observers.
474      * 
475      * @throws InvalidArgsException
476      *             This exception will be thrown if the native resource object
477      *             does not exist.
478      * @throws SimulatorException
479      *             This exception will be thrown for other errors.
480      */
481     public native void notifyAllObservers() throws InvalidArgsException,
482             SimulatorException;
483
484     /**
485      * Native function to release the memory allocated to the native object for
486      * SimulatorResourceServer.
487      */
488     private native void dispose();
489 }