[IOT-1538] Add support for Protocol-Independent ID
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_manager_jni.cpp
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 #include "simulator_resource_model_jni.h"
22 #include "simulator_device_info_jni.h"
23 #include "simulator_platform_info_jni.h"
24 #include "simulator_exceptions_jni.h"
25 #include "simulator_resource_utils_jni.h"
26 #include "simulator_utils_jni.h"
27 #include "jni_sharedobject_holder.h"
28 #include "jni_listener_holder.h"
29 #include "jni_string.h"
30
31 #include "simulator_manager.h"
32
33 class JNILogger : public ILogger
34 {
35     public:
36         void setJavaLogger(JNIEnv *env, jobject logger)
37         {
38             m_logger = env->NewWeakGlobalRef(logger);
39         }
40
41         void write(std::string time, ILogger::Level level, std::string message)
42         {
43             JNIEnv *env = GetEnv();
44             if (nullptr == env)
45                 return;
46
47             jobject logger = env->NewLocalRef(m_logger);
48             if (!logger)
49             {
50                 ReleaseEnv();
51                 return;
52             }
53
54             jclass loggerCls = env->GetObjectClass(logger);
55             if (!loggerCls)
56             {
57                 ReleaseEnv();
58                 return;
59             }
60
61             jmethodID writeMId = env->GetMethodID(loggerCls, "write",
62                                                   "(Ljava/lang/String;ILjava/lang/String;)V");
63             if (!writeMId)
64             {
65                 ReleaseEnv();
66                 return;
67             }
68
69             jstring msg = env->NewStringUTF(message.c_str());
70             jstring timeStr = env->NewStringUTF(time.c_str());
71             env->CallVoidMethod(logger, writeMId, timeStr, static_cast<jint>(level), msg);
72             env->DeleteLocalRef(msg);
73             env->DeleteLocalRef(timeStr);
74             ReleaseEnv();
75         }
76
77     private:
78         jweak m_logger;
79 };
80
81 static void onResourceFound(jobject listener, SimulatorRemoteResourceSP remoteResource)
82 {
83     JNIEnv *env = GetEnv();
84     if (!env)
85         return;
86
87     jclass listenerCls = env->GetObjectClass(listener);
88     jmethodID callbackMethod = env->GetMethodID(listenerCls, "onResourceFound",
89                                "(Lorg/oic/simulator/client/SimulatorRemoteResource;)V");
90
91     jobject resource = CreateSimulatorRemoteResource(env, remoteResource);
92     if (!resource)
93         return;
94
95     env->CallVoidMethod(listener, callbackMethod, resource);
96     ReleaseEnv();
97 }
98
99 static void onDeviceInfoReceived(jobject listener, const std::string &hostUri,
100                                  DeviceInfo &deviceInfo)
101 {
102     JNIEnv *env = GetEnv();
103     if (!env)
104         return;
105
106     jclass listenerCls = env->GetObjectClass(listener);
107     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onDeviceFound",
108                                  "(Ljava/lang/String;Lorg/oic/simulator/DeviceInfo;)V");
109
110
111     jstring jHostUri = env->NewStringUTF(hostUri.c_str());
112     jobject jDeviceInfo = JniDeviceInfo(env).toJava(deviceInfo);
113     if (!jDeviceInfo)
114     {
115         ReleaseEnv();
116         return;
117     }
118
119     env->CallVoidMethod(listener, listenerMethodId, jHostUri, jDeviceInfo);
120     if (env->ExceptionCheck())
121     {
122         ReleaseEnv();
123         return;
124     }
125
126     ReleaseEnv();
127 }
128
129 static void onPlatformInfoReceived(jobject listener, const std::string &hostUri,
130                                    PlatformInfo &platformInfo)
131 {
132     JNIEnv *env = GetEnv();
133     if (!env)
134         return;
135
136     jclass listenerCls = env->GetObjectClass(listener);
137     jmethodID listenerMethodId = env->GetMethodID(listenerCls, "onPlatformFound",
138                                  "(Ljava/lang/String;Lorg/oic/simulator/PlatformInfo;)V");
139
140     jstring jHostUri = env->NewStringUTF(hostUri.c_str());
141     jobject jPlatformInfo = JniPlatformInfo(env).toJava(platformInfo);
142     if (!jPlatformInfo)
143     {
144         ReleaseEnv();
145         return;
146     }
147
148     env->CallVoidMethod(listener, listenerMethodId, jHostUri, jPlatformInfo);
149     if (env->ExceptionCheck())
150     {
151         ReleaseEnv();
152         return;
153     }
154
155     ReleaseEnv();
156 }
157
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
161
162 JNIEXPORT jobject JNICALL
163 Java_org_oic_simulator_SimulatorManager_nativeCreateResource
164 (JNIEnv *env, jobject /*object*/, jstring jConfigPath)
165 {
166     VALIDATE_INPUT_RET(env, !jConfigPath, "Path is null!", nullptr)
167
168     try
169     {
170         JniString jniPath(env, jConfigPath);
171         SimulatorResourceSP resource = SimulatorManager::getInstance()->createResource(
172                                            jniPath.get());
173         return CreateSimulatorResource(env, resource);
174     }
175     catch (InvalidArgsException &e)
176     {
177         ThrowInvalidArgsException(env, e.code(), e.what());
178     }
179     catch (SimulatorException &e)
180     {
181         ThrowSimulatorException(env, e.code(), e.what());
182     }
183
184     return nullptr;
185 }
186
187 JNIEXPORT jobject JNICALL
188 Java_org_oic_simulator_SimulatorManager_nativeCreateResources
189 (JNIEnv *env, jobject /*object*/, jstring jConfigPath, jint jCount)
190 {
191     VALIDATE_INPUT_RET(env, !jConfigPath, "Path is null!", nullptr)
192     VALIDATE_INPUT_RET(env, !jConfigPath || jCount < 0, "Invalid count value!", nullptr)
193
194     try
195     {
196         JniString jniPath(env, jConfigPath);
197         std::vector<SimulatorResourceSP> resources =
198             SimulatorManager::getInstance()->createResource(jniPath.get(), jCount);
199         return CreateSimulatorResourceVector(env, resources);
200     }
201     catch (InvalidArgsException &e)
202     {
203         ThrowInvalidArgsException(env, e.code(), e.what());
204     }
205     catch (SimulatorException &e)
206     {
207         ThrowSimulatorException(env, e.code(), e.what());
208     }
209
210     return nullptr;
211 }
212
213 JNIEXPORT jobject JNICALL
214 Java_org_oic_simulator_SimulatorManager_nativeCreateSingleResource
215 (JNIEnv *env, jobject /*object*/, jstring jName, jstring jUri, jstring jResourceType)
216 {
217     VALIDATE_INPUT_RET(env, !jName, "Name is null!", nullptr)
218     VALIDATE_INPUT_RET(env, !jUri, "URI is null!", nullptr)
219     VALIDATE_INPUT_RET(env, !jResourceType, "Resource type is null!", nullptr)
220
221     try
222     {
223         JniString jniName(env, jName);
224         JniString jniUri(env, jUri);
225         JniString jniResourceType(env, jResourceType);
226
227         SimulatorSingleResourceSP resource = SimulatorManager::getInstance()->createSingleResource(
228                 jniName.get(), jniUri.get(), jniResourceType.get());
229         return CreateSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
230     }
231     catch (InvalidArgsException &e)
232     {
233         ThrowInvalidArgsException(env, e.code(), e.what());
234     }
235     catch (SimulatorException &e)
236     {
237         ThrowSimulatorException(env, e.code(), e.what());
238     }
239
240     return nullptr;
241 }
242
243 JNIEXPORT jobject JNICALL
244 Java_org_oic_simulator_SimulatorManager_nativeCreateCollectionResource
245 (JNIEnv *env, jobject /*object*/, jstring jName, jstring jUri, jstring jResourceType)
246 {
247     VALIDATE_INPUT_RET(env, !jName, "Name is null!", nullptr)
248     VALIDATE_INPUT_RET(env, !jUri, "URI is null!", nullptr)
249     VALIDATE_INPUT_RET(env, !jResourceType, "Resource type is null!", nullptr)
250
251     try
252     {
253         JniString jniName(env, jName);
254         JniString jniUri(env, jUri);
255         JniString jniResourceType(env, jResourceType);
256
257         SimulatorCollectionResourceSP resource = SimulatorManager::getInstance()->createCollectionResource(
258                     jniName.get(), jniUri.get(), jniResourceType.get());
259         return CreateSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
260     }
261     catch (InvalidArgsException &e)
262     {
263         ThrowInvalidArgsException(env, e.code(), e.what());
264     }
265     catch (SimulatorException &e)
266     {
267         ThrowSimulatorException(env, e.code(), e.what());
268     }
269
270     return nullptr;
271 }
272
273 JNIEXPORT void JNICALL
274 Java_org_oic_simulator_SimulatorManager_nativeSearchResource
275 (JNIEnv *env, jobject /*object*/, jstring jResourceType, jobject jListener)
276 {
277     VALIDATE_CALLBACK(env, jListener)
278
279     ResourceFindCallback callback =  std::bind([](
280                                          std::shared_ptr<SimulatorRemoteResource> resource,
281                                          const std::shared_ptr<JniListenerHolder> &listenerRef)
282     {
283         onResourceFound(listenerRef->get(), resource);
284     }, std::placeholders::_1, JniListenerHolder::create(env, jListener));
285
286     try
287     {
288         if (!jResourceType)
289         {
290             SimulatorManager::getInstance()->findResource(callback);
291         }
292         else
293         {
294             JniString type(env, jResourceType);
295             SimulatorManager::getInstance()->findResource(type.get(), callback);
296         }
297
298     }
299     catch (InvalidArgsException &e)
300     {
301         ThrowInvalidArgsException(env, e.code(), e.what());
302     }
303     catch (SimulatorException &e)
304     {
305         ThrowSimulatorException(env, e.code(), e.what());
306     }
307 }
308
309 JNIEXPORT void JNICALL
310 Java_org_oic_simulator_SimulatorManager_nativeSetDeviceInfo
311 (JNIEnv *env, jobject /*object*/, jstring jDeviceName)
312 {
313     VALIDATE_INPUT(env, !jDeviceName, "Device name is null!")
314
315     try
316     {
317         JniString jniDeviceName(env, jDeviceName);
318         SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get());
319     }
320     catch (InvalidArgsException &e)
321     {
322         ThrowInvalidArgsException(env, e.code(), e.what());
323     }
324     catch (SimulatorException &e)
325     {
326         ThrowSimulatorException(env, e.code(), e.what());
327     }
328 }
329
330 JNIEXPORT void JNICALL
331 Java_org_oic_simulator_SimulatorManager_nativeSetDeviceInfo2
332 (JNIEnv *env, jobject /*object*/, jstring jDeviceName, jstring jProtocolIndependentID)
333 {
334     VALIDATE_INPUT(env, !jDeviceName, "Device name is null!")
335
336     try
337     {
338         JniString jniDeviceName(env, jDeviceName);
339         JniString jniProtocolIndependentID(env, jProtocolIndependentID);
340         SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get(), jniProtocolIndependentID.get());
341     }
342     catch (InvalidArgsException &e)
343     {
344         ThrowInvalidArgsException(env, e.code(), e.what());
345     }
346     catch (SimulatorException &e)
347     {
348         ThrowSimulatorException(env, e.code(), e.what());
349     }
350 }
351
352 JNIEXPORT void JNICALL
353 Java_org_oic_simulator_SimulatorManager_nativeFindDevices
354 (JNIEnv *env, jobject /*object*/, jstring jHostUri, jobject jListener)
355 {
356     VALIDATE_CALLBACK(env, jListener)
357
358     DeviceInfoCallback callback =  std::bind([](const std::string & host, DeviceInfo & deviceInfo,
359                                    const std::shared_ptr<JniListenerHolder> &listenerRef)
360     {
361         onDeviceInfoReceived(listenerRef->get(), host, deviceInfo);
362     }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, jListener));
363
364     try
365     {
366         JniString jniHostUri(env, jHostUri);
367         SimulatorManager::getInstance()->getDeviceInfo(jniHostUri.get(), callback);
368     }
369     catch (InvalidArgsException &e)
370     {
371         ThrowInvalidArgsException(env, e.code(), e.what());
372     }
373     catch (SimulatorException &e)
374     {
375         ThrowSimulatorException(env, e.code(), e.what());
376     }
377 }
378
379 JNIEXPORT void JNICALL
380 Java_org_oic_simulator_SimulatorManager_nativeSetPlatformInfo
381 (JNIEnv *env, jobject /*object*/, jobject jPlatformInfo)
382 {
383     VALIDATE_INPUT(env, !jPlatformInfo, "Platform info is null!")
384
385     try
386     {
387         PlatformInfo info = JniPlatformInfo(env).toCpp(jPlatformInfo);
388         SimulatorManager::getInstance()->setPlatformInfo(info);
389     }
390     catch (SimulatorException &e)
391     {
392         ThrowSimulatorException(env, e.code(), e.what());
393     }
394 }
395
396 JNIEXPORT void JNICALL
397 Java_org_oic_simulator_SimulatorManager_nativeGetPlatformInformation
398 (JNIEnv *env, jobject /*object*/, jstring jHostUri, jobject jListener)
399 {
400     VALIDATE_CALLBACK(env, jListener)
401
402     PlatformInfoCallback callback =  std::bind([](const std::string & host,
403                                      PlatformInfo & platformInfo,
404                                      const std::shared_ptr<JniListenerHolder> &listenerRef)
405     {
406         onPlatformInfoReceived(listenerRef->get(), host, platformInfo);
407     }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, jListener));
408
409     try
410     {
411         JniString jniHostUri(env, jHostUri);
412         SimulatorManager::getInstance()->getPlatformInfo(jniHostUri.get(), callback);
413     }
414     catch (InvalidArgsException &e)
415     {
416         ThrowInvalidArgsException(env, e.code(), e.what());
417     }
418     catch (SimulatorException &e)
419     {
420         ThrowSimulatorException(env, e.code(), e.what());
421     }
422 }
423
424 JNIEXPORT void JNICALL
425 Java_org_oic_simulator_SimulatorManager_nativeSetLogger
426 (JNIEnv *env, jobject /*object*/, jobject jLogger)
427 {
428     static std::shared_ptr<JNILogger> target(new JNILogger());
429     target->setJavaLogger(env, jLogger);
430     SimulatorManager::getInstance()->setLogger(target);
431 }
432
433 #ifdef __cplusplus
434 }
435 #endif