Update Resource Container
authorMinji Park <minjii.park@samsung.com>
Thu, 9 Jul 2015 11:02:06 +0000 (20:02 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 9 Jul 2015 11:20:47 +0000 (11:20 +0000)
- modify Bundle Resource and samples
- update container for checking config file loaded and listresources

Change-Id: Ib37cfcfd1f4f24ce9573d744abaed057e0b813ae
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1595
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/resourceContainer/bundle-api/include/BundleResource.h
service/resource-manipulation/modules/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp
service/resource-manipulation/modules/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp
service/resource-manipulation/modules/resourceContainer/include/internal/ResourceContainerImpl.h
service/resource-manipulation/modules/resourceContainer/src/BundleInfoInternal.cpp
service/resource-manipulation/modules/resourceContainer/src/ResourceContainerImpl.cpp

index f45682a..d2e4db4 100644 (file)
@@ -101,6 +101,7 @@ namespace OIC
                 void registerObserver(NotificationReceiver *pNotiReceiver);
 
             public:
+                string m_bundleId;
                 string m_name, m_uri, m_resourceType, m_address;
                 map< string, vector< map< string, string > > > m_mapResourceProperty;
 
index 9e37e6e..808ee6e 100644 (file)
@@ -50,7 +50,7 @@ void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resour
     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
 
     for (vector< resourceInfo >::iterator itor = resourceConfig.begin();
-            itor != resourceConfig.end(); itor++)
+         itor != resourceConfig.end(); itor++)
     {
         createResource(*itor);
     }
@@ -61,7 +61,7 @@ void HueSampleBundleActivator::deactivateBundle()
     std::cout << "HueSampleBundle::deactivateBundle called" << std::endl;
 
     for (std::vector< BundleResource * >::iterator itor = m_vecResources.begin();
-            itor != m_vecResources.end(); itor++)
+         itor != m_vecResources.end(); itor++)
     {
         destroyResource(*itor);
     }
@@ -74,9 +74,10 @@ void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
     if (resourceInfo.resourceType == "oic.light.control")
     {
         static int lightCount = 1;
-        HueLighthueLight = new HueLight(m_connector, resourceInfo.address);
+        HueLight *hueLight = new HueLight(m_connector, resourceInfo.address);
         resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++);
         std::cout << "Registering resource " << resourceInfo.uri << std::endl;
+        hueLight->m_bundleId = m_bundleId;
         hueLight->m_uri = resourceInfo.uri;
         hueLight->m_resourceType = resourceInfo.resourceType;
         hueLight->m_name = resourceInfo.name;
@@ -112,7 +113,7 @@ void HueSampleBundleActivator::destroyResource(BundleResource *resource)
 }
 
 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
-        std::string bundleId)
+                                       std::string bundleId)
 {
     bundle = new HueSampleBundleActivator();
     bundle->activateBundle(resourceContainer, bundleId);
index c59ada1..8fd8a55 100644 (file)
@@ -85,6 +85,8 @@ void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo)
     // create DISensor resource
     DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs);
 
+    newResource->m_bundleId = m_bundleId;
+
     newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string(
                              discomfortIndexSensorCount++);
     newResource->m_resourceType = resourceInfo.resourceType;
index afaf1e7..b89dca3 100644 (file)
@@ -95,6 +95,7 @@ namespace OIC
                 map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
                 map< std::string, ResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
                 map< std::string, BundleResource * > m_mapResources; //<uri, resourcePtr>
+                map< std::string, list<string> > m_mapBundleResources; //<bundleID, vector<uri>>
                 string m_configFile;
                 Configuration *m_config = NULL;
                 map<string, JavaVM *> m_bundleVM;
index eb07d02..f79bd27 100644 (file)
@@ -30,6 +30,9 @@ namespace OIC
             m_activator = nullptr;
             m_deactivator = nullptr;
             m_bundleHandle = nullptr;
+
+            m_loaded = false;
+            m_activated = false;
         }
 
         BundleInfoInternal::~BundleInfoInternal()
@@ -178,7 +181,8 @@ namespace OIC
             return m_java_activator_object;
         }
 
