1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
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"
31 #include "simulator_manager.h"
33 class JNILogger : public ILogger
36 void setJavaLogger(JNIEnv *env, jobject logger)
38 m_logger = env->NewWeakGlobalRef(logger);
41 void write(std::string time, ILogger::Level level, std::string message)
43 JNIEnv *env = getEnv();
47 jobject logger = env->NewLocalRef(m_logger);
54 jclass loggerCls = env->GetObjectClass(logger);
61 jmethodID writeMId = env->GetMethodID(loggerCls, "write",
62 "(Ljava/lang/String;ILjava/lang/String;)V");
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);
81 void onResourceFound(jobject listener, std::shared_ptr<SimulatorRemoteResource> remoteResource)
83 JNIEnv *env = getEnv();
87 jclass listenerCls = env->GetObjectClass(listener);
88 jmethodID callbackMethod = env->GetMethodID(listenerCls, "onResourceFound",
89 "(Lorg/oic/simulator/client/SimulatorRemoteResource;)V");
91 jobject resource = createSimulatorRemoteResource(env, remoteResource);
95 env->CallVoidMethod(listener, callbackMethod, resource);
103 JNIEXPORT jobject JNICALL
104 Java_org_oic_simulator_SimulatorManager_createResource
105 (JNIEnv *env, jclass object, jstring configPath)
107 VALIDATE_INPUT_RET(env, !configPath, "Path is null!", nullptr)
111 JniString jniPath(env, configPath);
112 SimulatorResourceSP resource = SimulatorManager::getInstance()->createResource(
114 return createSimulatorResource(env, resource);
116 catch (InvalidArgsException &e)
118 throwInvalidArgsException(env, e.code(), e.what());
120 catch (SimulatorException &e)
122 throwSimulatorException(env, e.code(), e.what());
128 JNIEXPORT jobject JNICALL
129 Java_org_oic_simulator_SimulatorManager_createResources
130 (JNIEnv *env, jclass object, jstring configPath, jint count)
132 VALIDATE_INPUT_RET(env, !configPath, "Path is null!", nullptr)
133 VALIDATE_INPUT_RET(env, !count || count < 0, "Invalid count value!", nullptr)
137 JniString jniPath(env, configPath);
138 std::vector<SimulatorResourceSP> resources =
139 SimulatorManager::getInstance()->createResource(jniPath.get(), count);
140 return createSimulatorResourceVector(env, resources);
142 catch (InvalidArgsException &e)
144 throwInvalidArgsException(env, e.code(), e.what());
146 catch (SimulatorException &e)
148 throwSimulatorException(env, e.code(), e.what());
154 JNIEXPORT jobject JNICALL
155 Java_org_oic_simulator_SimulatorManager_createSingleResource
156 (JNIEnv *env, jclass object, jstring name, jstring uri, jstring resourceType)
158 VALIDATE_INPUT_RET(env, !name, "Name is null!", nullptr)
159 VALIDATE_INPUT_RET(env, !uri, "URI is null!", nullptr)
160 VALIDATE_INPUT_RET(env, !resourceType, "Resource type is null!", nullptr)
164 JniString jniName(env, name);
165 JniString jniUri(env, uri);
166 JniString jniResourceType(env, resourceType);
168 SimulatorSingleResourceSP resource = SimulatorManager::getInstance()->createSingleResource(
169 jniName.get(), jniUri.get(), jniResourceType.get());
170 return createSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
172 catch (InvalidArgsException &e)
174 throwInvalidArgsException(env, e.code(), e.what());
176 catch (SimulatorException &e)
178 throwSimulatorException(env, e.code(), e.what());
184 JNIEXPORT jobject JNICALL
185 Java_org_oic_simulator_SimulatorManager_createCollectionResource
186 (JNIEnv *env, jclass object, jstring name, jstring uri, jstring resourceType)
188 VALIDATE_INPUT_RET(env, !name, "Name is null!", nullptr)
189 VALIDATE_INPUT_RET(env, !uri, "URI is null!", nullptr)
190 VALIDATE_INPUT_RET(env, !resourceType, "Resource type is null!", nullptr)
194 JniString jniName(env, name);
195 JniString jniUri(env, uri);
196 JniString jniResourceType(env, resourceType);
198 SimulatorCollectionResourceSP resource = SimulatorManager::getInstance()->createCollectionResource(
199 jniName.get(), jniUri.get(), jniResourceType.get());
200 return createSimulatorResource(env, std::dynamic_pointer_cast<SimulatorResource>(resource));
202 catch (InvalidArgsException &e)
204 throwInvalidArgsException(env, e.code(), e.what());
206 catch (SimulatorException &e)
208 throwSimulatorException(env, e.code(), e.what());
214 JNIEXPORT void JNICALL
215 Java_org_oic_simulator_SimulatorManager_searchResource
216 (JNIEnv *env, jobject object, jstring resourceType, jobject listener)
218 VALIDATE_CALLBACK(env, listener)
220 ResourceFindCallback callback = std::bind([](
221 std::shared_ptr<SimulatorRemoteResource> resource,
222 const std::shared_ptr<JniListenerHolder> &listenerRef)
224 onResourceFound(listenerRef->get(), resource);
225 }, std::placeholders::_1, JniListenerHolder::create(env, listener));
231 SimulatorManager::getInstance()->findResource(callback);
235 JniString type(env, resourceType);
236 SimulatorManager::getInstance()->findResource(type.get(), callback);
240 catch (InvalidArgsException &e)
242 throwInvalidArgsException(env, e.code(), e.what());
244 catch (SimulatorException &e)
246 throwSimulatorException(env, e.code(), e.what());
250 JNIEXPORT void JNICALL
251 Java_org_oic_simulator_SimulatorManager_setDeviceInfo
252 (JNIEnv *env, jobject object, jstring deviceName)
254 VALIDATE_INPUT(env, !deviceName, "Device name is null!")
258 JniString jniDeviceName(env, deviceName);
259 SimulatorManager::getInstance()->setDeviceInfo(jniDeviceName.get());
261 catch (InvalidArgsException &e)
263 throwInvalidArgsException(env, e.code(), e.what());
265 catch (SimulatorException &e)
267 throwSimulatorException(env, e.code(), e.what());
271 JNIEXPORT void JNICALL
272 Java_org_oic_simulator_SimulatorManager_findDevices
273 (JNIEnv *env, jobject object, jstring hostUri, jobject listener)
275 VALIDATE_CALLBACK(env, listener)
277 DeviceInfoCallback callback = std::bind([](const std::string & host, DeviceInfo & deviceInfo,
278 const std::shared_ptr<JniListenerHolder> &listenerRef)
280 onDeviceInfoReceived(listenerRef->get(), host, deviceInfo);
281 }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, listener));
285 JniString jniHostUri(env, hostUri);
286 SimulatorManager::getInstance()->getDeviceInfo(jniHostUri.get(), callback);
288 catch (InvalidArgsException &e)
290 throwInvalidArgsException(env, e.code(), e.what());
292 catch (SimulatorException &e)
294 throwSimulatorException(env, e.code(), e.what());
298 JNIEXPORT void JNICALL
299 Java_org_oic_simulator_SimulatorManager_setPlatformInfo
300 (JNIEnv *env, jobject object, jobject platformInfo)
302 VALIDATE_INPUT(env, !platformInfo, "Platform info is null!")
306 JniPlatformInfo jniPlatformInfo(env);
307 PlatformInfo info = jniPlatformInfo.toCpp(platformInfo);
308 SimulatorManager::getInstance()->setPlatformInfo(info);
310 catch (SimulatorException &e)
312 throwSimulatorException(env, e.code(), e.what());
316 JNIEXPORT void JNICALL
317 Java_org_oic_simulator_SimulatorManager_getPlatformInformation
318 (JNIEnv *env, jobject object, jstring hostUri, jobject listener)
320 VALIDATE_CALLBACK(env, listener)
322 PlatformInfoCallback callback = std::bind([](const std::string & host, PlatformInfo & platformInfo,
323 const std::shared_ptr<JniListenerHolder> &listenerRef)
325 onPlatformInfoReceived(listenerRef->get(), host, platformInfo);
326 }, std::placeholders::_1, std::placeholders::_2, JniListenerHolder::create(env, listener));
330 JniString jniHostUri(env, hostUri);
331 SimulatorManager::getInstance()->getPlatformInfo(jniHostUri.get(), callback);
333 catch (InvalidArgsException &e)
335 throwInvalidArgsException(env, e.code(), e.what());
337 catch (SimulatorException &e)
339 throwSimulatorException(env, e.code(), e.what());
343 JNIEXPORT void JNICALL
344 Java_org_oic_simulator_SimulatorManager_setLogger
345 (JNIEnv *env, jobject object, jobject logger)
347 static std::shared_ptr<JNILogger> target(new JNILogger());
348 target->setJavaLogger(env, logger);
349 SimulatorManager::getInstance()->setLogger(target);