replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / unittest / NSConsumerSimulator.h
1 //******************************************************************
2 //
3 // Copyright 2016 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 _NS_CONSUMER_SIMULATOR_H_
22 #define _NS_CONSUMER_SIMULATOR_H_
23
24 #include <iostream>
25
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 class NSConsumerSimulator
30 {
31 private:
32     std::function<void(const int&, const std::string&, const std::string&,
33             const std::string&)> m_messageFunc;
34     std::function<void(const int&, const int&)> m_syncFunc;
35
36     std::shared_ptr<OC::OCResource> m_syncResource;
37     std::shared_ptr<OC::OCResource> m_msgResource;
38     std::shared_ptr<OC::OCResource> m_topicResource;
39
40     bool isTopicPost;
41
42 public:
43     NSConsumerSimulator()
44     : m_messageFunc(), m_syncFunc(),
45       m_syncResource(), isTopicPost(false) { };
46     ~NSConsumerSimulator() = default;
47
48     NSConsumerSimulator(const NSConsumerSimulator &) = delete;
49     NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete;
50
51     NSConsumerSimulator(NSConsumerSimulator &&) = delete;
52     NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete;
53
54     void findProvider()
55     {
56         OC::OCPlatform::findResource("", std::string("/oic/res?rt=x.org.iotivity.notification"),
57                 OCConnectivityType::CT_DEFAULT,
58                 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
59                 OC::QualityOfService::LowQos);
60     }
61
62     void syncToProvider(int & type, const int & id, const std::string & providerID)
63     {
64         if (m_syncResource == nullptr)
65         {
66             return;
67         }
68
69         OC::OCRepresentation rep;
70         rep.setValue("x.org.iotivity.ns.providerid", providerID);
71         rep.setValue("x.org.iotivity.ns.messageid", id);
72         rep.setValue("x.org.iotivity.ns.state", type);
73
74         m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
75     }
76
77     bool cancelObserves()
78     {
79         if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) &&
80                 !syncResourceCancelObserve(OC::QualityOfService::HighQos))
81             return true;
82         return false;
83     }
84
85     void setCallback(std::function<void(const int&, const std::string&,
86             const std::string&, const std::string&)> messageFunc,
87             const std::function<void(const int&, const int&)> & syncFunc)
88     {
89         m_messageFunc = messageFunc;
90         m_syncFunc = syncFunc;
91     }
92
93 private:
94     static void onPost(const OC::HeaderOptions &/*headerOption*/,
95                 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
96     {
97     }
98     void findResultCallback(std::shared_ptr<OC::OCResource> resource)
99     {
100         if(resource->uri() == "/notification")
101         {
102             resource->get(std::string("x.org.iotivity.notification"), std::string("oic.if.baseline"),
103                     OC::QueryParamsMap(), std::bind(&NSConsumerSimulator::onGet, this,
104                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
105                             resource), OC::QualityOfService::LowQos);
106         }
107     }
108     void onGet(const OC::HeaderOptions &/*headerOption*/,
109             const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
110             std::shared_ptr<OC::OCResource> resource)
111     {
112         OC::QueryParamsMap map;
113         map.insert(std::pair<std::string,std::string>(std::string("x.org.iotivity.ns.consumerid"),
114                 std::string("123456789012345678901234567890123456")));
115
116         try
117         {
118             std::vector<std::string> rts{"x.org.iotivity.notification"};
119
120             m_msgResource
121                 = OC::OCPlatform::constructResourceObject(
122                         std::string(resource->host()), std::string(resource->uri() + "/message"),
123                         OCConnectivityType(resource->connectivityType()), true, rts,
124                         std::vector<std::string>(resource->getResourceInterfaces()));
125
126             m_msgResource->observe(OC::ObserveType::Observe, map,
127                             std::bind(&NSConsumerSimulator::onObserve, this,
128                                     std::placeholders::_1, std::placeholders::_2,
129                                     std::placeholders::_3, std::placeholders::_4, resource),
130                             OC::QualityOfService::LowQos);
131         }
132         catch(std::exception & e)
133         {
134             std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
135         }
136         m_syncResource
137             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
138                     resource->connectivityType(), true, resource->getResourceTypes(),
139                     resource->getResourceInterfaces());
140
141         m_syncResource->observe(OC::ObserveType::Observe, map,
142                 std::bind(&NSConsumerSimulator::onObserve, this,
143                         std::placeholders::_1, std::placeholders::_2,
144                         std::placeholders::_3, std::placeholders::_4, resource),
145                 OC::QualityOfService::LowQos);
146
147
148         m_topicResource
149             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
150                     resource->connectivityType(), true, resource->getResourceTypes(),
151                     resource->getResourceInterfaces());
152
153     }
154     void onObserve(const OC::HeaderOptions &/*headerOption*/,
155             const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
156             std::shared_ptr<OC::OCResource> )
157     {
158         if (rep.getUri() == "/notification/message" && rep.hasAttribute("x.org.iotivity.ns.messageid")
159                 && rep.getValue<int>("x.org.iotivity.ns.messageid") != 1)
160         {
161             m_messageFunc(int(rep.getValue<int>("x.org.iotivity.ns.messageid")),
162                           std::string(rep.getValueToString("x.org.iotivity.ns.title")),
163                           std::string(rep.getValueToString("x.org.iotivity.ns.contenttext")),
164                           std::string(rep.getValueToString("x.org.iotivity.ns.source")));
165
166             if(rep.getValue<int>("x.org.iotivity.ns.messageid") == 3)
167             {
168                 m_topicResource->get(std::string("x.org.iotivity.notification"),
169                         std::string("oic.if.baseline"), OC::QueryParamsMap(),
170                         std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
171                                 std::placeholders::_2, std::placeholders::_3, m_topicResource),
172                                 OC::QualityOfService::LowQos);
173             }
174         }
175         else if (rep.getUri() == "/notification/sync")
176         {
177             m_syncFunc(int(rep.getValue<int>("x.org.iotivity.ns.state")), int(rep.getValue<int>("x.org.iotivity.ns.messageid")));
178         }
179     }
180
181     void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
182             const OC::OCRepresentation & rep , const int /*eCode*/,
183             std::shared_ptr<OC::OCResource> /*resource*/)
184     {
185
186         if(!isTopicPost)
187         {
188             isTopicPost = true;
189             OC::OCRepresentation postRep;
190
191             std::vector<OC::OCRepresentation> topicArr =
192                             rep.getValue<std::vector<OC::OCRepresentation>>("x.org.iotivity.ns.topiclist");
193
194             std::vector<OC::OCRepresentation> postTopicArr;
195
196             for(std::vector<OC::OCRepresentation>::iterator it = topicArr.begin();
197                     it != topicArr.end(); ++it)
198             {
199                 /* std::cout << *it; ... */
200                 OC::OCRepresentation topic = *it;
201                 OC::OCRepresentation postTopic;
202
203                 postTopic.setValue("x.org.iotivity.ns.topicname", topic.getValueToString("x.org.iotivity.ns.topicname"));
204                 postTopic.setValue("x.org.iotivity.ns.topicstate", (int) topic.getValue<int>("x.org.iotivity.ns.topicstate"));
205
206                 postTopicArr.push_back(topic);
207
208 //                std::cout << "tName : " << tName << std::endl;
209 //                std::cout << "tState : " << tState << std::endl;
210             }
211
212             postRep.setValue<std::vector<OC::OCRepresentation>>
213                 ("x.org.iotivity.ns.topiclist", postTopicArr);
214
215             OC::QueryParamsMap map;
216             map.insert(std::pair<std::string,std::string>(std::string("x.org.iotivity.ns.consumerid"),
217                     std::string("123456789012345678901234567890123456")));
218             m_topicResource->post(postRep, map, &onPost, OC::QualityOfService::LowQos);
219         }
220     }
221
222     OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
223     {
224         return m_msgResource->cancelObserve(qos);
225     }
226
227     OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
228     {
229         return m_syncResource->cancelObserve(qos);
230     }
231 };
232
233
234 #endif //_NS_CONSUMER_SIMULATOR_H_