71a04d8af09a914a004a153dc65e7617e4cfb92c
[platform/upstream/iotivity.git] / service / notification-manager / NotificationManager / src / NotificationManager.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 "NotificationManager.h"
22
23 NotificationManager *NotificationManager::s_instance = NULL;
24 //OCPlatform *NotificationManager::s_nmOCPlatform = NULL;
25 PlatformConfig NotificationManager::s_cfg;
26 //(ServiceType::InProc, ModeType::Both, "134.134.161.33", 5683, QualityOfService::NonConfirmable);
27
28 NotificationManager::NotificationManager()
29 {
30
31     m_print = NULL;
32     m_onfound = NULL;
33     m_onObserve = NULL;
34     m_startHosting = NULL;
35     m_findHosting = NULL;
36     m_addExtraStr = NULL;
37 }
38
39 NotificationManager::~NotificationManager()
40 {
41 }
42
43 void NotificationManager::initialize()
44 {
45
46     Configure(s_cfg);
47
48     setPrint(NULL);
49     setOnFoundHostingCandidate(NULL);
50     setStartHosting(NotificationManager::getInstance()->m_startHosting);
51     setFindHosting(NotificationManager::getInstance()->m_findHosting);
52     setAddExtraStr(NotificationManager::getInstance()->m_addExtraStr);
53
54 #ifndef ISFORDEMO
55     findHostingCandidate();
56 #endif
57
58 }
59
60 void NotificationManager::registerHostingEventListener()
61 {
62     // TODO : Initial HostingEventListener (v1.0)
63 }
64
65 void NotificationManager::findHostingCandidate()
66 {
67     try
68     {
69         ResourceManager::getInstance()->findNMResource("" , "coap://224.0.1.187/oc/core" , true);
70     }
71     catch(OCException e)
72     {
73     }
74 }
75
76 NotificationManager *NotificationManager::getInstance()
77 {
78     if(!s_instance)
79     {
80         s_instance = new NotificationManager();
81     }
82
83     return s_instance;
84 }
85
86 int NotificationManager::setPrint(std::function< void(AttributeMap &inputAttMap) > func)
87 {
88     if(func != NULL)
89     {
90         try
91         {
92             NotificationManager::getInstance()->m_print = func;
93         }
94         catch(exception e)
95         {
96             return false;
97         }
98     }
99     else
100     {
101         NotificationManager::getInstance()->m_print =
102                 std::function< void(AttributeMap &inputAttMap) >(
103                         std::bind(&ResourceManager::printAttributeMap ,
104                                 ResourceManager::getInstance() , std::placeholders::_1));
105     }
106     return true;
107 }
108
109 int NotificationManager::setOnFoundHostingCandidate(
110         std::function< void(std::shared_ptr< OCResource > resource) > func)
111 {
112     if(func != NULL)
113     {
114         try
115         {
116             NotificationManager::getInstance()->m_onfound = func;
117         }
118         catch(exception e)
119         {
120             return false;
121         }
122     }
123     else
124     {
125         NotificationManager::getInstance()->m_onfound = std::function<
126                 void(std::shared_ptr< OCResource > resource) >(
127                 std::bind(&ResourceManager::onFoundReport , ResourceManager::getInstance() ,
128                         std::placeholders::_1));
129     }
130
131     return true;
132 }
133
134 int NotificationManager::setOnObserve(std::function< void(AttributeMap &inputAttMap) > func)
135 {
136     if(func != NULL)
137     {
138         try
139         {
140             NotificationManager::getInstance()->m_onObserve = func;
141         }
142         catch(exception e)
143         {
144             return false;
145         }
146     }
147     return true;
148 }
149
150 std::function< void(AttributeMap &inputAttMap) > NotificationManager::getPrint()
151 {
152     return m_print;
153 }
154
155 std::function< void(std::shared_ptr< OCResource > resource) > NotificationManager::getOnFoundHostingCandidate()
156 {
157     return m_onfound;
158 }
159
160 std::function< void(AttributeMap &inputAttMap) > NotificationManager::getOnObserve()
161 {
162     return m_onObserve;
163 }
164
165 int NotificationManager::setStartHosting(
166         std::function< void(std::shared_ptr< OCResource > resource) > &func)
167 {
168     try
169     {
170         func = std::function< void(std::shared_ptr< OCResource > resource) >(
171                 std::bind(&ResourceManager::startHosting , ResourceManager::getInstance() ,
172                         std::placeholders::_1));
173     }
174     catch(exception e)
175     {
176         return false;
177     }
178
179     return true;
180 }
181
182 int NotificationManager::setFindHosting(std::function< void() > &func)
183 {
184     try
185     {
186         func = std::function< void() >(
187                 std::bind(&NotificationManager::findHostingCandidate ,
188                         NotificationManager::getInstance()));
189     }
190     catch(exception e)
191     {
192         return false;
193     }
194
195     return true;
196 }
197
198 int NotificationManager::setAddExtraStr(std::function< void(std::string) > &func)
199 {
200     try
201     {
202         func = std::function< void(std::string str) >(
203                 std::bind(&ResourceManager::addExtraStr , ResourceManager::getInstance() ,
204                         std::placeholders::_1));
205     }
206     catch(exception e)
207     {
208         return false;
209     }
210
211     return true;
212 }
213
214 std::function< void(std::shared_ptr< OCResource > resource) > NotificationManager::getStartHosting()
215 {
216     if(m_startHosting)
217     {
218         return m_startHosting;
219     }
220     else
221     {
222         return NULL;
223     }
224 }
225
226 std::function< void() > NotificationManager::getFindHosting()
227 {
228     if(m_findHosting)
229     {
230         return m_findHosting;
231     }
232     else
233     {
234         return NULL;
235     }
236 }
237
238 std::function< void(std::string) > NotificationManager::getAddExtraStr()
239 {
240     if(m_addExtraStr)
241     {
242         return m_addExtraStr;
243     }
244     else
245     {
246         return NULL;
247     }
248 }