Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / client / get_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 "get_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
24 #include "logger.h"
25
26 #define TAG "GET_REQUEST_GEN"
27
28 GETRequestGenerator::GETRequestGenerator(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 GETRequestGenerator::startSending()
38 {
39     // Create thread and start sending requests in dispatched thread
40     m_thread.reset(new std::thread(&GETRequestGenerator::SendAllRequests, this));
41     m_thread->detach();
42 }
43
44 void GETRequestGenerator::stopSending()
45 {
46     m_stopRequested = true;
47 }
48
49 void GETRequestGenerator::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     QPGenerator queryParamGen(m_requestSchema->getQueryParams());
56     do
57     {
58         // Get the next possible queryParameter
59         auto queryParam = queryParamGen.next();
60
61         try
62         {
63             // Send the request
64             m_requestSender.send(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this,
65                                  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
66             m_requestCnt++;
67         }
68         catch (SimulatorException &e)
69         {
70             m_stopRequested = true;
71             break;
72         }
73     }
74     while (!m_stopRequested && queryParamGen.hasNext());
75
76     completed();
77 }
78
79 void GETRequestGenerator::onResponseReceived(SimulatorResult result,
80         const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
81 {
82     OC_LOG(DEBUG, TAG, "Response recieved");
83     m_responseCnt++;
84     completed();
85 }
86
87 void GETRequestGenerator::completed()
88 {
89     if (m_requestCnt == m_responseCnt)
90     {
91         if (m_stopRequested)
92         {
93             OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
94             m_callback(m_id, OP_ABORT);
95         }
96         else
97         {
98             OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
99             m_callback(m_id, OP_COMPLETE);
100         }
101     }
102 }