2b35cb6d7fffa742a4edc1e195f32a572ecae1dd
[platform/upstream/iotivity.git] / service / resource-manipulation / modules / common / primitiveResource / src / PresenceSubscriber.cpp
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 #include <PresenceSubscriber.h>
22
23 #include <internal/AssertUtils.h>
24
25 #include <OCPlatform.h>
26
27 namespace OIC
28 {
29     namespace Service
30     {
31
32         void subscribePresence(OCDoHandle& handle, const std::string& host,
33                 OCConnectivityType connectivityType, SubscribeCallback presenceHandler)
34         {
35             using SubscribePresence = OCStackResult (*)(OC::OCPlatform::OCPresenceHandle&,
36                     const std::string&, OCConnectivityType, SubscribeCallback);
37
38             invokeOCFunc(static_cast<SubscribePresence>(OC::OCPlatform::subscribePresence),
39                     handle, host, connectivityType, presenceHandler);
40         }
41
42         void subscribePresence(OCDoHandle& handle, const std::string& host,
43                 const std::string& resourceType, OCConnectivityType connectivityType,
44                 SubscribeCallback presenceHandler)
45         {
46             using SubscribePresence = OCStackResult (*)(OC::OCPlatform::OCPresenceHandle&,
47                     const std::string&, const std::string&, OCConnectivityType, SubscribeCallback);
48
49             invokeOCFunc(static_cast<SubscribePresence>(OC::OCPlatform::subscribePresence),
50                     handle, host, resourceType, connectivityType, presenceHandler);
51         }
52
53         void unsubscribePresence(OCDoHandle handle)
54         {
55             invokeOCFunc(OC::OCPlatform::unsubscribePresence, handle);
56         }
57
58
59         PresenceSubscriber::PresenceSubscriber() :
60             m_handle{ nullptr }
61         {
62         }
63
64         PresenceSubscriber::PresenceSubscriber(PresenceSubscriber&& from) :
65             m_handle{ nullptr }
66         {
67             std::swap(m_handle, from.m_handle);
68         }
69
70         PresenceSubscriber::PresenceSubscriber(const std::string& host,
71                 OCConnectivityType connectivityType, SubscribeCallback presenceHandler) :
72                 m_handle{ nullptr }
73         {
74             subscribePresence(m_handle, host, connectivityType, presenceHandler);
75         }
76
77         PresenceSubscriber::PresenceSubscriber(const std::string& host,
78                 const std::string& resourceType, OCConnectivityType connectivityType,
79                 SubscribeCallback presenceHandler) :
80                 m_handle{ nullptr }
81         {
82             subscribePresence(m_handle, host, resourceType, connectivityType, presenceHandler);
83         }
84
85         PresenceSubscriber::~PresenceSubscriber()
86         {
87             if (m_handle)
88             {
89                 try
90                 {
91                     unsubscribe();
92                 }
93                 catch (...)
94                 {
95                 }
96             }
97         }
98
99         PresenceSubscriber& PresenceSubscriber::operator=(PresenceSubscriber&& from)
100         {
101             unsubscribe();
102             std::swap(m_handle, from.m_handle);
103             return *this;
104         }
105
106         void PresenceSubscriber::unsubscribe()
107         {
108             if (m_handle == nullptr) return;
109
110             unsubscribePresence(m_handle);
111
112             m_handle = nullptr;
113         }
114
115         bool PresenceSubscriber::isSubscribing() const
116         {
117             return m_handle != nullptr;
118         }
119
120     }
121 }