Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / 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
38 namespace OIC
39 {
40 namespace Service
41 {
42
43 class RCSDiscoveryManager;
44 class ResourceHosting
45 {
46 private:
47     typedef std::shared_ptr<HostingObject> HostingObjectPtr;
48     typedef std::shared_ptr<RCSRemoteResourceObject> RemoteObjectPtr;
49     typedef std::shared_ptr<PrimitiveResource> PrimiteveResourcePtr;
50
51     typedef std::function<
52             void(OCStackResult, const unsigned int, const std::string&)> SubscribeCallback;
53     typedef std::function<
54             void(std::shared_ptr<RCSRemoteResourceObject>)> DiscoveryCallback;
55     typedef std::function<void()> DestroyedCallback;
56
57 public:
58     void startHosting();
59     void stopHosting();
60
61     static ResourceHosting * getInstance();
62
63 private:
64     ResourceHosting();
65     ~ResourceHosting() = default;
66
67     ResourceHosting(const ResourceHosting&) = delete;
68     ResourceHosting(ResourceHosting&&) = delete;
69     ResourceHosting& operator=(const ResourceHosting&) const = delete;
70     ResourceHosting& operator=(ResourceHosting&&) const = delete;
71
72     static ResourceHosting * s_instance;
73     static std::mutex s_mutexForCreation;
74
75     std::list<HostingObjectPtr> hostingObjectList;
76
77     RCSDiscoveryManager * discoveryManager;
78     PresenceSubscriber presenceHandle;
79
80     SubscribeCallback pPresenceCB;
81     DiscoveryCallback pDiscoveryCB;
82
83     void initializeResourceHosting();
84
85     void requestMulticastPresence();
86     void requestMulticastDiscovery();
87     void requestDiscovery(std::string address = std::string());
88
89     void presenceHandler(OCStackResult ret, const unsigned int seq, const std::string & address);
90     void discoverHandler(RemoteObjectPtr remoteResource);
91
92     HostingObjectPtr findRemoteResource(RemoteObjectPtr remoteResource);
93     bool isSameRemoteResource(RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2);
94
95     void destroyedHostingObject(HostingObjectPtr destroyedPtr);
96
97 };
98
99 } /* namespace Service */
100 } /* namespace OIC */
101
102 #endif /* RH_RESOURCEHOSTING_H_ */