X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fresource-container%2Fsrc%2FResourceContainerImpl.cpp;h=5042f0c827687c4da47993cee80ad3ed4977211b;hb=442026128ead8780fa45d0db8a6f17be7c9220e0;hp=9607622e609ec290ad9212bc810736ac4103190f;hpb=067ef0c2a218b8b9168ae342bee969cf9693fcd5;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/resource-container/src/ResourceContainerImpl.cpp b/service/resource-container/src/ResourceContainerImpl.cpp index 9607622..5042f0c 100644 --- a/service/resource-container/src/ResourceContainerImpl.cpp +++ b/service/resource-container/src/ResourceContainerImpl.cpp @@ -59,79 +59,73 @@ namespace OIC void ResourceContainerImpl::startContainer(const std::string &configFile) { - OC_LOG(INFO, CONTAINER_TAG, "Starting resource container."); + OIC_LOG(INFO, CONTAINER_TAG, "Starting resource container."); #if (JAVA_SUPPORT) - OC_LOG(INFO, CONTAINER_TAG, "Resource container has Java support."); + OIC_LOG(INFO, CONTAINER_TAG, "Resource container has Java support."); #else - OC_LOG(INFO, CONTAINER_TAG, "Resource container without Java support."); + OIC_LOG(INFO, CONTAINER_TAG, "Resource container without Java support."); #endif activationLock.lock(); - if (!configFile.empty()) - { - m_config = new Configuration(configFile); - - if (m_config->isLoaded()) + try{ + if (!configFile.empty()) { - configInfo bundles; - m_config->getConfiguredBundles(&bundles); + m_config = new Configuration(configFile); - for (unsigned int i = 0; i < bundles.size(); i++) + if (m_config->isLoaded()) { - BundleInfoInternal *bundleInfo = new BundleInfoInternal(); - bundleInfo->setPath(bundles[i][BUNDLE_PATH]); - bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]); - bundleInfo->setID(bundles[i][BUNDLE_ID]); - if (!bundles[i][BUNDLE_ACTIVATOR].empty()) + configInfo bundles; + m_config->getConfiguredBundles(&bundles); + + for (unsigned int i = 0; i < bundles.size(); i++) { - string activatorName = bundles[i][BUNDLE_ACTIVATOR]; - std::replace(activatorName.begin(), activatorName.end(), '.', '/'); - ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName); - ((BundleInfoInternal *) bundleInfo)->setLibraryPath( - bundles[i][BUNDLE_LIBRARY_PATH]); - } + shared_ptr bundleInfo(new BundleInfoInternal); + bundleInfo->setPath(bundles[i][BUNDLE_PATH]); + bundleInfo->setVersion(bundles[i][BUNDLE_VERSION]); + bundleInfo->setID(bundles[i][BUNDLE_ID]); + if (!bundles[i][BUNDLE_ACTIVATOR].empty()) + { + string activatorName = bundles[i][BUNDLE_ACTIVATOR]; + std::replace(activatorName.begin(), activatorName.end(), '.', '/'); + bundleInfo->setActivatorName(activatorName); + bundleInfo->setLibraryPath(bundles[i][BUNDLE_LIBRARY_PATH]); + } - OC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", - std::string(bundles[i][BUNDLE_ID] + ";" + - bundles[i][BUNDLE_PATH]).c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Init Bundle:(%s)", + std::string(bundles[i][BUNDLE_ID] + ";" + + bundles[i][BUNDLE_PATH]).c_str()); - registerBundle(bundleInfo); - activateBundle(bundleInfo); + registerBundle(bundleInfo); + activateBundle(bundleInfo); + } + } + else + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path."); } } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Container started with invalid configfile path."); + OIC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided."); } - } - else - { - OC_LOG_V(INFO, CONTAINER_TAG, "No configuration file for the container provided."); - } - - map::iterator activatorIterator; - for (activatorIterator = m_activators.begin(); activatorIterator != m_activators.end(); - activatorIterator++) - { - activatorIterator->second.timed_join( - boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC)); - // wait for bundles to be activated + OIC_LOG(INFO, CONTAINER_TAG, "Resource container started."); + }catch(...){ + OIC_LOG(INFO, CONTAINER_TAG, "Resource container failed starting."); } activationLock.unlock(); } void ResourceContainerImpl::stopContainer() { - OC_LOG(INFO, CONTAINER_TAG, "Stopping resource container."); + OIC_LOG(INFO, CONTAINER_TAG, "Stopping resource container."); - for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin(); + for (std::map< std::string, shared_ptr >::iterator it = m_bundles.begin(); it != m_bundles.end(); ++it) { - BundleInfoInternal *bundleInfo = it->second; - deactivateBundle(bundleInfo); - unregisterBundle(bundleInfo); + deactivateBundle(it->second); + unregisterBundle(it->second); } if (!m_mapServers.empty()) @@ -151,10 +145,12 @@ namespace OIC delete m_config; } - void ResourceContainerImpl::activateBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::activateBundle(shared_ptr bundleInfo) { activationLock.lock(); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (bundleInfoInternal->isLoaded()) { @@ -163,25 +159,31 @@ namespace OIC activationLock.unlock(); } - void ResourceContainerImpl::deactivateBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::deactivateBundle(shared_ptr bundleInfo) { - if (((BundleInfoInternal *) bundleInfo)->isActivated()) + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + if (bundleInfoInternal->isActivated()) { - deactivateBundle(bundleInfo->getID()); + deactivateBundle(bundleInfoInternal->getID()); } } void ResourceContainerImpl::activateBundle(const std::string &id) { - OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)", std::string(m_bundles[id]->getID()).c_str()); + activationLock.lock(); - auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this, - id); - boost::thread activator(f); - activator.timed_join(boost::posix_time::seconds(BUNDLE_SET_GET_WAIT_SEC)); + try{ + activateBundleThread(id); + } + catch(...){ + OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s) failed", + std::string(m_bundles[id]->getID()).c_str()); + } activationLock.unlock(); - OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)", std::string(m_bundles[id]->getID()).c_str()); } @@ -200,35 +202,51 @@ namespace OIC } // loads the bundle - void ResourceContainerImpl::registerBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerBundle(shared_ptr bundleInfo) { - OC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Registering bundle: (%s)", std::string(bundleInfo->getPath()).c_str()); + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (has_suffix(bundleInfo->getPath(), ".jar")) { #if(JAVA_SUPPORT) - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(true); + bundleInfoInternal->setJavaBundle(true); + bundleInfoInternal->setSoBundle(false); registerJavaBundle(bundleInfo); +#else + // android .jar library + bundleInfoInternal->setSoBundle(false); + bundleInfoInternal->setJavaBundle(false); + registerExtBundle(bundleInfo); #endif } - else + else if(has_suffix(bundleInfo->getPath(), ".so")) { - ((BundleInfoInternal *) bundleInfo)->setJavaBundle(false); + bundleInfoInternal->setSoBundle(true); + bundleInfoInternal->setJavaBundle(false); registerSoBundle(bundleInfo); } + // other cases might be for example .apk for android, which are loaded in the wrapper + else{ + bundleInfoInternal->setSoBundle(false); + bundleInfoInternal->setJavaBundle(false); + registerExtBundle(bundleInfo); + } } - void ResourceContainerImpl::unregisterBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::unregisterBundle(shared_ptr bundleInfo) { - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); if (bundleInfoInternal->isLoaded() && !bundleInfoInternal->isActivated()) { - if (!bundleInfoInternal->getJavaBundle()) + if (bundleInfoInternal->getSoBundle()) { unregisterBundleSo(bundleInfo->getID()); } - else + else if(bundleInfoInternal->getJavaBundle()) { #if(JAVA_SUPPORT) unregisterBundleJava(bundleInfo->getID()); @@ -241,7 +259,7 @@ namespace OIC { void *bundleHandle = m_bundles[id]->getBundleHandle(); - OC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister bundle: (%s)", std::string(m_bundles[id]->getID()).c_str()); const char *error; @@ -249,11 +267,10 @@ namespace OIC if ((error = dlerror()) != NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error); + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error (%s)", error); } else { - delete m_bundles[id]; m_bundles.erase(id); } } @@ -266,8 +283,9 @@ namespace OIC RCSResourceObject::Ptr server = nullptr; int ret = EINVAL; - OC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" , - std::string(strUri + ", " + strResourceType).c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Registration of resource (%s)" , + std::string(strUri + ", " + strResourceType + "," + + resource->m_bundleId).c_str()); registrationLock.lock(); if (m_mapResources.find(strUri) == m_mapResources.end()) @@ -292,23 +310,34 @@ namespace OIC std::bind(&ResourceContainerImpl::setRequestHandler, this, std::placeholders::_1, std::placeholders::_2)); - OC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)", - std::string(strUri + ", " + - strResourceType).c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)", + std::string(strUri + ", " + + strResourceType).c_str()); - if (m_config->isHasInput(resource->m_bundleId)) + if (m_config && m_config->isHasInput(resource->m_bundleId)) { + OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has input (%s)", + std::string(strUri + ", " + + strResourceType).c_str()); discoverInputResource(strUri); } + else{ + OIC_LOG_V(INFO, CONTAINER_TAG, "Resource has no input (%s)", + std::string(strUri + ", " + + strResourceType).c_str()); + } + OIC_LOG_V(INFO, CONTAINER_TAG, "Registration finished (%s)", + std::string(strUri + ", " + + strResourceType).c_str()); // to get notified if bundle resource attributes are updated - resource->registerObserver((NotificationReceiver *) this); + resource->registerObserver(this); ret = 0; } } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "resource with (%s)", std::string(strUri + " already exists.").c_str()); ret = -EEXIST; } @@ -322,20 +351,30 @@ namespace OIC string strUri = resource->m_uri; string strResourceType = resource->m_resourceType; - OC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Unregistration of resource (%s)", std::string(resource->m_uri + ", " + resource->m_resourceType).c_str()); - if (m_config->isHasInput(resource->m_bundleId)) + if (m_config && m_config->isHasInput(resource->m_bundleId)) { + OIC_LOG_V(INFO, CONTAINER_TAG, "Calling undiscover (%s)", + std::string(resource->m_uri + ", " + + resource->m_resourceType).c_str()); undiscoverInputResource(strUri); } if (m_mapServers.find(strUri) != m_mapServers.end()) { + OIC_LOG_V(INFO, CONTAINER_TAG, "Resetting server (%s)", + std::string(resource->m_uri + ", " + + resource->m_resourceType).c_str()); m_mapServers[strUri].reset(); m_mapResources.erase(m_mapResources.find(strUri)); + + OIC_LOG_V(INFO, CONTAINER_TAG, "Remove bundle resource (%s)", + std::string(resource->m_uri + ", " + + resource->m_resourceType).c_str()); m_mapBundleResources[resource->m_bundleId].remove(strUri); } } @@ -356,6 +395,9 @@ namespace OIC { m_config->getResourceConfiguration(bundleId, configOutput); } + else{ + OIC_LOG_V(DEBUG, CONTAINER_TAG, "no config present "); + } } RCSGetResponse ResourceContainerImpl::getRequestHandler(const RCSRequest &request, @@ -363,6 +405,7 @@ namespace OIC { RCSResourceAttributes attr; std::string strResourceUri = request.getResourceUri(); + OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s",strResourceUri.c_str()); if (m_mapServers.find(strResourceUri) != m_mapServers.end() && m_mapResources.find(strResourceUri) != m_mapResources.end()) @@ -378,6 +421,7 @@ namespace OIC } } + OIC_LOG_V(INFO, CONTAINER_TAG, "Container get request for %s finished, %zu attributes",strResourceUri.c_str(), attr.size()); return RCSGetResponse::create(std::move(attr), 200); } @@ -389,6 +433,8 @@ namespace OIC std::list lstAttributes; std::string strResourceUri = request.getResourceUri(); + OIC_LOG_V(INFO, CONTAINER_TAG, "Container set request for %s, %zu attributes",strResourceUri.c_str(), attributes.size()); + if (m_mapServers.find(strResourceUri) != m_mapServers.end() && m_mapResources.find(strResourceUri) != m_mapResources.end()) { @@ -408,6 +454,7 @@ namespace OIC } } + OIC_LOG_V(INFO, CONTAINER_TAG, "Calling handleSetAttributeRequest"); m_mapResources[strResourceUri]->handleSetAttributesRequest(attr); }; boost::thread setThread(setFunction); @@ -420,7 +467,7 @@ namespace OIC void ResourceContainerImpl::onNotificationReceived(const std::string &strResourceUri) { - OC_LOG_V(INFO, CONTAINER_TAG, + OIC_LOG_V(INFO, CONTAINER_TAG, "notification from (%s)", std::string(strResourceUri + ".").c_str()); if (m_mapServers.find(strResourceUri) != m_mapServers.end()) @@ -445,52 +492,56 @@ namespace OIC void ResourceContainerImpl::startBundle(const std::string &bundleId) { + OIC_LOG_V(INFO, CONTAINER_TAG, "startBundle %s",bundleId.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { if (!m_bundles[bundleId]->isActivated()) activateBundle(m_bundles[bundleId]); else { - OC_LOG(ERROR, CONTAINER_TAG, "Bundle already started"); + OIC_LOG(ERROR, CONTAINER_TAG, "Bundle already started"); } } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", std::string(bundleId + "\' is not registered.").c_str()); } } void ResourceContainerImpl::stopBundle(const std::string &bundleId) { + OIC_LOG_V(INFO, CONTAINER_TAG, "stopBundle %s",bundleId.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { if (m_bundles[bundleId]->isActivated()) deactivateBundle(m_bundles[bundleId]); else { - OC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated"); + OIC_LOG(ERROR, CONTAINER_TAG, "Bundle not activated"); } } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", std::string(bundleId + "\' is not registered.").c_str()); } } void ResourceContainerImpl::addBundle(const std::string &bundleId, - const std::string &bundleUri, const std::string &bundlePath, - const std::string &activator, std::map< string, string > params) + const std::string &bundleUri, + const std::string &bundlePath, + const std::string &activator, + std::map< string, string > params) { (void) bundleUri; if (m_bundles.find(bundleId) != m_bundles.end()) - OC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist"); + OIC_LOG(ERROR, CONTAINER_TAG, "BundleId already exist"); else { - BundleInfoInternal *bundleInfo = new BundleInfoInternal(); + shared_ptr bundleInfo = std::make_shared(); bundleInfo->setID(bundleId); bundleInfo->setPath(bundlePath); bundleInfo->setActivatorName(activator); @@ -498,11 +549,11 @@ namespace OIC { string activatorName = activator; // modify activator for Java bundle std::replace(activatorName.begin(), activatorName.end(), '.', '/'); - ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName); - ((BundleInfoInternal *)bundleInfo)->setLibraryPath(params[BUNDLE_LIBRARY_PATH]); + bundleInfo->setActivatorName(activatorName); + bundleInfo->setLibraryPath(params[BUNDLE_LIBRARY_PATH]); } - OC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Add Bundle: (%s)", std::string(bundleInfo->getID() + "; " + bundleInfo->getPath()).c_str()); @@ -512,9 +563,10 @@ namespace OIC void ResourceContainerImpl::removeBundle(const std::string &bundleId) { + OIC_LOG_V(INFO, CONTAINER_TAG, "removeBundle %s",bundleId.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { - BundleInfoInternal *bundleInfo = m_bundles[bundleId]; + shared_ptr bundleInfo = m_bundles[bundleId]; if (bundleInfo->isActivated()) deactivateBundle(bundleInfo); @@ -523,20 +575,22 @@ namespace OIC } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", - std::string(bundleId + "\' is not registered.").c_str()); + OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", + std::string(bundleId + "\' is not ced.").c_str()); } } std::list> ResourceContainerImpl::listBundles() { + OIC_LOG_V(INFO, CONTAINER_TAG, + "list bundles (%d)", m_bundles.size()); std::list > ret; - for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin(); + for (std::map< std::string, shared_ptr >::iterator it = m_bundles.begin(); it != m_bundles.end(); ++it) { { std::unique_ptr bundleInfo(new BundleInfoInternal); - (bundleInfo)->setBundleInfo(it->second); + bundleInfo->setBundleInfo(it->second); ret.push_back(std::move(bundleInfo)); } } @@ -565,7 +619,7 @@ namespace OIC } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", std::string(bundleId + "\' is not registered.").c_str()); } } @@ -573,22 +627,25 @@ namespace OIC void ResourceContainerImpl::removeResourceConfig(const std::string &bundleId, const std::string &resourceUri) { + OIC_LOG_V(INFO, CONTAINER_TAG, "removeResourceConfig %s, %s",bundleId.c_str(), + resourceUri.c_str()); if (m_bundles.find(bundleId) != m_bundles.end()) { - if (!m_bundles[bundleId]->getJavaBundle()) + if (m_bundles[bundleId]->getSoBundle()) { removeSoBundleResource(bundleId, resourceUri); } } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Bundle with ID \'(%s)", std::string(bundleId + "\' is not registered.").c_str()); } } std::list< string > ResourceContainerImpl::listBundleResources(const std::string &bundleId) { + OIC_LOG_V(INFO, CONTAINER_TAG, "listBundleResources %s",bundleId.c_str()); std::list < string > ret; if (m_mapBundleResources.find(bundleId) != m_mapBundleResources.end()) @@ -600,61 +657,128 @@ namespace OIC } - void ResourceContainerImpl::registerSoBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerSoBundle(shared_ptr bundleInfo) { + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle"); const char *error; activator_t *bundleActivator = NULL; deactivator_t *bundleDeactivator = NULL; resourceCreator_t *resourceCreator = NULL; resourceDestroyer_t *resourceDestroyer = NULL; - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + void *bundleHandle = NULL; bundleHandle = dlopen(bundleInfo->getPath().c_str(), RTLD_LAZY); + if ((error = dlerror()) != NULL) + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); + } if (bundleHandle != NULL) { + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Activator name %s", + bundleInfoInternal->getActivatorName().c_str()); bundleActivator = (activator_t *) dlsym(bundleHandle, ("" + bundleInfoInternal->getActivatorName() + "_externalActivateBundle").c_str()); + if ((error = dlerror()) != NULL) + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); + } + else{ + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + + "_externalActivateBundle").c_str()); + } bundleDeactivator = (deactivator_t *) dlsym(bundleHandle, ("" + bundleInfoInternal->getActivatorName() + "_externalDeactivateBundle").c_str()); + if ((error = dlerror()) != NULL) + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); + } + else{ + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + + "_externalDeactivateBundle").c_str()); + } resourceCreator = (resourceCreator_t *) dlsym(bundleHandle, ("" + bundleInfoInternal->getActivatorName() + "_externalCreateResource").c_str()); + if ((error = dlerror()) != NULL) + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); + } + else{ + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + + "_externalCreateResource").c_str()); + } resourceDestroyer = (resourceDestroyer_t *) dlsym(bundleHandle, ("" + bundleInfoInternal->getActivatorName() + "_externalDestroyResource").c_str()); + if ((error = dlerror()) != NULL) + { + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error while loading .so bundle: (%s)", error); + } + else{ + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Looked up %s", ("" + + bundleInfoInternal->getActivatorName() + + "_externalDestroyResource").c_str()); + } if ((error = dlerror()) != NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error); + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error); } else { - ((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); + bundleInfoInternal->setBundleActivator(bundleActivator); + bundleInfoInternal->setBundleDeactivator(bundleDeactivator); + bundleInfoInternal->setResourceCreator(resourceCreator); + bundleInfoInternal->setResourceDestroyer(resourceDestroyer); + bundleInfoInternal->setLoaded(true); + bundleInfoInternal->setBundleHandle(bundleHandle); + + m_bundles[bundleInfo->getID()] = bundleInfoInternal; } } else { if ((error = dlerror()) != NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error); + OIC_LOG_V(ERROR, CONTAINER_TAG, "Error : (%s)", error); } } + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Register SO bundle finished"); + } + + void ResourceContainerImpl::registerExtBundle(shared_ptr bundleInfo){ + OIC_LOG_V(INFO, CONTAINER_TAG, "Registering ext bundle (%s)", + std::string(bundleInfo->getID()).c_str()); + OIC_LOG_V(INFO, CONTAINER_TAG, "Activator name (%s)", + std::string(bundleInfo->getActivatorName()).c_str()); + + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + + m_bundles[bundleInfo->getID()] = bundleInfoInternal; + // in this case at least the resource configuration needs to be loaded + // in order to mark potential input resources for soft sensors + std::vector< resourceInfo > temp; + OIC_LOG_V(INFO, CONTAINER_TAG, "Loading resource config(%s)", + std::string(bundleInfo->getID()).c_str()); + getResourceConfiguration(bundleInfo->getID(), + &temp); + + OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered"); } void ResourceContainerImpl::activateSoBundle(const std::string &bundleId) @@ -669,10 +793,10 @@ namespace OIC else { //Unload module and return error - OC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful."); + OIC_LOG(ERROR, CONTAINER_TAG, "Activation unsuccessful."); } - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + shared_ptr bundleInfoInternal = m_bundles[bundleId]; bundleInfoInternal->setActivated(true); } @@ -682,14 +806,21 @@ namespace OIC auto foundDiscoverResource = m_mapDiscoverResourceUnits.find(outputResourceUri); if (foundDiscoverResource != m_mapDiscoverResourceUnits.end()) { + OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource."); m_mapDiscoverResourceUnits.erase(foundDiscoverResource); + OIC_LOG(DEBUG, CONTAINER_TAG, "Erase discover resource done."); } } void ResourceContainerImpl::discoverInputResource(const std::string &outputResourceUri) { + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Discover input resource %s", outputResourceUri.c_str()); auto foundOutputResource = m_mapResources.find(outputResourceUri); - auto resourceProperty = foundOutputResource->second->m_mapResourceProperty; + + resourceInfo info; + m_config->getResourceConfiguration(foundOutputResource->second->m_bundleId, + outputResourceUri, &info); + map< string, vector< map< string, string > > > resourceProperty = info.resourceProperty; try { @@ -697,6 +828,7 @@ namespace OIC } catch (std::out_of_range &e) { + OIC_LOG_V(DEBUG, CONTAINER_TAG, "No input resource %s", outputResourceUri.c_str()); return; } @@ -723,13 +855,17 @@ namespace OIC std::string type = makeValue(INPUT_RESOURCE_TYPE); std::string attributeName = makeValue(INPUT_RESOURCE_ATTRIBUTENAME); + + OIC_LOG_V(DEBUG, CONTAINER_TAG, "Start discovery: %s, %s, %s", uri.c_str(), + type.c_str(), attributeName.c_str()); DiscoverResourceUnit::Ptr newDiscoverUnit = std::make_shared < DiscoverResourceUnit > (outputResourceUri); newDiscoverUnit->startDiscover( DiscoverResourceUnit::DiscoverResourceInfo(uri, type, attributeName), std::bind(&SoftSensorResource::onUpdatedInputResource, - std::static_pointer_cast< SoftSensorResource > (foundOutputResource->second), + std::static_pointer_cast< SoftSensorResource > + (foundOutputResource->second), std::placeholders::_1, std::placeholders::_2)); auto foundDiscoverResource = m_mapDiscoverResourceUnits.find( @@ -754,7 +890,7 @@ namespace OIC { deactivator_t *bundleDeactivator = m_bundles[id]->getBundleDeactivator(); - OC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string( + OIC_LOG_V(INFO, CONTAINER_TAG, "De-activating bundle: (%s)", std::string( m_bundles[id]->getID()).c_str()); if (bundleDeactivator != NULL) @@ -765,7 +901,7 @@ namespace OIC else { //Unload module and return error - OC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful."); + OIC_LOG(ERROR, CONTAINER_TAG, "De-activation unsuccessful."); } } @@ -782,7 +918,7 @@ namespace OIC } else { - OC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful."); + OIC_LOG(ERROR, CONTAINER_TAG, "addResource unsuccessful."); } } @@ -800,14 +936,14 @@ namespace OIC } else { - OC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful."); + OIC_LOG(ERROR, CONTAINER_TAG, "removeResource unsuccessful."); } } } void ResourceContainerImpl::activateBundleThread(const std::string &id) { - OC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Activating bundle: (%s)", std::string(m_bundles[id]->getID()).c_str()); if (m_bundles[id]->getJavaBundle()) @@ -816,12 +952,12 @@ namespace OIC activateJavaBundle(id); #endif } - else + else if(m_bundles[id]->getSoBundle()) { activateSoBundle (id); } - OC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Bundle activated: (%s)", std::string(m_bundles[id]->getID()).c_str()); } @@ -831,28 +967,30 @@ namespace OIC return m_bundleVM[bundleId]; } - void ResourceContainerImpl::registerJavaBundle(RCSBundleInfo *bundleInfo) + void ResourceContainerImpl::registerJavaBundle(shared_ptr bundleInfo) { - OC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Registering Java bundle (%s)", std::string(bundleInfo->getID()).c_str()); JavaVM *jvm; JNIEnv *env; JavaVMInitArgs vm_args; JavaVMOption options[3]; - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo; + shared_ptr bundleInfoInternal = + std::static_pointer_cast(bundleInfo); + if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r")) { fclose(file); - OC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)", + OIC_LOG_V(INFO, CONTAINER_TAG, "Resource bundle (%s)", std::string(bundleInfo->getPath() + " available.").c_str()); } else { - OC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Resource bundle (%s)", std::string(bundleInfo->getPath() + " not available.").c_str()); return; @@ -862,19 +1000,19 @@ namespace OIC options[0].optionString = optionString; char classpath[1000]; strcpy(classpath, "-Djava.class.path="); - strcat(classpath, bundleInfo->getPath().c_str()); + strncat(classpath, bundleInfo->getPath().c_str(), BUNDLE_PATH_MAXLEN); - OC_LOG(INFO, CONTAINER_TAG, + OIC_LOG(INFO, CONTAINER_TAG, std::string("Configured classpath: ").append(classpath).c_str()); options[1].optionString = classpath; char libraryPath[1000]; strcpy(libraryPath, "-Djava.library.path="); - strcat(libraryPath, bundleInfo->getLibraryPath().c_str()); + strncat(libraryPath, bundleInfo->getLibraryPath().c_str(), BUNDLE_PATH_MAXLEN); options[2].optionString = libraryPath; - OC_LOG(INFO, CONTAINER_TAG, + OIC_LOG(INFO, CONTAINER_TAG, std::string("Configured library path: ").append(libraryPath).c_str()); vm_args.version = JNI_VERSION_1_4; @@ -887,28 +1025,29 @@ namespace OIC if (res < 0) { - OC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM."); + OIC_LOG(ERROR, CONTAINER_TAG, "cannot create JavaVM."); return; } else { - OC_LOG(INFO, CONTAINER_TAG, "JVM successfully created."); + OIC_LOG(INFO, CONTAINER_TAG, "JVM successfully created."); } m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm)); const char *className = bundleInfoInternal->getActivatorName().c_str(); - OC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string( + OIC_LOG_V(INFO, CONTAINER_TAG, "Looking up class: (%s)", std::string( bundleInfoInternal->getActivatorName() + "|").c_str()); jclass bundleActivatorClass = env->FindClass(className); if (bundleActivatorClass == NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", std::string( bundleInfoInternal->getID() - + " bundle activator(" + bundleInfoInternal->getActivatorName() + + " bundle activator(" + + bundleInfoInternal->getActivatorName() + ") not found ").c_str()); return; } @@ -918,7 +1057,7 @@ namespace OIC if (activateMethod == NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", std::string( bundleInfoInternal->getID() + " activate bundle method not found ").c_str()); return; @@ -930,7 +1069,7 @@ namespace OIC if (deactivateMethod == NULL) { - OC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", + OIC_LOG_V(ERROR, CONTAINER_TAG, "Cannot register bundle (%s)", std::string( bundleInfoInternal->getID() + " deactivate bundle method not found ").c_str()); return; @@ -950,18 +1089,20 @@ namespace OIC bundleInfoInternal->setLoaded(true); - m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo); + m_bundles[bundleInfo->getID()] = bundleInfoInternal; - OC_LOG(INFO, CONTAINER_TAG, "Bundle registered"); + OIC_LOG(INFO, CONTAINER_TAG, "Bundle registered"); } + + void ResourceContainerImpl::activateJavaBundle(string bundleId) { - OC_LOG(INFO, CONTAINER_TAG, "Activating java bundle"); + OIC_LOG(INFO, CONTAINER_TAG, "Activating java bundle"); JavaVM *vm = getJavaVM(bundleId); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + bundleInfoInternal = m_bundles[bundleId]; JNIEnv *env; int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4); @@ -969,12 +1110,12 @@ namespace OIC { if (vm->AttachCurrentThread((void **) &env, NULL) != 0) { - OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach "); + OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach "); } } else if (envStat == JNI_EVERSION) { - OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported "); + OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported "); } env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(), @@ -985,10 +1126,10 @@ namespace OIC void ResourceContainerImpl::deactivateJavaBundle(string bundleId) { - OC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle"); + OIC_LOG(INFO, CONTAINER_TAG, "Deactivating java bundle"); JavaVM *vm = getJavaVM(bundleId); - BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId]; + shared_ptrbundleInfoInternal = m_bundles[bundleId]; JNIEnv *env; int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4); @@ -996,12 +1137,12 @@ namespace OIC { if (vm->AttachCurrentThread((void **) &env, NULL) != 0) { - OC_LOG(ERROR, CONTAINER_TAG, "Failed to attach "); + OIC_LOG(ERROR, CONTAINER_TAG, "Failed to attach "); } } else if (envStat == JNI_EVERSION) { - OC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported "); + OIC_LOG(ERROR, CONTAINER_TAG, "Env: version not supported "); } env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(), @@ -1012,14 +1153,13 @@ namespace OIC void ResourceContainerImpl::unregisterBundleJava(string id) { - OC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string( + OIC_LOG_V(INFO, CONTAINER_TAG, "Unregister Java bundle: (%s)", std::string( m_bundles[id]->getID()).c_str()); - OC_LOG(INFO, CONTAINER_TAG, "Destroying JVM"); + OIC_LOG(INFO, CONTAINER_TAG, "Destroying JVM"); m_bundleVM[id]->DestroyJavaVM(); - delete m_bundles[id]; m_bundles.erase(id); } #endif