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