Base code for simulator client controller module
authorHarish Kumara Marappa <h.marappa@samsung.com>
Thu, 6 Aug 2015 12:40:16 +0000 (18:10 +0530)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 7 Aug 2015 00:58:36 +0000 (00:58 +0000)
Change-Id: I954468e4c9109858136d9e5755dec82c730ddf48
Signed-off-by: Harish Kumara Marappa <h.marappa@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1889
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
14 files changed:
service/simulator/SConscript
service/simulator/examples/client-controller/SConscript [new file with mode: 0644]
service/simulator/examples/client-controller/client_controller.cpp [new file with mode: 0644]
service/simulator/inc/simulator_manager.h
service/simulator/java/jni/simulator_manager_jni.cpp
service/simulator/java/jni/simulator_manager_jni.h
service/simulator/java/jni/simulator_remote_resource_jni.cpp [new file with mode: 0644]
service/simulator/java/jni/simulator_remote_resource_jni.h [new file with mode: 0644]
service/simulator/src/simulator_client.cpp [new file with mode: 0644]
service/simulator/src/simulator_client.h [new file with mode: 0644]
service/simulator/src/simulator_error_codes.h
service/simulator/src/simulator_manager.cpp
service/simulator/src/simulator_remote_resource.cpp [new file with mode: 0644]
service/simulator/src/simulator_remote_resource.h [new file with mode: 0644]

index 9c4e897..8363afc 100644 (file)
@@ -76,3 +76,4 @@ simulator_env.InstallTarget(simulatorsdk, 'libSimulator')
 
 #Build sample application
 SConscript('examples/server/SConscript')
