Merge branch 'master' into 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 std::string&, const std::string&, const std::string&)> m_messageFunc;
33     std::function<void(int, const std::string&)> m_syncFunc;
34
35     std::shared_ptr<OC::OCResource> m_syncResource;
36
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 std::string & id)
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("ID", id);
68         rep.setValue("STATE", type);
69
70         m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
71     }
72
73     void setCallback(const std::function<void(const std::string&, const std::string&, const std::string&)> & messageFunc,
74             const std::function<void(int, const std::string&)> & syncFunc)
75     {
76         m_messageFunc = messageFunc;
77         m_syncFunc = syncFunc;
78     }
79
80 private:
81     static void onPost(const OC::HeaderOptions &/*headerOption*/,
82                 const OC::OCRepresentation & /*rep*/ , const int eCode)
83     {
84         std::cout << __func__ << " result : " << eCode << std::endl;
85     }
86     void findResultCallback(std::shared_ptr<OC::OCResource> resource)
87     {
88         std::cout << __func__ << " " << resource->host() << std::endl;
89         resource->get(OC::QueryParamsMap(),
90                 std::bind(&NSConsumerSimulator::onGet, this,
91                         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
92                 OC::QualityOfService::LowQos);
93     }
94     void onGet(const OC::HeaderOptions &/*headerOption*/,
95             const OC::OCRepresentation & rep , const int eCode,
96             std::shared_ptr<OC::OCResource> resource)
97     {
98         std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
99
100         std::shared_ptr<OC::OCResource> msgResource
101             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/message",
102                     resource->connectivityType(), false, resource->getResourceTypes(),
103                     resource->getResourceInterfaces());
104         m_syncResource
105             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
106                     resource->connectivityType(), false, resource->getResourceTypes(),
107                     resource->getResourceInterfaces());
108
109         msgResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
110                 std::bind(&NSConsumerSimulator::onObserve, this,
111                         std::placeholders::_1, std::placeholders::_2,
112                         std::placeholders::_3, std::placeholders::_4, resource),
113                 OC::QualityOfService::LowQos);
114         m_syncResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
115                 std::bind(&NSConsumerSimulator::onObserve, this,
116                         std::placeholders::_1, std::placeholders::_2,
117                         std::placeholders::_3, std::placeholders::_4, resource),
118                 OC::QualityOfService::LowQos);
119
120     }
121     void onObserve(const OC::HeaderOptions &/*headerOption*/,
122             const OC::OCRepresentation &rep , const int &eCode, const int &,
123             std::shared_ptr<OC::OCResource> )
124     {
125         std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
126         std::cout << " uri : " << rep.getUri() << std::endl;
127
128         if (rep.getUri() == "/notification/message" && rep.hasAttribute("ID")
129                 && rep.getValueToString("ID") != "0000-0000-0000-0000")
130         {
131             std::cout << "ID : " << rep.getValueToString("ID") << std::endl;
132             std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
133             std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
134             m_messageFunc(std::string(rep.getValueToString("ID")),
135                           std::string(rep.getValueToString("TITLE")),
136                           std::string(rep.getValueToString("CONTENT")));
137         }
138         else if (rep.getUri() == "/notification/sync")
139         {
140             m_syncFunc(int(rep.getValue<int>("STATE")),
141                        std::string(rep.getValueToString("ID")));
142         }
143     }
144 };
145
146
147 #endif //_NS_CONSUMER_SIMULATOR_H_