5dbcc32fee0c5c54a67f1fb0719cb7da888b3be2
[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 #define BUNDLE_SET_GET_WAIT_SEC 10
45
46 using namespace OIC::Service;
47
48 namespace OIC
49 {
50     namespace Service
51     {
52
53         class ResourceContainerImpl: public RCSResourceContainer, public ResourceContainerBundleAPI
54         {
55             public:
56                 // methods from ResourceContainer
57                 void startContainer(const std::string &configFile);
58                 void stopContainer();
59                 void activateBundle(RCSBundleInfo *bundleInfo);
60                 void deactivateBundle(RCSBundleInfo *bundleInfo);
61                 void activateBundle(const std::string &bundleId);
62                 void deactivateBundle(const std::string &bundleId);
63                 void registerBundle(RCSBundleInfo *bundleinfo);
64                 void unregisterBundle(RCSBundleInfo *bundleinfo);
65                 void unregisterBundleSo(const std::string &id);
66
67                 // methods from ResourceContainerBundleAPI
68                 void registerResource(BundleResource::Ptr resource);
69                 void unregisterResource(BundleResource::Ptr resource);
70
71                 void getBundleConfiguration(const std::string &bundleId, configInfo *configOutput);
72                 void getResourceConfiguration(const std::string &bundleId,
73                                               std::vector< resourceInfo > *configOutput);
74
75                 RCSGetResponse getRequestHandler(const RCSRequest &request,
76                                                  const RCSResourceAttributes &attributes);
77                 RCSSetResponse setRequestHandler(const RCSRequest &request,
78                                                  const RCSResourceAttributes &attributes);
79
80                 void onNotificationReceived(const std::string &strResourceUri);
81
82                 static ResourceContainerImpl *getImplInstance();
83                 static RCSResourceObject::Ptr buildResourceObject(const std::string &strUri,
84                         const std::string &strResourceType);
85
86                 void startBundle(const std::string &bundleId);
87                 void stopBundle(const std::string &bundleId);
88
89                 void addBundle(const std::string &bundleId, const std::string &bundleUri,
90                                const std::string &bundlePath, const std::string &activator,
91                                std::map< string, string > params);
92                 void removeBundle(const std::string &bundleId);
93
94                 std::list< RCSBundleInfo * > listBundles();
95
96                 void addResourceConfig(const std::string &bundleId, const std::string &resourceUri,
97                                        std::map< string, string > params);
98                 void removeResourceConfig(const std::string &bundleId, const std::string &resourceUri);
99
100                 std::list< string > listBundleResources(const std::string &bundleId);
101
102 #if(JAVA_SUPPORT)
103                 JavaVM *getJavaVM(string bundleId);
104                 void unregisterBundleJava(string id);
105 #endif
106
107             private:
108                 map< std::string, BundleInfoInternal * > m_bundles; // <bundleID, bundleInfo>
109                 map< std::string, RCSResourceObject::Ptr > m_mapServers; //<uri, serverPtr>
110                 map< std::string, BundleResource::Ptr > m_mapResources; //<uri, resourcePtr>
111                 map< std::string, list< string > > m_mapBundleResources; //<bundleID, vector<uri>>
112                 map< std::string, list< DiscoverResourceUnit::Ptr > > m_mapDiscoverResourceUnits;
113                 //<uri, DiscoverUnit>
114                 string m_configFile;
115                 Configuration *m_config;
116                 // holds for a bundle the threads for bundle activation
117                 map< std::string, boost::thread > m_activators;
118                 // used for synchronize the resource registration of multiple bundles
119                 std::mutex registrationLock;
120                 // used to synchronize the startup of the container with other operation
121                 // such as individual bundle activation
122                 std::recursive_mutex activationLock;
123
124                 ResourceContainerImpl();
125                 virtual ~ResourceContainerImpl();
126
127                 ResourceContainerImpl(const ResourceContainerImpl &) = delete;
128                 ResourceContainerImpl(ResourceContainerImpl &&) = delete;
129                 ResourceContainerImpl &operator=(const ResourceContainerImpl &) const = delete;
130                 ResourceContainerImpl &operator=(ResourceContainerImpl &&) const = delete;
131
132                 void activateSoBundle(const std::string &bundleId);
133                 void deactivateSoBundle(const std::string &bundleId);
134                 void addSoBundleResource(const std::string &bundleId, resourceInfo newResourceInfo);
135                 void removeSoBundleResource(const std::string &bundleId,
136                                             const std::string &resourceUri);
137                 void registerSoBundle(RCSBundleInfo *bundleInfo);
138                 void discoverInputResource(const std::string &outputResourceUri);
139                 void undiscoverInputResource(const std::string &outputResourceUri);
140                 void activateBundleThread(const std::string &bundleId);
141
142 #if(JAVA_SUPPORT)
143                 map<string, JavaVM *> m_bundleVM;
144
145                 void registerJavaBundle(RCSBundleInfo *bundleInfo);
146                 void activateJavaBundle(string bundleId);
147                 void deactivateJavaBundle(string bundleId);
148
149 #endif
150
151         };
152     }
153 }
154 #endif