Bundle deactivation
authorMarkus Jung <markus.jung@samsung.com>
Tue, 7 Jul 2015 11:04:39 +0000 (20:04 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 8 Jul 2015 05:37:57 +0000 (05:37 +0000)
Implemented bundle deactivation which includes the removal of registered resources.
Modified ContainerTest to load, start and stop different bundles and test the
bundle activation/deactviation.

Change-Id: I5149851278aa457bee5f86c039f8f3a8320b8652
Signed-off-by: Markus Jung <markus.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1568
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/resourceContainer/bundle-java-api/src/main/java/org/iotivity/resourcecontainer/bundle/api/BaseActivator.java
service/resource-manipulation/modules/resourceContainer/examples/HueJavaSampleBundle/hue/src/main/java/org/iotivity/bundle/hue/HueBundleActivator.java
service/resource-manipulation/modules/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp
service/resource-manipulation/modules/resourceContainer/include/internal/org_iotivity_resourcecontainer_bundle_api_BaseActivator.h
service/resource-manipulation/modules/resourceContainer/src/BaseActivator.cpp
service/resource-manipulation/modules/resourceContainer/src/ContainerTest.cpp
service/resource-manipulation/modules/resourceContainer/src/ResourceContainerImpl.cpp

index b286fa5..a6890ab 100644 (file)
@@ -5,9 +5,11 @@ import java.util.Vector;
 
 public class BaseActivator implements BundleActivator {
     private String bundleId;
+    private Vector<BundleResource> bundleResources = new Vector<BundleResource>();
 
     public BaseActivator(String bundleId) {
         this.bundleId = bundleId;
+        
     }
 
     static {
@@ -23,10 +25,14 @@ public class BaseActivator implements BundleActivator {
     }
 
     public void deactivateBundle() {
-
+        System.out.println("Deactivating bundle (Base Activator).");
+        for(BundleResource bundleResource : bundleResources){
+            unregisterResource(bundleResource);
+        }
     }
 
     public void registerResource(BundleResource resource) {
+        bundleResources.add(resource);
         registerJavaResource(resource, resource.getAttributeKeys(), bundleId,
                 resource.getURI(), resource.getResourceType(),
                 resource.getName());
@@ -47,14 +53,15 @@ public class BaseActivator implements BundleActivator {
     }
 
     public void unregisterResource(BundleResource resource) {
-
+        System.out.println("Making native call.");
+        unregisterJavaResource(resource, resource.getURI());
     }
 
     public native void registerJavaResource(BundleResource resource,
             String[] attributes, String bundleId, String uri,
             String resourceType, String name);
 
-    public native void unregisterJavaResource(BundleResource resource);
+    public native void unregisterJavaResource(BundleResource resource, String uri);
 
     public native int getNumberOfConfiguredResources(String bundleId);
 
index e871175..2b6ea0b 100644 (file)
@@ -37,7 +37,7 @@ HueSampleBundleActivator::~HueSampleBundleActivator()
 }
 
 void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
-                                  std::string bundleId)
+        std::string bundleId)
 {
     std::cout << "HueSampleBundle::activateBundle called" << std::endl;
 
@@ -45,12 +45,12 @@ void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resour
     m_bundleId = bundleId;
     m_connector = new HueConnector();
 
-    vector<resourceInfo> resourceConfig;
+    vector< resourceInfo > resourceConfig;
 
     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
 
-    for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
-         itor != resourceConfig.end(); itor++)
+    for (vector< resourceInfo >::iterator itor = resourceConfig.begin();
+            itor != resourceConfig.end(); itor++)
     {
         createResource(*itor);
     }
@@ -60,19 +60,19 @@ void HueSampleBundleActivator::deactivateBundle()
 {
     std::cout << "HueSampleBundle::deactivateBundle called" << std::endl;
 
-    for (std::vector<BundleResource *>::iterator itor = m_vecResources.begin();
-         itor != m_vecResources.end(); itor++)
+    for (std::vector< BundleResource * >::iterator itor = m_vecResources.begin();
+            itor != m_vecResources.end(); itor++)
     {
         destroyResource(*itor);
     }
 }
 
-
 void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
 {
     std::cout << "HueSampleBundle::createResource called" << std::endl;
 
-    if(resourceInfo.resourceType == "oic.light.control"){
+    if (resourceInfo.resourceType == "oic.light.control")
+    {
         static int lightCount = 1;
         HueLight* hueLight = new HueLight(m_connector, resourceInfo.address);
         resourceInfo.uri = "/hue/light/" + std::to_string(lightCount++);
@@ -86,26 +86,30 @@ void HueSampleBundleActivator::createResource(resourceInfo resourceInfo)
     }
 }
 
-
 void HueSampleBundleActivator::destroyResource(BundleResource *resource)
 {
-    std::cout << "HueSampleBundle::destroyResource called" << std::endl;
+    std::cout << "HueSampleBundle::destroyResource called" << resource->m_uri << std::endl;
 
-    std::vector <BundleResource *>::iterator itor;
+    std::vector< BundleResource * >::iterator itor;
 
     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
 
+    m_pResourceContainer->unregisterResource(resource);
+
+    //TODO
+    /*std::cout << "Clearing up memory.\n";
+
     if (itor != m_vecResources.end())
-        m_vecResources.erase(itor);
+        m_vecResources.erase(itor);*/
 
     // check
     //delete resource;
 
-    m_pResourceContainer->unregisterResource(resource);
+
 }
 
 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
-                                       std::string bundleId)
+        std::string bundleId)
 {
     bundle = new HueSampleBundleActivator();
     bundle->activateBundle(resourceContainer, bundleId);
@@ -113,8 +117,5 @@ extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceConta
 
 extern "C" void externalDeactivateBundle()
 {
-    if (!bundle)
-    {
-        bundle->deactivateBundle();
-    }
+    bundle->deactivateBundle();
 }
index ca8469c..ca33ba6 100644 (file)
@@ -21,7 +21,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActiva
  * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
  */
 JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
-  (JNIEnv *, jobject, jobject);
+  (JNIEnv *, jobject, jobject, jstring);
 
 /*
  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
index 507e5bb..fa24c69 100644 (file)
@@ -25,6 +25,8 @@
 
 using namespace OIC::Service;
 
+std::map<string, JavaBundleResource*> java_resources;
+
 /*
  * Class:     org_iotivity_resourcecontainer_bundle_api_BaseActivator
  * Method:    registerJavaResource
@@ -33,6 +35,7 @@ using namespace OIC::Service;
 JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_registerJavaResource
   (JNIEnv *env, jobject obj, jobject bundleResource, jobjectArray attributes, jstring bundleId, jstring uri, jstring resourceType, jstring res_name){
     //return;
+    //static std::map<jobject, JavaBundleResource > javaBundles;
     const char *str_bundleId = env->GetStringUTFChars(bundleId, 0);
     const char *str_uri = env->GetStringUTFChars(uri, 0);
     const char *str_resourceType = env->GetStringUTFChars(resourceType, 0);
@@ -44,7 +47,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActiva
     javaBundleResource->m_resourceType = string(str_resourceType, strlen(str_resourceType));
     javaBundleResource->m_name = string(str_res_name, strlen(str_res_name));
     container->registerResource(javaBundleResource);
-
+    cout << "JavaBundles: " << &java_resources << endl;
+    cout << "Bundle resource: " << &java_resources[str_uri] << endl;
+    cout << "Uri: " << str_uri << endl;
+    java_resources[str_uri] = javaBundleResource;
 }
 
 /*
@@ -53,8 +59,17 @@ JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActiva
  * Signature: (Lorg/iotivity/resourcecontainer/bundle/api/BundleResource;)V
  */
 JNIEXPORT void JNICALL Java_org_iotivity_resourcecontainer_bundle_api_BaseActivator_unregisterJavaResource
-  (JNIEnv *, jobject, jobject){
-
+  (JNIEnv *env, jobject obj, jobject bundleResource, jstring uri){
+    const char *str_uri = env->GetStringUTFChars(uri, 0);
+    cout << "unregister java resource " << endl;
+    cout << "JavaBundles: " << &java_resources << endl;
+    cout << "Bundle resource: " << &java_resources[str_uri] << endl;
+    cout << "Uri: " << str_uri << endl;
+    if(java_resources[str_uri] != NULL){
+        ResourceContainerImpl *container = ResourceContainerImpl::getImplInstance();
+        container->unregisterResource(java_resources[str_uri]);
+        java_resources.erase(str_uri);
+    }
 }
 
 /*
index ec5618b..33ab1c9 100644 (file)
@@ -21,6 +21,7 @@
 #include "ResourceContainer.h"
 #include "BundleInfo.h"
 #include "oc_logger.hpp"
+#include <iostream>
 
 using namespace OIC::Service;
 using OC::oc_log_stream;
@@ -34,7 +35,6 @@ auto info_logger = []() -> boost::iostreams::stream<OC::oc_log_stream> &
     return os;
 };
 
-
 int main()
 {
     info_logger()->set_module("ContainerTest");
@@ -53,52 +53,26 @@ int main()
         info_logger() << "Available bundle: " << bi->getID() << endl;
     }
 
-    /*int menu;
-
-    cout << "press \'1\' to test discomfortIndexSensor" << endl;
-    cin >> menu;
-
-    if (menu == 1)
-    {
-        string testingSoftSensor = "/sampleBundle/discomfortIndex/0";
-
-        SoftSensorResource::SensorData Thing_TempHumSensor;
-        SoftSensorResource::SensorData Thing_TempHumSensor1;
-
-        map < string, string > data;
-        data["name"] = "temperature";
-        data["type"] = "int";
-        data["value"] = "25";
-
-        map < string, string > data1;
-        data1["name"] = "humidity";
-        data1["type"] = "int";
-        data1["value"] = "40";
-
-        Thing_TempHumSensor.sensorName = "Thing_TempHumSensor";
-        Thing_TempHumSensor.data.push_back(data);
-        Thing_TempHumSensor.data.push_back(data1);
-
-        Thing_TempHumSensor1.sensorName = "Thing_TempHumSensor1";
-        Thing_TempHumSensor1.data.push_back(data);
-        Thing_TempHumSensor1.data.push_back(data1);
+    cout << "Press enter to stop all bundles " << endl;
+    getchar();
 
-        container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor);
-        container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor1);
-
-        cout << endl << endl;
+    for(bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++){
+        BundleInfo* bi = *bundleIt;
+        info_logger() << "Stopping bundle: " << bi->getID() << endl;
+        container->stopBundle(bi->getID());
+    }
 
-        // TEST :: change data (change humidity data of Thing_TempHumSensor1)
-        data["value"] = "33";
-        data1["value"] = "80";
-        Thing_TempHumSensor1.data.clear();
-        Thing_TempHumSensor1.data.push_back(data);
-        Thing_TempHumSensor1.data.push_back(data1);
-        container->setInputAttribute(testingSoftSensor, Thing_TempHumSensor1);
-    }*/
+    cout << "Press enter to restart all bundles " << endl;
+    getchar();
 
-    while (1)
-    {
-        ;
+    for(bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++){
+        BundleInfo* bi = *bundleIt;
+        info_logger() << "Starting bundle: " << bi->getID() << endl;
+        container->startBundle(bi->getID());
     }
+
+    cout << "Press enter to stop container " << endl;
+    getchar();
+    container->stopContainer();
+    cout << "Container stopped. Bye" << endl;
 }
index 53615db..ae5394b 100644 (file)
@@ -157,7 +157,7 @@ namespace OIC
             }
             bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
 
-            jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
+            jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
                     "()V");
 
             if (deactivateMethod == NULL)
