From 37bbf3358c62db35b0d97c73fa9ed587775e144e Mon Sep 17 00:00:00 2001 From: Minji Park Date: Fri, 17 Jul 2015 18:00:59 +0900 Subject: [PATCH] update container and sample bundles for so bundle dynamic configuraion - implement features for dynamic configurataion - update so sample bundles Change-Id: I3ff054affee1796e89c4d295ecd82b518c753d6f Signed-off-by: Minji Park Reviewed-on: https://gerrit.iotivity.org/gerrit/1715 Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../bundle-api/include/BundleActivator.h | 4 + .../include/HueSampleBundleActivator.h | 34 ++-- .../src/HueSampleBundleActivator.cpp | 28 +++- .../examples/ResourceContainerConfig.xml | 2 +- .../include/SoftSensorBundleActivator.h | 4 +- .../src/SoftSensorBundleActivator.cpp | 70 +++++---- .../resourceContainer/include/BundleInfoInternal.h | 19 ++- .../include/ResourceContainerImpl.h | 2 + .../resourceContainer/src/BundleInfoInternal.cpp | 26 ++- .../src/resourceContainer/src/Configuration.cpp | 175 +++++++++++---------- .../src/resourceContainer/src/ContainerSample.cpp | 96 +++++++++-- .../src/ResourceContainerImpl.cpp | 156 ++++++++++++++++-- .../TestBundle/include/TestBundleActivator.h | 3 + .../TestBundle/src/TestBundleActivator.cpp | 20 +++ 14 files changed, 466 insertions(+), 173 deletions(-) diff --git a/service/resource-manipulation/src/resourceContainer/bundle-api/include/BundleActivator.h b/service/resource-manipulation/src/resourceContainer/bundle-api/include/BundleActivator.h index c98b0fd..38d5cdc 100644 --- a/service/resource-manipulation/src/resourceContainer/bundle-api/include/BundleActivator.h +++ b/service/resource-manipulation/src/resourceContainer/bundle-api/include/BundleActivator.h @@ -68,6 +68,10 @@ namespace OIC * @return void */ virtual void deactivateBundle(); + + virtual void createResource(resourceInfo resourceInfo) = 0; + + virtual void destroyResource(BundleResource *pBundleResource) = 0; }; } } diff --git a/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/include/HueSampleBundleActivator.h b/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/include/HueSampleBundleActivator.h index 7c3a3e3..c62f367 100644 --- a/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/include/HueSampleBundleActivator.h +++ b/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/include/HueSampleBundleActivator.h @@ -34,24 +34,24 @@ namespace OIC { class HueSampleBundleActivator: public BundleActivator { - public: - HueSampleBundleActivator(); - ~HueSampleBundleActivator(); - - void activateBundle(ResourceContainerBundleAPI *resourceContainer, - std::string bundleId); - void deactivateBundle(); - - void createResource(resourceInfo); - void destroyResource(BundleResource *); - - std::string m_bundleId; - ResourceContainerBundleAPI *m_pResourceContainer; - std::vector< BundleResource * > m_vecResources; - private: - HueConnector* m_connector; + public: + HueSampleBundleActivator(); + ~HueSampleBundleActivator(); + + void activateBundle(ResourceContainerBundleAPI *resourceContainer, + std::string bundleId); + void deactivateBundle(); + + void createResource(resourceInfo resourceInfo); + void destroyResource(BundleResource *pBundleResource); + + std::string m_bundleId; + ResourceContainerBundleAPI *m_pResourceContainer; + std::vector< BundleResource * > m_vecResources; + private: + HueConnector *m_connector; }; } } -#endif /* SAMPLEBUNDLE_H_ */ +#endif /* HUESAMPLEBUNDLEACTIVATOR_H_ */ diff --git a/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp b/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp index 808ee6e..895c50e 100644 --- a/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp +++ b/service/resource-manipulation/src/resourceContainer/examples/HueSampleBundle/src/HueSampleBundleActivator.cpp @@ -60,11 +60,12 @@ 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 (unsigned int i = 0; i < m_vecResources.size(); i++) { - destroyResource(*itor); + destroyResource(m_vecResources.at(i)); } + + delete m_connector; } void HueSampleBundleActivator::createResource(resourceInfo resourceInfo) @@ -87,17 +88,18 @@ void HueSampleBundleActivator::createResource(resourceInfo resourceInfo) } } -void HueSampleBundleActivator::destroyResource(BundleResource *resource) +void HueSampleBundleActivator::destroyResource(BundleResource *pBundleResource) { - std::cout << "HueSampleBundle::destroyResource called" << resource->m_uri << std::endl; + std::cout << "HueSampleBundle::destroyResource called" << pBundleResource->m_uri << std::endl; std::vector< BundleResource * >::iterator itor; - itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource); + itor = std::find(m_vecResources.begin(), m_vecResources.end(), pBundleResource); if (itor != m_vecResources.end()) { - m_pResourceContainer->unregisterResource(resource); + m_pResourceContainer->unregisterResource(pBundleResource); + m_vecResources.erase(itor); } //TODO @@ -109,7 +111,6 @@ void HueSampleBundleActivator::destroyResource(BundleResource *resource) // check //delete resource; - } extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer, @@ -122,4 +123,15 @@ extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceConta extern "C" void externalDeactivateBundle() { bundle->deactivateBundle(); + delete bundle; +} + +extern "C" void externalCreateResource(resourceInfo resourceInfo) +{ + bundle->createResource(resourceInfo); +} + +extern "C" void externalDestroyResource(BundleResource *pBundleResource) +{ + bundle->destroyResource(pBundleResource); } diff --git a/service/resource-manipulation/src/resourceContainer/examples/ResourceContainerConfig.xml b/service/resource-manipulation/src/resourceContainer/examples/ResourceContainerConfig.xml index 03fd4f9..08ae4d3 100644 --- a/service/resource-manipulation/src/resourceContainer/examples/ResourceContainerConfig.xml +++ b/service/resource-manipulation/src/resourceContainer/examples/ResourceContainerConfig.xml @@ -59,7 +59,7 @@ oic.bundle.hueJavaSample - ../../../../../../../../service/resource-manipulation/modules/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-SNAPSHOT-jar-with-dependencies.jar + ../../../../../../../../service/resource-manipulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar . /hueJava org.iotivity.bundle.hue.HueBundleActivator diff --git a/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h b/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h index 5448f27..347e4fe 100644 --- a/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h +++ b/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/include/SoftSensorBundleActivator.h @@ -39,8 +39,8 @@ class SoftSensorBundleActivator : public BundleActivator void activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId); void deactivateBundle(); - void createResource(resourceInfo); - void destroyResource(BundleResource *); + void createResource(resourceInfo resourceInfo); + void destroyResource(BundleResource *pBundleResource); std::string m_bundleId; ResourceContainerBundleAPI *m_pResourceContainer; diff --git a/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp b/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp index 8fd8a55..f801c7a 100644 --- a/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp +++ b/service/resource-manipulation/src/resourceContainer/examples/SoftSensorSampleBundle/src/SoftSensorBundleActivator.cpp @@ -54,49 +54,48 @@ void SoftSensorBundleActivator::deactivateBundle() { std::cout << "SoftSensorSampleBundle::deactivateBundle called" << std::endl; - for (std::vector::iterator itor = m_vecResources.begin(); - itor != m_vecResources.end(); itor++) + for (unsigned int i = 0; i < m_vecResources.size(); i++) { - destroyResource(*itor); + destroyResource(m_vecResources.at(i)); } - - m_vecResources.clear(); } void SoftSensorBundleActivator::createResource(resourceInfo resourceInfo) { std::cout << "SoftSensorSampleBundle::createResource called" << std::endl; - static int discomfortIndexSensorCount = 1; + if (resourceInfo.resourceType == "oic.softsensor") + { + static int discomfortIndexSensorCount = 1; - std::vector< std::map< std::string, std::string > >::iterator itor_vec; - std::map< std::string, std::string >::iterator itor_map; - std::vector inputs; + std::vector< std::map< std::string, std::string > >::iterator itor_vec; + std::map< std::string, std::string >::iterator itor_map; + std::vector inputs; - for (itor_vec = resourceInfo.resourceProperty["input"].begin(); - itor_vec != resourceInfo.resourceProperty["input"].end(); itor_vec++) - { - for (itor_map = (*itor_vec).begin(); itor_map != (*itor_vec).end(); itor_map++) + for (itor_vec = resourceInfo.resourceProperty["input"].begin(); + itor_vec != resourceInfo.resourceProperty["input"].end(); itor_vec++) { - inputs.push_back(itor_map->second); + for (itor_map = (*itor_vec).begin(); itor_map != (*itor_vec).end(); itor_map++) + { + inputs.push_back(itor_map->second); + } } - } - std::cout << "SoftSensorSampleBundle::creating new discomfort index sensor " << std::endl; - // create DISensor resource - DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs); + std::cout << "SoftSensorSampleBundle::creating new discomfort index sensor " << std::endl; + // create DISensor resource + DiscomfortIndexSensorResource *newResource = new DiscomfortIndexSensorResource(inputs); - newResource->m_bundleId = m_bundleId; + newResource->m_bundleId = m_bundleId; + newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string( + discomfortIndexSensorCount++); + newResource->m_resourceType = resourceInfo.resourceType; + newResource->m_mapResourceProperty = resourceInfo.resourceProperty; - newResource->m_uri = "/softsensor/discomfortIndex/" + std::to_string( - discomfortIndexSensorCount++); - newResource->m_resourceType = resourceInfo.resourceType; - newResource->m_mapResourceProperty = resourceInfo.resourceProperty; + // setting input Attributes count + newResource->inputCount = newResource->m_mapResourceProperty["input"].size(); - // setting input Attributes count - newResource->inputCount = newResource->m_mapResourceProperty["input"].size(); - - m_pResourceContainer->registerResource(newResource); - m_vecResources.push_back(newResource); + m_pResourceContainer->registerResource(newResource); + m_vecResources.push_back(newResource); + } } void SoftSensorBundleActivator::destroyResource(BundleResource *resource) @@ -110,10 +109,8 @@ void SoftSensorBundleActivator::destroyResource(BundleResource *resource) if (itor != m_vecResources.end()) { m_pResourceContainer->unregisterResource(resource); + m_vecResources.erase(itor); } - - //TODO - /*std::cout << "Clearing up memory.\n";*/ } extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer, @@ -126,4 +123,15 @@ extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceConta extern "C" void externalDeactivateBundle() { bundle->deactivateBundle(); + delete bundle; +} + +extern "C" void externalCreateResource(resourceInfo resourceInfo) +{ + bundle->createResource(resourceInfo); +} + +extern "C" void externalDestroyResource(BundleResource *pBundleResource) +{ + bundle->destroyResource(pBundleResource); } diff --git a/service/resource-manipulation/src/resourceContainer/include/BundleInfoInternal.h b/service/resource-manipulation/src/resourceContainer/include/BundleInfoInternal.h index 8467da3..078ad43 100644 --- a/service/resource-manipulation/src/resourceContainer/include/BundleInfoInternal.h +++ b/service/resource-manipulation/src/resourceContainer/include/BundleInfoInternal.h @@ -29,11 +29,14 @@ using namespace std; using namespace OIC::Service; -namespace OIC{ - namespace Service{ - +namespace OIC +{ + namespace Service + { typedef void activator_t(ResourceContainerBundleAPI *, std::string bundleId); typedef void deactivator_t(void); + typedef void resourceCreator_t(resourceInfo resourceInfo); + typedef void resourceDestroyer_t(BundleResource *pBundleResource); class BundleInfoInternal: public BundleInfo { @@ -73,19 +76,27 @@ namespace OIC{ void setBundleDeactivator(deactivator_t *); deactivator_t *getBundleDeactivator(); + void setResourceCreator(resourceCreator_t *); + resourceCreator_t *getResourceCreator(); + + void setResourceDestroyer(resourceDestroyer_t *); + resourceDestroyer_t *getResourceDestroyer(); + void setBundleHandle(void *); void *getBundleHandle(); void setJavaBundle(bool javaBundle); bool getJavaBundle(); - void setBundleInfo(BundleInfo* bundleInfo); + void setBundleInfo(BundleInfo *bundleInfo); private: bool m_loaded, m_activated, m_java_bundle; int m_id; activator_t *m_activator; deactivator_t *m_deactivator; + resourceCreator_t *m_resourceCreator; + resourceDestroyer_t *m_resourceDestroyer; void *m_bundleHandle; string m_activator_name; jmethodID m_java_activator, m_java_deactivator; diff --git a/service/resource-manipulation/src/resourceContainer/include/ResourceContainerImpl.h b/service/resource-manipulation/src/resourceContainer/include/ResourceContainerImpl.h index e736338..30fe8b2 100644 --- a/service/resource-manipulation/src/resourceContainer/include/ResourceContainerImpl.h +++ b/service/resource-manipulation/src/resourceContainer/include/ResourceContainerImpl.h @@ -107,6 +107,8 @@ namespace OIC void activateSoBundle(string bundleId); void deactivateJavaBundle(string bundleId); void deactivateSoBundle(string bundleId); + void addSoBundleResource(string bundleId, resourceInfo newResourceInfo); + void removeSoBundleResource(string bundleId, string resourceUri); }; } } diff --git a/service/resource-manipulation/src/resourceContainer/src/BundleInfoInternal.cpp b/service/resource-manipulation/src/resourceContainer/src/BundleInfoInternal.cpp index 8785ee0..2727ddf 100644 --- a/service/resource-manipulation/src/resourceContainer/src/BundleInfoInternal.cpp +++ b/service/resource-manipulation/src/resourceContainer/src/BundleInfoInternal.cpp @@ -111,6 +111,26 @@ namespace OIC return m_deactivator; } + void BundleInfoInternal::setResourceCreator(resourceCreator_t *resourceCreator) + { + m_resourceCreator = resourceCreator; + } + + resourceCreator_t *BundleInfoInternal::getResourceCreator() + { + return m_resourceCreator; + } + + void BundleInfoInternal::setResourceDestroyer(resourceDestroyer_t *resourceDestroyer) + { + m_resourceDestroyer = resourceDestroyer; + } + + resourceDestroyer_t *BundleInfoInternal::getResourceDestroyer() + { + return m_resourceDestroyer; + } + void BundleInfoInternal::setBundleHandle(void *handle) { m_bundleHandle = handle; @@ -141,11 +161,13 @@ namespace OIC return m_activator_name; } - void BundleInfoInternal::setLibraryPath(string libpath){ + void BundleInfoInternal::setLibraryPath(string libpath) + { m_library_path = libpath; } - string BundleInfoInternal::getLibraryPath(){ + string BundleInfoInternal::getLibraryPath() + { return m_library_path; } diff --git a/service/resource-manipulation/src/resourceContainer/src/Configuration.cpp b/service/resource-manipulation/src/resourceContainer/src/Configuration.cpp index c292e33..4cd8a3f 100644 --- a/service/resource-manipulation/src/resourceContainer/src/Configuration.cpp +++ b/service/resource-manipulation/src/resourceContainer/src/Configuration.cpp @@ -73,33 +73,36 @@ namespace OIC string strKey, strValue; - try + if (m_loaded) { - //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl; - - for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) + try { - std::map< std::string, std::string > bundleMap; - //cout << "Bundle: " << bundle->name() << endl; - for (subItem = bundle->first_node(); subItem; subItem = subItem->next_sibling()) - { - strKey = subItem->name(); - strValue = subItem->value(); + //cout << "Name of first node is: " << m_xmlDoc.first_node()->name() << endl; - if (strlen(subItem->value()) > 0) + for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) + { + std::map< std::string, std::string > bundleMap; + //cout << "Bundle: " << bundle->name() << endl; + for (subItem = bundle->first_node(); subItem; subItem = subItem->next_sibling()) { - bundleMap.insert(std::make_pair(trim_both(strKey), trim_both(strValue))); - //cout << strKey << " " << strValue << endl; + strKey = subItem->name(); + strValue = subItem->value(); + + if (strlen(subItem->value()) > 0) + { + bundleMap.insert(std::make_pair(trim_both(strKey), trim_both(strValue))); + //cout << strKey << " " << strValue << endl; + } } + configOutput->push_back(bundleMap); } - configOutput->push_back(bundleMap); - } - } - catch (rapidxml::parse_error &e) - { - cout << "xml parsing failed !!" << endl; - cout << e.what() << endl; + } + catch (rapidxml::parse_error &e) + { + cout << "xml parsing failed !!" << endl; + cout << e.what() << endl; + } } } @@ -109,38 +112,41 @@ namespace OIC string strBundleId, strPath, strVersion; - try + if (m_loaded) { - std::map< std::string, std::string > bundleConfigMap; - - // - for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) + try { - // - strBundleId = bundle->first_node("id")->value(); + std::map< std::string, std::string > bundleConfigMap; - if (!strBundleId.compare(bundleId)) + // + for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) { - bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId))); + // + strBundleId = bundle->first_node("id")->value(); - // - strPath = bundle->first_node("path")->value(); - bundleConfigMap.insert(std::make_pair("path", trim_both(strPath))); + if (!strBundleId.compare(bundleId)) + { + bundleConfigMap.insert(std::make_pair("id", trim_both(strBundleId))); + + // + strPath = bundle->first_node("path")->value(); + bundleConfigMap.insert(std::make_pair("path", trim_both(strPath))); - // - strVersion = bundle->first_node("version")->value(); - bundleConfigMap.insert(std::make_pair("version", trim_both(strVersion))); + // + strVersion = bundle->first_node("version")->value(); + bundleConfigMap.insert(std::make_pair("version", trim_both(strVersion))); - configOutput->push_back(bundleConfigMap); + configOutput->push_back(bundleConfigMap); - break; + break; + } } } - } - catch (rapidxml::parse_error &e) - { - cout << "xml parsing failed !!" << endl; - cout << e.what() << endl; + catch (rapidxml::parse_error &e) + { + cout << "xml parsing failed !!" << endl; + cout << e.what() << endl; + } } } @@ -154,68 +160,71 @@ namespace OIC string strBundleId; string strKey, strValue; - try + if (m_loaded) { - // - for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) + try { - // - strBundleId = bundle->first_node("id")->value(); - - if (!strBundleId.compare(bundleId)) + // + for (bundle = m_xmlDoc.first_node()->first_node("bundle"); bundle; bundle = bundle->next_sibling()) { - // - for (resource = bundle->first_node("resources")->first_node("resourceInfo"); resource; - resource = resource->next_sibling()) - { - resourceInfo tempResourceInfo; + // + strBundleId = bundle->first_node("id")->value(); - for (item = resource->first_node(); item; item = item->next_sibling()) + if (!strBundleId.compare(bundleId)) + { + // + for (resource = bundle->first_node("resources")->first_node("resourceInfo"); resource; + resource = resource->next_sibling()) { - strKey = item->name(); - strValue = item->value(); + resourceInfo tempResourceInfo; - if (!strKey.compare("name")) - tempResourceInfo.name = trim_both(strValue); + for (item = resource->first_node(); item; item = item->next_sibling()) + { + strKey = item->name(); + strValue = item->value(); - else if (!strKey.compare("uri")) - tempResourceInfo.uri = trim_both(strValue); + if (!strKey.compare("name")) + tempResourceInfo.name = trim_both(strValue); - else if (!strKey.compare("address")) - tempResourceInfo.address = trim_both(strValue); + else if (!strKey.compare("uri")) + tempResourceInfo.uri = trim_both(strValue); - else if (!strKey.compare("resourceType")) - tempResourceInfo.resourceType = trim_both(strValue); + else if (!strKey.compare("address")) + tempResourceInfo.address = trim_both(strValue); - else - { - for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling()) + else if (!strKey.compare("resourceType")) + tempResourceInfo.resourceType = trim_both(strValue); + + else { - map< string, string > propertyMap; + for (subItem = item->first_node(); subItem; subItem = subItem->next_sibling()) + { + map< string, string > propertyMap; - strKey = subItem->name(); + strKey = subItem->name(); - for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling()) - { - string newStrKey = subItem2->name(); - string newStrValue = subItem2->value(); + for (subItem2 = subItem->first_node(); subItem2; subItem2 = subItem2->next_sibling()) + { + string newStrKey = subItem2->name(); + string newStrValue = subItem2->value(); - propertyMap[trim_both(newStrKey)] = trim_both(newStrValue); - } + propertyMap[trim_both(newStrKey)] = trim_both(newStrValue); + } - tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(propertyMap); + tempResourceInfo.resourceProperty[trim_both(strKey)].push_back(propertyMap); + } } } + configOutput->push_back(tempResourceInfo); } - configOutput->push_back(tempResourceInfo); } } } - } - catch (rapidxml::parse_error &e) - { - std::cout << "xml parsing failed !!" << std::endl; - std::cout << e.what() << std::endl; + catch (rapidxml::parse_error &e) + { + std::cout << "xml parsing failed !!" << std::endl; + std::cout << e.what() << std::endl; + } } } diff --git a/service/resource-manipulation/src/resourceContainer/src/ContainerSample.cpp b/service/resource-manipulation/src/resourceContainer/src/ContainerSample.cpp index 8eeeb0f..830bf19 100644 --- a/service/resource-manipulation/src/resourceContainer/src/ContainerSample.cpp +++ b/service/resource-manipulation/src/resourceContainer/src/ContainerSample.cpp @@ -46,37 +46,111 @@ int main() ResourceContainer *container = ResourceContainer::getInstance(); container->startContainer("examples/ResourceContainerConfig.xml"); + /*so bundle add test*/ + cout << "++++++++++++++++++++++++++" << endl; + cout << "+++ Test for SO Bundle +++" << endl; + cout << "++++++++++++++++++++++++++" << endl; + cout << "Press enter to add SO bundle " << endl; + getchar(); + std::map bundleParams; + container->addBundle("oic.bundle.hueSample", "", "libHueBundle.so", bundleParams); + std::list bundles = container->listBundles(); std::list::iterator bundleIt; + cout << "\t>>> bundle list size : " << bundles.size() << endl; for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) { - BundleInfo *bi = *bundleIt; - info_logger() << "Available bundle: " << bi->getID() << endl; + cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + } + + cout << "\nPress enter to start SO bundle " << endl; + getchar(); + container->startBundle("oic.bundle.hueSample"); + + std::map resourceParams; + cout << "Press enter to add SO bundle resource " << endl; + getchar(); + resourceParams["resourceType"] = "oic.light.control"; + resourceParams["address"] = "http://192.168.0.2/api/newdeveloper/lights/1"; + container->addResourceConfig("oic.bundle.hueSample", "", resourceParams); + container->addResourceConfig("oic.bundle.hueSample", "", resourceParams); + + std::list resources = container->listBundleResources("oic.bundle.hueSample"); + std::list::iterator resourceIt; + cout << "\t>>> resource list size : " << resources.size() << endl; + for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++) + { + cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl; + } + + cout << "\nPress enter to remove SO bundle resource " << endl; + getchar(); + container->removeResourceConfig("oic.bundle.hueSample", "/hue/light/1"); + + resources = container->listBundleResources("oic.bundle.hueSample"); + cout << "\t>>> resource list size : " << resources.size() << endl; + for (resourceIt = resources.begin(); resourceIt != resources.end(); resourceIt++) + { + cout << "\t>>> resource uri : " << (*resourceIt).c_str() << endl; } - cout << "Press enter to stop all bundles " << endl; + cout << "\nPress enter to stop SO Bundle " << endl; getchar(); + container->stopBundle("oic.bundle.hueSample"); + cout << "Press enter to test remove SO Bundle " << endl; + getchar(); + container->removeBundle("oic.bundle.hueSample"); + + bundles = container->listBundles(); + cout << "\t>>> bundle list size : " << bundles.size() << endl; for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) { - BundleInfo *bi = *bundleIt; - info_logger() << "Stopping bundle: " << bi->getID() << endl; - container->stopBundle(bi->getID()); + cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; } - cout << "Press enter to restart all bundles " << endl; + /*java bundle add test*/ + cout << "\n++++++++++++++++++++++++++++" << endl; + cout << "+++ Test for JAVA Bundle +++" << endl; + cout << "++++++++++++++++++++++++++++" << endl; + cout << "Press enter to add java bundle " << endl; getchar(); + bundleParams["libraryPath"] = "."; + bundleParams["activator"] = "org.iotivity.bundle.hue.HueBundleActivator"; + container->addBundle("oic.bundle.hueJavaSample", "/hueJava", + "../../../../../../../../service/resource-manipulation/src/resourceContainer/examples/HueJavaSampleBundle/hue/target/hue-0.1-jar-with-dependencies.jar", + bundleParams); + bundles = container->listBundles(); + cout << "\t>>> bundle list size : " << bundles.size() << endl; for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) { - BundleInfo *bi = *bundleIt; - info_logger() << "Starting bundle: " << bi->getID() << endl; - container->startBundle(bi->getID()); + cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; } - cout << "Press enter to stop container " << endl; + cout << "\nPress enter to start java bundle " << endl; + getchar(); + container->startBundle("oic.bundle.hueJavaSample"); + + cout << "Press enter to stop java Bundle " << endl; + getchar(); + container->stopBundle("oic.bundle.hueJavaSample"); + + cout << "Press enter to test remove java Bundle " << endl; + getchar(); + container->removeBundle("oic.bundle.hueJavaSample"); + + bundles = container->listBundles(); + cout << "\t>>> bundle list size : " << bundles.size() << endl; + for (bundleIt = bundles.begin(); bundleIt != bundles.end(); bundleIt++) + { + cout << "\t>>> bundle Id : " << (*bundleIt)->getID().c_str() << endl; + } + + cout << "\nPress enter to stop container " << endl; getchar(); container->stopContainer(); + cout << "Container stopped. Bye" << endl; } diff --git a/service/resource-manipulation/src/resourceContainer/src/ResourceContainerImpl.cpp b/service/resource-manipulation/src/resourceContainer/src/ResourceContainerImpl.cpp index b46859d..eaa16a7 100644 --- a/service/resource-manipulation/src/resourceContainer/src/ResourceContainerImpl.cpp +++ b/service/resource-manipulation/src/resourceContainer/src/ResourceContainerImpl.cpp @@ -114,7 +114,7 @@ namespace OIC } else { - error_logger() << "Container started with invalid configfile path" << endl;; + error_logger() << "Container started with invalid configfile path" << endl; } } @@ -128,6 +128,8 @@ namespace OIC deactivateBundle(bundleInfo); unregisterBundle(bundleInfo); } + + delete m_config; } void ResourceContainerImpl::activateBundle(BundleInfo *bundleInfo) @@ -184,8 +186,6 @@ namespace OIC { info_logger() << "Registering bundle: " << bundleInfo->getPath() << endl; - m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *) bundleInfo); - if (has_suffix(bundleInfo->getPath(), ".jar")) { ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true); @@ -227,6 +227,7 @@ namespace OIC } else { + delete m_bundles[id]; m_bundles.erase(id); } } @@ -238,11 +239,13 @@ namespace OIC info_logger() << "Destroying JVM" << endl; m_bundleVM[id]->DestroyJavaVM(); + + delete m_bundles[id]; + m_bundles.erase(id); } void ResourceContainerImpl::registerResource(BundleResource *resource) { - string strUri = resource->m_uri; string strResourceType = resource->m_resourceType; ResourceObject::Ptr server = nullptr; @@ -278,6 +281,9 @@ namespace OIC string strUri = resource->m_uri; string strResourceType = resource->m_resourceType; + info_logger() << "Unregistration of resource " << resource->m_uri << "," << resource->m_resourceType + << endl; + if (m_mapServers.find(strUri) != m_mapServers.end()) { m_mapServers[strUri].reset(); @@ -290,13 +296,19 @@ namespace OIC void ResourceContainerImpl::getBundleConfiguration(std::string bundleId, configInfo *configOutput) { - m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput); + if (m_config) + { + m_config->getBundleConfiguration(bundleId, (configInfo *) configOutput); + } } void ResourceContainerImpl::getResourceConfiguration(std::string bundleId, std::vector< resourceInfo > *configOutput) { - m_config->getResourceConfiguration(bundleId, configOutput); + if (m_config) + { + m_config->getResourceConfiguration(bundleId, configOutput); + } } RCSGetResponse ResourceContainerImpl::getRequestHandler( @@ -364,23 +376,65 @@ namespace OIC void ResourceContainerImpl::startBundle(string bundleId) { - activateBundle(m_bundles[bundleId]); + if (m_bundles.find(bundleId) != m_bundles.end()) + activateBundle(m_bundles[bundleId]); } void ResourceContainerImpl::stopBundle(string bundleId) { - deactivateBundle(m_bundles[bundleId]); + if (m_bundles.find(bundleId) != m_bundles.end()) + deactivateBundle(m_bundles[bundleId]); } void ResourceContainerImpl::addBundle(string bundleId, string bundleUri, string bundlePath, std::map< string, string > params) { - // To be implemented + if (m_bundles.find(bundleId) != m_bundles.end()) + error_logger() << "BundleId already exist" << endl; + + else + { + BundleInfo *bundleInfo = BundleInfo::build(); + bundleInfo->setID(bundleId); + bundleInfo->setPath(bundlePath); + if (params.find("activator") != params.end()) + { + string activatorName = params["activator"]; + std::replace(activatorName.begin(), activatorName.end(), '.', '/'); + ((BundleInfoInternal *)bundleInfo)->setActivatorName(activatorName); + ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params["libraryPath"]); + } + if (params.find("activator") != params.end()) + { + string activatorName = params["activator"]; + std::replace(activatorName.begin(), activatorName.end(), '.', '/'); + ((BundleInfoInternal *)bundleInfo)->setActivatorName(activatorName); + ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params["libraryPath"]); + } + + info_logger() << "Add Bundle:" << bundleInfo->getID().c_str() << ";" << + bundleInfo->getPath().c_str() << + endl; + + registerBundle(bundleInfo); + } } void ResourceContainerImpl::removeBundle(string bundleId) { - // To be implemented + if (m_bundles.find(bundleId) != m_bundles.end()) + { + BundleInfoInternal *bundleInfo = m_bundles[bundleId]; + if (bundleInfo->isActivated()) + deactivateBundle(bundleInfo); + + if (bundleInfo->isLoaded()) + unregisterBundle(bundleInfo); + } + else + { + error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl; + } } std::list< BundleInfo * > ResourceContainerImpl::listBundles() @@ -401,12 +455,42 @@ namespace OIC void ResourceContainerImpl::addResourceConfig(string bundleId, string resourceUri, std::map< string, string > params) { - // To be implemented + if (m_bundles.find(bundleId) != m_bundles.end()) + { + if (!m_bundles[bundleId]->getJavaBundle()) + { + resourceInfo newResourceInfo; + newResourceInfo.uri = resourceUri; + + if (params.find("name") != params.end()) + newResourceInfo.name = params["name"]; + if (params.find("resourceType") != params.end()) + newResourceInfo.resourceType = params["resourceType"]; + if (params.find("address") != params.end()) + newResourceInfo.address = params["address"]; + + addSoBundleResource(bundleId, newResourceInfo); + } + } + else + { + error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl; + } } void ResourceContainerImpl::removeResourceConfig(string bundleId, string resourceUri) { - // To be implemented + if (m_bundles.find(bundleId) != m_bundles.end()) + { + if (!m_bundles[bundleId]->getJavaBundle()) + { + removeSoBundleResource(bundleId, resourceUri); + } + } + else + { + error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl; + } } std::list< string > ResourceContainerImpl::listBundleResources(string bundleId) @@ -440,12 +524,12 @@ namespace OIC if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r")) { fclose(file); - info_logger() << "Resource bundle " << bundleInfo->getPath() << " available." << endl; + info_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " available." << endl; //return true; } else { - error_logger() << "Resource bundle " << bundleInfo->getPath() << " not available" << endl; + error_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " not available" << endl; return; } @@ -535,6 +619,9 @@ namespace OIC bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator); bundleInfoInternal->setLoaded(true); + + m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); + info_logger() << "Bundle registered" << endl; } @@ -544,6 +631,8 @@ namespace OIC activator_t *bundleActivator = NULL; deactivator_t *bundleDeactivator = NULL; + resourceCreator_t *resourceCreator = NULL; + resourceDestroyer_t *resourceDestroyer = NULL; //sstream << bundleInfo.path << std::ends; @@ -555,6 +644,9 @@ namespace OIC bundleActivator = (activator_t *) dlsym(bundleHandle, "externalActivateBundle"); bundleDeactivator = (deactivator_t *) dlsym(bundleHandle, "externalDeactivateBundle"); + resourceCreator = (resourceCreator_t *)dlsym(bundleHandle, "externalCreateResource"); + resourceDestroyer = (resourceDestroyer_t *)dlsym(bundleHandle, "externalDestroyResource"); + if ((error = dlerror()) != NULL) { error_logger() << error << endl; @@ -563,8 +655,12 @@ namespace OIC { ((BundleInfoInternal *) bundleInfo)->setBundleActivator(bundleActivator); ((BundleInfoInternal *) bundleInfo)->setBundleDeactivator(bundleDeactivator); + ((BundleInfoInternal *) bundleInfo)->setResourceCreator(resourceCreator); + ((BundleInfoInternal *) bundleInfo)->setResourceDestroyer(resourceDestroyer); ((BundleInfoInternal *) bundleInfo)->setLoaded(true); ((BundleInfoInternal *) bundleInfo)->setBundleHandle(bundleHandle); + + m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); } } else @@ -664,6 +760,38 @@ namespace OIC } } + void ResourceContainerImpl::addSoBundleResource(string bundleId, resourceInfo newResourceInfo) + { + resourceCreator_t *resourceCreator; + + resourceCreator = m_bundles[bundleId]->getResourceCreator(); + + if (resourceCreator != NULL) + { + resourceCreator(newResourceInfo); + } + else + { + error_logger() << "addResource unsuccessful." << endl; + } + } + + void ResourceContainerImpl::removeSoBundleResource(string bundleId, string resourceUri) + { + if (m_mapResources.find(resourceUri) != m_mapResources.end()) + { + resourceDestroyer_t *resourceDestroyer = m_bundles[bundleId]->getResourceDestroyer(); + + if (resourceDestroyer != NULL) + { + resourceDestroyer(m_mapResources[resourceUri]); + } + else + { + error_logger() << "removeResource unsuccessful." << endl; + } + } + } } } diff --git a/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/include/TestBundleActivator.h b/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/include/TestBundleActivator.h index 4748d59..7b45ba5 100644 --- a/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/include/TestBundleActivator.h +++ b/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/include/TestBundleActivator.h @@ -37,6 +37,9 @@ class TestBundleActivator : public BundleActivator void activateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId); void deactivateBundle(); + + void createResource(resourceInfo resourceInfo); + void destroyResource(BundleResource *pBundleResource); }; #endif /* TESTBUNDLE_H_ */ diff --git a/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/src/TestBundleActivator.cpp b/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/src/TestBundleActivator.cpp index 1fa1a7e..853fffd 100644 --- a/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/src/TestBundleActivator.cpp +++ b/service/resource-manipulation/src/resourceContainer/unittests/TestBundle/src/TestBundleActivator.cpp @@ -41,6 +41,16 @@ void TestBundleActivator::deactivateBundle() std::cout << "TestBundleActivator::deactivateBundle .. " << std::endl; } +void TestBundleActivator::createResource(resourceInfo resourceInfo) +{ + std::cout << "TestBundleActivator::createResource .. " << std::endl; +} + +void TestBundleActivator::destroyResource(BundleResource *pBundleResource) +{ + std::cout << "TestBundleActivator::destroyResource .. " << std::endl; +} + extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer, std::string bundleId) { @@ -55,3 +65,13 @@ extern "C" void externalDeactivateBundle() bundle->deactivateBundle(); } } + +extern "C" void externalCreateResource(resourceInfo resourceInfo) +{ + bundle->createResource(resourceInfo); +} + +extern "C" void externalDestroyResource(BundleResource *pBundleResource) +{ + bundle->destroyResource(pBundleResource); +} -- 2.7.4