There are Two modifications.
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / HostingHandler.cpp
1 /*
2  * HostingHandler.cpp
3  *
4  *  Created on: 2014. 10. 15.
5  *      Author: jyong2
6  */
7
8 #include "HostingHandler.h"
9
10 HostingHandler *HostingHandler::s_instance = NULL;
11 mutex HostingHandler::s_mutexForCreation;
12
13 std::function< void(bool isHosting) > HostingHandler::s_findHostingCandidate;
14 std::function< void(std::shared_ptr< OCResource > resource) > HostingHandler::s_startHosting;
15 std::function< void(OCResourceHandle resourceHandle) > HostingHandler::s_notify;
16 std::function< void(std::string) > HostingHandler::s_addExtraStr;
17
18 HostingHandler::HostingHandler()
19 {
20
21 }
22
23 HostingHandler::~HostingHandler()
24 {
25
26 }
27
28 HostingHandler *HostingHandler::getInstance()
29 {
30         if(!s_instance)
31         {
32                 s_mutexForCreation.lock();
33                 if(!s_instance)
34                 {
35                         s_instance = new HostingHandler();
36                 }
37                 s_mutexForCreation.unlock();
38         }
39
40         return s_instance;
41 }
42
43 void HostingHandler::initialize()
44 {
45         // Create Hosting Handler instance
46         HostingHandler *ptr = HostingHandler::getInstance();
47
48         // Registration interface about hosting
49         HostingInterface hostingInterface;
50
51         hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
52         hostingInterface.setStartHosting(ptr->s_startHosting);
53         hostingInterface.setNotifyObservers(ptr->s_notify);
54         hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
55
56         hostingInterface.setOnFoundHostingCandidate(
57                         std::function< void(std::shared_ptr< OCResource > resource) >(
58                                         std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() , std::placeholders::_1)));
59         hostingInterface.setOnObserve(
60                         std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
61                                         std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1, std::placeholders::_2)));
62
63         ptr->s_addExtraStr("virtual");
64         ptr->startFindHost();
65 }
66
67 void HostingHandler::initialize(HostingConfig cfg)
68 {
69         // Create Hosting Handler instance
70         HostingHandler *ptr = HostingHandler::getInstance();
71
72         // Registration interface about hosting
73         HostingInterface hostingInterface;
74
75         hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
76         hostingInterface.setStartHosting(ptr->s_startHosting);
77         hostingInterface.setNotifyObservers(ptr->s_notify);
78         hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
79
80         hostingInterface.setOnFoundHostingCandidate(
81                         std::function< void(std::shared_ptr< OCResource > resource) >(
82                                         std::bind(&HostingHandler::onFoundCandidate , HostingHandler::getInstance() , std::placeholders::_1)));
83         hostingInterface.setOnObserve(
84                         std::function< void(AttributeMap &inputAttMap, OCResourceHandle resourceHandle) >(
85                                         std::bind(&HostingHandler::onObserve , HostingHandler::getInstance() , std::placeholders::_1, std::placeholders::_2)));
86
87         // Set Hosting Config
88         ptr->setHostingConfig(cfg);
89         ptr->changeHostingMode(ptr->hostingCfg.hostingMode);
90
91         ptr->s_addExtraStr("virtual");
92         ptr->startFindHost();
93 }
94
95 void HostingHandler::setHostingConfig(HostingConfig cfg)
96 {
97         hostingCfg.automaticMethod      = cfg.automaticMethod;
98         hostingCfg.frequency            = cfg.frequency;
99         hostingCfg.hostingMode          = cfg.hostingMode;
100         hostingCfg.notifyMethod         = cfg.notifyMethod;
101 }
102
103 void HostingHandler::changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod)
104 {
105         if(hostingCfg.hostingMode != hostingMode)
106         {
107                 hostingCfg.hostingMode = hostingMode;
108                 if(hostingCfg.hostingMode == HostingMode::AutomaticMode)
109                 {
110                         runAutomaticHosting(autoMethod);
111                 }
112                 else
113                 {
114                         stopAutomaticHosting();
115                 }
116         }
117 }
118
119 void HostingHandler::runAutomaticHosting(AutomaticMethod autoMethod)
120 {
121         // TODO Triggering from Event Listener.
122         if(hostingCfg.automaticMethod != autoMethod)
123         {
124                 hostingCfg.automaticMethod = autoMethod;
125                 switch(hostingCfg.automaticMethod)
126                 {
127                 case AutomaticMethod::None:
128                         break;
129                 case AutomaticMethod::Timer:
130                 case AutomaticMethod::DeviceStatus:
131                 case AutomaticMethod::NetworkStatusChange:
132                 default:
133                         break;
134                 }
135         }
136 }
137
138 void HostingHandler::stopAutomaticHosting()
139 {
140         if(hostingCfg.automaticMethod != AutomaticMethod::None)
141         {
142 // TODO
143         }
144 }
145
146 void HostingHandler::changeAutomaticHostingMethod(AutomaticMethod autoMethod)
147 {
148 //      TODO
149 }
150 void HostingHandler::changeNotifiyMethod(NotifyMethod notifyMethod)
151 {
152 //      TODO
153 }
154 void HostingHandler::changeNotifyFrequencyType(NotifyFrequency notifyFrequency)
155 {
156 //      TODO
157 }
158
159 void HostingHandler::startFindHost()
160 {
161         if(hostingCfg.hostingMode != HostingMode::None)
162         {
163                 s_findHostingCandidate(true);
164         }
165 }
166
167 void HostingHandler::onFoundCandidate(std::shared_ptr< OCResource > resource)
168 {
169         // TODO
170         // Condition of Hosting
171         s_startHosting(resource);
172 }
173
174 void HostingHandler::onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle)
175 {
176
177         switch(hostingCfg.notifyMethod)
178         {
179         case NotifyMethod::None:
180                 break;
181         case NotifyMethod::Equalization:
182         case NotifyMethod::Granularity:
183         case NotifyMethod::Frequence:
184         default:
185                 notifyFrequence(resourceHandle);
186                 break;
187         }
188 }
189
190 void HostingHandler::notifyFrequence(OCResourceHandle resourceHandle)
191 {
192
193         switch(hostingCfg.frequency)
194         {
195         case NotifyFrequency::None:
196                 break;
197         case NotifyFrequency::Periodically:
198         case NotifyFrequency::OnTime:
199         default:
200                 s_notify(resourceHandle);
201                 break;
202         }
203 }