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