+SConscript('examples/client-controller/SConscript')
diff --git a/service/simulator/examples/client-controller/SConscript b/service/simulator/examples/client-controller/SConscript
new file mode 100644 (file)
index 0000000..ade7410
--- /dev/null
@@ -0,0 +1,29 @@
+Import('env')
+lib_env = env.Clone()
+SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
+sim_env = lib_env.Clone()
+
+######################################################################
+# Build flags
+######################################################################
+sim_env.AppendUnique(CPPPATH = ['../../../../extlibs/timer'])
+sim_env.AppendUnique(CPPPATH = ['../../inc'])
+sim_env.AppendUnique(CPPPATH = ['../../src'])
+sim_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall', '-pthread'])
+sim_env.AppendUnique(CPPDEFINES = ['LINUX'])
+sim_env.AppendUnique(LIBS = ['SimulatorManager'])
+
+sim_env.AppendUnique(LIBPATH = [env.get('BUILD_DIR')])
+sim_env.AppendUnique(RPATH = [env.get('BUILD_DIR')])
+sim_env.PrependUnique(LIBS = ['SimulatorManager'])
+
+if sim_env.get('SECURED') == '1':
+    sim_env.AppendUnique(LIBS = ['tinydtls'])
+
+######################################################################
+# Source files and Targets
+######################################################################
+clientcontroller = sim_env.Program('client-controller', 'client_controller.cpp')
+
+Alias("clientcontroller", clientcontroller)
+env.AppendTarget('clientcontroller')
diff --git a/service/simulator/examples/client-controller/client_controller.cpp b/service/simulator/examples/client-controller/client_controller.cpp
new file mode 100644 (file)
index 0000000..94c788e
--- /dev/null
@@ -0,0 +1,213 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "simulator_manager.h"
+
+class AppLogger : public ILogger
+{
+    public:
+        void write(std::string time, ILogger::Level level, std::string message)
+        {
+            std::cout << "[APPLogger] " << time << " " << ILogger::getString(level) << " " << message;
+        }
+};
+std::shared_ptr<AppLogger> gAppLogger(new AppLogger());
+
+class ClientController
+{
+    public:
+        void startTest()
+        {
+            printMenu();
+            bool cont = true;
+            while (cont)
+            {
+                int choice = -1;
+                std::cout << "Enter your choice: ";
+                std::cin >> choice;
+                if (choice < 0 || choice > 3)
+                {
+                    std::cout << "Invaild choice !" << std::endl; continue;
+                }
+
+                switch (choice)
+                {
+                    case 1: findResource(); break;
+                    case 2: displayResource(); break;
+                    case 3: printMenu(); break;
+                    case 0: cont = false;
+                }
+            }
+        }
+
+    private:
+        void printMenu()
+        {
+            std::cout << "########### SIMULATOR CLIENT CONTROLLER ###########" << std::endl;
+            std::cout << "1. Find resource" << std::endl;
+            std::cout << "2. Display resource information" << std::endl;
+            std::cout << "3: Help" << std::endl;
+            std::cout << "0. Exit" << std::endl;
+            std::cout << "###################################################" << std::endl;
+        }
+
+        int selectResource(std::vector<SimulatorRemoteResourcePtr> resourceList)
+        {
+            if (0 == resourceList.size())
+            {
+                std::cout << "No resouces!" << std::endl;
+                return -1;
+            }
+
+            int index = 1;
+            for (auto & resource : resourceList)
+            {
+                std::cout << index++ << ": " << resource->getURI().c_str() << std::endl;
+            }
+
+            int choice = -1;
+            std::cout << "Choose the resource: ";
+            std::cin >> choice;
+
+            if (choice < 1 || choice > index - 1)
+            {
+                std::cout << "Invalid choice !" << std::endl;
+                choice = -1;
+            }
+
+            return choice;
+        }
+
+        void findResource()
+        {
+            std::string resourceType;
+            std::cout << "Enter resource type : ";
+            std::cin >> resourceType;
+
+            ResourceFindCallback callback = [this](std::shared_ptr<SimulatorRemoteResource> resource)
+            {
+                std::cout << "Resource found ######" << std::endl;
+                displayResource(resource);
+            };
+
+            SimulatorResult result = SimulatorManager::getInstance()->findResource
+                                          (resourceType, callback);
+            if (SIMULATOR_SUCCESS != result)
+                std::cout << "SimulatorManager::findResource returned error : " << result << std::endl;
+        }
+
+        void displayResource()
+        {
+            std::vector<SimulatorRemoteResourcePtr> resourceList =
+                SimulatorManager::getInstance()->getFoundResources();
+
+            int index = selectResource(resourceList);
+            if (-1 == index)
+                return;
+
+            displayResource(resourceList[index - 1]);
+        }
+
+        void displayResource(SimulatorRemoteResourcePtr resource)
+        {
+            std::cout << "#############################" << std::endl;
+            std::cout << "URI: " << resource->getURI().c_str() << std::endl;
+            std::cout << "Resource Types: ";
+            for (auto &type : resource->getResourceTypes())
+                std::cout << type << " ";
+            std::cout << "\nInterface Types: ";
+            for (auto &type : resource->getResourceInterfaces())
+                std::cout << type << " ";
+            std::cout << std::boolalpha << "\nisObservable : " << resource->isObservable() << std::endl;
+            std::cout << "#############################" << std::endl;
+        }
+};
+
+void printMainMenu()
+{
+    std::cout << "############### MAIN MENU###############" << std::endl;
+    std::cout << "1. Client Controller Test" << std::endl;
+    std::cout << "2. Set Logger" << std::endl;
+    std::cout << "3. Help" << std::endl;
+    std::cout << "0. Exit" << std::endl;
+    std::cout << "######################################" << std::endl;
+}
+
+void setLogger()
+{
+    std::cout << "1. Default console logger" << std::endl;
+    std::cout << "2. Default file logger" << std::endl;
+    std::cout << "3. custom logger" << std::endl;
+
+    int choice = -1;
+    std::cin >> choice;
+    if (choice <= 0 || choice > 3)
+    {
+        std::cout << "Invalid selection !" << std::endl;
+        return;
+    }
+
+    switch (choice)
+    {
+        case 1:
+            {
+                if (false == SimulatorManager::getInstance()->setDefaultConsoleLogger())
+                    std::cout << "Failed to set the default console logger" << std::endl;
+            } break;
+        case 2:
+            {
+                std::string filePath;
+                std::cout << "Enter the file path (without file name) : ";
+                std::cin >> filePath;
+                if (false == SimulatorManager::getInstance()->setDefaultFileLogger(filePath))
+                    std::cout << "Failed to set default file logger" << std::endl;
+            } break;
+        case 3: SimulatorManager::getInstance()->setLogger(gAppLogger);
+    }
+}
+
+int main(void)
+{
+    ClientController clientController;
+    printMainMenu();
+    bool cont = true;
+    while (cont == true)
+    {
+        int choice = -1;
+        std::cout << "Enter your choice: ";
+        std::cin >> choice;
+        if (choice < 0 || choice > 3)
+        {
+            std::cout << "Invaild choice !" << std::endl; continue;
+        }
+
+        switch (choice)
+        {
+            case 1: clientController.startTest();
+                std::cout << "Welcome back to main menu !" << std::endl;
+                break;
+            case 2: setLogger(); break;
+            case 3: printMainMenu(); break;
+            case 0: cont = false;
+        }
+    }
+
+    std::cout << "Terminating test !!!" << std::endl;
+}
index 424791c..b2caf77 100644 (file)
@@ -29,6 +29,7 @@
 #define SIMULATOR_MANAGER_H_
 
 #include <vector>
