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