239b28a0af48cf651994a6e6c617cb9f039b7710
[platform/upstream/iotivity.git] / service / resource-hosting / src / ResourceHosting.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 RH_RESOURCEHOSTING_H_
22 #define RH_RESOURCEHOSTING_H_
23
24 #include <atomic>
25 #include <functional>
26 #include <list>
27 #include <memory>
28 #include <mutex>
29
30 #include "RCSDiscoveryManager.h"
31 #include "RCSRemoteResourceObject.h"
32 #include "HostingObject.h"
33
34 namespace OIC
35 {
36 namespace Service
37 {
38
39 class ResourceHosting
40 {
41 private:
42     typedef RCSRemoteResourceObject::Ptr RemoteObjectPtr;
43     typedef std::lock_guard<std::mutex> RHLock;
44     typedef std::string HostingObjectKey;
45
46     typedef std::function<void(RemoteObjectPtr)> DiscoveryCallback;
47     typedef HostingObject::DestroyedCallback DestroyedCallback;
48
49 public:
50     void startHosting();
51     void stopHosting();
52
53     static ResourceHosting * getInstance();
54
55 private:
56     ResourceHosting();
57     ~ResourceHosting() = default;
58
59     ResourceHosting(ResourceHosting&&) = delete;
60     ResourceHosting(const ResourceHosting&) = delete;
61     ResourceHosting& operator=(ResourceHosting&&) = delete;
62     ResourceHosting& operator=(const ResourceHosting&) = delete;
63
64     std::mutex m_mutexForList;
65     std::atomic_bool m_isStartedHosting;
66
67     std::unordered_map<HostingObjectKey, HostingObject::Ptr> m_hostingObjects;
68     RCSDiscoveryManager::DiscoveryTask::Ptr m_discoveryTask;
69
70     void createDiscoveryListener();
71     void discoveryHandler(RemoteObjectPtr remoteResource);
72
73     inline HostingObjectKey generateHostingObjectKey(std::string address, std::string uri)
74     {
75         return HostingObjectKey(address + uri);
76     }
77     inline HostingObjectKey generateHostingObjectKey(RemoteObjectPtr rResource)
78     {
79         return HostingObjectKey(rResource->getAddress() + rResource->getUri());
80     }
81
82     HostingObject::Ptr findRemoteResource(RemoteObjectPtr remoteResource);
83
84     void destroyedHostingObject(const HostingObjectKey & key);
85 };
86
87 } /* namespace Service */
88 } /* namespace OIC */
89
90 #endif /* RH_RESOURCEHOSTING_H_ */