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>
public class BaseActivator implements BundleActivator {
private String bundleId;
+ private Vector<BundleResource> bundleResources = new Vector<BundleResource>();
public BaseActivator(String bundleId) {
this.bundleId = bundleId;
+
}
static {
}
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());
}
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);
public void deactivateBundle() {
System.out.println("Deactivate bundle called.");
+ super.deactivateBundle();
}
// test call
}
void HueSampleBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
- std::string bundleId)
+ std::string bundleId)
{
std::cout << "HueSampleBundle::activateBundle called" << std::endl;
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);
}
{
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++);
}
}
-
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);
extern "C" void externalDeactivateBundle()
{
- if (!bundle)
- {
- bundle->deactivateBundle();
- }
+ bundle->deactivateBundle();
}
* 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
using namespace OIC::Service;
+std::map<string, JavaBundleResource*> java_resources;
+
/*
* Class: org_iotivity_resourcecontainer_bundle_api_BaseActivator
* Method: registerJavaResource
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);
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;
}
/*
* 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);
+ }
}
/*
#include "ResourceContainer.h"
#include "BundleInfo.h"
#include "oc_logger.hpp"
+#include <iostream>
using namespace OIC::Service;
using OC::oc_log_stream;
return os;
};
-
int main()
{
info_logger()->set_module("ContainerTest");
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;
}
}
bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
- jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
+ jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
"()V");
if (deactivateMethod == NULL)
{
activateSoBundle(id);
}
+ m_bundles[id]->setActivated(true);
info_logger() << "Bundle activated: " << m_bundles[id]->getID() << endl;
void ResourceContainerImpl::deactivateBundle(BundleInfo *bundleInfo)
{
- if (((BundleInfoInternal *) bundleInfo)->isActivated())
+ BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
+ if (bundleInfoInternal->isActivated())
{
deactivateBundle(bundleInfo->getID());
}
{
deactivateSoBundle(id);
}
+ m_bundles[id]->setActivated(false);
}
std::list< string > ResourceContainerImpl::listBundleResources(string id)
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)
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)