+#include "simulator_remote_resource.h"
 #include "simulator_error_codes.h"
 #include "simulator_resource.h"
 #include "simulator_logger.h"
@@ -98,6 +99,31 @@ class SimulatorManager
         SimulatorResult deleteResources(const std::string &resourceType = "");
 
         /**
+         * API for discovering resources of a particular resource type.
+         * Callback is called when a resource is found.
+         *
+         * @param resourceType - required resource type
+         * @param callback - Returns SimulatorRemoteResource.
+         *
+         * @return SimulatorResult - return value of this API.
+         *                         It returns SIMULATOR_SUCCESS if success.
+         *
+         * NOTE: SimulatorResult is defined in simulator_error_codes.h.
+         */
+        SimulatorResult findResource(const std::string &resourceType, ResourceFindCallback callback);
+
+        /**
+         * API for getting list of already found resources.
+         *
+         * @param resourceType - resource type
+         *
+         * @return List of SimulatorRemoteResource
+         *
+         */
+        std::vector<SimulatorRemoteResourcePtr> getFoundResources(
+            const std::string resourceType = "");
+
+        /**
          * API for setting logger target for receiving the log messages.
          *
          * @param logger - ILogger interface for handling the log messages.
index 406dcfa..a7553cb 100644 (file)
@@ -22,6 +22,7 @@
 #include "simulator_resource_jni.h"
 #include "simulator_common_jni.h"
 #include "simulator_manager.h"
+#include "simulator_remote_resource_jni.h"
 
 SimulatorClassRefs gSimulatorClassRefs;
 std::mutex gEnvMutex;
@@ -104,6 +105,86 @@ class JNILogger : public ILogger
 };
 
 
+jobject SimulatorRemoteResourceToJava(JNIEnv *env, jlong resource)
+{
+    jmethodID constructor = env->GetMethodID(gSimulatorClassRefs.classSimulatorRemoteResource, "<init>",
+                            "(J)V");
+    if (NULL == constructor)
+    {
+        return NULL;
+    }
+
+    jobject resourceObj = (jobject) env->NewObject(gSimulatorClassRefs.classSimulatorRemoteResource,
+                          constructor, resource);
+    if (NULL == resourceObj)
+    {
+        return NULL;
+    }
+
+    return resourceObj;
+}
+
+class JNIFoundResourceListener
+{
+    public:
+        void setJavaFoundResourceListener(JNIEnv *env, jobject listener)
+        {
+            m_listener = env->NewWeakGlobalRef(listener);
+        }
+
+        void onFoundResource(std::shared_ptr<SimulatorRemoteResource> resource)
+        {
+            JNIEnv *env = getEnv();
+            if (nullptr == env)
+                return;
+
+            jobject foundResourceListener = env->NewLocalRef(m_listener);
+            if (!foundResourceListener)
+            {
+                releaseEnv();
+                return;
+            }
+
+            jclass foundResourceCls = env->GetObjectClass(foundResourceListener);
+            if (!foundResourceCls)
+            {
+                releaseEnv();
+                return;
+            }
+
+            jmethodID foundResourceMId = env->GetMethodID(foundResourceCls, "onResourceCallback",
+                                         "(Lorg/oic/simulator/clientcontroller/SimulatorRemoteResource;)V");
+            if (!foundResourceMId)
+            {
+                releaseEnv();
+                return;
+            }
+
+            JniSimulatorRemoteResource *jniSimulatorResource = new JniSimulatorRemoteResource(resource);
+
+            if (!jniSimulatorResource)
+            {
+                releaseEnv();
+                return;
+            }
+
+            jobject simulatorResource = SimulatorRemoteResourceToJava(env,
+                                        reinterpret_cast<jlong>(jniSimulatorResource));
+
+            env->CallVoidMethod(foundResourceListener, foundResourceMId, simulatorResource);
+            if ((env)->ExceptionCheck())
+            {
+                releaseEnv();
+                return;
+            }
+
+            releaseEnv();
+        }
+
+    private:
+        jweak m_listener;
+
+};
 JNIEXPORT jobject JNICALL
 Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResource
 (JNIEnv *env, jclass object, jstring jConfigPath)
@@ -183,6 +264,76 @@ Java_org_iotivity_simulator_SimulatorManagerNativeInterface_deleteResources
     SimulatorManager::getInstance()->deleteResources(type);
 }
 
+JNIEXPORT jint JNICALL
+Java_org_oic_simulator_SimulatorManagerNativeInterface_findResource
+(JNIEnv *env, jobject object, jstring resourceType, jobject listener)
+{
+    const char *typeCStr = NULL;
+
+    if (resourceType)
+    {
+        typeCStr = env->GetStringUTFChars(resourceType, NULL);
+    }
+
+    JNIFoundResourceListener *resourceListener = new JNIFoundResourceListener();
+    resourceListener->setJavaFoundResourceListener(env, listener);
+
+    SimulatorResult result = SimulatorManager::getInstance()->findResource(typeCStr,
+                             std::bind(&JNIFoundResourceListener::onFoundResource, resourceListener, std::placeholders::_1));
+
+    if (typeCStr)
+        env->ReleaseStringUTFChars(resourceType, typeCStr);
+    return result;
+}
+
+JNIEXPORT jobject JNICALL
+Java_org_oic_simulator_SimulatorManagerNativeInterface_getFoundResources
+(JNIEnv *env, jobject object, jstring resourceType)
+{
+    const char *typeCStr = NULL;
+    if (resourceType)
+    {
+        typeCStr = env->GetStringUTFChars(resourceType, NULL);
+    }
+
+    std::vector<SimulatorRemoteResourcePtr> resourceList;
+    resourceList = SimulatorManager::getInstance()->getFoundResources(typeCStr);
+    if (resourceList.empty())
+    {
+        if (typeCStr)
+            env->ReleaseStringUTFChars(resourceType, typeCStr);
+        return NULL;
+    }
+
+    jobject vectorObj = env->NewObject(gSimulatorClassRefs.classVector,
+                                       gSimulatorClassRefs.classVectorCtor);
+    if (!vectorObj)
+    {
+        if (typeCStr)
+            env->ReleaseStringUTFChars(resourceType, typeCStr);
+        return NULL;
+    }
+
+    // Convert to java SimulatorRemoteResource object
+    for (unsigned int i = 0; i < resourceList.size(); i++)
+    {
+        JniSimulatorRemoteResource *jniSimulatorResource = new JniSimulatorRemoteResource(resourceList[i]);
+        if (!jniSimulatorResource)
+        {
+            if (typeCStr)
+                env->ReleaseStringUTFChars(resourceType, typeCStr);
+            return NULL;
+        }
+
+        jobject resource = SimulatorRemoteResourceToJava(env,
+                           reinterpret_cast<jlong>(jniSimulatorResource));
+        env->CallVoidMethod(vectorObj, gSimulatorClassRefs.classVectorAddElement, resource);
+    }
+
+    if (typeCStr)
+        env->ReleaseStringUTFChars(resourceType, typeCStr);
+    return vectorObj;
+}
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_simulator_SimulatorManagerNativeInterface_setLogger
index a18d062..2089536 100644 (file)
 extern "C" {
 #endif
 
-JNIEXPORT void JNICALL
-Java_org_iotivity_simulator_SimulatorManagerNativeInterface_initialize
-(JNIEnv *env, jclass object);
-
-JNIEXPORT void JNICALL
-Java_org_iotivity_simulator_SimulatorManagerNativeInterface_terminate
-(JNIEnv *env, jclass object);
-
 JNIEXPORT jobject JNICALL
 Java_org_iotivity_simulator_SimulatorManagerNativeInterface_createResource
 (JNIEnv *env, jclass object, jstring jConfigPath);
@@ -59,6 +51,14 @@ JNIEXPORT void JNICALL
 Java_org_iotivity_simulator_SimulatorManagerNativeInterface_setLogger
 (JNIEnv *env, jclass object, jobject logger);
 
+JNIEXPORT jint JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_findResource
+(JNIEnv *env, jobject interfaceObject, jstring jResourceType, jobject jListener);
+
+JNIEXPORT jobject JNICALL
+Java_org_iotivity_simulator_SimulatorManagerNativeInterface_getFoundResources
+(JNIEnv *env, jobject interfaceObject, jstring jResourceType);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.cpp b/service/simulator/java/jni/simulator_remote_resource_jni.cpp
new file mode 100644 (file)
index 0000000..03ad55c
--- /dev/null
@@ -0,0 +1,61 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "simulator_remote_resource_jni.h"
+#include "simulator_common_jni.h"
+
+JniSimulatorRemoteResource::JniSimulatorRemoteResource
+(SimulatorRemoteResourcePtr &resource) : m_sharedResource(resource)
+{}
+
+JniSimulatorRemoteResource *JniSimulatorRemoteResource::getJniSimulatorResourcePtr(JNIEnv *env,
+        jobject thiz)
+{
+    JniSimulatorRemoteResource *resource = GetHandle<JniSimulatorRemoteResource>(env, thiz);
+    if (env->ExceptionCheck())
+    {
+        std::cout << "Failed to get native handle from SimulatorRemoteResource";
+    }
+    if (!resource)
+    {
+        return nullptr;
+    }
+    return resource;
+}
+
+std::string JniSimulatorRemoteResource::getURI()
+{
+    return m_sharedResource->getURI();
+}
+
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorRemoteResource_getURI
+(JNIEnv *env, jobject thiz)
+{
+    std::cout << "SimulatorRemoteResource_getURI Entry";
+    JniSimulatorRemoteResource *resource = JniSimulatorRemoteResource::getJniSimulatorResourcePtr(env,
+                                           thiz);
+
+    if (!resource)
+        return nullptr;
+
+    return env->NewStringUTF(resource->getURI().c_str());
+}
\ No newline at end of file
diff --git a/service/simulator/java/jni/simulator_remote_resource_jni.h b/service/simulator/java/jni/simulator_remote_resource_jni.h
new file mode 100644 (file)
index 0000000..41105ca
--- /dev/null
@@ -0,0 +1,55 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#ifndef SIMULATOR_REMOTE_RESOURCE_JNI_H_
+#define SIMULATOR_REMOTE_RESOURCE_JNI_H_
+
+#include <jni.h>
+#include "simulator_remote_resource.h"
+#include "simulator_client.h"
+
+class JniSimulatorRemoteResource
+{
+    public:
+
+        JniSimulatorRemoteResource(SimulatorRemoteResourcePtr &resource);
+        ~JniSimulatorRemoteResource();
+        static JniSimulatorRemoteResource *getJniSimulatorResourcePtr(JNIEnv *env, jobject thiz);
+        std::string getURI();
+
+    private:
+        std::shared_ptr<SimulatorRemoteResource> m_sharedResource;
+};
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT jstring JNICALL
+Java_org_iotivity_simulator_SimulatorRemoteResource_getURI
+(JNIEnv *, jobject);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //SIMULATOR_REMOTE_RESOURCE_JNI_H_
+
diff --git a/service/simulator/src/simulator_client.cpp b/service/simulator/src/simulator_client.cpp
new file mode 100644 (file)
index 0000000..297d6fc
--- /dev/null
@@ -0,0 +1,123 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "simulator_client.h"
+#include "simulator_logger.h"
+
+SimulatorClient *SimulatorClient::getInstance()
+{
+    static SimulatorClient s_instance;
+    return &s_instance;
+}
+
+SimulatorResult SimulatorClient::findResource(const std::string &resourceType,
+        ResourceFindCallback callback)
+{
+    // Construct the request query for discovery
+    std::string query = OC_MULTICAST_DISCOVERY_URI;
+    query.append("?rt=");
+    query.append(resourceType);
+
+    OCStackResult result = OC::OCPlatform::findResource("", query,
+                           CT_DEFAULT,
+                           std::bind(&SimulatorClient::onResourceFound, this,
+                                     std::placeholders::_1, callback));
+
+    if (OC_STACK_OK != result)
+    {
+        SIM_LOG(ILogger::ERROR, "Failed to find the resource!");
+        return SIMULATOR_ERROR;
+    }
+
+    return SIMULATOR_SUCCESS;
+}
+
+std::vector<SimulatorRemoteResourcePtr> SimulatorClient::getFoundResources(
+    const std::string &resourceType)
+{
+    std::lock_guard<std::recursive_mutex> lock(m_resourceListMutex);
+
+    std::map<std::string, SimulatorRemoteResourcePtr> tempList;
+    for (auto resourceTableEntry : m_resourceList)
+    {
+        if (resourceType.compare(resourceTableEntry.first))
+        {
+            tempList = resourceTableEntry.second;
+            break;
+        }
+        else
+        {
+            for (auto resourceEntry : resourceTableEntry.second)
+            {
+                tempList[resourceEntry.first] = resourceEntry.second;
+            }
+        }
+    }
+
+    // Convert map to vector
+    std::vector<SimulatorRemoteResourcePtr> resourceList;
+    for (auto & resource : tempList)
+        resourceList.push_back(resource.second);
+
+    return resourceList;
+}
+
+void SimulatorClient::onResourceFound(std::shared_ptr<OC::OCResource> resource,
+                                      ResourceFindCallback callback)
+{
+    if (nullptr == resource)
+    {
+        return;
+    }
+
+    std::string serverId = resource->sid();
+
+    // Construct SimulatorRemoteResource
+    SimulatorRemoteResourcePtr simulatorResource(new SimulatorRemoteResource(resource));
+
+    // Add the resource to list
+    for (auto & resourceType : resource->getResourceTypes())
+        addResourceToList(resourceType, serverId, simulatorResource);
+
+    callback(simulatorResource);
+}
+
+void SimulatorClient::addResourceToList(const std::string &resourceType, const std::string &sid,
+                                        SimulatorRemoteResourcePtr &resource)
+{
+    std::lock_guard<std::recursive_mutex> lock(m_resourceListMutex);
+
+    if (!resourceType.empty() && !sid.empty() && nullptr != resource)
+    {
+        auto resourceTableEntry = m_resourceList.find(resourceType);
+        if (m_resourceList.end() != resourceTableEntry)
+        {
+            auto resourceEntry = resourceTableEntry->second.find(sid);
+            if (resourceTableEntry->second.end() != resourceEntry)
+            {
+                return;
+            }
+        }
+
+        m_resourceList[resourceType].insert(std::pair<std::string, SimulatorRemoteResourcePtr>(sid,
+                                            resource));
+    }
+}
+
diff --git a/service/simulator/src/simulator_client.h b/service/simulator/src/simulator_client.h
new file mode 100644 (file)
index 0000000..329382e
--- /dev/null
@@ -0,0 +1,89 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+/**
+ * @file   simulator_client.h
+ *
+ * @brief   This file provides a class for realizing simulator client functionality.
+ *
+ */
+
+#ifndef SIMULATOR_CLIENT_H_
+#define SIMULATOR_CLIENT_H_
+
+#include "simulator_remote_resource.h"
+#include "simulator_error_codes.h"
+
+/**
+ * @class   SimulatorClient
+ * @brief   This class provides a set of functions for discovering the resources currently running on the servers.
+ */
+class SimulatorClient
+{
+    public:
+
+        /**
+         * API for getting singleton instance of SimulatorClient class.
+         *
+         * @return Instance of SimulatorClient class.
+         *
+         */
+        static SimulatorClient *getInstance(void);
+
+        /**
+         * API for discovering resources of a particular resource type.
+         * Callback is called when a resource is found.
+         *
+         * @param resourceType - required resource type
+         * @param callback - Returns SimulatorRemoteResource.
+         *
+         * @return SimulatorResult - return value of this API.
+         *                         It returns SIMULATOR_SUCCESS if success.
+         *
+         * NOTE: SimulatorResult is defined in simulator_error_codes.h.
+         */
+        SimulatorResult findResource(const std::string &resourceType, ResourceFindCallback callback);
+
+        /**
+         * API for getting list of already found resources.
+         *
+         * @param resourceType - resource type
+         *
+         * @return List of SimulatorRemoteResource
+         *
+         */
+        std::vector<SimulatorRemoteResourcePtr> getFoundResources(
+            const std::string &resourceType = "");
+
+    private:
+
+        SimulatorClient() = default;
+        ~SimulatorClient() = default;
+
+        void onResourceFound(std::shared_ptr<OC::OCResource> resource, ResourceFindCallback callback);
+        void addResourceToList(const std::string &resourceType, const std::string &sid,
+                               SimulatorRemoteResourcePtr &resource);
+
+        std::recursive_mutex m_resourceListMutex;
+        std::map<std::string, std::map<std::string, SimulatorRemoteResourcePtr>> m_resourceList;
+};
+
+#endif //SIMULATOR_CLIENT_H_
+
index 9a04341..7b09e4e 100644 (file)
@@ -29,6 +29,10 @@ typedef enum
     SIMULATOR_BAD_INPUT,
     SIMULATOR_RESOURCE_NOT_FOUND,
     SIMULATOR_RESOURCE_BUSY,