-        void BundleInfoInternal::setBundleInfo(BundleInfo* bundleInfo){
+        void BundleInfoInternal::setBundleInfo(BundleInfo *bundleInfo)
+        {
             m_ID = bundleInfo->getID();
             m_path = bundleInfo->getPath();
             m_version = bundleInfo->getPath();
index cb3a496..ff86b83 100644 (file)
@@ -83,26 +83,34 @@ namespace OIC
             info_logger() << "Starting resource container" << endl;
 
             m_config = new Configuration(configFile);
-            configInfo bundles;
-            m_config->getConfiguredBundles(&bundles);
 
-            for (int i = 0; i < bundles.size(); i++)
+            if (m_config->isLoaded())
             {
-                BundleInfo *bundleInfo = BundleInfo::build();
-                bundleInfo->setPath(bundles[i]["path"]);
-                bundleInfo->setVersion(bundles[i]["version"]);
-                bundleInfo->setID(bundles[i]["id"]);
-                if (!bundles[i]["activator"].empty())
+                configInfo bundles;
+                m_config->getConfiguredBundles(&bundles);
+
+                for (int i = 0; i < bundles.size(); i++)
                 {
-                    string activatorName = bundles[i]["activator"];
-                    std::replace(activatorName.begin(), activatorName.end(), '.', '/');
-                    ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
+                    BundleInfo *bundleInfo = BundleInfo::build();
+                    bundleInfo->setPath(bundles[i]["path"]);
+                    bundleInfo->setVersion(bundles[i]["version"]);
+                    bundleInfo->setID(bundles[i]["id"]);
+                    if (!bundles[i]["activator"].empty())
+                    {
+                        string activatorName = bundles[i]["activator"];
+                        std::replace(activatorName.begin(), activatorName.end(), '.', '/');
+                        ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
 
+                    }
+                    info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
+                                  << endl;
+                    registerBundle(bundleInfo);
+                    activateBundle(bundleInfo);
                 }
-                info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
-                              << endl;
-                registerBundle(bundleInfo);
-                activateBundle(bundleInfo);
+            }
+            else
+            {
+                error_logger() << "Container started with invalid configfile path" << endl;;
             }
         }
 
@@ -149,7 +157,6 @@ namespace OIC
             {
                 activateSoBundle(id);
             }
-            m_bundles[id]->setActivated(true);
 
             info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
 
@@ -166,7 +173,6 @@ namespace OIC
             {
                 deactivateSoBundle(id);
             }
-            m_bundles[id]->setActivated(false);
         }
 
         // loads the bundle
@@ -208,6 +214,10 @@ namespace OIC
             {
                 error_logger() << error << endl;
             }
+            else
+            {
+                m_bundles.erase(id);
+            }
         }
 
         void ResourceContainerImpl::registerResource(BundleResource *resource)
@@ -226,6 +236,7 @@ namespace OIC
             {
                 m_mapServers[strUri] = server;
                 m_mapResources[strUri] = resource;
+                m_mapBundleResources[resource->m_bundleId].push_back(strUri);
 
                 resource->registerObserver(this);
 
@@ -251,6 +262,7 @@ namespace OIC
             {
                 m_mapServers[strUri].reset();
                 m_mapResources.erase(m_mapResources.find(strUri));
+                m_mapBundleResources[resource->m_bundleId].remove(strUri);
             }
         }
 
@@ -380,10 +392,9 @@ namespace OIC
         {
             std::list< string > ret;
 
-            for (map <string, BundleResource *>::iterator itor = m_mapResources.begin();
-                 itor != m_mapResources.end(); itor++)
+            if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end())
             {
-                ret.push_back(itor->first);
+                ret = m_mapBundleResources[bundleId];
             }
 
             return ret;
@@ -531,6 +542,8 @@ namespace OIC
 
             env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
                                 bundleInfoInternal->getJavaBundleActivatorMethod());
+
+            m_bundles[bundleId]->setActivated(true);
         }
 
         void ResourceContainerImpl::activateSoBundle(string bundleId)
@@ -574,6 +587,8 @@ namespace OIC
 
             env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
                                 bundleInfoInternal->getJavaBundleDeactivatorMethod());
+
+            m_bundles[bundleId]->setActivated(false);
         }
 
         void ResourceContainerImpl::deactivateSoBundle(string id)