Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / client / put_request_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 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 #include "put_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
24 #include "logger.h"
25
26 #define TAG "PUT_REQUEST_GEN"
27
28 PUTRequestGenerator::PUTRequestGenerator(int id,
29         const std::shared_ptr<OC::OCResource> &ocResource,
30         const std::shared_ptr<RequestModel> &requestSchema,
31         RequestGeneration::ProgressStateCallback callback)
32     :   RequestGeneration(RequestType::RQ_TYPE_GET, id, callback),
33         m_stopRequested(false),
34         m_requestSchema(requestSchema),
35         m_requestSender(ocResource) {}
36
37 void PUTRequestGenerator::startSending()
38 {
39     // Create thread and start sending requests in dispatched thread
40     m_thread.reset(new std::thread(&PUTRequestGenerator::SendAllRequests, this));
41     m_thread->detach();
42 }
43
44 void PUTRequestGenerator::stopSending()
45 {
46     m_stopRequested = true;
47 }
48
49 void PUTRequestGenerator::SendAllRequests()
50 {
51     OC_LOG(DEBUG, TAG, "Sending OP_START event");
52     m_callback(m_id, OP_START);
53
54     std::shared_ptr<SimulatorResourceModelSchema> repSchema =
55         m_requestSchema->getRequestRepSchema();
56
57     if (!repSchema)
58     {
59         OC_LOG(ERROR, TAG, "Request representation model is null!");
60         m_callback(m_id, OP_ABORT);
61         return;
62     }
63
64     SimulatorResourceModel representation = repSchema->buildResourceModel();
65
66     // Create attribute combination generator for generating resource model
67     // with different attribute values
68     std::vector<SimulatorResourceAttribute> attributes;
69     for (auto &attributeElement : representation.getAttributeValues())
70     {
71         SimulatorResourceAttribute attribute;
72         attribute.setName(attributeElement.first);
73         attribute.setValue(attributeElement.second);
74         attribute.setProperty(repSchema->get(attributeElement.first));
75         attributes.push_back(attribute);
76     }
77
78     if (!attributes.size())
79     {
80         OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
81         m_callback(m_id, OP_COMPLETE);
82         return;
83     }
84
85     QPGenerator queryParamGen(m_requestSchema->getQueryParams());
86     do
87     {
88         // Get the next possible queryParameter
89         std::map<std::string, std::string> queryParam = queryParamGen.next();
90
91         AttributeCombinationGen attrCombGen(attributes);
92
93         // Get the new model from attribute combination generator
94         SimulatorResourceModel resModel;
95         while (!m_stopRequested && attrCombGen.next(resModel))
96         {
97             // Send the request
98             m_requestSender.send(queryParam, resModel,
99                                  std::bind(&PUTRequestGenerator::onResponseReceived, this,
100                                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
101
102             m_requestCnt++;
103         }
104     }
105     while (!m_stopRequested && queryParamGen.hasNext());
106
107     completed();
108 }
109
110 void PUTRequestGenerator::onResponseReceived(SimulatorResult result,
111         const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
112 {
113     OC_LOG(DEBUG, TAG, "Response recieved");
114     m_responseCnt++;
115     completed();
116 }
117
118 void PUTRequestGenerator::completed()
119 {
120     if (m_requestCnt == m_responseCnt)
121     {
122         if (m_stopRequested)
123         {
124             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
125             m_callback(m_id, OP_ABORT);
126         }
127         else
128         {
129             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
130             m_callback(m_id, OP_COMPLETE);
131         }
132     }
133 }