2d461bba53836e8a0873fa0420b3c53bfa570c38
[platform/upstream/iotivity.git] / service / things-manager / sdk / java / src / org / iotivity / service / tm / GroupSynchronization.java
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
23  * This file contains class which provides functions
24  * related to Group Synchronization.
25  */
26
27 package org.iotivity.service.tm;
28
29 import java.util.Map;
30 import java.util.Vector;
31
32 import org.iotivity.base.OcException;
33 import org.iotivity.base.OcResource;
34 import org.iotivity.base.OcResourceHandle;
35
36 import android.util.Log;
37
38 /**
39  * This class provides a set of APIs relating to Group Synchronization.
40  */
41 public class GroupSynchronization {
42
43     private IFindGroupListener          groupListener;
44     private final String                LOG_TAG = this.getClass()
45                                                         .getSimpleName();
46
47     private static GroupSynchronization s_groupSynchronizationInstance;
48
49     // Native methods
50     private static native int nativeFindGroup(
51             Vector<String> collectionResourceTypes);
52
53     private static native int nativeCreateGroup(String collectionResourceType);
54
55     private static native int nativeJoinGroupString(
56             String collectionResourceType, OcResourceHandle resourceHandle);
57
58     private static native int nativeJoinGroupObject(OcResource resource,
59             OcResourceHandle resourceHandle);
60
61     private static native int nativeLeaveGroup(String collectionResourceType,
62             OcResourceHandle resourceHandle);
63
64     private static native int nativeLeaveGroupForResource(OcResource resource,
65             String collectionResourceType, OcResourceHandle resourceHandle);
66
67     private static native void nativeDeleteGroup(String collectionResourceType);
68
69     private static native Map<String, OcResourceHandle> nativeGetGroupList();
70
71     static {
72         System.loadLibrary("gnustl_shared");
73         System.loadLibrary("oc_logger");
74         System.loadLibrary("connectivity_abstraction");
75         System.loadLibrary("ca-interface");
76         System.loadLibrary("octbstack");
77         System.loadLibrary("oc");
78         System.loadLibrary("TGMSDKLibrary");
79         System.loadLibrary("ocstack-jni");
80         System.loadLibrary("things-manager-jni");
81     }
82
83     /**
84      * Function for Getting instance of GroupSynchronization object.
85      *
86      * @return GroupSynchronization instance.
87      */
88     public static GroupSynchronization getInstance() {
89         if (null == s_groupSynchronizationInstance) {
90             s_groupSynchronizationInstance = new GroupSynchronization();
91         }
92         return s_groupSynchronizationInstance;
93     }
94
95     /**
96      * Provides interface for getting group discovery status.
97      */
98     public interface IFindGroupListener {
99         /**
100          * This callback method will be called to notify whether group is found
101          * or not.
102          *
103          * @param resource
104          *            URI of resource.
105          */
106         public void onGroupFindCallback(OcResource resource);
107     }
108
109     /**
110      * Set listener for receiving notification on whether the requested group is
111      * found or not.
112      *
113      * @param listener
114      *            IFindGroupListener to receive group discovery status.
115      */
116     public void setGroupListener(IFindGroupListener listener) {
117         groupListener = listener;
118     }
119
120     /**
121      * API for finding a specific remote group when a resource tries to join a
122      * group.
123      * <p>
124      * IFindGroupListener::onGroupFindCallback will be called to notify whether
125      * requested group found or not.
126      * </p>
127      *
128      * @param resourceTypes
129      *            resource types of a group to find and join
130      *
131      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
132      *         if success.
133      */
134     public OCStackResult findGroup(Vector<String> resourceTypes) {
135         OCStackResult result;
136         if (null == groupListener) {
137             result = OCStackResult.OC_STACK_ERROR;
138         } else {
139             int ordinal = nativeFindGroup(resourceTypes);
140             result = OCStackResult.conversion(ordinal);
141         }
142         return result;
143     }
144
145     /**
146      * API for creating a new group.
147      *
148      * @param resourceType
149      *            resource type of a group to create
150      *
151      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
152      *         if success.
153      */
154     public OCStackResult createGroup(String resourceType) {
155         OCStackResult result;
156         int ordinal = nativeCreateGroup(resourceType);
157         result = OCStackResult.conversion(ordinal);
158         return result;
159     }
160
161     /**
162      * API for joining a group. This API is used when a resource that has a
163      * group tries to find a specific remote resource and makes it join a group.
164      *
165      * @param resourceType
166      *            resource type of a group to join.
167      * @param resourceHandle
168      *            resource handle to join a group.
169      *
170      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
171      *         if success.
172      */
173     public OCStackResult joinGroup(String resourceType,
174             OcResourceHandle resourceHandle) {
175         OCStackResult result;
176
177         int ordinal = nativeJoinGroupString(resourceType, resourceHandle);
178         result = OCStackResult.conversion(ordinal);
179
180         return result;
181     }
182
183     /**
184      * API for joining a group. This API is used when a resource that doesn't
185      * have a group tries to find and join a specific remote group.
186      *
187      * @param resource
188      *            group resource to join.
189      * @param resourceHandle
190      *            resource handle to join a group.
191      *
192      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
193      *         if success.
194      */
195     public OCStackResult joinGroup(OcResource resource,
196             OcResourceHandle resourceHandle) throws OcException {
197
198         OCStackResult result;
199         int ordinal = nativeJoinGroupObject(resource, resourceHandle);
200         result = OCStackResult.conversion(ordinal);
201
202         return result;
203     }
204
205     /**
206      * API for leaving a joined group.
207      *
208      * @param resourceType
209      *            resource type of a group to leave.
210      * @param resourceHandle
211      *            resource handle to leave a group.
212      *
213      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
214      *         if success.
215      */
216     public OCStackResult leaveGroup(String resourceType,
217             OcResourceHandle resourceHandle) {
218
219         OCStackResult result;
220         int ordinal = nativeLeaveGroup(resourceType, resourceHandle);
221         result = OCStackResult.conversion(ordinal);
222
223         return result;
224     }
225
226     /**
227      * API for leaving a joined group.
228      *
229      * @param resource
230      *            resource of a group to leave.
231      * @param resourceType
232      *            resource type of a group to leave.
233      * @param resourceHandle
234      *            resource handle to leave a group.
235      *
236      * @return OCStackResult - return value of this API. It returns OC_STACK_OK
237      *         if success.
238      */
239     public OCStackResult leaveGroup(OcResource resource, String resourceType,
240             OcResourceHandle resourceHandle) {
241
242         OCStackResult result;
243         int ordinal = nativeLeaveGroupForResource(resource, resourceType,
244                 resourceHandle);
245         result = OCStackResult.conversion(ordinal);
246
247         return result;
248     }
249
250     /**
251      * API for deleting a group.
252      *
253      * @param collectionResourceType
254      *            resource type of a group to delete.
255      */
256     public void deleteGroup(String collectionResourceType) {
257         nativeDeleteGroup(collectionResourceType);
258     }
259
260     /**
261      * API for getting a list of joined groups.
262      *
263      * @return Returns group resource type and group resource handle as a map
264      *         table.
265      */
266     public Map<String, OcResourceHandle> getGroupList() {
267         return nativeGetGroupList();
268     }
269
270     // ******** JNI UTILITY FUNCTION **********//
271
272     /*
273      * This callback method is called from JNI to notify whether group is found
274      * or not.
275      */
276     public void onGroupFindCallback(OcResource resource) {
277         if (null != groupListener) {
278             Log.i("ThingsManagerCallback : onGroupFindCallback",
279                     "Received Callback from JNI");
280             groupListener.onGroupFindCallback(resource);
281         }
282     }
283 }