Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / 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 <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             typedef OCStackResult (*SubscribePresence)(OC::OCPlatform::OCPresenceHandle&,
36                     const std::string&, OCConnectivityType, SubscribeCallback);
37
38             invokeOCFunc(static_cast<SubscribePresence>(OC::OCPlatform::subscribePresence),
39                     handle, host, connectivityType, std::move(presenceHandler));
40         }
41
42         void subscribePresence(OCDoHandle& handle, const std::string& host,
43                 const std::string& resourceType, OCConnectivityType connectivityType,
44                 SubscribeCallback presenceHandler)
45         {
46             typedef OCStackResult (*SubscribePresence)(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, std::move(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, std::move(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,
83                     std::move(presenceHandler));
84         }
85
86         PresenceSubscriber::~PresenceSubscriber()
87         {
88             if (m_handle)
89             {
90                 try
91                 {
92                     unsubscribe();
93                 }
94                 catch (...)
95                 {
96                 }
97             }
98         }
99
100         PresenceSubscriber& PresenceSubscriber::operator=(PresenceSubscriber&& from)
101         {
102             unsubscribe();
103             std::swap(m_handle, from.m_handle);
104             return *this;
105         }
106
107         void PresenceSubscriber::unsubscribe()
108         {
109             if (m_handle == nullptr) return;
110
111             unsubscribePresence(m_handle);
112
113             m_handle = nullptr;
114         }
115
116         bool PresenceSubscriber::isSubscribing() const
117         {
118             return m_handle != nullptr;
119         }
120
121     }
122 }