Bundle isolation - use separate threads for activation
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceContainer / include / ResourceContainerImpl.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef RESOURCECONTAINERIMPL_H_
22 #define RESOURCECONTAINERIMPL_H_
23
24 #include "RCSResourceContainer.h"
25 #include "ResourceContainerBundleAPI.h"
26 #include "BundleInfoInternal.h"
27
28 #include "RCSRequest.h"
29 #include "RCSResponse.h"
30 #include "RCSResourceObject.h"
31
32 #include "DiscoverResourceUnit.h"
33
34 #include <boost/thread.hpp>
35 #include <boost/date_time.hpp>
36
37 #if(JAVA_SUPPORT)
38 #include <jni.h>
39 #endif
40
41 #include <map>
42
43 #define BUNDLE_ACTIVATION_WAIT_SEC 10
44
45 using namespace OIC::Service;
46
47 namespace OIC
48 {
49     namespace Service
50     {
51
52         class ResourceContainerImpl: public RCSResourceContainer, public ResourceContainerBundleAPI
53         {
54         public:
55             ResourceContainerImpl();
56             virtual ~ResourceContainerImpl();
57
58             // methods from ResourceContainer
59             void startContainer(const std::string &configFile);
60             void stopContainer();
61             void activateBundle(RCSBundleInfo *bundleInfo);
62             void deactivateBundle(RCSBundleInfo *bundleInfo);
63             void activateBundle(const std::string &bundleId);
64             void deactivateBundle(const std::string &bundleId);
65             void registerBundle(RCSBundleInfo *bundleinfo);
66             void unregisterBundle(RCSBundleInfo *bundleinfo);
67             void unregisterBundleSo(const std::string &id);
68
69             // methods from ResourceContainerBundleAPI
70             void registerResource(BundleResource *resource);
71             void unregisterResource(BundleResource *resource);
72
73             void getBundleConfiguration(const std::string &bundleId, configInfo *configOutput);
74             void getResourceConfiguration(const std::string &bundleId,
75                     std::vector< resourceInfo > *configOutput);
76
77             RCSGetResponse getRequestHandler(const RCSRequest &request,
78                     const RCSResourceAttributes &attributes);
79             RCSSetResponse setRequestHandler(const RCSRequest &request,
80                     const RCSResourceAttributes &attributes);
81
82             void onNotificationReceived(const std::string &strResourceUri);
83
84             static ResourceContainerImpl *getImplInstance();
85             static RCSResourceObject::Ptr buildResourceObject(const std::string &strUri,
86                     const std::string &strResourceType);
87
88             void startBundle(const std::string &bundleId);
89             void stopBundle(const std::string &bundleId);
90
91             void addBundle(const std::string &bundleId, const std::string &bundleUri,
92                     const std::string &bundlePath, std::map< string, string > params);
93             void removeBundle(const std::string &bundleId);
94
95             std::list< RCSBundleInfo * > listBundles();
96
97             void addResourceConfig(const std::string &bundleId, const std::string &resourceUri,
98                     std::map< string, string > params);
99             void removeResourceConfig(const std::string &bundleId, const std::string &resourceUri);
100
101             std::list< string > listBundleResources(const std::string &bundleId);
102
103 #if(JAVA_SUPPORT)
104             JavaVM *getJavaVM(string bundleId);
105             void unregisterBundleJava(string id);
106 #endif
107
108         private:
109             map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
110             map< std::string, RCSResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
111             map< std::string, BundleResource * > m_mapResources; //<uri, resourcePtr>
112             map< std::string, list< string > > m_mapBundleResources; //<bundleID, vector<uri>>
113             map< std::string, list< DiscoverResourceUnit::Ptr > > m_mapDiscoverResourceUnits; //<uri, DiscoverUnit>
114             string m_configFile;
115             Configuration *m_config;
116             vector< boost::thread > m_activators; // holds threads for bundle activation
117             std::mutex registrationLock; // used for synchronize the resource registration of multiple bundles
118
119             void activateSoBundle(const std::string &bundleId);
120             void deactivateSoBundle(const std::string &bundleId);
121             void addSoBundleResource(const std::string &bundleId, resourceInfo newResourceInfo);
122             void removeSoBundleResource(const std::string &bundleId,
123                     const std::string &resourceUri);
124             void registerSoBundle(RCSBundleInfo *bundleInfo);
125             void discoverInputResource(const std::string & outputResourceUri);
126             void undiscoverInputResource(const std::string & outputResourceUri);
127             void activateBundleThread(RCSBundleInfo *bundleInfo);
128
129 #if(JAVA_SUPPORT)
130             map<string, JavaVM *> m_bundleVM;
131
132             void registerJavaBundle(RCSBundleInfo *bundleInfo);
133             void activateJavaBundle(string bundleId);
134             void deactivateJavaBundle(string bundleId);
135
136 #endif
137
138         };
139     }
140 }
141 #endif