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 "simulator_resource_model.h"
25 #define TAG "GET_REQUEST_GEN"
27 GETRequestGenerator::GETRequestGenerator(int id, RequestSenderSP &requestSender,
28 AutoRequestGeneration::ProgressStateCallback callback)
29 : AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
31 m_stopRequested(false) {}
33 GETRequestGenerator::GETRequestGenerator(int id, RequestSenderSP &requestSender,
34 const std::map<std::string, std::vector<std::string>> &queryParams,
35 AutoRequestGeneration::ProgressStateCallback callback)
36 : AutoRequestGeneration(RequestType::RQ_TYPE_GET, id, requestSender, callback),
37 m_queryParamGen(queryParams),
39 m_stopRequested(false) {}
41 void GETRequestGenerator::startSending()
43 // Check if the operation is already in progress
44 std::lock_guard<std::mutex> lock(m_statusLock);
47 OC_LOG(ERROR, TAG, "Operation already in progress !");
48 throw OperationInProgressException("Another GET request generation session is already in progress!");
51 // Create thread and start sending requests in dispatched thread
52 m_thread = std::make_shared<std::thread>(&GETRequestGenerator::SendAllRequests, this);
57 void GETRequestGenerator::stopSending()
59 m_stopRequested = true;
62 void GETRequestGenerator::SendAllRequests()
64 // Notify the progress status
65 OC_LOG(DEBUG, TAG, "Sending OP_START event");
66 m_callback(m_id, OP_START);
72 // Get the next possible queryParameter
73 std::map<std::string, std::string> queryParam = m_queryParamGen.next();
78 m_requestSender->sendRequest(queryParam, std::bind(&GETRequestGenerator::onResponseReceived, this,
79 std::placeholders::_1, std::placeholders::_2), true);
82 catch (SimulatorException &e)
84 m_stopRequested = true;
89 while (m_queryParamGen.hasNext());
91 m_requestsSent = true;
95 void GETRequestGenerator::onResponseReceived(SimulatorResult result,
96 SimulatorResourceModelSP repModel)
98 OC_LOG_V(INFO, TAG, "Response recieved result:%d", result);
103 void GETRequestGenerator::completed()
105 if (m_requestCnt == m_responseCnt)
109 OC_LOG(DEBUG, TAG, "Sending OP_ABORT event");
110 m_callback(m_id, OP_ABORT);
114 OC_LOG(DEBUG, TAG, "Sending OP_COMPLETE event");
115 m_callback(m_id, OP_COMPLETE);