1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "get_request_generator.h"
22 #include "request_model.h"
23 #include "simulator_exceptions.h"
26 #define TAG "GET_REQUEST_GEN"
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) {}
37 void GETRequestGenerator::startSending()
39 // Create thread and start sending requests in dispatched thread
40 m_thread.reset(new std::thread(&GETRequestGenerator::SendAllRequests, this));
44 void GETRequestGenerator::stopSending()
46 m_stopRequested = true;
49 void GETRequestGenerator::SendAllRequests()
51 // Notify the progress status
52 OC_LOG(DEBUG, TAG, "Sending OP_START event");
53 m_callback(m_id, OP_START);
55 QPGenerator queryParamGen(m_requestSchema->getQueryParams());
58 // Get the next possible queryParameter
59 auto queryParam = queryParamGen.next();
64 m_requestSender.send(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this,
65 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
68 catch (SimulatorException &e)
70 m_stopRequested = true;
74 while (!m_stopRequested && queryParamGen.hasNext());
79 void GETRequestGenerator::onResponseReceived(SimulatorResult result,
80 const SimulatorResourceModel &repModel, const RequestInfo &reqInfo)
82 OC_LOG(DEBUG, TAG, "Response recieved");
87 void GETRequestGenerator::completed()
89 if (m_requestCnt == m_responseCnt)
93 OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
94 m_callback(m_id, OP_ABORT);
98 OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
99 m_callback(m_id, OP_COMPLETE);