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