@@ -296,6 +296,7 @@ namespace OIC
             {
                 activateSoBundle(id);
             }
+            m_bundles[id]->setActivated(true);
 
             info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
 
@@ -311,7 +312,8 @@ namespace OIC
 
         void ResourceContainerImpl::deactivateBundle(BundleInfo *bundleInfo)
         {
-            if (((BundleInfoInternal *) bundleInfo)->isActivated())
+            BundleInfoInternal  *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
+            if (bundleInfoInternal->isActivated())
             {
                 deactivateBundle(bundleInfo->getID());
             }
@@ -371,6 +373,7 @@ namespace OIC
             {
                 deactivateSoBundle(id);
             }
+            m_bundles[id]->setActivated(false);
         }
 
         std::list< string > ResourceContainerImpl::listBundleResources(string id)
@@ -446,7 +449,10 @@ namespace OIC
 
         void ResourceContainerImpl::unregisterResource(BundleResource *resource)
         {
-            // To be implemented
+            string strUri = resource->m_uri;
+            string strResourceType = resource->m_resourceType;
+            cout << "Resource container unregisterResource called. " << strUri <<  endl;
+            m_mapServers[strUri].reset(); // reset shared pointer
         }
 
         void ResourceContainerImpl::unregisterBundle(BundleInfo *bundleInfo)
@@ -545,7 +551,12 @@ namespace OIC
 
         void ResourceContainerImpl::stopContainer()
         {
-            // deactivate all bundles and unload them
+            info_logger() << "Stopping resource container.";
+            for(std::map<std::string, BundleInfoInternal*>::iterator it = m_bundles.begin(); it != m_bundles.end(); ++it){
+                BundleInfoInternal* bundleInfo = it->second;
+                deactivateBundle(bundleInfo);
+                unregisterBundle(bundleInfo);
+            }
         }
 
         JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)