Merge remote-tracking branch 'origin/notification-service'
[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 public:
41     NSConsumerSimulator()
42     : m_messageFunc(), m_syncFunc(),
43       m_syncResource() { };
44     ~NSConsumerSimulator() = default;
45
46     NSConsumerSimulator(const NSConsumerSimulator &) = delete;
47     NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete;
48
49     NSConsumerSimulator(NSConsumerSimulator &&) = delete;
50     NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete;
51
52     void findProvider()
53     {
54         OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.r.notification"),
55                 OCConnectivityType::CT_DEFAULT,
56                 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
57                 OC::QualityOfService::LowQos);
58     }
59
60     void syncToProvider(int & type, const int & id, const std::string & providerID)
61     {
62         if (m_syncResource == nullptr)
63         {
64             std::cout << "m_syncResource is null" << std::endl;
65             return;
66         }
67
68         OC::OCRepresentation rep;
69         rep.setValue("PROVIDER_ID", providerID);
70         rep.setValue("MESSAGE_ID", id);
71         rep.setValue("STATE", type);
72
73         m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
74     }
75
76     bool cancelObserves()
77     {
78         if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) &&
79                 !syncResourceCancelObserve(OC::QualityOfService::HighQos))
80             return true;
81         return false;
82     }
83
84     void setCallback(std::function<void(const int&, const std::string&,
85             const std::string&, const std::string&)> messageFunc,
86             const std::function<void(const int&, const int&)> & syncFunc)
87     {
88         m_messageFunc = messageFunc;
89         m_syncFunc = syncFunc;
90     }
91
92 private:
93     static void onPost(const OC::HeaderOptions &/*headerOption*/,
94                 const OC::OCRepresentation & /*rep*/ , const int eCode)
95     {
96         std::cout << __func__ << " result : " << eCode << std::endl;
97     }
98     void findResultCallback(std::shared_ptr<OC::OCResource> resource)
99     {
100
101         std::cout << __func__ << " " << resource->host() << std::endl;
102
103         if(resource->uri() == "/notification")
104         {
105             resource->get(OC::QueryParamsMap(),
106                     std::bind(&NSConsumerSimulator::onGet, this,
107                             std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
108                             resource), OC::QualityOfService::LowQos);
109         }
110     }
111     void onGet(const OC::HeaderOptions &/*headerOption*/,
112             const OC::OCRepresentation & rep , const int eCode,
113             std::shared_ptr<OC::OCResource> resource)
114     {
115         std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
116
117         OC::QueryParamsMap map;
118         map.insert(std::pair<std::string,std::string>(std::string("consumerid"),
119                 std::string("123456789012345678901234567890123456")));
120
121         try
122         {
123             std::cout << "resourc : host " << resource->host() << std::endl;
124             std::cout << "resourc : uri " << resource->uri() << std::endl;
125             std::cout << " resource->connectivityType() "
126                     <<  resource->connectivityType() << std::endl;
127             std::cout << "resourc : getResourceInterfaces "
128                     << resource->getResourceInterfaces()[0] << std::endl;
129             std::cout << "resourc : getResourceTypes "
130                     << resource->getResourceTypes()[0] << std::endl;
131
132             std::vector<std::string> rts{"oic.r.notification"};
133
134             m_msgResource
135                 = OC::OCPlatform::constructResourceObject(
136                         std::string(resource->host()), std::string(resource->uri() + "/message"),
137                         OCConnectivityType(resource->connectivityType()), true, rts,
138                         std::vector<std::string>(resource->getResourceInterfaces()));
139
140             m_msgResource->observe(OC::ObserveType::Observe, map,
141                             std::bind(&NSConsumerSimulator::onObserve, this,
142                                     std::placeholders::_1, std::placeholders::_2,
143                                     std::placeholders::_3, std::placeholders::_4, resource),
144                             OC::QualityOfService::LowQos);
145         }
146         catch(std::exception & e)
147         {
148             std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
149         }
150         m_syncResource
151             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
152                     resource->connectivityType(), true, resource->getResourceTypes(),
153                     resource->getResourceInterfaces());
154
155         m_syncResource->observe(OC::ObserveType::Observe, map,
156                 std::bind(&NSConsumerSimulator::onObserve, this,
157                         std::placeholders::_1, std::placeholders::_2,
158                         std::placeholders::_3, std::placeholders::_4, resource),
159                 OC::QualityOfService::LowQos);
160
161
162         m_topicResource
163             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
164                     resource->connectivityType(), true, resource->getResourceTypes(),
165                     resource->getResourceInterfaces());
166
167     }
168     void onObserve(const OC::HeaderOptions &/*headerOption*/,
169             const OC::OCRepresentation &rep , const int &eCode, const int &,
170             std::shared_ptr<OC::OCResource> )
171     {
172         std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
173         std::cout << " uri : " << rep.getUri() << std::endl;
174
175         if (rep.getUri() == "/notification/message" && rep.hasAttribute("MESSAGE_ID")
176                 && rep.getValue<int>("MESSAGE_ID") != 1)
177         {
178             std::cout << "ID : " << rep.getValue<int>("ID") << std::endl;
179             std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
180             std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
181             m_messageFunc(int(rep.getValue<int>("MESSAGE_ID")),
182                           std::string(rep.getValueToString("TITLE")),
183                           std::string(rep.getValueToString("CONTENT")),
184                           std::string(rep.getValueToString("SOURCE")));
185
186             if(rep.getValue<int>("MESSAGE_ID") == 3)
187             {
188                 m_topicResource->get(OC::QueryParamsMap(),
189                         std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
190                                 std::placeholders::_2, std::placeholders::_3, m_topicResource),
191                                 OC::QualityOfService::LowQos);
192             }
193         }
194         else if (rep.getUri() == "/notification/sync")
195         {
196             std::cout << "else if (rep.getUri() == sync) " << std::endl;
197             m_syncFunc(int(rep.getValue<int>("STATE")), int(rep.getValue<int>("ID")));
198         }
199     }
200
201     void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
202             const OC::OCRepresentation & rep , const int eCode,
203             std::shared_ptr<OC::OCResource> resource)
204     {
205         //TO-DO using this function.
206         (void) rep;
207         (void) eCode;
208         (void) resource;
209         std::cout << "onTopicGet()" << std::endl;
210     }
211
212     OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
213     {
214         return m_msgResource->cancelObserve(qos);
215     }
216
217     OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
218     {
219         return m_syncResource->cancelObserve(qos);
220     }
221 };
222
223
224 #endif //_NS_CONSUMER_SIMULATOR_H_