Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / include / ResourcePresence.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 RB_RESOURCEPRESENCE_H_
22 #define RB_RESOURCEPRESENCE_H_
23
24 #include <functional>
25 #include <list>
26 #include <string>
27 #include <boost/atomic.hpp>
28 #include <mutex>
29 #include <condition_variable>
30
31 #include "BrokerTypes.h"
32 #include "ExpiryTimer.h"
33
34 namespace OIC
35 {
36     namespace Service
37     {
38         class ResourcePresence : public std::enable_shared_from_this<ResourcePresence>
39         {
40         public:
41             ResourcePresence();
42             ~ResourcePresence();
43
44             void initializeResourcePresence(PrimitiveResourcePtr pResource);
45
46             void addBrokerRequester(BrokerID _id, BrokerCB _cb);
47             void removeBrokerRequester(BrokerID _id);
48             void removeAllBrokerRequester();
49
50             void requestResourceState() const;
51             void changePresenceMode(BROKER_MODE newMode);
52
53             bool isEmptyRequester() const;
54             int  requesterListSize() const;
55             const PrimitiveResourcePtr getPrimitiveResource() const;
56             BROKER_STATE getResourceState() const;
57
58         private:
59             std::unique_ptr<std::list<BrokerRequesterInfoPtr>> requesterList;
60             PrimitiveResourcePtr primitiveResource;
61             ExpiryTimer expiryTimer;
62
63             BROKER_STATE state;
64             BROKER_MODE mode;
65
66             bool isWithinTime;
67             boost::atomic_long receivedTime;
68             std::mutex cbMutex;
69             unsigned int timeoutHandle;
70
71             RequestGetCB pGetCB;
72             TimerCB pTimeoutCB;
73             TimerCB pPollingCB;
74
75             void registerDevicePresence();
76         public:
77             void getCB(const HeaderOptions &hos, const ResponseStatement& rep, int eCode);
78             void timeOutCB(unsigned int msg);
79         private:
80             void verifiedGetResponse(int eCode);
81
82             void pollingCB(unsigned int msg = 0);
83
84             void executeAllBrokerCB(BROKER_STATE changedState);
85             void setResourcestate(BROKER_STATE _state);
86         };
87     } // namespace Service
88 } // namespace OIC
89
90 #endif /* RB_RESOURCEPRESENCE_H_ */
91