1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #ifndef RESOURCECONTAINERIMPL_H_
22 #define RESOURCECONTAINERIMPL_H_
24 #include "RCSResourceContainer.h"
25 #include "ResourceContainerBundleAPI.h"
26 #include "BundleInfoInternal.h"
28 #include "RCSRequest.h"
29 #include "RCSResponse.h"
30 #include "RCSResourceObject.h"
32 #include "DiscoverResourceUnit.h"
34 #include <boost/thread.hpp>
35 #include <boost/date_time.hpp>
43 #define BUNDLE_ACTIVATION_WAIT_SEC 10
44 #define BUNDLE_SET_GET_WAIT_SEC 10
46 using namespace OIC::Service;
53 class ResourceContainerImpl: public RCSResourceContainer, public ResourceContainerBundleAPI
56 // methods from ResourceContainer
57 void startContainer(const std::string &configFile);
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);
67 // methods from ResourceContainerBundleAPI
68 void registerResource(BundleResource::Ptr resource);
69 void unregisterResource(BundleResource::Ptr resource);
71 void getBundleConfiguration(const std::string &bundleId, configInfo *configOutput);
72 void getResourceConfiguration(const std::string &bundleId,
73 std::vector< resourceInfo > *configOutput);
75 RCSGetResponse getRequestHandler(const RCSRequest &request,
76 const RCSResourceAttributes &attributes);
77 RCSSetResponse setRequestHandler(const RCSRequest &request,
78 const RCSResourceAttributes &attributes);
80 void onNotificationReceived(const std::string &strResourceUri);
82 static ResourceContainerImpl *getImplInstance();
83 static RCSResourceObject::Ptr buildResourceObject(const std::string &strUri,
84 const std::string &strResourceType);
86 void startBundle(const std::string &bundleId);
87 void stopBundle(const std::string &bundleId);
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);
94 std::list< RCSBundleInfo * > listBundles();
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);
100 std::list< string > listBundleResources(const std::string &bundleId);
103 JavaVM *getJavaVM(string bundleId);
104 void unregisterBundleJava(string id);
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>
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;
124 ResourceContainerImpl();
125 virtual ~ResourceContainerImpl();
127 ResourceContainerImpl(const ResourceContainerImpl &) = delete;
128 ResourceContainerImpl(ResourceContainerImpl &&) = delete;
129 ResourceContainerImpl &operator=(const ResourceContainerImpl &) const = delete;
130 ResourceContainerImpl &operator=(ResourceContainerImpl &&) const = delete;
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);
143 map<string, JavaVM *> m_bundleVM;
145 void registerJavaBundle(RCSBundleInfo *bundleInfo);
146 void activateJavaBundle(string bundleId);
147 void deactivateJavaBundle(string bundleId);