4 * Created on: 2014. 10. 15.
8 #include "HostingHandler.h"
10 HostingHandler *HostingHandler::s_instance = NULL;
11 mutex HostingHandler::s_mutexForCreation;
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;
18 HostingHandler::HostingHandler()
23 HostingHandler::~HostingHandler()
28 HostingHandler *HostingHandler::getInstance()
32 s_mutexForCreation.lock();
35 s_instance = new HostingHandler();
37 s_mutexForCreation.unlock();
43 void HostingHandler::initialize()
45 // Create Hosting Handler instance
46 HostingHandler *ptr = HostingHandler::getInstance();
48 // Registration interface about hosting
49 HostingInterface hostingInterface;
51 hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
52 hostingInterface.setStartHosting(ptr->s_startHosting);
53 hostingInterface.setNotifyObservers(ptr->s_notify);
54 hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
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)));
63 ptr->s_addExtraStr("virtual");
67 void HostingHandler::initialize(HostingConfig cfg)
69 // Create Hosting Handler instance
70 HostingHandler *ptr = HostingHandler::getInstance();
72 // Registration interface about hosting
73 HostingInterface hostingInterface;
75 hostingInterface.setFindHosting(ptr->s_findHostingCandidate);
76 hostingInterface.setStartHosting(ptr->s_startHosting);
77 hostingInterface.setNotifyObservers(ptr->s_notify);
78 hostingInterface.setAddExtraStr(ptr->s_addExtraStr);
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)));
88 ptr->setHostingConfig(cfg);
89 ptr->changeHostingMode(ptr->hostingCfg.hostingMode);
91 ptr->s_addExtraStr("virtual");
95 void HostingHandler::setHostingConfig(HostingConfig cfg)
97 hostingCfg.automaticMethod = cfg.automaticMethod;
98 hostingCfg.frequency = cfg.frequency;
99 hostingCfg.hostingMode = cfg.hostingMode;
100 hostingCfg.notifyMethod = cfg.notifyMethod;
103 void HostingHandler::changeHostingMode(HostingMode hostingMode, AutomaticMethod autoMethod)
105 if(hostingCfg.hostingMode != hostingMode)
107 hostingCfg.hostingMode = hostingMode;
108 if(hostingCfg.hostingMode == HostingMode::AutomaticMode)
110 runAutomaticHosting(autoMethod);
114 stopAutomaticHosting();
119 void HostingHandler::runAutomaticHosting(AutomaticMethod autoMethod)
121 // TODO Triggering from Event Listener.
122 if(hostingCfg.automaticMethod != autoMethod)
124 hostingCfg.automaticMethod = autoMethod;
125 switch(hostingCfg.automaticMethod)
127 case AutomaticMethod::None:
129 case AutomaticMethod::Timer:
130 case AutomaticMethod::DeviceStatus:
131 case AutomaticMethod::NetworkStatusChange:
138 void HostingHandler::stopAutomaticHosting()
140 if(hostingCfg.automaticMethod != AutomaticMethod::None)
146 void HostingHandler::changeAutomaticHostingMethod(AutomaticMethod autoMethod)
150 void HostingHandler::changeNotifiyMethod(NotifyMethod notifyMethod)
154 void HostingHandler::changeNotifyFrequencyType(NotifyFrequency notifyFrequency)
159 void HostingHandler::startFindHost()
161 if(hostingCfg.hostingMode != HostingMode::None)
163 s_findHostingCandidate(true);
167 void HostingHandler::onFoundCandidate(std::shared_ptr< OCResource > resource)
170 // Condition of Hosting
171 s_startHosting(resource);
174 void HostingHandler::onObserve(AttributeMap &AttMap, OCResourceHandle resourceHandle)
177 switch(hostingCfg.notifyMethod)
179 case NotifyMethod::None:
181 case NotifyMethod::Equalization:
182 case NotifyMethod::Granularity:
183 case NotifyMethod::Frequence:
185 notifyFrequence(resourceHandle);
190 void HostingHandler::notifyFrequence(OCResourceHandle resourceHandle)
193 switch(hostingCfg.frequency)
195 case NotifyFrequency::None:
197 case NotifyFrequency::Periodically:
198 case NotifyFrequency::OnTime:
200 s_notify(resourceHandle);