Bundle isolation - use separate threads for activation
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / include / ResourceContainerImpl.h
index 78314f5..221f9d0 100644 (file)
@@ -21,8 +21,6 @@
 #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
@@ -48,84 +51,87 @@ 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