#ifndef RESOURCECONTAINERIMPL_H_
#define RESOURCECONTAINERIMPL_H_
-
-
#include "RCSResourceContainer.h"
#include "ResourceContainerBundleAPI.h"
#include "BundleInfoInternal.h"
#include "DiscoverResourceUnit.h"
+#include <boost/thread.hpp>
+#include <boost/date_time.hpp>
+
#if(JAVA_SUPPORT)
- #include <jni.h>
+#include <jni.h>
#endif
#include <map>
+#define BUNDLE_ACTIVATION_WAIT_SEC 10
+
using namespace OIC::Service;
namespace OIC
class ResourceContainerImpl: public RCSResourceContainer, public ResourceContainerBundleAPI
{
- public:
- ResourceContainerImpl();
- virtual ~ResourceContainerImpl();
-
- // methods from ResourceContainer
- void startContainer(const std::string &configFile);
- void stopContainer();
- void activateBundle(RCSBundleInfo *bundleInfo);
- void deactivateBundle(RCSBundleInfo *bundleInfo);
- void activateBundle(const std::string &bundleId);
- void deactivateBundle(const std::string &bundleId);
- void registerBundle(RCSBundleInfo *bundleinfo);
- void unregisterBundle(RCSBundleInfo *bundleinfo);
- void unregisterBundleSo(const std::string &id);
+ public:
+ ResourceContainerImpl();
+ virtual ~ResourceContainerImpl();
+ // methods from ResourceContainer
+ void startContainer(const std::string &configFile);
+ void stopContainer();
+ void activateBundle(RCSBundleInfo *bundleInfo);
+ void deactivateBundle(RCSBundleInfo *bundleInfo);
+ void activateBundle(const std::string &bundleId);
+ void deactivateBundle(const std::string &bundleId);
+ void registerBundle(RCSBundleInfo *bundleinfo);
+ void unregisterBundle(RCSBundleInfo *bundleinfo);
+ void unregisterBundleSo(const std::string &id);
- // methods from ResourceContainerBundleAPI
- void registerResource(BundleResource *resource);
- void unregisterResource(BundleResource *resource);
+ // methods from ResourceContainerBundleAPI
+ void registerResource(BundleResource *resource);
+ void unregisterResource(BundleResource *resource);
- void getBundleConfiguration(const std::string &bundleId, configInfo *configOutput);
- void getResourceConfiguration(const std::string &bundleId,
- std::vector< resourceInfo > *configOutput);
+ void getBundleConfiguration(const std::string &bundleId, configInfo *configOutput);
+ void getResourceConfiguration(const std::string &bundleId,
+ std::vector< resourceInfo > *configOutput);
- RCSGetResponse getRequestHandler(const RCSRequest &request,
- const RCSResourceAttributes &attributes);
- RCSSetResponse setRequestHandler(const RCSRequest &request,
- const RCSResourceAttributes &attributes);
+ RCSGetResponse getRequestHandler(const RCSRequest &request,
+ const RCSResourceAttributes &attributes);
+ RCSSetResponse setRequestHandler(const RCSRequest &request,
+ const RCSResourceAttributes &attributes);
- void onNotificationReceived(const std::string &strResourceUri);
+ void onNotificationReceived(const std::string &strResourceUri);
- static ResourceContainerImpl *getImplInstance();
- static RCSResourceObject::Ptr buildResourceObject(const std::string &strUri, const std::string &strResourceType);
+ static ResourceContainerImpl *getImplInstance();
+ static RCSResourceObject::Ptr buildResourceObject(const std::string &strUri,
+ const std::string &strResourceType);
- void startBundle(const std::string &bundleId);
- void stopBundle(const std::string &bundleId);
+ void startBundle(const std::string &bundleId);
+ void stopBundle(const std::string &bundleId);
- void addBundle(const std::string &bundleId, const std::string &bundleUri, const std::string &bundlePath,
- std::map<string, string> params);
- void removeBundle(const std::string &bundleId);
+ void addBundle(const std::string &bundleId, const std::string &bundleUri,
+ const std::string &bundlePath, std::map< string, string > params);
+ void removeBundle(const std::string &bundleId);
- std::list<RCSBundleInfo *> listBundles();
+ std::list< RCSBundleInfo * > listBundles();
- void addResourceConfig(const std::string &bundleId, const std::string &resourceUri, std::map<string, string> params);
- void removeResourceConfig(const std::string &bundleId, const std::string &resourceUri);
+ void addResourceConfig(const std::string &bundleId, const std::string &resourceUri,
+ std::map< string, string > params);
+ void removeResourceConfig(const std::string &bundleId, const std::string &resourceUri);
- std::list<string> listBundleResources(const std::string &bundleId);
+ std::list< string > listBundleResources(const std::string &bundleId);
#if(JAVA_SUPPORT)
- JavaVM *getJavaVM(string bundleId);
- void unregisterBundleJava(string id);
+ JavaVM *getJavaVM(string bundleId);
+ void unregisterBundleJava(string id);
#endif
-
- private:
- map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
- map< std::string, RCSResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
- map< std::string, BundleResource * > m_mapResources; //<uri, resourcePtr>
- map< std::string, list<string> > m_mapBundleResources; //<bundleID, vector<uri>>
- map< std::string, list<DiscoverResourceUnit::Ptr>>
- m_mapDiscoverResourceUnits; //<uri, DiscoverUnit>
- string m_configFile;
- Configuration *m_config;
-
- void activateSoBundle(const std::string &bundleId);
- void deactivateSoBundle(const std::string &bundleId);
- void addSoBundleResource(const std::string &bundleId, resourceInfo newResourceInfo);
- void removeSoBundleResource(const std::string &bundleId, const std::string &resourceUri);
- void registerSoBundle(RCSBundleInfo *bundleInfo);
- void discoverInputResource(const std::string & outputResourceUri);
- void undiscoverInputResource(const std::string & outputResourceUri);
+ private:
+ map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
+ map< std::string, RCSResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
+ map< std::string, BundleResource * > m_mapResources; //<uri, resourcePtr>
+ map< std::string, list< string > > m_mapBundleResources; //<bundleID, vector<uri>>
+ map< std::string, list< DiscoverResourceUnit::Ptr > > m_mapDiscoverResourceUnits; //<uri, DiscoverUnit>
+ string m_configFile;
+ Configuration *m_config;
+ vector< boost::thread > m_activators; // holds threads for bundle activation
+ std::mutex registrationLock; // used for synchronize the resource registration of multiple bundles
+
+ void activateSoBundle(const std::string &bundleId);
+ void deactivateSoBundle(const std::string &bundleId);
+ void addSoBundleResource(const std::string &bundleId, resourceInfo newResourceInfo);
+ void removeSoBundleResource(const std::string &bundleId,
+ const std::string &resourceUri);
+ void registerSoBundle(RCSBundleInfo *bundleInfo);
+ void discoverInputResource(const std::string & outputResourceUri);
+ void undiscoverInputResource(const std::string & outputResourceUri);
+ void activateBundleThread(RCSBundleInfo *bundleInfo);
#if(JAVA_SUPPORT)
- map<string, JavaVM *> m_bundleVM;
+ map<string, JavaVM *> m_bundleVM;
- void registerJavaBundle(RCSBundleInfo *bundleInfo);
- void activateJavaBundle(string bundleId);
- void deactivateJavaBundle(string bundleId);
+ void registerJavaBundle(RCSBundleInfo *bundleInfo);
+ void activateJavaBundle(string bundleId);
+ void deactivateJavaBundle(string bundleId);
#endif
#include <dlfcn.h>
#include <unistd.h>
#include <string.h>
-#include <iostream>
#include <fstream>
#include <iostream>
#include <fstream>
#include <stdio.h>
+#include <thread>
+#include <mutex>
+
#include "ResourceContainerImpl.h"
#include "BundleActivator.h"
#include "oc_logger.hpp"
#include "SoftSensorResource.h"
+
using OC::oc_log_stream;
using namespace OIC::Service;
info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
<< endl;
registerBundle(bundleInfo);
- activateBundle(bundleInfo);
+
+ auto f = std::bind(&ResourceContainerImpl::activateBundleThread, this, bundleInfo);
+
+ boost::thread activator(f);
+ m_activators.push_back(std::move(activator));
}
}
else
{
info_logger() << "No configuration file for the container provided" << endl;
}
+ vector<boost::thread>::iterator activatorIterator;
+
+ for(activatorIterator = m_activators.begin(); activatorIterator != m_activators.end(); activatorIterator++){
+ activatorIterator->timed_join(boost::posix_time::seconds(BUNDLE_ACTIVATION_WAIT_SEC)); // wait for bundles to be activated
+ }
}
void ResourceContainerImpl::stopContainer()
void ResourceContainerImpl::registerResource(BundleResource *resource)
{
+
string strUri = resource->m_uri;
string strResourceType = resource->m_resourceType;
RCSResourceObject::Ptr server = nullptr;
info_logger() << "Registration of resource " << strUri << "," << strResourceType << endl;
+ registrationLock.lock();
if (m_mapResources.find(strUri) == m_mapResources.end())
{
server = buildResourceObject(strUri, strResourceType);
{
error_logger() << "resource with " << strUri << " already exists." << endl;
}
+ registrationLock.unlock();
}
void ResourceContainerImpl::unregisterResource(BundleResource *resource)
}
}
+ void ResourceContainerImpl::activateBundleThread(RCSBundleInfo* rcsBundleInfo){
+ activateBundle(rcsBundleInfo);
+ }
+
#if(JAVA_SUPPORT)
JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
{