Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / client / post_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 "post_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
24 #include "logger.h"
25
26 #define TAG "POST_REQUEST_GEN"
27
28 POSTRequestGenerator::POSTRequestGenerator(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 POSTRequestGenerator::startSending()
38 {
39     // Create thread and start sending requests in dispatched thread
40     m_thread.reset(new std::thread(&POSTRequestGenerator::SendAllRequests, this));
41     m_thread->detach();
42 }
43
44 void POSTRequestGenerator::stopSending()
45 {
46     m_stopRequested = true;
47 }
48
49 void POSTRequestGenerator::SendAllRequests()
50 {
51     // Notify the progress status
52     OC_LOG(DEBUG, TAG, "Sending OP_START event");
53     m_callback(m_id, OP_START);
54
55     std::shared_ptr<SimulatorResourceModelSchema> repSchema =
56         m_requestSchema->getRequestRepSchema();
57
58     if (!repSchema)
59     {
60         OC_LOG(ERROR, TAG, "Request representation model is null!");
61         m_callback(m_id, OP_COMPLETE);
62         return;
63     }
64
65     // Create attribute generator for value manipulation
66     std::vector<AttributeGenerator> attributeGenList;
67     for (auto &propertyElement : repSchema->getChildProperties())
68         attributeGenList.push_back(AttributeGenerator(propertyElement.first, propertyElement.second));
69
70     if (!attributeGenList.size())
71     {
72         OC_LOG(ERROR, TAG, "Zero attribute found from resource model!");
73         return;
74     }
75
76     QPGenerator queryParamGen(m_requestSchema->getQueryParams());
77
78     do
79     {
80         // Get the next possible queryParameter
81         std::map<std::string, std::string> queryParam = queryParamGen.next();
82
83         for (auto &attributeGen : attributeGenList)
84         {
85             if (m_stopRequested) break;
86
87             while (!m_stopRequested && attributeGen.hasNext())
88             {
89                 SimulatorResourceModel repModel;
90                 SimulatorResourceAttribute attribute;
91                 if (true == attributeGen.next(attribute))
92                     repModel.add(attribute.getName(), attribute.getValue());
93
94                 // Send the request
95                 m_requestSender.send(queryParam, repModel,
96                                      std::bind(&POSTRequestGenerator::onResponseReceived,
97                                                this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
98
99                 m_requestCnt++;
100             }
101         }
102     }
103     while (!m_stopRequested && queryParamGen.hasNext());
104
105     completed();
106 }
107
108 void POSTRequestGenerator::onResponseReceived(SimulatorResult result,
109         const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
110 {
111     OC_LOG(DEBUG, TAG, "Response recieved");
112     m_responseCnt++;
113     completed();
114 }
115
116 void POSTRequestGenerator::completed()
117 {
118     if (m_requestCnt == m_responseCnt)
119     {
120         if (m_stopRequested)
121         {
122             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
123             m_callback(m_id, OP_ABORT);
124         }
125         else
126         {
127             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
128             m_callback(m_id, OP_COMPLETE);
129         }
130     }
131 }