+    SIMULATOR_RESOURCE_ALREADY_REGISTERED,
+    SIMULATOR_RESOURCE_NOT_REGISTERED,
+    SIMULATOR_OPERATION_NOT_ALLOWED,
+    SIMULATOR_RESOURCE_ALREADY_OBSERVING,
 
     // Attribute udpate automation related
     SIMULATOR_AUTOMATION_ALREADY_STARTED,
index 5d195fb..ecba1b7 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "simulator_manager.h"
 #include "resource_manager.h"
+#include "simulator_client.h"
 
 using namespace OC;
 
@@ -70,6 +71,18 @@ SimulatorResult SimulatorManager::deleteResources(const std::string &resourceTyp
     return ResourceManager::getInstance()->deleteResources(resourceType);
 }
 
+SimulatorResult SimulatorManager::findResource(const std::string &resourceType,
+        ResourceFindCallback callback)
+{
+    return SimulatorClient::getInstance()->findResource(resourceType, callback);
+}
+
+std::vector<SimulatorRemoteResourcePtr> SimulatorManager::getFoundResources(
+    const std::string resourceType)
+{
+    return SimulatorClient::getInstance()->getFoundResources(resourceType);
+}
+
 void SimulatorManager::setLogger(std::shared_ptr<ILogger> logger)
 {
     simLogger().setCustomTarget(logger);
diff --git a/service/simulator/src/simulator_remote_resource.cpp b/service/simulator/src/simulator_remote_resource.cpp
new file mode 100644 (file)
index 0000000..eb3cf90
--- /dev/null
@@ -0,0 +1,61 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "simulator_remote_resource.h"
+#include "OCApi.h"
+
+SimulatorRemoteResource::SimulatorRemoteResource(std::shared_ptr<OC::OCResource> resource)
+    : m_ocResource(resource) {}
+
+std::string SimulatorRemoteResource::getURI() const
+{
+    return m_ocResource->uri();
+}
+
+std::string SimulatorRemoteResource::getHost() const
+{
+    return m_ocResource->host();
+}
+
+std::string SimulatorRemoteResource::getSID() const
+{
+    return m_ocResource->sid();
+}
+
+OCConnectivityType SimulatorRemoteResource::getConnectivityType() const
+{
+    return m_ocResource->connectivityType();
+}
+
+std::vector < std::string > SimulatorRemoteResource::getResourceTypes() const
+{
+    return m_ocResource->getResourceTypes();
+}
+
+std::vector < std::string > SimulatorRemoteResource::getResourceInterfaces() const
+{
+    return m_ocResource->getResourceInterfaces();
+}
+
+bool SimulatorRemoteResource::isObservable() const
+{
+    return m_ocResource->isObservable();
+}
+
diff --git a/service/simulator/src/simulator_remote_resource.h b/service/simulator/src/simulator_remote_resource.h
new file mode 100644 (file)
index 0000000..662cca7
--- /dev/null
@@ -0,0 +1,71 @@
+/******************************************************************
+ *
+ * Copyright 2015 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+/**
+ * @file   simulator_remote_resource.h
+ *
+ * @brief   This file provides a class for handling remote resources by the client.
+ *
+ */
+
+#ifndef SIMULATOR_REMOTE_RESOURCE_H_
+#define SIMULATOR_REMOTE_RESOURCE_H_
+
+#include "simulator_resource_model.h"
+#include "simulator_error_codes.h"
+
+#include <string>
+#include <vector>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+/**
+ * @class   SimulatorRemoteResource
+ * @brief   This class provides a set of functions for the client to hande the resources currently running on the servers.
+ */
+class SimulatorRemoteResource
+{
+    public:
+        SimulatorRemoteResource(std::shared_ptr<OC::OCResource> resource);
+
+        std::string getURI() const;
+
+        std::string getHost() const;
+
+        std::string getSID() const;
+
+        OCConnectivityType getConnectivityType() const;
+
+        std::vector < std::string > getResourceTypes() const;
+
+        std::vector < std::string > getResourceInterfaces() const;
+
+        bool isObservable() const;
+
+    private:
+
+        std::shared_ptr<OC::OCResource> m_ocResource;
+};
+
+typedef std::shared_ptr<SimulatorRemoteResource> SimulatorRemoteResourcePtr;
+typedef std::function<void(SimulatorRemoteResourcePtr)> ResourceFindCallback;
+
+#endif //SIMULATOR_REMOTE_RESOURCE_H_
+