5120fd88c1de58c87bbbfb428d2f24663f895ea6
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / server / SimulatorResource.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.server;
18
19 import java.util.Vector;
20
21 import org.oic.simulator.InvalidArgsException;
22 import org.oic.simulator.SimulatorException;
23 import org.oic.simulator.SimulatorResourceModel;
24
25 public class SimulatorResource {
26
27     private native void dispose();
28
29     protected long mNativeHandle;
30
31     protected SimulatorResource() {
32     }
33
34     @Override
35     protected void finalize() throws Throwable {
36         try {
37             dispose();
38         } catch (Throwable t) {
39             throw t;
40         } finally {
41             super.finalize();
42         }
43     }
44
45     public enum Type {
46         SINGLE, COLLECTION
47     }
48
49     public enum AutoUpdateType {
50         ONE_TIME, REPEAT
51     }
52
53     /**
54      * API to get the name of the resource.
55      *
56      * @return Name of the resource.
57      *
58      * @throws SimulatorException
59      *             This exception will be thrown if the native resource object
60      *             does not exist or for some general errors.
61      */
62     public native String getName() throws SimulatorException;
63
64     /**
65      * API to get the type which indicates whether resource is single or
66      * collection resource.
67      *
68      * @return Type of resource.
69      *
70      * @throws SimulatorException
71      *             This exception will be thrown if the native resource object
72      *             does not exist or for some general errors.
73      */
74     public native Type getType() throws SimulatorException;
75
76     /**
77      * API to get the resource URI.
78      *
79      * @return Resource URI.
80      *
81      * @throws SimulatorException
82      *             This exception will be thrown if the native resource object
83      *             does not exist or for some general errors.
84      */
85     public native String getURI() throws SimulatorException;
86
87     /**
88      * API to get the resource type.
89      *
90      * @return Resource type.
91      *
92      * @throws SimulatorException
93      *             This exception will be thrown if the native resource object
94      *             does not exist or for some general errors.
95      */
96     public native String getResourceType() throws SimulatorException;
97
98     /**
99      * API to get the interfaces resource is bound with.
100      *
101      * @return Interface type.
102      *
103      * @throws SimulatorException
104      *             This exception will be thrown if the native resource object
105      *             does not exist or for some general errors.
106      */
107     public native Vector<String> getInterface() throws SimulatorException;
108
109     /**
110      * API to get the observable state of resource.
111      *
112      * @return Observable state - true if resource is observable, otherwise
113      *         false.
114      *
115      * @throws SimulatorException
116      *             This exception will be thrown if the native resource object
117      *             does not exist or for some general errors.
118      */
119     public native boolean isObservable() throws SimulatorException;
120
121     /**
122      * API to get the start state of resource.
123      *
124      * @return Start state - true if resource is started, otherwise false.
125      *
126      * @throws SimulatorException
127      *             This exception will be thrown if the native resource object
128      *             does not exist or for some general errors.
129      */
130     public native boolean isStarted() throws SimulatorException;
131
132     /**
133      * API to get the {@link SimulatorResourceModel} of the simulated resource.
134      *
135      * @return {@link SimulatorResourceModel} object on success, otherwise null.
136      *
137      * @throws SimulatorException
138      *             This exception will be thrown if simulated resource is not
139      *             proper.
140      */
141     public native SimulatorResourceModel getResourceModel()
142             throws SimulatorException;
143
144     /**
145      * API to set the name of the resource.
146      *
147      * @param name
148      *            - Name to be set.
149      *
150      * @throws InvalidArgsException
151      *             This exception will be thrown if the resource name is
152      *             invalid.
153      * @throws SimulatorException
154      *             This exception will be thrown if the native resource object
155      *             does not exist or for some general errors.
156      */
157     public native void setName(String name) throws InvalidArgsException,
158             SimulatorException;
159
160     /**
161      * API to set the resource URI.
162      *
163      * @param uri
164      *            - URI to be set.
165      *
166      * @throws InvalidArgsException
167      *             This exception will be thrown if the resource URI is invalid.
168      * @throws SimulatorException
169      *             This exception will be thrown if the native resource object
170      *             does not exist or for some general errors.
171      */
172     public native void setURI(String uri) throws InvalidArgsException,
173             SimulatorException;
174
175     /**
176      * API to set the resource type.
177      *
178      * @param resourceType
179      *            - resource type string.
180      *
181      * @throws InvalidArgsException
182      *             This exception will be thrown if the resource type is
183      *             invalid.
184      * @throws SimulatorException
185      *             This exception will be thrown if the native resource object
186      *             does not exist or for some general errors.
187      */
188     public native void setResourceType(String resourceType)
189             throws InvalidArgsException, SimulatorException;
190
191     /**
192      * API to add interface type for resource.
193      *
194      * @param interfaceType
195      *            - interface to be added for resource.
196      *
197      * @throws InvalidArgsException
198      *             This exception will be thrown if the interface type is
199      *             invalid.
200      * @throws SimulatorException
201      *             This exception will be thrown if the native resource object
202      *             does not exist or for some general errors.
203      */
204     public native void addInterface(String interfaceType)
205             throws InvalidArgsException, SimulatorException;
206
207     /**
208      * API to make the resource observable or not.
209      *
210      * @param state
211      *            - true make the resource observable, otherwise non-observable.
212      *
213      * @throws SimulatorException
214      *             This exception will be thrown if the native resource object
215      *             does not exist or for some general errors.
216      */
217     public native void setObservable(boolean state) throws SimulatorException;
218
219     /**
220      * API to set the listener for receiving the notifications when observer is
221      * registered or unregistered with resource.
222      *
223      * @param listener
224      *            - Callback to be set for receiving the notifications.
225      *
226      * @throws InvalidArgsException
227      *             This exception will be thrown if the listener is invalid.
228      * @throws SimulatorException
229      *             This exception will be thrown if the native resource object
230      *             does not exist or for some general errors.
231      */
232     public native void setObserverListener(ObserverListener listener)
233             throws InvalidArgsException, SimulatorException;
234
235     /**
236      * API to set listener for receiving notifications when resource's model
237      * gets changed.
238      *
239      * @param listener
240      *            {@link ResourceModelChangeListener}.
241      *
242      * @throws InvalidArgsException
243      *             This exception will be thrown on invalid input.
244      * @throws SimulatorException
245      *             This exception will be thrown for other errors.
246      */
247     public native void setResourceModelChangeListener(
248             ResourceModelChangeListener listener) throws InvalidArgsException,
249             SimulatorException;
250
251     /**
252      * API to start the resource.
253      *
254      * @throws SimulatorException
255      *             This exception will be thrown if the native resource object
256      *             does not exist or for some general errors.
257      */
258     public native void start() throws SimulatorException;
259
260     /**
261      * API to stop the resource.
262      *
263      * @throws SimulatorException
264      *             This exception will be thrown if the native resource object
265      *             does not exist or for some general errors.
266      */
267     public native void stop() throws SimulatorException;
268
269     /**
270      * API to get observers which are registered with resource.
271      *
272      * @return observers as an array of {@link Observer}.
273      *
274      * @throws SimulatorException
275      *             This exception will be thrown if the native resource object
276      *             does not exist or for some general errors.
277      */
278     public native Vector<Observer> getObservers() throws SimulatorException;
279
280     /**
281      * API to notify current resource model to specific observer.
282      *
283      * @param observerId
284      *            - Observer ID to notify.
285      *
286      * @throws SimulatorException
287      *             This exception will be thrown if the native resource object
288      *             does not exist or for some general errors.
289      */
290     public native void notifyObserver(int observerId) throws SimulatorException;
291
292     /**
293      * API to notify all registered observers.
294      *
295      * @throws SimulatorException
296      *             This exception will be thrown if the native resource object
297      *             does not exist or for some general errors.
298      */
299     public native void notifyAllObservers() throws SimulatorException;
300
301     /**
302      * Listener for receiving notification when observer is registered or
303      * unregistered with the resource.
304      */
305     public interface ObserverListener {
306         /**
307          * Method will be invoked when a observer is registered with resource.
308          *
309          * @param uri
310          *            URI of the resource.
311          * @param observer
312          *            {@link ObserverInfo} object containing the details of
313          *            observer.
314          */
315         public void onObserverAdded(String resourceURI, Observer observer);
316
317         /**
318          * Method will be invoked when a observer is Unregistered with resource.
319          *
320          * @param resourceURI
321          *            URI of the resource.
322          * @param observer
323          *            {@link ObserverInfo} object containing the details of
324          *            observer.
325          */
326         public void onObserverRemoved(String resourceURI, Observer observer);
327     }
328
329     /**
330      * Listener for receiving notification on completion of automatically
331      * updating attribute value from its range or value set property.
332      */
333     public interface AutoUpdateListener {
334         /**
335          * Method for receiving automation complete notifications.
336          *
337          * @param uri
338          *            URI of resource.
339          * @param id
340          *            Identifier for auto resource/attribute update session.
341          */
342         public void onUpdateComplete(String uri, int id);
343     }
344
345     /**
346      * Listener for receiving notifications whenever there is a change in the
347      * resource model.
348      */
349     public interface ResourceModelChangeListener {
350         /**
351          * Method will be invoked to notify about the changes in the resource
352          * model.
353          *
354          * @param uri
355          *            URI of resource.
356          * @param resourceModel
357          *            {@link SimulatorResourceModel} of the resource.
358          */
359         public void onResourceModelChanged(String uri,
360                 SimulatorResourceModel resourceModel);
361     }
362 }