Imported Upstream version 1.0.0
[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 <cstdbool>
25 #include <iostream>
26 #include <list>
27 #include <memory>
28 #include <functional>
29 #include <string>
30 #include <atomic>
31
32 #include "octypes.h"
33 #include "RCSAddress.h"
34 #include "PresenceSubscriber.h"
35 #include "HostingObject.h"
36 #include "PrimitiveResource.h"
37 #include "RCSDiscoveryManager.h"
38
39 namespace OIC
40 {
41 namespace Service
42 {
43
44 class RCSDiscoveryManager;
45 class ResourceHosting
46 {
47 private:
48     typedef std::shared_ptr<HostingObject> HostingObjectPtr;
49     typedef std::weak_ptr<HostingObject> HostingObjectWeakPtr;
50
51     typedef std::shared_ptr<RCSRemoteResourceObject> RemoteObjectPtr;
52     typedef std::shared_ptr<PrimitiveResource> PrimiteveResourcePtr;
53
54     typedef std::function<
55             void(std::shared_ptr<RCSRemoteResourceObject>)> DiscoveryCallback;
56     typedef std::function<void()> DestroyedCallback;
57
58 public:
59     void startHosting();
60     void stopHosting();
61
62     static ResourceHosting * getInstance();
63
64 private:
65     ResourceHosting();
66     ~ResourceHosting() = default;
67
68     ResourceHosting(const ResourceHosting&) = delete;
69     ResourceHosting(ResourceHosting&&) = delete;
70     ResourceHosting& operator=(const ResourceHosting&) const = delete;
71     ResourceHosting& operator=(ResourceHosting&&) const = delete;
72
73     static ResourceHosting * s_instance;
74     static std::mutex s_mutexForCreation;
75     std::mutex mutexForList;
76
77     std::list<HostingObjectPtr> hostingObjectList;
78
79     RCSDiscoveryManager * discoveryManager;
80     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
81
82     DiscoveryCallback pDiscoveryCB;
83
84     void initializeResourceHosting();
85
86     void requestMulticastDiscovery();
87
88     void discoverHandler(RemoteObjectPtr remoteResource);
89
90     HostingObjectPtr findRemoteResource(RemoteObjectPtr remoteResource);
91     bool isSameRemoteResource(RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2);
92
93     void destroyedHostingObject(HostingObjectWeakPtr destroyedWeakPtr);
94
95 };
96
97 } /* namespace Service */
98 } /* namespace OIC */
99
100 #endif /* RH_RESOURCEHOSTING_H_ */